How can I make my flexbox layout take 100% vertical space?
You should set height
of html, body, .wrapper
to 100%
(in order to inherit full height) and then just set a flex
value greater than 1
to .row3
and not on the others.
.wrapper, html, body {
height: 100%;
margin: 0;
}
.wrapper {
display: flex;
flex-direction: column;
}
#row1 {
background-color: red;
}
#row2 {
background-color: blue;
}
#row3 {
background-color: green;
flex:2;
display: flex;
}
#col1 {
background-color: yellow;
flex: 0 0 240px;
min-height: 100%;/* chrome needed it a question time , not anymore */
}
#col2 {
background-color: orange;
flex: 1 1;
min-height: 100%;/* chrome needed it a question time , not anymore */
}
#col3 {
background-color: purple;
flex: 0 0 240px;
min-height: 100%;/* chrome needed it a question time , not anymore */
}
<div class="wrapper">
<div id="row1">this is the header</div>
<div id="row2">this is the second line</div>
<div id="row3">
<div id="col1">col1</div>
<div id="col2">col2</div>
<div id="col3">col3</div>
</div>
</div>
Fill remaining vertical space with CSS using display:flex
Make it simple : DEMO
section { display: flex; flex-flow: column; height: 300px;}
header { background: tomato; /* no flex rules, it will grow */}
div { flex: 1; /* 1 and it will fill whole space left if no flex value are set to other children*/ background: gold; overflow: auto;}
footer { background: lightgreen; min-height: 60px; /* min-height has its purpose :) , unless you meant height*/}
<section> <header> header: sized to content <br/>(but is it really?) </header> <div> main content: fills remaining space<br> x <br>x<br>x<br>x<br>x<br>x<br>x<br>x<br>x<br>x<br> <!-- uncomment to see it break --> x<br>x<br>x<br>x<br>x<br>x<br>x<br>x<br>x<br> x <br>x<br>x<br>x<br>x<br>x<br>x<br>x<br>x<br> x <br>x<br>x<br>x<br>x<br>x<br>x<br>x<br>x<br> x <br>x<br>x<br>x<br>x<br>x<br>x<br>x<br>x<br> <!-- --> </div> <footer> footer: fixed height in px </footer></section>
How can I make Flexbox children 100% height of their parent?
Use align-items: stretch
Similar to David Storey's answer, my workaround is:
.flex-2 {
display: flex;
align-items: stretch;
}
Note that height: 100%
should be removed from the child component (see comments).
Alternatively to align-items
, you can use align-self
just on the .flex-2-child
item you want stretched.
Flexbox fill available space vertically
So you can try this:
flex-direction: column
for the flex container.flex: 1
for the element that needs to fill the remaining space.
See demo below where the flexbox
spans the viewport height:
body { margin: 0;}*{ box-sizing: border-box;}.row { display: flex; flex-direction: column; height: 100vh;}.flex { flex: 1;}.row, .row > * { border: 1px solid;}
<div class="row"> <div>some content</div> <div class="flex">This fills the available space</div> <!-- fills/grows available space --> <div>another content</div></div>
How to fill vertical space with flexbox?
An initial setting of a flex container is align-content: stretch
.
That means that flex lines will equally share all available space in the container along the cross axis. (A similar effect to flex: 1
on all items on the main axis.)
However, when you define a height for a flex item when the cross axis is vertical, or width, when the cross axis is horizontal, you override the align-content
default.
In your row-direction container, the cross axis is vertical. So if you remove height: 100%
, that allows align-content: stretch
to work.
Learn more about flex alignment on the cross axis here:
- How does flex-wrap work with align-self, align-items and align-content?
Learn more about flex alignment on the main axis here:
- In CSS Flexbox, why are there no "justify-items" and "justify-self" properties?
.container { display: flex; background: #ccc;}
.left { flex: 2; display: flex; flex-wrap: wrap; /* <--- this brings align-content into play */ /* flex-direction: row; <--- default setting; can be omitted */ /* align-items: stretch; <--- default setting; can be omitted */ /* height: 100%; */}
.right { flex: 1; border: 1px solid #000;}
.item { border: 1px solid #000; flex: 1 0 100%; align-self: stretch;}
<div class="container"> <div class="left"> <div class="item">Item 1</div> <div class="item">Item 2</div> </div> <div class="right"> Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. </div></div>
flexbox layout: take remaining space but do not expand more than 100% of remaining space
You did not close properly your div <div/>
instead </div>
, Also, less CSS can be used
body{margin:0}
<div style="height: 100vh; display: flex; flex-direction: column;">
<div>
This is some fixed heighted content
</div>
<div style="flex:1;overflow:auto;">
A lot of text content here that overflows the remaining space.
So this div should take up the remaining available space (100vh - space of div above)
and the inner content should become scrollable
But it ends up expanding the div to the text content and thus the whole screen becomes
scrollable instead of just the divA lot of text content here that overflows the remaining space.
So this div should take up the remaining available space (100vh - space of div above)
and the inner content should become scrollable
But it ends up expanding the div to the text content and thus the whole screen becomes
scrollable instead of just the divA lot of text content here that overflows the remaining space.
So this div should take up the remaining available space (100vh - space of div above)
and the inner content should become scrollable
But it ends up expanding the div to the text content and thus the whole screen becomes
scrollable instead of just the divA lot of text content here that overflows the remaining space.
So this div should take up the remaining available space (100vh - space of div above)
and the inner content should become scrollable
But it ends up expanding the div to the text content and thus the whole screen becomes
scrollable instead of just the divA lot of text content here that overflows the remaining space.
So this div should take up the remaining available space (100vh - space of div above)
and the inner content should become scrollable
But it ends up expanding the div to the text content and thus the whole screen becomes
scrollable instead of just the divA lot of text content here that overflows the remaining space.
So this div should take up the remaining available space (100vh - space of div above)
and the inner content should become scrollable
But it ends up expanding the div to the text content and thus the whole screen becomes
scrollable instead of just the div
</div>
</div>
Have flexbox fill up vertical space of browser for sidemenu
With your initial code sample, all you need is to also give .container
a height.
.container, .wrapper, html, body { height:100%; margin:0;}
.wrapper { display: flex; flex-direction: column;}
.row3 { background-color: green; flex:2; display: flex;}
<div class="container"> <div class="wrapper"> <div class="row3">Option One</div> <div class="row3">Option Two</div> <div class="row3">Option Three</div> </div></div>
Better way to set distance between flexbox items
- Flexbox doesn't have collapsing margins.
- Flexbox doesn't have anything akin to
border-spacing
for tables (edit: CSS propertygap
fulfills this role in newer browsers, Can I use)
Therefore achieving what you are asking for is a bit more difficult.
In my experience, the "cleanest" way that doesn't use :first-child
/:last-child
and works without any modification on flex-wrap:wrap
is to set padding:5px
on the container and margin:5px
on the children. That will produce a 10px
gap between each child and between each child and their parent.
Demo
.upper {
margin: 30px;
display: flex;
flex-direction: row;
width: 300px;
height: 80px;
border: 1px red solid;
padding: 5px; /* this */
}
.upper > div {
flex: 1 1 auto;
border: 1px red solid;
text-align: center;
margin: 5px; /* and that, will result in a 10px gap */
}
.upper.mc /* multicol test */ {
flex-direction: column;
flex-wrap: wrap;
width: 200px;
height: 200px;
}
<div class="upper">
<div>aaa<br/>aaa</div>
<div>aaa</div>
<div>aaa<br/>aaa</div>
<div>aaa<br/>aaa<br/>aaa</div>
<div>aaa</div>
<div>aaa</div>
</div>
<div class="upper mc">
<div>aaa<br/>aaa</div>
<div>aaa</div>
<div>aaa<br/>aaa</div>
<div>aaa<br/>aaa<br/>aaa</div>
<div>aaa</div>
<div>aaa</div>
</div>
Related Topics
Why Anchor Tag Does Not Take Height and Width of Its Containing Element
Html: How to Have a Form Tag in Each Table Row in a Xhtml Valid Way
Css ''Background-Color" Attribute Not Working on Checkbox Inside ≪Div≫
How to Position Two Elements Side by Side Using Css
Make an HTML Svg Object Also a Clickable Link
Why Should I Prepend My Custom Attributes with "Data-"
How Do HTML Parses Work If They'Re Not Using Regexp
HTML Select Box Options on Hover
Absolute Positioning Inside Absolute Position
Is <Div> Inside <Label> Block Correct
Setting the Character Encoding in Form Submit For Internet Explorer
Html5 Canvas and Anti-Aliasing
Animate/Shrink Navbar on Scroll Using Bootstrap
What Is Use of 'Initial' Value in Css
Why Can't Radio Buttons Be "Readonly"
How to Create a Curve Between Two Gradient Using CSS