How To Put 2 Divs Side By Side

7 min read

Putting two divs side by side is one of the most common layout tasks in web development, and it is also one of the best ways to understand how CSS controls the placement of elements on a page. Whether you are building a two-column blog layout, a product comparison section, a login form with supporting text, or a dashboard with multiple panels, learning how to put 2 divs side by side gives you a practical foundation for modern page design. In this guide, you will learn the most reliable methods, when to use each one, and how to avoid the most common beginner mistakes that prevent div elements from sitting next to each other.

Why Two Divs Do Not Always Appear Side by Side

By default, a <div> element is a block-level element. Still, this means it behaves like a full-width block and normally pushes the next element onto a new line. On top of that, if you place two divs inside the same container without applying any layout styles, they will usually appear stacked vertically, one above the other. This is completely normal and not an error That's the whole idea..

To make two divs appear next to each other, you need to change how they participate in the layout. You can do this using modern layout methods such as CSS Flexbox and CSS Grid, or older techniques like inline-block and float. The best choice depends on your goals, browser support needs, and whether you want a simple side-by-side layout or a more complex responsive design Easy to understand, harder to ignore. Nothing fancy..

The Basic HTML Structure for Two Divs

Before exploring CSS methods, start with a clean HTML structure. A typical example looks like this:

Box 1
Box 2

Here, the outer .container div acts as the parent wrapper, while the two inner divs are the elements you want to place side by side. Giving each div a class is helpful because it lets you style them individually and clearly in your CSS Not complicated — just consistent. Worth knowing..

Not the most exciting part, but easily the most useful It's one of those things that adds up..

A simple starting style might look like this:

.box {
  background-color: #f4f4f4;
  padding: 20px;
  border: 1px solid #ccc;
}

At this point, the boxes may still appear stacked unless you apply a layout method to the parent container. That is the key point: the parent container controls how the child divs are positioned That alone is useful..

Method 1: Use CSS Flexbox to Place Divs Side by Side

The most beginner-friendly and widely used method is Flexbox. Flexbox is designed for arranging items in a single row or column, making it perfect for putting two divs side by side That's the part that actually makes a difference..

Here is the simplest way to do it:

.container {
  display: flex;
}

That single property is often enough. By default, flex-direction is row, so the child divs will be placed horizontally from left to right Less friction, more output..

If you want to control the spacing between the two boxes, you can use the gap property:

.container {
  display: flex;
  gap: 20px;
}

This creates a 20-pixel space between the divs, which is much cleaner than manually adding margins.

Making Flexbox Side-by-Side Layouts Responsive

One of the strengths of Flexbox is how easily it adapts to smaller screens. If you want the divs to stack vertically on narrow screens, you can use flex-wrap:

.container {
  display: flex;
  flex-wrap: wrap;
  gap: 20px;
}

If you want each div to take up a reasonable amount of space before wrapping, you can add widths:

.box {
  flex: 1 1 300px;
}

This allows each box to grow, shrink, and wrap neatly when there is not enough room. This makes Flexbox side-by-side layouts one of the most practical solutions for real-world responsive websites Not complicated — just consistent..

Method 2: Use CSS Grid for Precise Two-Column Layouts

Another excellent way to place two divs side by side is CSS Grid. While Flexbox is excellent for one-dimensional layouts, Grid is ideal when you want more control over rows and columns Worth keeping that in mind..

Here is a simple Grid solution:

.container {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 20px;
}

In this example, the container creates two equal-width columns. Since the two divs fill the available grid cells, they naturally appear side by side That alone is useful..

When CSS Grid Is Better Than Flexbox

CSS Grid is especially useful when you need:

  • Equal-height columns without extra work
  • Precise alignment across both rows and columns
  • A two-column layout that becomes a one-column layout at smaller sizes
  • More structured layouts such as dashboards or card grids

As an example, if you want the divs to stack on mobile, you can adjust the grid template:

@media (max-width: 768px) {
  .container {
    grid-template-columns: 1fr;
  }
}

This is clean, efficient, and highly reliable for how to put 2 divs side by side in a way that remains responsive.

Method 3: Use inline-block for a Simple Classic Approach

Before Flexbox and Grid became standard, developers often used display: inline-block to place divs horizontally. This method still works and can be useful in simple situations

where you don't need the advanced alignment or distribution features of modern layout modules.

Here is how it looks in practice:

.container {
  font-size: 0; /* Removes whitespace gap between elements */
}

.box {
  display: inline-block;
  width: 50%;
  font-size: 16px; /* Reset font size for content */
  vertical-align: top; /* Prevents alignment issues with varying content heights */
  box-sizing: border-box; /* Ensures padding/border don't break the 50% width */
}

The inline-block Whitespace Issue

A notorious quirk of inline-block is that HTML whitespace (line breaks or spaces between the div tags in your markup) renders as a visible gap—roughly 4px—between the boxes. This breaks perfect 50%/50% splits.

Common fixes include:

  • Setting font-size: 0 on the parent (and resetting it on the children), as shown above. On the flip side, * Using HTML comments to bridge the tags (</div><! g.Now, * **Removing whitespace in the HTML** (e. , <div class="box"></div><div class="box"></div>). -- --><div class="box">).

While functional, this fragility is why inline-block has largely been superseded by Flexbox and Grid for primary layout structures That's the part that actually makes a difference..

Method 4: Floats (Legacy Context Only)

For completeness, it is worth mentioning float: left, the original method for side-by-side layouts before inline-block.

.box {
  float: left;
  width: 50%;
  box-sizing: border-box;
}

.container::after {
  content: "";
  display: table;
  clear: both; /* Essential: prevents container collapse */
}

Why avoid this today: Floats remove elements from the normal document flow, requiring a "clearfix" hack (like the ::after pseudo-element above) to prevent the parent container from collapsing to zero height. They also lack vertical alignment controls and make centering difficult. Reserve floats strictly for wrapping text around images.


Quick Comparison: Which Method Should You Choose?

Method Best For Responsiveness Vertical Alignment Browser Support
Flexbox General purpose, dynamic content, single-row/column layouts. Excellent (flex-wrap, gap). Trivial (align-items). Day to day, Universal (IE10+ with prefixes). Here's the thing —
CSS Grid Structured 2D layouts, strict columns/rows, dashboards. Excellent (media queries on grid-template). Trivial (align-items / place-items). And Universal (IE10+ with prefixes). Plus,
Inline-block Simple fallbacks, email templates, legacy browser support. Manual (media queries on width). Manual (vertical-align). Here's the thing — Universal (IE8+).
Floats Text wrapping around images; not recommended for layout. Difficult. Very Difficult. Universal.

Easier said than done, but still worth knowing Not complicated — just consistent..


Conclusion

For modern web development, Flexbox is the default winner for placing two divs side by side. It requires the least amount of code (display: flex), handles spacing natively with gap, and adapts to responsive requirements effortlessly via flex-wrap.

Reach for CSS Grid when your design demands strict two-dimensional control—such as a dashboard where cards must align perfectly across both rows and columns simultaneously.

The older techniques (inline-block, float) remain valid CSS tools, but they belong in specific niches: inline-block for inline-level formatting contexts (like navigation pills or icon groups) and float strictly for editorial text wrapping. Using them for primary page layout introduces unnecessary fragility and maintenance debt.

By mastering Flexbox and Grid, you cover 99% of layout scenarios with cleaner, more readable, and future-proof code Most people skip this — try not to. And it works..

Just Came Out

Just Went Up

You Might Find Useful

Hand-Picked Neighbors

Thank you for reading about How To Put 2 Divs Side By Side. We hope the information has been useful. Feel free to contact us if you have any questions. See you next time — don't forget to bookmark!
⌂ Back to Home