CSS baseline-source Property

The baseline-source property determines whether an inline-level box uses its first or last baseline for alignment with surrounding content.

It is useful for inline-block, inline-flex, and inline-grid elements that contain multiple lines or rows of content. By selecting the baseline used for alignment, the property provides control over how these elements sit alongside text and other inline-level boxes.

baseline-source Syntax

selector {
  baseline-source: auto;
}

The property accepts three keyword values.

.automatic-baseline {
  baseline-source: auto;
}
.first-baseline {
  baseline-source: first;
}
.last-baseline {
  baseline-source: last;
}

CSS-wide values are also supported.

.example {
  baseline-source: inherit;
  baseline-source: initial;
  baseline-source: revert;
  baseline-source: revert-layer;
  baseline-source: unset;
}

These declarations demonstrate the available global values. In actual stylesheets, use the value appropriate for the intended cascade behavior rather than declaring all of them together.

The property does not accept lengths, percentages, or numeric offsets. It selects a baseline source rather than specifying how far an element should move.

Property Values

Value Description
auto Uses the last baseline for inline-block elements and the first baseline for other applicable inline-level boxes.
first Selects the first baseline set of the inline-level box for alignment.
last Selects the last baseline set of the inline-level box for alignment.

The initial value is auto. The property applies to inline-level boxes, is not inherited, and has a computed value equal to the specified keyword. Its animation type is discrete.

When a box has only one available baseline, selecting first or last generally produces the same alignment. The difference becomes important when a box provides multiple baseline sets.

How baseline-source Works

Text is aligned using invisible reference lines called baselines. In ordinary horizontal text, characters typically sit along an alphabetic baseline.

An inline-level element containing multiple lines or rows can have more than one possible baseline. The baseline-source property tells the browser which baseline set to prefer when aligning that element with surrounding inline content.

First Baseline

The first baseline is associated with the first relevant line or row of the box's content.

Last Baseline

The last baseline is associated with the last relevant line or row of the box's content.

Example with Multiple Lines

<p>
  Label
  <span class="multi-line">First line<br>Second line</span>
  Text
</p>
.multi-line {
  display: inline-block;
  baseline-source: first;
}

When supported, the browser uses the inline-block's first baseline for alignment rather than its default last baseline.

Baseline Selection Is Not Positioning

The property does not set a fixed vertical offset. Instead, it changes the reference used by the line formatting algorithm to align the element.

As a result, the visible position can change when the element's content, font, or line count changes.

The auto Value

The auto value allows the browser to choose the baseline source according to the element's box type.

.default-alignment {
  baseline-source: auto;
}

Default for inline-block

For an inline-block, auto selects the last baseline.

.inline-block-example {
  display: inline-block;
  baseline-source: auto;
}

For a multi-line inline-block, this generally aligns the last line of its content with surrounding inline text.

Default for inline-flex and inline-grid

For other applicable inline-level boxes, including inline-flex and inline-grid containers, auto selects the first baseline.

.inline-flex-example {
  display: inline-flex;
  baseline-source: auto;
}
.inline-grid-example {
  display: inline-grid;
  baseline-source: auto;
}

The selected baseline is determined using the baseline rules for the relevant layout model.

When to Use auto

Use auto when the element's default baseline alignment is appropriate and no explicit first or last baseline preference is required.

The first Value

The first value selects the first available baseline set for alignment.

.first-line-alignment {
  display: inline-block;
  baseline-source: first;
}

For an inline-block containing multiple lines, this can align the first line of the element with adjacent text.

Example with Two Lines

<p>
  Reference
  <span class="first-line-alignment">
    First line<br>
    Second line
  </span>
</p>
.first-line-alignment {
  display: inline-block;
  padding: 8px;
  background-color: #e8f2fb;
  baseline-source: first;
}

The first line inside the inline-block becomes the preferred baseline source.

Useful Applications

The first value is useful when the top line of a multi-line label, inline component, or compact content group should align with surrounding text.

It is particularly useful when the default last-baseline behavior of an inline-block produces an unwanted alignment.

The last Value

The last value selects the last available baseline set for alignment.

.last-line-alignment {
  display: inline-flex;
  flex-direction: column;
  baseline-source: last;
}

For an inline-flex container with multiple rows of content, this requests alignment using the last baseline rather than the default first baseline.

Example with a Column Layout

<p>
  Reference
  <span class="last-line-alignment">
    <span>First row</span>
    <span>Last row</span>
  </span>
</p>
.last-line-alignment {
  display: inline-flex;
  flex-direction: column;
  padding: 8px;
  background-color: #e8f2fb;
  baseline-source: last;
}

The last row provides the preferred baseline for alignment with the surrounding text.

Useful Applications

The last value is useful for multi-row inline components where the bottom line or row should align with adjacent text.

It also provides an explicit way to request last-baseline alignment without depending on the element's default box-type behavior.

Using baseline-source with inline-block

An inline-block element participates in an inline formatting context while establishing its own internal layout.

When it contains multiple lines of text, its first and last baselines may be different.

Default Last-Baseline Alignment

.inline-label {
  display: inline-block;
  width: 120px;
  baseline-source: auto;
}

Because auto selects the last baseline for inline-block elements, the final line normally supplies the alignment reference.

Selecting the First Baseline

.inline-label {
  display: inline-block;
  width: 120px;
  baseline-source: first;
}

The first line now supplies the preferred alignment reference in browsers that implement the property.

When the Box Has No Text Baseline

An inline-block without a usable content baseline may use a synthesized baseline according to CSS layout rules. For example, some inline-block boxes use a baseline derived from their margin box.

In these situations, selecting first or last may not produce the same result as it does for an element containing multiple lines of text.

Using baseline-source with inline-flex

An inline-flex element behaves as an inline-level flex container. Its baseline information is derived from its flex items and the flex layout rules.

Default First-Baseline Alignment

.inline-flex-label {
  display: inline-flex;
  flex-direction: column;
  baseline-source: auto;
}

The auto value selects the first baseline for inline-flex containers.

Selecting the Last Baseline

.inline-flex-label {
  display: inline-flex;
  flex-direction: column;
  baseline-source: last;
}

The browser uses the last available baseline set for the inline-flex container's alignment.

Example with Stacked Text

<span class="stacked-label">
  <span>Primary</span>
  <span>Secondary</span>
</span>
.stacked-label {
  display: inline-flex;
  flex-direction: column;
  gap: 4px;
  baseline-source: last;
}

The last row becomes the preferred baseline source, which can help align the secondary text with adjacent inline content.

Remember that the final alignment also depends on the surrounding line box and the baseline information supplied by the flex items.

Using baseline-source with inline-grid

An inline-grid element is an inline-level grid container. It can provide baseline information from its grid items and rows.

Default Baseline

.inline-grid-label {
  display: inline-grid;
  baseline-source: auto;
}

The auto value selects the first baseline for an inline-grid container.

Selecting the Last Baseline

.inline-grid-label {
  display: inline-grid;
  grid-template-rows: auto auto;
  baseline-source: last;
}

The last available baseline set is used for alignment.

Example with Two Rows

<span class="grid-label">
  <span>Heading</span>
  <span>Detail</span>
</span>
.grid-label {
  display: inline-grid;
  grid-template-rows: auto auto;
  row-gap: 4px;
  baseline-source: last;
}

This can be useful for compact inline components that contain a heading and a secondary line.

The actual baseline is determined by the grid's baseline calculation rules. If the requested baseline is unavailable, the browser may synthesize one.

Working with vertical-align

The vertical-align property controls the alignment of inline-level boxes within a line box. The baseline-source property determines which baseline an eligible box supplies for that alignment.

Baseline Alignment

.inline-component {
  display: inline-block;
  vertical-align: baseline;
  baseline-source: first;
}

The element requests ordinary baseline alignment while selecting its first baseline as the alignment reference.

Using top or bottom

.top-aligned {
  display: inline-block;
  vertical-align: top;
}
.bottom-aligned {
  display: inline-block;
  vertical-align: bottom;
}

These declarations align the element relative to the line box rather than selecting one of its internal baselines for ordinary baseline alignment.

The vertical-align Shorthand Relationship

The CSS Inline Layout Module Level 3 specification defines vertical-align as a shorthand involving baseline alignment properties, including baseline-source.

However, browser implementation of the newer shorthand behavior and its longhands may differ. Avoid relying on a particular shorthand and longhand combination without testing it in the target browsers.

For established layouts where baseline-source support is unavailable, vertical-align: top, middle, or bottom may provide a simpler alignment alternative, although these values do not duplicate first- or last-baseline selection.

Related Baseline Properties

CSS provides several properties that affect baseline alignment. They serve different purposes and should not be treated as interchangeable.

Property Purpose
baseline-source Selects the first or last baseline set of an inline-level box.
alignment-baseline Specifies the baseline used when aligning a box with its alignment context.
dominant-baseline Controls the dominant baseline used in text layout.
baseline-shift Applies a baseline offset or selects subscript and superscript positioning.
vertical-align Controls vertical alignment of inline-level boxes and applicable table cells.

Selecting vs. Shifting a Baseline

.first-baseline {
  baseline-source: first;
}
.shifted-baseline {
  baseline-shift: 5px;
}

The first declaration selects which baseline set is used. The second specifies a baseline offset in contexts where baseline-shift applies.

Baseline Alignment in Flexbox and Grid

Flexbox and Grid also provide baseline alignment through properties such as align-items and align-self.

.flex-container {
  display: flex;
  align-items: baseline;
}

This aligns flex items using their baselines. It is a different operation from selecting the baseline source of an inline-level container.

baseline-source Example

The following example compares auto, first, and last using inline-flex containers with vertically stacked text.

The dashed line indicates the surrounding text baseline. In supporting browsers, the first and last values determine which row of the inline-flex container aligns with that line.

<div class="baseline-source-demo">
  <div class="demo-row">
    <span>Reference</span>
    <span class="demo-box auto">
      <span>Auto</span>
      <span>Second</span>
      <span>Third</span>
    </span>
    <span>Text</span>
  </div>
  <div class="demo-row">
    <span>Reference</span>
    <span class="demo-box first">
      <span>First</span>
      <span>Second</span>
      <span>Third</span>
    </span>
    <span>Text</span>
  </div>
  <div class="demo-row">
    <span>Reference</span>
    <span class="demo-box last">
      <span>First</span>
      <span>Second</span>
      <span>Last</span>
    </span>
    <span>Text</span>
  </div>
</div>
.baseline-source-demo {
  max-width: 520px;
  font-family: Arial, sans-serif;
  color: #173b5e;
}
.baseline-source-demo .demo-row {
  padding: 30px 10px;
  border-bottom: 1px solid #d3e2ee;
  background-image: linear-gradient(to bottom, transparent calc(100% - 1px), #b4c7d7 calc(100% - 1px));
  white-space: nowrap;
}
.baseline-source-demo .demo-box {
  display: inline-flex;
  flex-direction: column;
  gap: 4px;
  min-width: 100px;
  margin: 0 12px;
  padding: 10px;
  border: 2px solid #1769aa;
  border-radius: 5px;
  background-color: #e8f2fb;
  text-align: center;
}
.baseline-source-demo .auto {
  baseline-source: auto;
}
.baseline-source-demo .first {
  baseline-source: first;
}
.baseline-source-demo .last {
  baseline-source: last;
}

The live browser result uses the same baseline selection values. Unsupported browsers may display the default alignment for all three examples.

Browser Result
Reference Auto Second Third Text
Reference First Second Third Text
Reference First Second Last Text

For inline-flex containers, auto normally selects the first baseline, making it equivalent to first in this example.

The last value requests alignment using the final row's baseline, which can change the vertical position of the container relative to the surrounding text.

Because browser support is limited, differences between these examples may not appear in every browser.

Important Considerations

Keep the following points in mind when using baseline-source:

  • Initial value: The default is auto.
  • Available values: The property accepts auto, first, and last.
  • Inline-level boxes: The property applies to inline-level boxes and is most useful when multiple baselines are available.
  • Default inline-block behavior: The auto value selects the last baseline for inline-block elements.
  • Other inline containers: The auto value selects the first baseline for other applicable inline-level boxes.
  • First baseline: The first value selects the first available baseline set.
  • Last baseline: The last value selects the last available baseline set.
  • Single baseline: When only one baseline is available, the first and last values may produce identical results.
  • Flexbox and Grid: Inline flex and grid containers can provide baseline information through their items and layout rules.
  • Not a positioning property: The property does not specify a fixed vertical offset.
  • No numeric values: Lengths and percentages are not valid values.
  • Not inherited: Child elements do not automatically inherit the specified baseline source.
  • Animation: Changes between keyword values are discrete rather than smoothly interpolated.
  • Baseline synthesis: Browsers may synthesize a baseline when the requested baseline is unavailable.
  • Browser support: Compatibility is limited, so test the property before relying on it for essential layout.
  • Fallback alignment: Established properties such as vertical-align can provide alternative alignment behavior, although not the same first- or last-baseline selection.

Use baseline-source when a multi-line inline component needs to align using a particular baseline rather than its default baseline source.

Browser Support

The baseline-source property has Limited availability and is not part of the Baseline widely available feature set. Support varies between browser engines and versions.

Browsers that do not recognize the property ignore its declarations and continue using their existing baseline alignment behavior.

Providing a Fallback

.inline-label {
  display: inline-block;
  vertical-align: top;
  baseline-source: first;
}

The vertical-align: top declaration provides an established alignment method. The baseline-source declaration expresses the intended first-baseline preference for browsers that support it, but it does not by itself replace the top-alignment setting.

Using Feature Detection

.inline-label {
  display: inline-block;
  vertical-align: top;
}
@supports (baseline-source: first) {
  .inline-label {
    vertical-align: baseline;
    baseline-source: first;
  }
}

This example uses top alignment as a fallback and switches to first-baseline alignment when the browser recognizes the property.

Feature queries test whether a declaration is recognized, not whether every aspect of the feature behaves identically across browsers.

Visit Can I Use? to check current browser compatibility.

Specifications

The baseline-source property is defined in the CSS Inline Layout Module Level 3 specification.

CSS Inline Layout Module Level 3