What is the Difference between Block, Inline, and Inline-block in CSS

Code Playground is only enabled on larger screen sizes.

Let's consider the following example:

<p>
  Lorem ipsum dolor sit amet consectetur adipisicing elit.
  <a href="/"
    >Fuga blanditiis delectus voluptatum molestias accusantium id repudiandae
    exercitationem!</a
  >
  Labore ipsum blanditiis vero, doloremque, nisi vel molestiae ea odio tempore
  recusandae dicta?
</p>
<p>
  Lorem ipsum dolor sit amet consectetur adipisicing elit. Illum itaque eaque
  esse inventore incidunt delectus quia? Eos nam fuga sint obcaecati magnam.
  Fuga voluptatem doloremque quisquam eligendi libero. Deleniti, rerum.
</p>

p vs a

Notice that the <p> element always starts on a new line and takes up as much horizontal space as possible.

On the other hand, the <a> element does not start on a new line and only takes up as much space as necessary.

This is, in fact, the difference between the block elements and inline elements.

Block elements automatically take up all the horizontal space available, and you can define custom width and height.

Inline elements only take as much space as necessary, and defining width and height has no effect on these elements.

Inline

However, you sometimes need to be more flexible when working on real-life projects. For example, you are trying to build a navigation bar that sits on top of the page, and you have a list of links here:

<ul>
  <li><a href="#">Link1</a></li>
  <li><a href="#">Link2</a></li>
  <li><a href="#">Link3</a></li>
  <li><a href="#">Link4</a></li>
</ul>

By default, <li> is a block element occupying the horizontal line.

This would waste a lot of vertical space. To fix this issue, you can change how the <li> elements are displayed using the display property.

li {
  display: inline;
}

And now, the <li> will be displayed as an inline element.

Block

You can also make an inline element behave like a block element in a similar way.

<div><span>#1</span><span>#2</span></div>
span {
  border: 2px solid black;
  display: block;
}

Inline block

If you need an inline element but you want to be able to customize its width and height, you can use inline-block instead.

span {
  border: 2px solid black;
  display: inline-block;
  width: 100px;
  height: 50px;
}

display vs. visibility

display can also control whether or not an element is rendered on the webpage. When display is set to none, the element will not be displayed.

This can create useful features when combined with JavaScript, allowing you to toggle the element on and off.

This behavior is very similar to another CSS property called visibility.

Their difference is that when visibility is set to hidden, the element is still on the webpage, just not displayed by the browser. It will take up the same space as before.

When display is set to none, the element will be completely removed from the webpage.

The display property is probably the most important CSS property we are going to cover in this chapter. It not only controls the display types of individual elements, but also the layout of its children.

However, this requires a deeper understanding of CSS, so we will come back to this topic later.