问题
For the HTML
<section class="flex-container">
<div class="item"></div>
<div class="item"></div>
</section>
I have defined the following css
.flex-container {
display: flex;
flex-wrap: wrap;
min-height: 100vh;
.item {
background-color: #6f42c1;
flex: 3;
}
.item:nth-child(2) {
background-color: #0c5460;
flex: 5;
}
@media (max-width: 768px) {
.item:first-child {
flex-basis: 100%;
}
.item:nth-child(2) {
flex-shrink: 0;
}
}
}
When the viewport is greater than 768px I get the desired behaviour, i.e., two divs taking up a proportion of the screen, see below
but when I drop below 768px, my second item (green div) disappears
How can I adjust my css such that I maintain proportioned divs after the flex items have wrapped?
回答1:
Simply change the direction:
body {
margin:0;
}
.flex-container {
display: flex;
flex-wrap: wrap;
min-height: 100vh;
}
.item {
background-color: #6f42c1;
flex: 3;
}
.item:nth-child(2) {
background-color: #0c5460;
flex: 5;
}
@media (max-width: 768px) {
.flex-container {
flex-direction: column;
}
}
<section class="flex-container">
<div class="item"></div>
<div class="item"></div>
</section>
来源:https://stackoverflow.com/questions/53438205/prevent-flex-box-item-shrinking-after-wrap