I am trying to stretch the .side-bar
div so that it takes up the height of the entire window.
I added flex to the flex container and specified the flex item widths and heights but the side-bar's height is displaying the same as the flex items.
Is it because of the order of my CSS classes?
* {
margin: 0;
}
.flex-container {
display: flex;
height: 500px;
border: 1px solid blue;
}
.flex-items {
width: 100px;
height: 250px;
border: 1px solid red;
}
.side-bar {
width: 400px;
align-self: stretch;
}
<div class="flex-container">
<div class="flex-items side-bar"></div>
<div class="flex-items"></div>
<div class="flex-items"></div>
<div class="flex-items"></div>
<div class="flex-items"></div>
<div class="flex-items"></div>
<div class="flex-items"></div>
<div class="flex-items"></div>
<div class="flex-items"></div>
<div class="flex-items"></div>
<div class="flex-items"></div>
<div class="flex-items"></div>
<div class="flex-items"></div>
<div class="flex-items"></div>
<div class="flex-items"></div>
<div class="flex-items"></div>
<div class="flex-items"></div>
<div class="flex-items"></div>
<div class="flex-items"></div>
<div class="flex-items"></div>
</div>
To center the inner div element we will make the parent a flex container. By adding the display: flex; property we make the section element a flex container allowing us to adjust the layout of the div which is now a flex item. To center out item horizontally we use the justify-content: center; .
The align-self CSS property overrides a grid or flex item's align-items value. In Grid, it aligns the item inside the grid area. In Flexbox, it aligns the item on the cross axis.
If you do want to align one item, or split a group on the main axis, use auto margins to do so; The align-items property sets all of the align-self values as a group. Use align-self on the flex child to set the value for an individual item.
By default, the child elements of a flexbox container will stretch vertically to fill the height of the container. This can be prevented by using the align-self property on the child element that you do not want to stretch.
You have specified a height on your flex items.
.flex-items {
width: 100px;
height: 250px;
border: 1px solid red;
}
This height rule overrides stretch
on align-items
and align-self
(here's why).
Also, adding align-self: stretch
to .side-bar
would do nothing anyway because stretch
is already applied – it's the default value.
Try this instead:
* {
margin: 0;
}
.flex-container {
display: flex;
height: 500px;
border: 1px solid blue;
}
.flex-items:not(.side-bar) { /* modified selector */
width: 100px;
height: 250px;
border: 1px solid red;
}
.side-bar {
width: 400px;
background-color: yellow;
border: 1px solid red;
}
<div class="flex-container">
<div class="flex-items side-bar"></div>
<div class="flex-items"></div>
<div class="flex-items"></div>
<div class="flex-items"></div>
<div class="flex-items"></div>
<div class="flex-items"></div>
<div class="flex-items"></div>
<div class="flex-items"></div>
<div class="flex-items"></div>
<div class="flex-items"></div>
<div class="flex-items"></div>
<div class="flex-items"></div>
<div class="flex-items"></div>
<div class="flex-items"></div>
<div class="flex-items"></div>
<div class="flex-items"></div>
<div class="flex-items"></div>
<div class="flex-items"></div>
<div class="flex-items"></div>
<div class="flex-items"></div>
</div>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With