CSS Layout - display: inline-block

display: inline-block

Compared to display: inline Compared to display: inline-block Width and height can be set on the element.

Similarly, if display: inline-block is set, the vertical and horizontal margins/padding will be retained, while display: inline will not.

Compared to display: block, the main difference is that display: inline-block does not add a line break after the element, so the element can be located next to other elements.

The following example demonstrates the different behaviors of display: inline, display: inline-block, and display: block:

Example

span.a {
  display: inline; /* span's default value */
  width: 100px;
  height: 100px;
  padding: 5px;
  border: 1px solid blue; 
  background-color: yellow; 
}
span.b {
  display: inline-block;
  width: 100px;
  height: 100px;
  padding: 5px;
  border: 1px solid blue; 
  background-color: yellow; 
}
span.c {
  display: block;
  width: 100px;
  height: 100px;
  padding: 5px;
  border: 1px solid blue; 
  background-color: yellow; 
}

Try It Yourself

Creating navigation links with inline-block

A common use of display:inline-block Used to display list items horizontally instead of vertically. The following example creates a horizontal navigation link:

Example

.nav {
  background-color: yellow; 
  list-style-type: none;
  text-align: center; 
  padding: 0;
  margin: 0;
}
.nav li {
  display: inline-block;
  font-size: 20px;
  padding: 20px;
}

Try It Yourself