I saw a flexbox layout like this on a website I recently saw and I'm wondering where the 13.333px
comes from in the flex: 0 0 calc(33.333% - 13.333px)
rule.
There is a margin-left
set on each of the flex items, and then obviously to make the left side flush with the side of the container, you use the .flex .flex-item:nth-child(3n+1)
rule to remove the margin from the start of every row.
In my head that means that there's 40px of padding in each row since two items still have the margins. So, I would expect it to be 33.333% - 40px
. But, doing that leaves extra space in the container.
Where does the 13.333px
come from? I'm assuming it's just something mathematical that I'm not getting.
.container {
max-width: 1140px;
margin: 0 auto;
}
.flex {
display: flex;
flex-wrap: wrap;
}
.flex .flex-item {
flex: 0 0 calc(33.333% - 13.333px);
margin: 0 0 16px 20px;
}
.flex .flex-item:nth-child(3n+1) {
margin-left: 0;
}
img {
max-width: 100%;
height: auto;
}
<div class="container">
<div class="flex">
<div class="flex-item"><img src="https://via.placeholder.com/800x400" /></div>
<div class="flex-item"><img src="https://via.placeholder.com/800x400" /></div>
<div class="flex-item"><img src="https://via.placeholder.com/800x400" /></div>
<div class="flex-item"><img src="https://via.placeholder.com/800x400" /></div>
<div class="flex-item"><img src="https://via.placeholder.com/800x400" /></div>
<div class="flex-item"><img src="https://via.placeholder.com/800x400" /></div>
<div class="flex-item"><img src="https://via.placeholder.com/800x400" /></div>
<div class="flex-item"><img src="https://via.placeholder.com/800x400" /></div>
<div class="flex-item"><img src="https://via.placeholder.com/800x400" /></div>
</div>
</div>
The align-self property specifies the alignment for the selected item inside the flexible container. The align-self property overrides the default alignment set by the container's align-items property.
Flexbox was introduced in CSS version 3 to arrange items in a container more efficiently and dynamically. Before flexbox, there were initially four layout methods which we have listed below. For the purpose of creating sections in a web page, the block layout is used. It is a layout method that is used for text.
The CSS display: flex property sets an HTML element as a block level flex container which takes the full width of its parent container. Any child elements that reside within the flex container are called flex items.
Here is the expanded formula so you can better see. Basically, we remove all the margin from one row which is created by 2 images (20px * 2
) then we divide everything by 3. So the 13.33px
is 2/3 * 20px
(only 2 of the 3 have 20px margin)
.container {
max-width: 1140px;
margin: 0 auto;
}
.flex {
display: flex;
flex-wrap: wrap;
}
.flex .flex-item {
flex: 0 0 calc((100% - 20px * 2)/3);
margin: 0 0 16px 20px;
}
.flex .flex-item:nth-child(3n+1) {
margin-left: 0;
}
img {
max-width: 100%;
height: auto;
}
<div class="container">
<div class="flex">
<div class="flex-item"><img src="https://via.placeholder.com/800x400" /></div>
<div class="flex-item"><img src="https://via.placeholder.com/800x400" /></div>
<div class="flex-item"><img src="https://via.placeholder.com/800x400" /></div>
<div class="flex-item"><img src="https://via.placeholder.com/800x400" /></div>
<div class="flex-item"><img src="https://via.placeholder.com/800x400" /></div>
<div class="flex-item"><img src="https://via.placeholder.com/800x400" /></div>
<div class="flex-item"><img src="https://via.placeholder.com/800x400" /></div>
<div class="flex-item"><img src="https://via.placeholder.com/800x400" /></div>
<div class="flex-item"><img src="https://via.placeholder.com/800x400" /></div>
</div>
</div>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With