admin管理员组文章数量:1427478
I made this example: /
What i want to achieve is making the divs always fit the #wrapper
height (which is 500px fixed) in any case.
So that when toggling a new div (click the button) all the divs should adjust themselves to fit the full wrapper height.
I would like to achieve this in pure css if possible but i have no ideas atm.
Any advice? Maybe some flexbox ?!
I made this example: https://jsfiddle/jpdjkdr0/
What i want to achieve is making the divs always fit the #wrapper
height (which is 500px fixed) in any case.
So that when toggling a new div (click the button) all the divs should adjust themselves to fit the full wrapper height.
I would like to achieve this in pure css if possible but i have no ideas atm.
Any advice? Maybe some flexbox ?!
Share Improve this question edited Mar 2, 2017 at 14:49 Filippo oretti asked Mar 2, 2017 at 14:40 Filippo orettiFilippo oretti 49.9k96 gold badges229 silver badges351 bronze badges 2- 2 You could use flexbox: jsfiddle/jpdjkdr0/2 – Jazcash Commented Mar 2, 2017 at 14:43
- @Jazcash thanks for the tip ;) – Filippo oretti Commented Mar 2, 2017 at 14:48
1 Answer
Reset to default 6You can add display: flex; flex-direction: column;
to the #wrapper
and set the child divs to flex: 1
.
This will allow them to grow to fill the available space: https://jsfiddle/jpdjkdr0/1/
.hide {
display:none;
}
#wrapper {
height: 500px;
width:500px;
border: 10px solid violet;
display: flex;
flex-direction: column;
}
#wrapper div {
flex: 1;
}
#div-1 {
background: brown;
color: white;
}
#div-2 {
background: green;
color: white;
}
#div-3 {
background: red;
color: white;
}
#div-4 {
background: blue;
color: white;
}
<script src="https://ajax.googleapis./ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button onclick="$('#div-1').toggle()">
show div
</button>
<div id="wrapper">
<div id="div-1" class="hide">
1
</div>
<div id="div-2">
2
</div>
<div id="div-3">
3
</div>
<div id="div-4">
4
</div>
</div>
本文标签: javascriptCSS how to make divs fill all the vertical spaceStack Overflow
版权声明:本文标题:javascript - CSS how to make divs fill all the vertical space - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745502921a2661113.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论