Flexbox child items use full space - html

I'm building a site with flexbox, I have a problem that I can't fix.
.min-h-screen {
min-height: 100vh;
display: flex;
flex-direction: column;
}
.parent {
flex:1;
background: red;
}
.child-one {
background: green;
display:flex;
}
.child-two {
background: yellow;
}
<div class="min-h-screen">
<div class="parent">
<div class="child-one">
<p>
child 1
</p>
</div>
<div class="child-two">
<p>
child 2
</p>
</div>
</div>
</div>
I want the child 1 and child 2 use the full space, right now they don't. How could I fix this. Already tried a lot. Height 100 % does not work.

Use nested flex containers.
.min-h-screen {
min-height: 100vh;
display: flex;
flex-direction: column;
}
.parent {
flex: 1;
background: red;
display: flex; /* new */
flex-direction: column; /* new */
}
.child-one {
flex: 1; /* new */
background: green;
display: flex;
}
.child-two {
flex: 1; /* new */
background: yellow;
}
body { margin: 0; }
<div class="min-h-screen">
<div class="parent">
<div class="child-one">
<p>
child 1
</p>
</div>
<div class="child-two">
<p>
child 2
</p>
</div>
</div>
</div>

You just gotta flex the parent of the those two elements too.
* {
margin: 0;
}
.min-h-screen {
min-height: 100vh;
display: flex;
flex-direction: column;
}
.parent {
flex: 1;
display: flex;
background: red;
}
.child-one {
background: green;
flex: 1;
}
.child-two {
background: yellow;
flex: 1;
}
<div class="min-h-screen">
<div class="parent">
<div class="child-one">
<p>
child 1
</p>
</div>
<div class="child-two">
<p>
child 2
</p>
</div>
</div>
</div>

.parent {
min-height: 100vh;
display:flex;
background: red;
flex-direction: column;
}
.child-one {
background: green;
flex: 1 0 50%;
}
.child-two {
background: yellow;
flex: 1 0 50%;
}
<div class="parent">
<div class="child-one">
<p>
child 1
</p>
</div>
<div class="child-two">
<p>
child 2
</p>
</div>
</div>

Related

How to set a row height in flex box?

How can I increase the height of a row in flex (the div that contains has the class "row")? I have one row with two columns.
.body2 {
display: flex;
}
.row {
display: flex;
flex-direction: row;
flex-wrap: wrap;
width: 100%;
}
.column {
display: flex;
flex-direction: column;
flex-basis: 100%;
flex: 1;
}
.blue-column {
background-color: blue;
height: 100px;
}
.green-column {
background-color: green;
height: 100px;
}
<section class="body2">
<div class='row'>
<div class='column'>
<div class='blue-column'>
Some Text in Column One
</div>
</div>
<div class='column'>
<div class='green-column'>
Some Text in Column Two
</div>
</div>
</div>
</section>
How can I increase the height of that row? I already tried height:something in .row but it doesn't work
If you set a height on the container, the height will apply to the container, but not necessarily to the content of the container, so it may not look like you've added height.
In this example, a height is added to the container, as illustrated with the red border:
.body2 {
display: flex;
}
.row {
display: flex;
flex: 1;
height: 150px;
border: 1px dashed red;
}
.column {
display: flex;
flex-direction: column;
flex-basis: 100%;
flex: 1;
}
.blue-column {
background-color: aqua;
height: 100px;
}
.green-column {
background-color: lightgreen;
height: 100px;
}
<section class="body2">
<div class='row'>
<div class='column'>
<div class='blue-column'>
Some Text in Column One
</div>
</div>
<div class='column'>
<div class='green-column'>
Some Text in Column Two
</div>
</div>
</div>
</section>
If you want to add height to the descendants, use height or flex-basis (since the nested flex container is in column-direction).
.body2 {
display: flex;
}
.row {
display: flex;
flex: 1;
height: 150px;
border: 1px dashed red;
}
.column {
display: flex;
flex-direction: column;
flex-basis: 100%;
flex: 1;
}
.blue-column {
background-color: aqua;
height: 75px;
}
.green-column {
background-color: lightgreen;
flex-basis: 125px;
}
<section class="body2">
<div class='row'>
<div class='column'>
<div class='blue-column'>
Some Text in Column One
</div>
</div>
<div class='column'>
<div class='green-column'>
Some Text in Column Two
</div>
</div>
</div>
</section>
If you want the descendants to take full height, use flex: 1 instead of an explicit height.
.body2 {
display: flex;
}
.row {
display: flex;
flex: 1;
height: 150px;
border: 1px dashed red;
}
.column {
display: flex;
flex-direction: column;
flex-basis: 100%;
flex: 1;
}
.blue-column {
background-color: aqua;
/* height: 75px; */
flex: 1;
}
.green-column {
background-color: lightgreen;
/* flex-basis: 125px; */
flex: 1;
}
<section class="body2">
<div class='row'>
<div class='column'>
<div class='blue-column'>
Some Text in Column One
</div>
</div>
<div class='column'>
<div class='green-column'>
Some Text in Column Two
</div>
</div>
</div>
</section>

Height 100% inside flex item

I have a layout that is mainly divided into 3 parts and the middle one should take a full height. And it does.
However, I need an additional div which will play a role of the backdrop and here the problem comes. The child doesn't want to take 100% height.
Here .body is a div that is being stretched when there is not enough content and .bg-gray is the one I want to take its parent full height.
Is there a way achieve this without using relative + absolute positioning?
Also, I'm looking for the answer to my question: why is this happening that way.
html, body {
padding: 0;
margin: 0;
}
.container {
min-height: 100vh;
display: flex;
flex-direction: column;
align-items: stretch;
}
.header {
height: 50px;
background-color: #e6e6e6;
}
.footer {
height: 50px;
background-color: #aaa444;
}
.body {
flex: 1;
}
.bg-gray {
background-color: #eee;
min-height: 100%;
flex: 1;
}
<div class="container">
<div class="header">
</div>
<div class="body">
<div class="bg-gray">
<div>
asdasd
</div>
</div>
</div>
<div class="footer">
</div>
</div>
Apply flexbox to the .body div.
.body {
flex: 1;
display: flex;
flex-direction: column;
}
html,
body {
padding: 0;
margin: 0;
}
.container {
min-height: 100vh;
display: flex;
flex-direction: column;
align-items: stretch;
}
.header {
height: 50px;
background-color: #e6e6e6;
}
.footer {
height: 50px;
background-color: #aaa444;
}
.body {
flex: 1;
display: flex;
flex-direction: column;
}
.bg-gray {
background-color: darkgrey;
min-height: 100%;
flex: 1;
}
.bg-gray div {
background: lightblue;
}
<div class="container">
<div class="header">
</div>
<div class="body">
<div class="bg-gray">
<div>
asdasd
</div>
</div>
</div>
<div class="footer">
</div>
</div>

Arranged divs with flex - increasing div is moving other divs [duplicate]

This question already has answers here:
Remove space (gaps) between multiple lines of flex items when they wrap
(1 answer)
How does flex-wrap work with align-self, align-items and align-content?
(2 answers)
Closed 5 years ago.
I already posted a question here in
Arrange a few divs
The problem is that the div on the right side is increasing and the divs content3 and 4 are moving down, but the should stay right under content_1
here ist the code
body {
margin: 0;
}
#wrapper {
width: 600px;
display: flex;
height: 200px;
margin: 0 auto;
}
.first {
flex: 2;
display: flex;
flex-wrap: wrap;
}
#content_1 {
background: red;
width: 100%;
}
#content_2 {
flex: 1;
background: #aaa;
}
#content_4 {
flex: 1;
background: #ddd;
}
#content_3 {
flex: 1;
background: #eee;
}
<div id="wrapper">
<div class="first">
<div id="content_1">content_1</div>
<div id="content_2">content_2</div>
<div id="content_3">content_3</div>
</div>
<div id="content_4">content_4</div>
</div>
Can someone help?
Maybe you need it to be responsive, so like this:
body {
margin: 0;
}
#wrapper {
display: flex;
height: 100vh;
margin: 0 auto;
}
.first {
flex: 2;
display: flex;
flex-wrap: wrap;
}
#content_1 {
background: red;
width: 100%;
}
#content_2 {
flex: 1;
background: #aaa;
}
#content_4 {
flex: 1;
background: #ddd;
}
#content_3 {
flex: 1;
background: #eee;
}
<div id="wrapper">
<div class="first">
<div id="content_1">content_1</div>
<div id="content_2">content_2</div>
<div id="content_3">content_3</div>
</div>
<div id="content_4">content_4</div>
</div>
And if you want to use fixed height for sections you need to add alignment property to the #wrapper in order to keep 3 & 4 always under 1:
body {
margin: 0;
}
#wrapper {
display: flex;
margin: 0 auto;
width:600px;
align-items: flex-start; /*added this*/
}
.first {
flex: 2;
display: flex;
flex-wrap: wrap;
}
#content_1 {
background: red;
width: 100%;
height:40px;
}
#content_2 {
flex: 1;
background: #aaa;
height:40px;
}
#content_4 {
flex: 1;
background: #ddd;
height:150px;
}
#content_3 {
flex: 1;
background: #eee;
height:40px;
}
<div id="wrapper">
<div class="first">
<div id="content_1">content_1</div>
<div id="content_2">content_2</div>
<div id="content_3">content_3</div>
</div>
<div id="content_4">content_4</div>
</div>

Accomplishing this grid with css

Can I accomplish this grid layout with flexbox? To have the first element take up 2 rows height and then continue after it?
Check image.
You can achive it by dividing this layout in 2 columns while the 2nd column will have a nested flexbox layout as well.
HTML Structure:
<div class="container">
<div class="col box1">1</div>
<div class="col col2">
<div class="box2">2</div>
<div class="box3">3</div>
<div class="box4">4</div>
<div class="box5">5</div>
</div>
</div>
Necessary Styles:
.container {
min-height: 100vh;
display: flex;
}
.col {
flex-grow: 1;
color: #fff;
}
.col2 {
flex-wrap: wrap;
display: flex;
}
.col2 > div {
flex-basis: 50%;
flex-grow: 1;
}
.box1 {
display: flex;
}
* {box-sizing: border-box;}
body {
margin: 0;
}
.container {
min-height: 100vh;
display: flex;
}
.col {
flex-grow: 1;
color: #fff;
}
.col2 {
flex-wrap: wrap;
display: flex;
}
.col2 > div {
flex-basis: 50%;
padding: 10px;
flex-grow: 1;
}
.box1 {
background: brown;
padding: 10px;
display: flex;
}
.box2 {
background: pink;
}
.box3 {
background: black;
}
.box4 {
background: yellow;
}
.box5 {
background: royalblue;
}
<div class="container">
<div class="col box1">1</div>
<div class="col col2">
<div class="box2">2</div>
<div class="box3">3</div>
<div class="box4">4</div>
<div class="box5">5</div>
</div>
</div>
You can use this HTML structure but you need to set fixed height on parent div. Then you just use flex-direction: column and flex-wrap: wrap.
* {
box-sizing: border-box;
}
.content {
height: 100vh;
display: flex;
flex-direction: column;
flex-wrap: wrap;
}
div div:first-child {
flex: 0 0 100%;
width: 50%;
background: #880015;
}
div div:not(:first-child) {
width: 25%;
flex: 0 0 50%;
border: 1px solid black;
}
<div class="content">
<div class="box">1</div>
<div class="box">2</div>
<div class="box">3</div>
<div class="box">4</div>
<div class="box">5</div>
</div>

Flexbox inside another flexbox?

I'm trying to get a flexbox working inside a flexbox. While the first (wrapping) flexbox works, the one inside does nothing. Is there anyway to get this to work?
What I'm looking to do is effectively have two sticky footers and have the height of both reach the the footers.
html, body {
height: 100%;
margin: 0; padding: 0; /* to avoid scrollbars */
}
#wrapper {
display: flex; /* use the flex model */
min-height: 100%;
flex-direction: column; /* learn more: http://philipwalton.github.io/solved-by-flexbox/demos/sticky-footer/ */
}
#header {
background: yellow;
height: 100px; /* can be variable as well */
}
#body {
flex: 1;
border: 1px solid orange;
height: 100%:
}
#wrapper2 {
display: flex; /* use the flex model */
min-height: 100%;
flex-direction: column;
}
#body2 {
border: 1px solid purple;
flex: 1;
}
#footer2 {
background: red;
flex: 0;
}
#footer{
background: lime;
}
<div id="wrapper">
<div id="body">Bodyof<br/>
variable<br/>
height<br/>
<div id="wrapper2">
<div id="body2">
blah
</div>
<div id="footer2">
blah<br />
blah
</div>
</div>
</div>
<div id="footer">
Footer<br/>
of<br/>
variable<br/>
height<br/>
</div>
</div>
JS Fiddle
You were almost there. Just two steps away:
Make #body a flex container.
Give .wrapper2 full height with flex: 1.
(I also got rid of percentage heights. You don't need them.)
body {
margin: 0;
}
#wrapper {
display: flex;
flex-direction: column;
min-height: 100vh;
}
#header {
background: yellow;
height: 100px;
}
#body {
flex: 1;
border: 1px solid orange;
display: flex; /* new */
flex-direction: column; /* new */
}
#wrapper2 {
display: flex;
flex-direction: column;
flex: 1; /* new */
}
#body2 {
border: 1px solid purple;
flex: 1;
}
#footer2 {
background: red;
}
#footer {
background: lime;
}
<div id="wrapper">
<div id="body">
Bodyof
<br>variable
<br>height
<br>
<div id="wrapper2">
<div id="body2">blah</div>
<div id="footer2">
blah
<br>blah
</div>
</div>
</div>
<div id="footer">
Footer
<br>of
<br>variable
<br>height
<br>
</div>
</div>
jsFiddle
Once the adjustments above are made, you can pin the inner (red) footer to the bottom with:
flex: 1 on #body2, which is what you have, or
margin-bottom: auto on #body2, or
margin-top: auto on #footer2, or
justify-content: space-between on the container (#wrapper2)