Your Calculated Grid Size
Code
1<div class="flex-container-4 flex-gap-20">
2 <div class="flex-item-1">1 of 4</div>
3 <div class="flex-item-2">2 of 4</div>
4 <div class="flex-item-3">3 of 4</div>
5 <div class="flex-item-4">4 of 4</div>
6</div>1.flex-container-4 {
2 display: flex;
3 flex-wrap: wrap;
4 --flex-items: 4;
5}
6
7.flex-gap-20 {
8 gap: 20px;
9 --flex-gap: 20px;
10}1.flex-item {
2 flex-basis: calc(((100% / var(--flex-items) * var(--flex-item-span)) - (var(--flex-gap) * (var(--flex-items) - var(--flex-item-span)) / var(--flex-items))));
3}
4
5.flex-item-1 {
6 --flex-item-span: 1;
7}
8
9.flex-item-2 {
10 --flex-item-span: 2;
11}
12
13.flex-item-3 {
14 --flex-item-span: 3;
15}
16
17.flex-item-4 {
18 --flex-item-span: 4;
19}How it works
creates a flexible container that arranges items horizontally with wrapping. This is equivalent to using flex "flex-wrap" in Tailwind CSS. You'll use this as your main wrapper to contain your grid items.
sets the spacing between grid items, where X is the gap size in pixels. This creates consistent spacing throughout your grid layout without needing to manage margins on individual items.
calculates the precise width for grid items using CSS custom properties. It determines the flex-basis by considering the total number of items, the item span, and gap spacing to create perfectly aligned grid layouts with consistent spacing.
creates a flexible grid item with a specific width fraction, where X represents the number of columns this item should span. This allows for precise control over item sizing within your responsive grid layout.