Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is align-self: stretch not working on a flex item?

Tags:

html

css

flexbox

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>
like image 489
Froy Avatar asked Dec 18 '15 23:12

Froy


People also ask

How do you align content inside a Flex item?

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; .

What is align-self in Flex?

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.

How do you align individual flex items?

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.

How do you not stretch something Flex?

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.


1 Answers

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>
like image 105
Michael Benjamin Avatar answered Nov 15 '22 17:11

Michael Benjamin