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.
Height: 100%; not working with Flex Box
You need to make it stretch since your flex container is align-items: center
You can remove the height 100%, I added a class to the divider, it comes down to this
.divider {
align-self: stretch;
}
If you did not have the align center, it would of worked by default because the align items defaults to stretch but since you changed it to center and your divider has no content so the line does not show. Setting the divider itself to stretch again solves the problem and no need for the extra css
.card { margin-bottom: 30px;}
.card>.card-header { font-weight: 500; text-transform: uppercase; font-size: 15px; margin-bottom: 6px;}
.card>.card-header.light { color: #fff;}
.card>.card-body { background-color: #fff; border-radius: 12px; padding: 24px; -webkit-box-shadow: -2px 4px 34px 1px rgba(0, 0, 0, 0.15); -moz-box-shadow: -2px 4px 34px 1px rgba(0, 0, 0, 0.15); box-shadow: -2px 4px 34px 1px rgba(0, 0, 0, 0.15);}
.card>.card-body.server-status { display: flex; align-items: center;}
.card>.card-body.server-status>.counter { width: 50%; font-weight: 500; color: #95a0b7; font-size: 32px; display: flex; flex-direction: column; align-items: center;}
.card>.card-body.server-status>.counter>span { font-size: 15px!important; color: #0d2c4a!important; text-transform: capitalize;}
.divider { align-self: stretch;}
<div class="card"> <div class="card-header light"> Active Services </div> <div class="card-body server-status"> <div class="counter"> 7/9 <span> Servers running </span> </div> <div class="divider" style="border-left:1px solid #0d2c4a;"></div> <div class="chart"></div> </div></div>
How to stretch flex child to fill height of the container?
When using Flexbox, the major mistake is to start using height: 100%
all over.
We are used to that in many cases, though when it comes to Flexbox, it often breaks it instead.
The solution is simple, just remove the height: 100%
, and it will work automatically.
The reason it does, is for flex item in row direction (the default), the align-items
control the vertical behavior, and as its default is stretch
this just work as is.
Stack snippet
<div style='display: flex'>
<div style='background-color: yellow; width: 20px'>
</div>
<div style='background-color: blue'>
some<br> cool<br> text
</div>
</div>
Flexbox 100% height Issue
In addition to Michael Coker's answer, if you go all the way with Flexbox, you can cut down both your markup and CSS quite much and get this amazing result.
I also included the background image and media query you commented/asked about just to show how simple this can be done.
Made some notes in the CSS. When it comes to the header and footer, you can give them a height if you want, but is not needed for this to work, so I left them out so one can see how Flexbox excel in distributing the content...wish I had this technique 20 years ago :)
html, body { margin: 0;}.box { display: flex; flex-direction: column; height: 100vh; /* instead of using 100% all over, use viewport units once */ background-size: cover; background: black url(http://lorempixel.com/500/500/nature/4/) no-repeat center;}.box .row.content,.content .one-fifth.column { flex: 1; /* fill the space equal, no matter row or column direction */ display: flex;}
.box .row.header,.box .row.footer { color: white; }.box .row.content { background: #fff; }.yellow-back { background: #ffe001; }.red-back { background: #e31e25; }.green-back { background: #66af45; }.purple-back { background: #954294; }
@media screen and (max-width: 600px) { /* smaller screens */ .box .row.content { flex-direction: column; /* by simply swap direction it work on smaller screen */ }}
<div class="box"> <div class="row header"> <p><b>header</b><br /><br />(sized to content)</p> </div>
<div class="row content"> <div class="one-fifth column green-back">Here</div> <div class="one-fifth column red-back">Here</div> <div class="one-fifth column">Here</div> <div class="one-fifth column yellow-back">Here</div> <div class="one-fifth column purple-back">Here</div> </div>
<div class="row footer"> <p><b>footer</b> (fixed height)</p> </div></div>
Keeping flexbox container and overflowing children within 100% height
Check this https://codepen.io/anon/pen/EJYwmq
The problem is that items inside flexboxes have an implicit "min-height" equals to it's real height. You can set it's min-height: 0px
.
html, body {
height: 100%;
margin: 0;
}
#container {
height: 100%;
background-color: gray;
display: flex;
flex-direction: column;
}
#b {
flex: 100% 1 1;
min-height: 0px; // this is important!
overflow: hidden;
display: flex; // make it a flex too so you have better control of the scrollbar
flex-direction: column;
}
#s2 {
min-height: 0px;
max-height: 100%;
overflow: auto;
background-color: lightblue;
}
height 100% in flex
You can overlap (nest) flex boxes :
html, body {
margin: 0;
padding: 0;
height: 100%;
}
.wrapper {
border: 1px solid red;
padding: 20px;
min-height: 100%;
display: flex;
/* change flex-direction from column to row will work */
flex-direction: column;
box-sizing:border-box;
}
.container {
display:flex;
flex-direction: column;/* up to your needs */
border: 1px solid green;
padding: 20px;
flex: 1;
}
.content {
border: 1px solid blue;
flex:1;
}
<div class="wrapper">
<div class="container">
<div class="content">
Hello there
</div>
</div>
</div>
Height of Flex Div Not 100%
You may want to go through
Height Calculation By Browsers : Containing Blocks and Children. Clearly height
and min-height
are not the same thing in CSS.
Change min-height
to height
for your body
and html
- see demo below:
html{ height: 100%; /* changed to height */ width: 100%;}body{ height: 100%; /* changed to height */ width: 100%; background-color: green;}.main{ display: flex; flex-direction: row; min-height: 100%; justify-content: space-between; background-color: blue;}
<div class="main"> <div>Text 1</div> <div>Text 2</div></div>
Flexbox: flex-flow column; flex-basis 100% not working without explicit height
grid and mediaquerie is , IMHO, a good way to manage the swhitching from a 1 column layout to a 2 columns layout.
Demo of the idea :
:root {/* possible use of var here */--col1 : 1fr;--col2 : 2fr;}main { display: grid; grid-template-columns: var(--col1) var(--col2);}main> * { border:solid 1px; margin:2px;}section { grid-column:1;}article { grid-column:2; grid-row:1 / 10;}
/* breakpoint at 768px */@media screen and (max-width: 768px) { main { display: flex; flex-flow: column; } main section + section {/* best is to use an id */ order: 1; }}
<main> <section> <h1>About</h1> <p>Blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah.</p> </section> <section><! -- that one might deserve an id to easy reorder its position --> <h1>Disclaimer</h1> <p>Here there be naughty things!!!</p> </section> <article class="blog"> <h1>Blog Entry</h1> <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vestibulum eleifend molestie orci. Donec pellentesque viverra magna, nec viverra velit laoreet non. Etiam blandit erat nulla, semper faucibus eros rhoncus vel.</p> </article>
</main>
Related Topics
Fixed Page Header Overlaps In-Page Anchors
Css Pseudo-Classes With Inline Styles
What's the Difference Between an Id and a Class
Set Cellpadding and Cellspacing in Css
Angular 5 File Upload: Failed to Set the 'Value' Property on 'Htmlinputelement'
Provide an Image for Whatsapp Link Sharing
How to Remove Blue Highlight on Hover in Select, Option Inputs HTML for Chrome
How to Limit the Visible Options in an HTML <Select> Dropdown
How to Disable a Checkbox Based on Conditions in Angular 6
How to Put CSS @Media Rules Inline
How to Remove Unwanted Space in This Table
Align Left and Right in Mat-Card-Title
Fixed Headers on Scroll Inst Working Correctly in Angular Application
How to Set Maximum Height for Table-Cell
Bootstrap Toggle Collapse Is Not Working