CSS Flex Last Incomplete Row Items Take The Width Of Complete Row Items
Solution 1:
You may want to consider something along the lines of invisible flex items at the end of the list.
HTML
<div class="parent">
<div class="child">1</div>
<div class="child">2</div>
<div class="child">3</div>
<div class="child">4</div>
<div class="child">5</div>
<div class="child">6</div>
<div class="child">7</div>
<div class="child">8</div>
<div class="child hidden">9</div> /* new */
<div class="child hidden">10</div> /* new */
</div>
CSS
.hidden {
visibility: hidden;
height: 0;
font-size: 0;
margin: 0 10px;
}
Solution 2:
Give top the pseudo element the same properties that you are giving the elements, but a height very low.
But if more than 2 pseudo element are needed, you need another technique
.parent {
background: red;
max-width: 500px;
height: 400px;
display: flex;
flex-wrap: wrap;
}
.parent::after {
content: '';
flex: 130px 1;
margin: 10px;
background-color: yellow;
height: 10px;
}
.child {
background: blue;
margin: 10px;
flex: 130px 1;
color: #FFFFFF;
text-align: center;
line-height: 50px;
font-size: 60px;
}
<div class="parent">
<div class="child">1</div>
<div class="child">2</div>
<div class="child">3</div>
<div class="child">4</div>
<div class="child">5</div>
<div class="child">6</div>
<div class="child">7</div>
<div class="child">8</div>
</div>
Note that the real height for the pseudo would be 0px, and the background yellow wouldn't be there...
Those are just for demo purposes
Edit: FF handles different the situation where the minimun height comes from min-height and the situation where it comes from flex-basis. But Chrome handles those just the same. So my previous solution worked only for Chrome. (Don't know for sure which is "standard".
I changed the min-height of .child and moved it to flex-basis, now it works ok both in Chrome and FF.
Solution 3:
UPDATE
Ok, I finally figured out why OPs was seeing everything so differently than I was. I was using Chrome and OP was using Firefox. The following is the newest demo that should be compatible with Chrome and Firefox.
- The major changes include wrapping the whole layout in a flex-column container.
- Removed the
margins: 10px;because flexbox withbox-sizingdoesn't handle margins or borders as expected. - Used a plethora of min and max height and width properties.
- Used
justify-content: space-betweenandalign-items: space-betweenon column and row directions which made a tight yet flexible grid.
https://jsfiddle.net/zer00ne/fkczpggy/
.case {
background: red;
display: flex;
flex-flow: column wrap;
justify-content: space-between;
align-items: space-around;
margin: auto 0;
max-width: 500px;
overflow-y: auto;
}
.parent {
background: red;
height: 99vh;
min-height: 100%;
max-width: 500px;
display: flex;
flex-wrap: wrap;
justify-content: space-between;
align-items: space-between;
flex: 0 1 auto;
overflow-y: auto;
}
.child {
background: blue;
min-width: 130px;
min-height: 130px;
border: 1% solid transparent;
color: #FFFFFF;
height: auto;
text-align: center;
font-size: 60px;
flex: 1 0 10%;
margin: 1%;
}
.child:last-of-type {
visibility: hidden;
}
<section class="case">
<div class="parent">
<div class="child">1</div>
<div class="child">2</div>
<div class="child">3</div>
<div class="child">4</div>
<div class="child">5</div>
<div class="child">6</div>
<div class="child">7</div>
<div class="child">8</div>
<div class="child">9</div>
</div>
</section>
OLD
Here's a lazy but effective way:
Updated to reflect dynamic variable number of divs
Use nth-of-type(n+9)
or this would be better:
last-of-type
Add the 9th div
Then add this single line to your CSS:
.child:nth-of-type(n+9) { visibility: hidden; }or
.child:last-of-type { visibility: hidden; }
https://jsfiddle.net/zer00ne/4fp88arh/
.parent {
background: red;
max-width: 500px;
height: 400px;
display: flex;
flex-wrap: wrap;
justify-content: flex-start;
}
/*.parent:after {
content: '';
flex-grow: 1000000000;
}*/
.child {
background: blue;
min-width: 130px;
margin: 10px;
flex-grow: 1;
color: #FFFFFF;
text-align: center;
line-height: 50px;
font-size: 60px;
}
/*.child:nth-of-type(n+9) {
visibility: hidden;
}*/
.child:last-of-type {
visibility: hidden;
}
<div class="parent">
<div class="child">1</div>
<div class="child">2</div>
<div class="child">3</div>
<div class="child">4</div>
<div class="child">5</div>
<div class="child">6</div>
<div class="child">7</div>
<div class="child">8</div>
<div class="child"></div>
</div>
Post a Comment for "CSS Flex Last Incomplete Row Items Take The Width Of Complete Row Items"