Challenges with Traditional Layouting

We will revisit the three column layout with inline-block display mode to understand why traditional layouting is not intuitive.

We'll cover the following

Whitespaces take space

Let’s recall the previous exercise, but this time, let’s also add a proper height to the elements, and let’s create six divs instead of three:

<div class="container"><!--
    --><div class="col">First</div><!--
    --><div class="col">Second</div><!--
    --><div class="col">Third</div><!--
    --><div class="col">Fourth</div><!--
    --><div class="col">Fifth</div><!--
    --><div class="col">Sixth</div><!--
--></div>
.container { 
  margin: 50px; 
  padding: 0; 
}

.col { 
  display: inline-block; 
  border: 2px solid black; 
  width: 33.33%; 
  box-sizing: border-box; 
  margin: 0 auto; 
  height: 100px; 
  background-color: lightblue;
}

The six elements are rendered as follows:

Let’s remove the comments from the HTML:

<div class="container">
    <div class="col">First</div>
    <div class="col">Second</div>
    <div class="col">Third</div>
    <div class="col">Fourth</div>
    <div class="col">Fifth</div>
    <div class="col">Sixth</div>
</div>

No comment… pun intended.

Our three-column layout fell apart because whitespaces take space.

To combat this, we have to set the font size of the container to zero. To make sure the elements retain their font size, we have to explicitly specify the font size in all elements inside the container.

.container { 
  margin: 50px; 
  padding: 0; 
  font-size: 0;
}

.col { 
  display: inline-block; 
  border: 2px solid black; 
  width: 33.33%; 
  box-sizing: border-box; 
  margin: 0 auto; 
  height: 100px; 
  background-color: lightblue;
  font-size: 16px;
}

Let’s add some more text inside the first element:

<div class="container">
    <div class="col">
        First 
        <p>Lorem ipsum.</p>
    </div>
    <div class="col">Second</div>
    <div class="col">Third</div>
    <div class="col">Fourth</div>
    <div class="col">Fifth</div>
    <div class="col">Sixth</div>
</div>

The page falls apart.

To fix this, we have to add the vertical-align: middle; rule to each element.

.container { 
  margin: 50px; 
  padding: 0; 
  font-size: 0;
}

.col { 
  display: inline-block; 
  border: 2px solid black; 
  width: 33.33%; 
  box-sizing: border-box; 
  margin: 0 auto; 
  height: 100px; 
  background-color: lightblue;
  font-size: 16px;
  vertical-align: middle;
}

Finally, after so many fixes, we can use our three-column layout without major restrictions as long as the content inside an element doesn’t exceed the height of the .col element. This is because the content will be hidden by the .col element(s) directly below the element with the long content.

Play around with the content a bit more to see how inline-block elements are created.

Get hands-on with 1200+ tech skills courses.