Sub topic 12
Overriding alignment for one item with align-self
This chapter assumes you know how to create a flex container, that align-items aligns items on the cross axis, and the basic box model. Here you will learn how one item can ignore the container's cross-axis alignment.
The problem
align-items sets one cross-axis alignment for every item in the container. Sometimes a single item needs to differ. A common case is a toolbar where all items are centered, but one badge should sit at the top.
How align-self works
align-self is set on a flex item, not on the container. It overrides the container's align-items value for that one item. It accepts the same keywords: flex-start, flex-end, center, baseline, and stretch. It also accepts auto, which is the default.
auto means "use the container's align-items value". So an item only differs from its siblings when you set something other than auto.
.toolbar {
display: flex;
align-items: center;
height: 120px;
gap: 1rem;
}
.toolbar .badge {
align-self: flex-start;
}
.toolbar .save {
align-self: flex-end;
}All children are centered vertically, except .badge, which goes to the top, and .save, which goes to the bottom. The row direction is the default, so the cross axis is vertical here.
The stretch gotcha
stretch only has an effect when the item's cross-size is auto. If the item has a fixed height (in a row container), it keeps that height, and align-self: stretch does nothing visible. Likewise, if you set align-items: center on the container and want one item to fill the full height, use align-self: stretch and make sure that item has no explicit height.
Follows the axis, not the screen
The cross axis depends on flex-direction. In a column container, the cross axis is horizontal. There, align-self: flex-end pushes the item to the right edge (in a left-to-right language), not to the bottom.
.sidebar {
display: flex;
flex-direction: column;
align-items: flex-start;
}
.sidebar .logout {
align-self: flex-end;
}Common mistakes
- Setting
align-selfon the container. It has no effect there. Usealign-itemson the container. - Expecting
align-selfto move an item along the main axis. Main-axis placement belongs tojustify-contentand auto margins. There is nojustify-selffor flex items. - Forgetting that in a wrapping container,
align-selfaligns the item within its own flex line, not within the whole container. To distribute the lines themselves, usealign-content.
Exercise
A column flex container has align-items: center. You want one child to hug the left edge (left-to-right language). Which declaration do you put on that child, and why left rather than top?
Show answer
Use align-self: flex-start. In a column container the cross axis is horizontal, so flex-start on the cross axis is the left edge. The main axis is vertical, so it would not move the child to the top.
Exercise
A row container has align-items: center. One child has height: 40px and align-self: stretch, but it does not grow to the container's height. Why?
Show answer
stretch only applies when the item's cross-size is auto. The fixed height of 40px prevents stretching. Remove the height, or use min-height instead.