The align-self
property allows you to override the align-items
property for a single flex item. It controls the alignment of an individual item along the cross axis of the flex container.
Example 1: align-self: center
.flex-container {
display: flex;
height: 200px;
align-items: flex-start; /* All items are aligned to the top by default */
border: 1px solid #000;
}
.flex-item {
width: 100px;
background-color: moccasin;
border: 1px solid #000;
}
.center-me {
align-self: center; /* This item is centered on the cross-axis */
}
Explanation
While the container has align-items: flex-start
, the .center-me
item is vertically centered because align-self: center
overrides the container's alignment property.
Example 2: align-self: flex-end
.flex-container {
display: flex;
height: 200px;
align-items: center; /* All items are centered by default */
border: 1px solid #000;
}
.flex-item {
width: 100px;
background-color: navajowhite;
border: 1px solid #000;
}
.end-me {
align-self: flex-end; /* This item is aligned to the bottom */
}
Explanation
In this case, the .end-me
item is aligned to the end of the cross axis (the bottom), even though the other items are centered according to the align-items
property of the container.
Example 3: align-self: flex-start
.flex-container {
display: flex;
height: 200px;
align-items: center;
border: 1px solid #000;
}
.flex-item {
width: 100px;
background-color: peachpuff;
border: 1px solid #000;
}
.start-me {
align-self: flex-start; /* This item is aligned to the top */
}
Explanation
The .start-me
item is aligned to the start of the cross axis (the top), overriding the align-items: center
set on the flex container.
Example 4: align-self: stretch
.flex-container {
display: flex;
height: 200px;
align-items: flex-start; /* Items don't stretch by default */
border: 1px solid #000;
}
.flex-item {
width: 100px;
background-color: papayawhip;
border: 1px solid #000;
}
.stretch-me {
align-self: stretch; /* This item stretches to fill the container's height */
}
Explanation
align-self: stretch
causes the .stretch-me
item to stretch to fill the height of the flex container, ignoring the align-items: flex-start
property. This is the default align-items
behavior if no height is set on the item.
Example 5: align-self: baseline
.flex-container {
display: flex;
height: 200px;
align-items: flex-start;
border: 1px solid #000;
}
.flex-item {
width: 100px;
background-color: mistyrose;
border: 1px solid #000;
}
.baseline-me {
font-size: 2em;
align-self: baseline; /* Aligns based on the text baseline */
}
Explanation
The .baseline-me
item is aligned with the other items based on their text's baseline. This is useful for aligning items that have different font sizes.
Example 6: auto
value
.flex-container {
display: flex;
height: 200px;
align-items: center;
border: 1px solid #000;
}
.flex-item {
width: 100px;
background-color: oldlace;
border: 1px solid #000;
align-self: auto; /* Inherits the align-items value from the container */
}
Explanation
The auto
value for align-self
is the default, and it simply inherits the align-items
value from the parent flex container. In this case, the item will be centered.
Example 7: Using with different flex-direction
.flex-container {
display: flex;
height: 200px;
flex-direction: column; /* Main axis is now vertical */
align-items: center; /* Aligns items horizontally */
border: 1px solid #000;
}
.flex-item {
height: 50px;
background-color: linen;
border: 1px solid #000;
}
.align-me {
align-self: flex-end; /* Aligns the item to the right edge */
}
Explanation
When flex-direction
is column
, the cross axis becomes horizontal. align-self: flex-end
will therefore align the .align-me
item to the right edge of the container.