CSS flex items specify the child elements of a flex container.It has following properties: order, flex-grow, flex-shrink, flex-basis, flex, & align-self.
In other word, the child elements of a flex container are known as flex items. An individual flex item has the followings properties:
order
, flex-grow
, flex-shrink
, flex-basis
, flex
, align-self
.
The order property of the flex items is used to set the order in which they appear in the parent container. The default order is 0.
Source Code
<div class="flex-container">
<div style="order: 3">1</div>
<div style="order: 2">2</div>
<div style="order: 4">3</div>
<div style="order: 1">4</div>
</div>
The flex-grow property specifies how much a flex item will grow relative to the rest of the flex items.
Flex-grow 0 means width of flex item as per content only.
Flex-grow 1 means the width of the flex item will be distributed equally to all items.
Source Code
<div class="flex-container">
<div style="flex-grow: 1">1</div>
<div style="flex-grow: 2">2</div>
<div style="flex-grow: 4">3</div>
</div>
The flex-basis property specifies the width of the flex item. The default value of the flex-basis is auto. Flex item width can be specified in %age, px, em, or rem.
Source Code
<div class="flex-container">
<div class="flex-item bg-info">1</div>
<div class="flex-item bg-primary" >2</div>
<div class="flex-item bg-warning" style="flex-basis: 250px">3</div>
</div>
Source Code: Output
It is a shorthand property for the flex-grow, flex-shrink, and flex-basis properties.
Source Code
<div class="flex-container">
<div class="flex-item" >1</div>
<div class="flex-item" style="flex: 0 0 200px">2</div>
<div class="flex-item">3</div>
<div class="flex-item">4</div>
</div>
Source Code: Output
The align-self property is used to align the selected flex item inside the flexible container.
The align-self:center
property is used to align the flex item in the middle of the container.Please keep in mind that flex item alignment can be done with respect to the flex container.
Source Code
<div class="flex-container">
<div class="flex-item bg-info" >1</div>
<div class="flex-item bg-primary" style="align-self: center">2</div>
<div class="flex-item bg-warning">3</div>
<div class="flex-item bg-info">4</div>
</div>
The align-self: flex-start
property is used to align the flex item at the top of the container.
Source Code
<div class="flex-container">
<div class="flex-item bg-info" >1</div>
<div class="flex-item bg-primary" style="align-self: flex-start">2</div>
<div class="flex-item bg-warning">3</div>CSS
</div>
The align-self: flex-end
property is used to align the flex item at the end of the container.
Source Code
<div class="flex-container">
<div class="flex-item bg-info" >1 </div>
<div class="flex-item bg-primary" style="align-self: flex-end">2 </div>
<div class="flex-item bg-warning">3</div>
</div>