How to Create a 2-Column Layout Grid with CSS?
Last Updated :
06 Aug, 2024
Creating a two-column layout is a common design pattern used in web development. This layout helps to organize content efficiently and is used across various types of websites. A 2-column layout divides the webpage into two sections, which can be useful for creating sidebars, main content areas, or other types of content distribution.
These are the following approaches:
Using CSS Flexbox
Flexbox is a modern CSS layout module that allows for flexible and efficient layouts. It is especially useful for creating complex layouts and aligning content. The "display: flex;"
property sets up a flex container. This enables the use of flexbox properties for its children. The "flex: 1;"
property makes each column take up an equal amount of space within the container. the child selectors apply different background colors to the two columns for visual distinction.
Example: This example shows the implementation of the above-explained approach.
HTML
<!DOCTYPE html>
<html>
<head>
<meta name="viewport"
content="width=device-width,
initial-scale=1">
<style>
.container {
display: flex;
/* Enables flexbox layout */
}
.column {
flex: 1;
/* Each column takes up
an equal amount of space */
padding: 10px;
/* Adds padding inside each column */
}
.column:nth-child(1) {
background-color: #f2f2f2;
/* Light grey background
for the first column */
}
.column:nth-child(2) {
background-color: #e6e6e6;
/* Slightly darker grey background
for the second column */
}
</style>
</head>
<body>
<h2>Flexbox 2-Column Layout</h2>
<div class="container">
<div class="column">
<h2>Column 1</h2>
<p>Some text..</p>
</div>
<div class="column">
<h2>Column 2</h2>
<p>Some text..</p>
</div>
</div>
</body>
</html>
Output:
using flexboxUsing CSS Grid
CSS Grid Layout is a powerful layout system that allows for two-dimensional layouts with rows and columns. The "display: grid;"
property sets up a grid container. "grid-template-columns: 1fr 1fr;"
This property creates two columns of equal width. The 1fr
unit represents a fraction of the available space. "gap: 10px;"
Adds a 10-pixel gap between the columns.
Example: This example shows the use of CSS grid to create 2-column layout.
HTML
<!DOCTYPE html>
<html>
<head>
<meta name="viewport"
content="width=device-width,
initial-scale=1">
<style>
.container {
display: grid;
/* Enables grid layout */
grid-template-columns: 1fr 1fr;
/* Creates two equal-width columns */
gap: 10px;
/* Adds space between columns */
}
.column {
padding: 10px;
/* Adds padding inside each column */
}
.column:nth-child(1) {
background-color: #f2f2f2;
/* Light grey background for the first column */
}
.column:nth-child(2) {
background-color: #e6e6e6;
/* Slightly darker grey
background for the second column */
}
</style>
</head>
<body>
<h2>CSS Grid 2-Column Layout</h2>
<div class="container">
<div class="column">
<h2>Column 1</h2>
<p>Some text..</p>
</div>
<div class="column">
<h2>Column 2</h2>
<p>Some text..</p>
</div>
</div>
</body>
</html>
Output:
Using CSS GridUsing Inline-Block Property
The inline-block
method is another way to achieve a column layout, although it's less common in modern layouts. "The font-size: 0;"
property removes any whitespace between inline-block elements. The "display: inline-block;"
property allows columns to sit side by side, and width: 49%;
ensures each column is nearly half the width of the container (accounting for any padding).
Example: This example shows the implementation of the above-explained approach.
HTML
<!DOCTYPE html>
<html>
<head>
<meta name="viewport"
content="width=device-width,
initial-scale=1">
<style>
.container {
font-size: 0;
/* Removes whitespace between
inline-block elements */
}
.column {
display: inline-block;
/* Allows columns to sit next to each other */
width: 49%;
/* Each column takes up 49% of the container width */
vertical-align: top;
/* Aligns columns to the top */
padding: 10px;
/* Adds padding inside each column */
box-sizing: border-box;
/* Includes padding in the column width */
}
.column:nth-child(1) {
background-color: #f2f2f2;
/* Light grey background
for the first column */
}
.column:nth-child(2) {
background-color: #e6e6e6;
/* Slightly darker grey
background for the second column */
}
</style>
</head>
<body>
<h2>Inline-Block 2-Column Layout</h2>
<div class="container">
<div class="column">
<h2>Column 1</h2>
<p>Some text..</p>
</div>
<div class="column">
<h2>Column 2</h2>
<p>Some text..</p>
</div>
</div>
</body>
</html>
Output:
Using Inline - block Conclusion
Each method for creating a 2-column layout offers distinct advantages and can be selected based on the specific requirements of a project. Flexbox and Grid are modern and versatile, making them suitable for most design scenarios. Float and Inline-Block, though older, are still useful for simpler layouts or legacy projects. The best approach depends on the design needs and browser compatibility requirements.
Similar Reads
How to Create a 4-Column Layout Grid with CSS? We can create a layout with multiple columns using CSS. It is easier to design complex layouts. In this article, we are going to discuss how to create a 4-column layout grid using CSS. We will discuss different approaches, their syntax, and code examples of each method. A 4-column layout grid is a c
3 min read
How to Create a 3-Column Layout Grid with CSS? Grid in CSS is a powerful layout system that allows you to create complex, responsive designs with ease. By defining rows and columns, you can position elements precisely within a grid container. Using grid properties like grid-template-columns, you can create flexible and adaptive layouts, such as
3 min read
How to Create a Four-Column Layout with Bootstrap ? In Bootstrap, creating a column layout is important for organizing content into a structured grid system. A four-column layout allows for efficient arrangement of content, such as displaying courses, products, or services, with flexibility in responsiveness for various screen sizes. Below are the ap
2 min read
How to create a Trello Layout with CSS Grid and Flexbox ? Trello layout is used to organize or manage information in stages. It can be created using CSS Grid and Flexbox. Let's create the layout as shown in the below image. In the image, Trello Layout consists of Trello List which holds the items or information in it. Layout Structure: Trello layout Exampl
2 min read
How to Create a Responsive CSS Grid Layout ? Here are different ways to create a responsive grid layout with CSS.1. Using auto-fill PropertyThis method can be used CSS Grid for a responsive layout. The grid-template-columns property adjusts columns based on space, keeping a minimum width of 200 pixels. Gaps between items are set with grid-gap.
3 min read