liveBook · Manning

Align-self

As explained in CSS in Depth, Second Edition

The CSS align-self property specifies the block-axis or cross-axis alignment of an individual item within its layout container.

When several flex items should align together except for one deliberate exception, use align-self on that individual item. It controls the item’s position along the container’s cross axis, while align-items supplies the shared default. The value auto defers to align-items; any other supported value overrides it. section 4.4.2

Choose align-self when one item needs a different cross-axis treatment:

  • Use auto to inherit the flex container’s align-items behavior. section 4.4.2
  • Use flex-start, flex-end, start, end, self-start, self-end, center, stretch, or baseline to override that shared setting. section 4.4.2
  • Remember that place-self combines align-self with justify-self; justify-self is ignored on flex containers. section 4.4.2

Raise the cents in a price tile

A stylized price uses flexbox to center the currency, dollars, and cents. The larger dollar value establishes the visual height, while the cents move to the top with an item-specific override: .cost { display: flex; justify-content: center; align-items: center; line-height: 0.7; } .cost-currency { font-size: 2rem; } .cost-dollars { font-size: 4rem; } .cost-cents { font-size: 1.5rem; align-self: flex-start; } The container centers all three items by default; align-self changes only the cents. The line-height value is tuned so the text tops align visually. section 4.4.3

Listing 4.11 Setting styles for the cost tile
.centered {
  text-align: center;
}

.cost {
  display: flex;
  justify-content: center;     #1
  align-items: center;         #1
  line-height: 0.7;
}

.cost-currency {
  font-size: 2rem;       #2
}
.cost-dollars {
  font-size: 4rem;        #2
}
.cost-cents {
  font-size: 1.5rem;      #2
  align-self: flex-start;     #3
}

.cta-button {
  display: block;
  background-color: #cc6b5a;
  color: white;
  padding: 0.5em 1em;
  text-decoration: none;
}

Inspect the complete cost-tile stylesheet, including the shared align-items rule and the item-level align-self override. section 4.4.3

How CSS in Depth, Second Edition covers it

How deep it goes
Defines the override rule, supported keywords, shorthand relationship, and a worked text-alignment example.
Examples
Uses a stylized $20.00 price tile whose cents receive an align-self: flex-start override.
What it assumes
Builds from flex containers and flex items, then distinguishes item-level alignment from container-level align-items.
What you can do after
Apply align-self via CSS to override a flex container’s shared alignment for a selected item.
How it connects
Connects align-self with flexbox, align-items, place-self, justify-self, and grid layout alignment properties.

Also explained in

Compare how 3 books explain align-self →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of CSS in Depth, Second Edition. The section links point to the parts of the book it draws on.