The Wayback Machine - https://web.archive.org/web/20250324030147/https://www.geeksforgeeks.org/how-to-define-two-column-layout-using-flexbox/
Open In App

How to Define Two Column Layout using Flexbox?

Last Updated : 20 Jan, 2025
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Share
Report
News Follow

Here are the methods to define a two-column layout using Flexbox:

Method 1. Using display: flex and flex-direction: row

This method creates a horizontal two-column layout by setting the flex-direction to row.

HTML
<html>
<head>
    <style>
        .container {
            display: flex;
            flex-direction: row;
        }
        .column {
            flex: 1;
            padding: 10px;
            box-sizing: border-box;
        }
        .left-column {
            background-color: #f0f0f0;
        }
        .right-column {
            background-color: #d0d0d0;
        }
    </style>
</head>
<body>
    <div class="container">
        <div class="column left-column">
            <h2>Left Column</h2>
            <p>This is the left column content.</p>
        </div>
        <div class="column right-column">
            <h2>Right Column</h2>
            <p>This is the right column content.</p>
        </div>
    </div>
</body>
</html>
  • The .container div is a flex container with flex-direction: row, aligning child elements horizontally.
  • Both columns have flex: 1, making them occupy equal space.

Method 2. Using Flex Container with Flex Wrap

This method ensures a responsive two-column layout that wraps columns to the next line on smaller screens.

HTML
<html>
<head>
    <style>
        .container {
            display: flex;
            flex-wrap: wrap;
        }
        .column {
            flex: 1;
            padding: 10px;
            box-sizing: border-box;
        }
        .left-column {
            background-color: #f0f0f0;
        }
        .right-column {
            background-color: #d0d0d0;
        }
        @media (max-width: 768px) {
            .column {
                flex: 100%;
            }
        }
    </style>
</head>
<body>
    <div class="container">
        <div class="column left-column">
            <h2>Left Column</h2>
            <p>This is the left column content.</p>
        </div>
        <div class="column right-column">
            <h2>Right Column</h2>
            <p>This is the right column content.</p>
        </div>
    </div>
</body>
</html>
  • The flex-wrap: wrap property ensures the columns wrap onto the next line when the container’s width is insufficient.
  • A media query adjusts the layout for screens smaller than 768px, stacking the columns vertically.

How to Define Two Column Layout using Flexbox – FAQs

How do I adjust the width of one column in a two-column layout?

Set different flex values for each column, e.g., flex: 2 for one column and flex: 1 for the other.

Can I add space between the two columns?

Use the gap property on the flex container, e.g., gap: 20px;, to add consistent spacing.

How can I align items inside each column?

Apply align-items or justify-content to the flex container or use text alignment styles on individual columns.

Is Flexbox compatible with all browsers?

Flexbox is supported in all modern browsers, but it’s best to test for older versions or use prefixes if necessary.

Can I mix Flexbox with other layout techniques like Grid?

Yes, you can combine Flexbox and CSS Grid for complex layouts, leveraging the strengths of both.



Next Article

Similar Reads

three90RightbarBannerImg