Explain Codes LogoExplain Codes Logo

Flexbox: 4 items per row

html
responsive-design
flexbox
css-grid
Anton ShumikhinbyAnton Shumikhin·Aug 23, 2024
TLDR

To create a 4-item per row layout with Flexbox, set .container to display: flex and flex-wrap: wrap. Then, specify .item with flex: 0 0 25%, allotting precisely one quarter of the row to each item.

.container { display: flex; flex-wrap: wrap; /* Let's split the band into multiple rows. */ } .item { flex: 0 0 25%; /* I need only a quarter of the stage. */ padding: 10px; /* Not too close, give me some room to dance! */ }
<div class="container"> <div class="item">1</div> <div class="item">2</div> <div class="item">3</div> <div class="item">4</div> <!-- The party continues onto the next row! --> </div>

The above approach creates a four-column grid that is flexible across different screen sizes.

The formula for perfect grid harmony

To fine-tune your grid's responsiveness, flex-grow, flex-shrink, and flex-basis are your tools. In flex: 1 0 21%, each value signifies flex-grow, flex-shrink, and flex-basis respectively. This discreetly translates to items having the freedom to grow, remaining rigid without shrinking, and initially occupying 21% of the container's width.

Also, employing box-sizing: border-box warrants that padding and borders are included in width calculation. This maintains consistency, even when styling items.

Creating responsive layouts dynamically

Responsiveness demands percentages for child width and margins. The calc() function helps regulate margins ensuring all items fit like hand in glove:

.item { flex: 1 0 calc(25% - 20px); /* I love the space minus those margins, it's more than a crush! */ margin: 10px; }

Apply align-items to make all items stretch equally, regardless of content, or define a specific height to maintain a consistent height for the item rows:

.container { display: flex; flex-wrap: wrap; align-items: stretch; /* All the items grown tall together. It's family love! */ }

Conquering advanced layout scenarios

Expected plot twists like flexible gutters can be managed with a trick involving calc() function and negative margins:

.container { margin-left: -10px; /* An unexpected move to offset those margins */ } .item { flex: 1 0 calc(25% - 20px); margin-left: 10px; /* Give me my personal space */ }

Notice that CSS Grid is also a robust alternative. If you need more control, use grid's grid-template-columns property and handle consistent spacing with grid-column-gap and grid-row-gap.

.grid-container { display: grid; grid-template-columns: repeat(4, 1fr); grid-gap: 10px; }

Additional tips for assertive layouts

Make sizing adapt with 'auto'

"auto" keyword offers items to show relative flexibility in sizing compared to the container's space:

.item { flex: 1 0 auto; /* I need space as much as I can get */ }

Fallback to 'inline-block'

If legacy browsers sing your tune, display: inline-block is there for a fallback layout:

.item { display: inline-block; width: calc(25% - 20px); /* We got to keep the show running, old music plays too! */ margin: 10px; }

Seamless item wrapping

For a pleasing layout, spacing is key. Proper use of padding, margin, and gap properties ensure flow and alignment:

.item { padding: 5px; margin-right: 10px; margin-bottom: 10px; }

Live examples on CodePen

Live examples on CodePen can be insightful and reveal practical implementation: