Open In App

How to float three div side by side using CSS?

Last Updated : 02 Jul, 2025
Summarize
Comments
Improve
Suggest changes
Share
Like Article
Like
Report

Aligning three <div> elements side by side is a fundamental layout technique in web design. Whether you're creating a profile card section, a features row, or a three-column layout for a homepage, placing elements in a horizontal line is a common requirement.

While the traditional approach relies on the float property, modern CSS offers cleaner and more flexible solutions like Flexbox and Grid that make layouts easier to build and maintain.

Method 1: Using float

Using the float property, you can align elements horizontally by applying float: left or float: right to each element. This causes the elements to float next to each other in a row, allowing them to stack side by side within the container.

index.html
<!DOCTYPE html>
<html>
<head>
  <style>
    .container {
      width: 100%;
      overflow: hidden; /* Clearfix alternative */
    }

    .box {
      float: left;
      width: 33.33%;
      height: 280px;
      padding: 15px;
      color: white;
      box-sizing: border-box;
    }

    .red { background-color: red; }
    .green { background-color: green; }
    .blue { background-color: blue; }

    h1 {
      text-align: center;
      color: green;
    }
  </style>
</head>
<body>
  <h1>GeeksforGeeks</h1>
  <div class="container">
    <div class="box red">
      <h2>Learn:</h2>
      GeeksforGeeks is a great platform to learn programming and prepare for interviews at top tech companies.
    </div>
    <div class="box green">
      <h2>GeeksforGeeks:</h2>
      The site offers free and premium content including MCQs and coding questions to help you ace placements.
    </div>
    <div class="box blue">
      <h2>Contribute:</h2>
      You can also write and contribute articles to the platform, helping fellow learners grow.
    </div>
  </div>
</body>
</html>

Output:

image
Using Float

Method 2: Using display (Flexbox ,Grid, Inline-block)

Modern CSS layout techniques allow elements to be aligned side by side using the display property. You can achieve this using:

  • display: flex – creates a flexible row-based layout that adapts to screen size and spacing needs.
  • display: inline-block – places elements next to each other like inline content while retaining block-level styling.
  • display: grid – offers powerful control over both rows and columns, making it ideal for two-dimensional layouts.

Each method has its own use case depending on the complexity and responsiveness of your layout.

index.html
<!DOCTYPE html>
<html>
<head>
  <style>
    .container {
      display: flex;
      gap: 10px;
      padding: 20px;
    }

    .box {
      flex: 1;
      padding: 15px;
      height: 280px;
      color: white;
      text-align: justify;
      box-sizing: border-box;
    }

    .red { background-color: red; }
    .green { background-color: green; }
    .blue { background-color: blue; }

    h1 {
      text-align: center;
      color: green;
    }
  </style>
</head>
<body>
  <h1>GeeksforGeeks</h1>
  <div class="container">
    <div class="box red"><h2>Learn:</h2> Access tutorials, articles, and videos on programming and computer science topics.</div>
    <div class="box green"><h2>Practice:</h2> Sharpen your skills with coding problems, quizzes, and interview questions.</div>
    <div class="box blue"><h2>Contribute:</h2> Join the GFG community by submitting articles and helping others grow.</div>
  </div>
</body>
</html>

Output:

using-display-property
Using display: flex

Conclusion

Lining up three <div>s side by side is easy once you know the right tools. While float used to be the go-to method, today we have better options like flexbox and grid that are simpler, cleaner, and responsive by default. For most layouts, Flexbox is your best friend—it's flexible, easy to use, and works great across devices.

You May Also Try


Similar Reads