X

Headline

This is an important message requiring you to make a choice if you're based in the EU.

How to Align Divs Side by Side

CSS allows aligning divs side by side in many ways. We’ll discuss some ways that are widely used.

The <div> tag is used to define parts of a page or a document. It is used for grouping large sections of HTML elements and styling them with CSS. Three or more different divs can be put side-by-side using CSS. It is very easy if you follow the steps described below.

Let’s see an example and try to discuss each part of the code together.

1. Create HTML

  • Create <main> tag in the <body> section with id “boxes” which should include our divs.
  • Create <div> elements. For our first <div> we use id with the name ”column1”, for second id “column2”, and for third id “column3”.
  • You can set titles for your divs using <h2> tag.
<body>
  <div id="boxes">
    <h2>W3 docs</h2>
    <div id="column1">
      <h2>What is Lorem Ipsum?</h2>
      Lorem Ipsum is simply dummying text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged.
    </div>
    <div id="column2">
      <h2>Why do we use it?</h2>
      It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using 'Content here, content here', making it look like readable English. Many desktop publishing packages and web page editors now use Lorem Ipsum as their default model text, and a search for 'lorem ipsum' will uncover many web sites still in their infancy. Various versions have evolved over the years, sometimes by accident, sometimes on purpose (injected humor and the like).
    </div>
    <div id="column3">
      <h2>Where does it come from?:</h2>
      Contrary to popular belief, Lorem Ipsum is not simply random text. It has roots in a piece of classical Latin literature from 45 BC, making it over 2000 years old. Richard McClintock, a Latin professor at Hampden-Sydney College in Virginia, looked up one of the more obscure Latin words, consectetur, from a Lorem Ipsum passage, and going through the cites of the word in classical literature, discovered the undoubtable source.
    </div>
  </div>
</body>

2. Add CSS

  • Use float property for defining in which side of the container the elements should be placed. The float property has three values: none left and right. In our example, we use the left value for the divs.
  • You can choose colors for the backgrounds by using the background-color property. We use the hex values for the backgrounds.
  • Set the size of your div with the CSS width and height properties.
  • Set the position for your titles using the text-align property.
  • Use CSS clear property which is directly related to float property. It defines that an element should be next to floating elements or it should be below them (clear).
  • Use the content property. The content property is used with ::before and ::after pseudo-elements to generate content inside an element.
  • Use display property which makes the element behave like HTML <table> element.
#boxes{
      content: "";
      display: table;
      clear: both;
      }
      div{
      float:left;
      height:470px; 
      width:23%;
      padding:0 10px;
      }
      #column1{ 
      background-color: #a1edcc; 
      } 
      #column2{ 
      background-color:#a0e9ed; 
      width:43%; 
      } 
      #column3{ 
      background-color:#f497f1; 
      } 
      h2{ 
      color: #000000; 
      text-align:center; 
      }

The float property is ignored if elements are absolutely positioned (position: absolute).

Let’s bring the code parts together and see how it works!

Here is the result of our code.

<!DOCTYPE html>  
<html>
  <head>
    <title>Title of the document</title>
    <style>  
      #boxes{
      content: "";
      display: table;
      clear: both;
      }
      div{
      float:left;
      height:470px; 
      width:23%;
      padding:0 10px;
      }
      #column1{ 
      background-color: #a1edcc; 
      } 
      #column2{ 
      background-color:#a0e9ed; 
      width:43%; 
      } 
      #column3{ 
      background-color:#f497f1; 
      } 
      h2{ 
      color: #000000; 
      text-align:center; 
      } 
    </style>
  </head>
  <body>
    <main id="boxes">
      <h2>W3docs</h2>
      <div id="column1">
        <h2>What is Lorem Ipsum?</h2>
        Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged.
      </div>
      <div id="column2">
        <h2>Why do we use it?</h2>
        It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using 'Content here, content here', making it look like readable English. Many desktop publishing packages and web page editors now use Lorem Ipsum as their default model text, and a search for 'lorem ipsum' will uncover many web sites still in their infancy. Various versions have evolved over the years, sometimes by accident, sometimes on purpose (injected humour and the like).
      </div>
      <div id="column3">
        <h2>Where does it come from?:</h2>
        Contrary to popular belief, Lorem Ipsum is not simply random text. It has roots in a piece of classical Latin literature from 45 BC, making it over 2000 years old. Richard McClintock, a Latin professor at Hampden-Sydney College in Virginia, looked up one of the more obscure Latin words, consectetur, from a Lorem Ipsum passage, and going through the cities of the word in classical literature, discovered the undoubtable source.
      </div>
    </main>
  </body>
</html>

Let’s see another example.

Example

<!DOCTYPE html>  
<html>
  <head>
    <title>Title of the document</title>
    <style>  
      .container { 
      width:600px; 
      height:190px; 
      background-color: #5cbbf2; 
      padding: 35px 15px 5px;
      } 
      .container:before,
      .container:after {
      content:"";
      display:table;
      clear: both;
      }
      .container div{
      float:left; 
      width:180px; 
      height:160px; 
      }
      #box2{ 
      background-color: #000000;  
      margin-left:30px; 
      margin-right:30px;
      } 
      p {
      color: white;
      padding:5px 10px;
      text-align:center;
      }
    </style>
  </head>
  <body>
    <h2>W3docs</h2>
    <div class="container">
      <div id="box1"> 
        <img src="https://pp.userapi.com/c622225/v622225117/10f33/47AAEI48pJU.jpg?ava=1"
          style="width:180px; height:160px;">
      </div>
      <div id="box2">
        <p> We can create as many divs as many we want side by side with the same height and also with the different heights. </p>
      </div>
      <div id="box3"> 
        <img src="https://pp.userapi.com/c622225/v622225117/10f33/47AAEI48pJU.jpg?ava=1"
          style="width:180px; height:160px;"> 
      </div>
    </div>
</html>

In this example we used CSS padding, margin properties. The padding property creates padding space on all sides of an element’s content. The margin property in CSS creates a space around the element. Also, you can choose any color you want from the color picker for the text.

Let’s see another example.

Example

<!DOCTYPE html>
<html>
  <title>Title of the document</title>
  <head>
    <style> 
      .box{
      display: -webkit-box;           
      display: -moz-box;         
      display: -ms-flexbox;      
      display: -webkit-flex;     
      display: flex;   
      width: 310px;
      height:310px;
      border:2px solid green;
      }
      .box div {
      width:100px;
      padding:15px;
      text-align:center;
      color:#000000;
      font-family: arial, sans-serif;
      }
      .green {background-color: green;}
      .blue {background-color: blue;}
      .gray {background-color: gray;}
      .pink {background-color: pink;}
    </style>
  </head>
  <body>
    <h2>Flex property example</h2>
    <div class="box">
      <div class="green">GREEN</div>
      <div class="blue">BLUE</div>
      <div class="gray">GRAY</div>
      <div class="pink">PINK</div>
    </div>
  </body>
</html>

Here we used display property with the "flex" value. The display property defines the type of the box which is used for an HTML element. The "flex" value displays an element as a block-level-flex container.

Let’s see another example.

Here is the result of our code.

<!DOCTYPE html>
<html>
  <title>Title of the document</title>
  <head>
    <style> 
      .box div {
      width:100px;
      display:inline-block;
      padding:15px;
      text-align:center;
      color:#000000;
      font-family: arial, sans-serif;
      }
      .green {background-color: green;}
      .blue {background-color: blue;}
      .gray {background-color: gray;}
      .pink {background-color: pink;}
      .yellow {background-color: yellow;}
    </style>
  </head>
  <body>
    <h2>Flex property example</h2>
    <div class="box">
      <div class="green">GREEN</div>
      <div class="blue">BLUE</div>
      <div class="gray">GRAY</div>
      <div class="pink">PINK</div>
      <div class="yellow">YELLOW</div>
    </div>
  </body>
</html>

In this example we used display property with the "inline-block" value. "Inline-block" value displays an element as an inline-level block container. We used font-family property which allows creating a prioritized list of font family names and/or generic family names for the selected element.


Do you find this helpful?

Related articles