grid-template-columns
property to set three equal or unequal columns.flex-wrap
to wrap items when there is not enough space.Media queries are used to apply different CSS styles based on the characteristics of the device, such as screen width, height, and orientation. In the context of our 3 - column grid, we use media queries to change the layout from a 3 - column to a single - column when the screen width is below a certain threshold.
display: grid
property.grid-template-columns
property to specify three columns. For example, grid-template-columns: repeat(3, 1fr)
creates three equal - width columns.grid-template-columns
property to 1fr
when the screen width is small.display: flex
.flex - basis
to set the initial size of each item.flex - direction
to column
when the screen width is small.Before the advent of CSS Grid and Flexbox, floats were used to create multi - column layouts. You would float three elements to the left and clear the floats to prevent layout issues. However, this method has many drawbacks, such as collapsing containers and difficulty in handling vertical alignment.
You can also use the display: inline - block
property to arrange elements in a row. But this method has issues with white - space between elements and vertical alignment.
<section>
and <article>
to structure your content. This makes your code more accessible and easier to maintain.rem
to create a more fluid layout.<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF - 8">
<meta name="viewport" content="width=device-width, initial - scale=1.0">
<style>
.grid - container {
display: grid;
grid-template-columns: repeat(3, 1fr);
gap: 20px;
}
.grid - item {
background-color: #f0f0f0;
padding: 20px;
}
@media (max - width: 768px) {
.grid - container {
grid-template-columns: 1fr;
}
}
</style>
</head>
<body>
<div class="grid - container">
<div class="grid - item">Column 1</div>
<div class="grid - item">Column 2</div>
<div class="grid - item">Column 3</div>
</div>
</body>
</html>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF - 8">
<meta name="viewport" content="width=device-width, initial - scale=1.0">
<style>
.flex - container {
display: flex;
gap: 20px;
}
.flex - item {
flex - basis: calc(33.333% - 20px);
background-color: #f0f0f0;
padding: 20px;
}
@media (max - width: 768px) {
.flex - container {
flex - direction: column;
}
.flex - item {
flex - basis: auto;
}
}
</style>
</head>
<body>
<div class="flex - container">
<div class="flex - item">Column 1</div>
<div class="flex - item">Column 2</div>
<div class="flex - item">Column 3</div>
</div>
</body>
</html>
Creating a 3 - column grid layout that breaks into a single - column layout at smaller widths is an essential skill in modern web design. Both CSS Grid and Flexbox provide powerful and flexible ways to achieve this. By following best practices like mobile - first design and using semantic HTML, you can create responsive and accessible websites.