I have the following snippet. It displays 4 cols in one row. When you click on any top button, the script will add a new a <div class="break-row" />
element based on the clicked button which will break the columns into new line. The problem here that the .break-row
element occupy vertical space even when its height is 0px. I think in this case the .col
element should fill up the vertical available space.
My target is to remove the grey empty area and .col
element stretch themself into that area. What is the explanation for that grey area and how can I get rid of it?
$('a').on('click', function(e) {
e.preventDefault();
$('.break-row').remove();
var breakAfter = $(this).data('breakafter');
if (breakAfter > 0) {
$('<div class="break-row" />').insertAfter('.col:nth-child(' + breakAfter + 'n)');
}
});
.row {
display: flex;
flex: 1 1 auto;
flex-wrap: wrap;
background: #222;
width: 500px;
height: 500px;
}
.col {
display: flex;
flex-flow: column;
flex: 1 1 auto;
}
.break-row {
width: 100%;
flex: 0 0 auto;
height: 0px;
}
a {
background: #456;
padding: 10px;
border-radius: 5px;
margin: 10px;
display: inline-block;
color: #fff;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<a href="#" data-breakafter="1">
Break after every col
</a>
<a href="#" data-breakafter="2">
Break after every 2 col
</a>
<a href="#" data-breakafter="3">
Break after every 3 col
</a>
<a href="#" data-breakafter="0">
No break
</a>
<div class="row">
<div class="col" style="background:#0f0;width:10%;">
aaa
</div>
<div class="col" style="background:#f00;width:40%;">
bbb
</div>
<div class="col" style="background:#0f0;width:30%;">
ccc
</div>
<div class="col" style="background:#f00;width:20%;">
ddd
</div>
</div>