Flexbox is a powerful layout system in CSS that allows you to create complex layouts with ease. One common use case for flexbox is creating a grid with a fixed number of items per row. In this tutorial, we will explore how to achieve this using flexbox.
To start, let’s consider the basic structure of our HTML and CSS. We have a container element (.parent
) that contains multiple child elements (.child
). We want to display these child elements in a grid with 4 items per row.
.parent {
display: flex;
flex-wrap: wrap;
}
.child {
/* styles for the child element */
}
The key property here is flex-wrap: wrap
, which tells flexbox to wrap the child elements to the next line when there is not enough space on the current line.
To control the number of items per row, we need to set a width on the child elements. One way to do this is by using the flex-basis
property, which sets the initial width of the flex item.
.child {
flex: 1 0 25%;
margin: 5px;
height: 100px;
background-color: blue;
}
In this example, we set flex-basis
to 25%
, which means each child element will take up 25% of the available space on the line. The flex-grow
property is set to 1
, which allows the child elements to grow and fill any available space.
However, using flex-basis
alone may not be enough, as it can be affected by other styles such as margins and padding. A more robust solution is to use the width
property in combination with box-sizing: border-box
.
.child {
width: calc(25% - 10px);
margin: 5px;
height: 100px;
background-color: blue;
box-sizing: border-box;
}
In this example, we use the calc
function to set the width of the child element to 25% minus the margin size. This ensures that the child elements fit perfectly in the grid without overflowing.
Another approach is to use negative margins and calc
for the gutters.
.parent {
display: flex;
flex-wrap: wrap;
margin-top: -10px;
margin-left: -10px;
}
.child {
width: calc(25% - 10px);
margin-left: 10px;
margin-top: 10px;
}
This approach can be useful when you need more control over the spacing between the child elements.
Alternatively, you can use CSS Grid to achieve a similar layout. CSS Grid is a more powerful and flexible layout system that allows you to create complex grids with ease.
.parent {
display: grid;
grid-template-columns: repeat(4, 1fr);
grid-column-gap: 10px;
grid-row-gap: 10px;
}
In this example, we use the grid-template-columns
property to define a grid with 4 columns. The repeat
function is used to create a repeating pattern of columns. The grid-column-gap
and grid-row-gap
properties are used to set the spacing between the columns and rows.
In conclusion, using flexbox to create a grid with a fixed number of items per row requires careful consideration of the width and margins of the child elements. By using flex-basis
, width
, and box-sizing: border-box
, you can create a robust and flexible layout that adapts to different screen sizes and devices. Alternatively, you can use CSS Grid to achieve a similar layout with more control over the spacing and alignment of the child elements.