Sub topic 12
Overriding alignment for one item with align-self
This chapter assumes you know the main axis and cross axis, and that you have used align-items on a flex container. It also assumes you are comfortable with flex-direction, the box model, and CSS selectors. Here you will learn how a single flex item can opt out of the container's cross-axis alignment.
What align-self does
align-items sets a default cross-axis alignment for every item in a flex container. align-self is set on one flex item and overrides that default for that item only.
The two properties accept the same values, and align-self adds auto:
| Value | Effect on the item |
|---|---|
| `auto` | Default. Use the container's `align-items` value. |
| `flex-start` | Place the item at the cross-start edge of its line. |
| `flex-end` | Place the item at the cross-end edge of its line. |
| `center` | Center the item on the cross axis. |
| `baseline` | Align the item's first text baseline with other baseline-aligned items. |
| `stretch` | Stretch the item to fill the line's cross size, if its cross size is `auto`. |
Because auto is the initial value, an item with no align-self always follows its container.
A first example
This page has a row of three boxes. The container centers them vertically. The second box overrides that and sits at the bottom.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>align-self demo</title>
<style>
.row {
display: flex;
align-items: center;
gap: 1rem;
height: 200px;
border: 2px solid #888;
}
.box {
width: 80px;
height: 80px;
background: #4a90d9;
}
.box.bottom {
align-self: flex-end;
}
</style>
</head>
<body>
<div class="row">
<div class="box">1</div>
<div class="box bottom">2</div>
<div class="box">3</div>
</div>
</body>
</html>Boxes 1 and 3 are centered vertically because of align-items: center. Box 2 has its own value, so it sits on the bottom edge of the container.
The rule on .box.bottom wins because it targets the item itself. The container's align-items is not a competing declaration on the same element. It is only a default that applies when the item's align-self is auto. So the order of the rules in your stylesheet does not matter here.
It follows the cross axis
align-self always works on the cross axis, and the cross axis depends on flex-direction.
- With
flex-direction: row, the cross axis is vertical.align-self: flex-endmoves the item down. - With
flex-direction: column, the cross axis is horizontal.align-self: flex-endmoves the item to the right (in a left-to-right language).
.sidebar {
display: flex;
flex-direction: column;
width: 300px;
}
.sidebar .close {
align-self: flex-end;
}Here the close button keeps its natural width and moves to the right edge. The other children stay stretched across the full 300px, because align-items defaults to stretch.
Stretch needs an auto cross size
stretch only works when the item's cross-axis size is auto. In a row, that means height must not be set (or must be auto). If you give the item height: 80px, it keeps 80px and stretch does nothing visible.
This explains a common surprise. A container has align-items: center, and you want one item to fill the full height. Setting align-self: stretch works only if that item has no explicit height.
.row {
display: flex;
align-items: center;
height: 200px;
}
.row .fill {
align-self: stretch;
/* no height declared, so it grows to 200px */
}Margins also count. A stretched item fills the line's cross size minus its own cross-axis margins.
Baseline alignment for one item
align-self: baseline lines up the first line of text in an item with the baseline of other baseline-aligned items. It is useful when one item has a larger font size or different padding than its neighbors.
.field {
display: flex;
align-items: flex-start;
gap: 0.5rem;
}
.field .label {
align-self: baseline;
}
.field .big-input {
font-size: 2rem;
align-self: baseline;
}Baseline alignment only has an effect among items that share it. If only one item in a line uses baseline, it falls back to behaving like flex-start. Set it on every item that should line up.
Limits of align-self
It does not move items along the main axis
There is no justify-self in Flexbox. The property is ignored on flex items. To move one item along the main axis, use auto margins, as covered in the chapter on pushing items apart. For example, margin-left: auto pushes an item to the far end of a row.
Auto margins beat it
On the cross axis, an auto margin takes all free space before align-self has any effect. If an item has margin-top: auto, it sits at the bottom of its line no matter what align-self says. If you set both and see nothing happen, check for an auto margin.
It aligns within the flex line, not the container
In a single-line container, the line fills the container's cross size, so the two look the same. With flex-wrap: wrap, each line has its own cross size, based on its tallest item. align-self positions an item inside its own line. To distribute the lines themselves inside a taller container, use align-content, which is the next topic.
.wrap {
display: flex;
flex-wrap: wrap;
align-items: flex-start;
height: 400px;
}
.wrap .special {
align-self: flex-end;
}In this example, .special moves to the bottom of its own line, not to the bottom of the 400px container.
Common uses
- A "Save" button that sits at the bottom of a card whose siblings are top-aligned.
- A badge or icon that stays at the top while neighboring text is centered.
- A sidebar close button aligned to one side in a column layout.
- One full-height divider in a row of vertically centered items.
Common mistakes
- Using
align-selfand expecting horizontal movement in a row. In a row it is vertical. - Expecting
stretchto work on an item with a fixedheight. - Writing
justify-selfin a flex container. It has no effect. - Forgetting that an auto margin on the cross axis overrides
align-self. - Setting
baselineon one item only.
Exercise
A container has display: flex; flex-direction: row; align-items: flex-start; height: 300px. It holds three items with no explicit height. You want only the last item to fill the full 300px. Which declaration do you add, and to which element?
Show answer
Add align-self: stretch to the last item. The item has no explicit height, so its cross size is auto and it stretches to 300px. The other two items keep following align-items: flex-start.
Exercise
In a flex-direction: column container that is 400px wide, one child has align-self: flex-end and no width. Where does it appear, and how wide is it?
Show answer
It sits at the right edge of the container (in a left-to-right language), and its width is its content width. In a column, the cross axis is horizontal. flex-end moves the item to the cross-end edge, and it no longer stretches to 400px.
Exercise
An item has align-self: center and margin-top: auto inside a row container. Does it end up centered vertically?
Show answer
No. The auto margin absorbs the free cross-axis space above the item, so the item sits at the bottom of its line. Auto margins take effect before align-self.