1. Introduction
This module defines inline layout, the CSS model for laying out a mixed stream of text and inline-level boxes, and defines controls for the block-axis alignment and sizing of this content within each line. It also adds a special layout mode for drop caps and similar initial letter styling.
Note: Line-breaking, justification, and other aspects of inline-axis positioning of inline-level content are handled in the CSS Text Module.
Many aspects of layout here depend on font metrics. While the relevant metrics exist in OpenType for Latin/Cyrillic/Greek and for CJK, they are missing for many other writing systems. For example, the visual top metric for Hebrew has no metric in the OpenType tables. For this module to work well for the world, we need fonts to provide the relevant metrics for all writing systems, and that means both that OpenType needs to allow such metrics and font designers need to provide accurate numbers. See issue and liaison statement.
1.1. Module Interactions
This module replaces and extends the CSS inline layout model and features defined in [CSS2] section 10.8.
1.2. Value Definitions
This specification follows the CSS property definition conventions from [CSS2] using the value definition syntax from [CSS-VALUES-3]. Value types not defined in this specification are defined in CSS Values & Units [CSS-VALUES-3]. Combination with other CSS modules may expand the definitions of these value types.
In addition to the property-specific values listed in their definitions, all properties defined in this specification also accept the CSS-wide keywords as their property value. For readability they have not been repeated explicitly.
2. Inline Layout Model
In inline layout, a mixed, recursive stream of text and inline-level boxes forming an inline formatting context within a block container are laid out by fragmenting them into a stack of line boxes. Within each line box, inline-level boxes are aligned to each other along the block axis, typically by the baselines of their text.
Any block container that directly contains inline-level content—such as inline boxes, atomic inlines, and text sequences—establishes an inline formatting context to lay out its contents using inline layout. The block container’s content edges form the containing block for each of the inline-level boxes participating in its inline formatting context.
The block container also generates a root inline box, which is an anonymous inline box that holds all of its inline-level contents. (Thus, all text in an inline formatting context is directly contained by an inline box, whether the root inline box or one of its descendants.) The root inline box inherits from its parent block container, but is otherwise unstyleable.
In an inline formatting context, content is laid out along the inline axis, ordered according to the Unicode bidirectional algorithm and its controls [CSS-WRITING-MODES-3] and distributed according to the typesetting controls in [CSS-TEXT-3]. Inline-axis margins, borders, and padding are respected between inline-level boxes (and their margins do not collapse). The resulting rectangular area that contains the boxes that form a single line of inline-level content is called a line box.
Note: Line boxes and inline boxes and inline-level boxes are each different things! See [CSS-DISPLAY-3] for an in-depth discussion of box types and related terminology.
2.1. Layout of Line Boxes
Line boxes are created as needed to hold inline-level content within an inline formatting context. When an inline box exceeds the logical width of a line box, or contains a forced line break, it is split (see CSS Text 3 § 5 Line Breaking and Word Boundaries) into several fragments [CSS-BREAK-3], which are partitioned across multiple line boxes. Like column boxes in multi-column layout [CSS-MULTICOL-1], line boxes are fragmentation containers generated by their formatting context, and are not part of the CSS box tree.
Note: Inline boxes can also be split into several fragments within the same line box due to bidirectional text processing. See [CSS-WRITING-MODES-3].
Line boxes are stacked as the direct contents of the block container box in its block flow direction and aligned within this container as specified by align-content [CSS-ALIGN-3]. Thus, an inline formatting context consists of a stack of line boxes. Line boxes are stacked with no separation (except as specified elsewhere, e.g. for float clearance) and they never overlap.
In general, the line-left edge of a line box touches the line-left edge of its containing block and the line-right edge touches the line-right edge of its containing block, and thus the logical width of a line box is equal to the inner logical width of its containing block (i.e. the block container’s content box). However, floating boxes or initial letter boxes can come between the containing block edge and the line box edge, reducing the space available to, and thus the logical width of, any such impacted line boxes. (See CSS 2 § 9.4.2 Inline formatting contexts/CSS 2 § 9.5 Floats and § 7 Initial Letters.)
The logical height of a line box is fitted to its contents once they have been block-axis aligned. This fit is controlled by line-height and line-fit-edge. The first/last line boxes in a block container may additionally be trimmed by text-box-trim.
2.2. Layout Within Line Boxes
As described above, user agents flow inline-level boxes into a stack of line boxes. Layout within each line box is performed, sizing and positioning each box fragment and line box independently, as follows:
-
Baseline Alignment: All in-flow inline-level boxes in the line box are aligned to each other in the block axis according to dominant-baseline and vertical-align. This is referred to as baseline alignment. Those with line-relative values for baseline-shift are assumed to be aligned so as to minimize the line box height.
-
Content Size Contribution Calculation: The layout bounds (i.e. the size contributions) of each inline-level box in the line box are calculated:
- For atomic inlines such as replaced elements and inline blocks: this is their margin box.
- For the root inline box, and for inline boxes with line-fit-edge: leading: this derived from their used line-height, ignoring any margin/border/padding; see § 5.3 Calculating the Logical Height Contributions (“Layout Bounds”) of Inline Boxes.
- For other inline boxes: this is derived from their line-fit-edge metrics, and includes any margin/border/padding; see § 5.3 Calculating the Logical Height Contributions (“Layout Bounds”) of Inline Boxes.
-
Line Box Sizing: The line box’s logical height is sized to exactly include the aligned layout bounds of all its inline-level boxes.
-
Content Positioning: The root inline box’s aligned subtree and boxes line-relative values for baseline-shift are positioned within the line box.
Define what to do for top/bottom/center aligned boxes that are taller than the rest of the content.
Note: Empty inline boxes still have margins, padding, borders, and a line-height, and thus influence these calculations just like boxes with content.
2.3. Phantom Line Boxes
Line boxes that contain no text, no preserved white space, no inline boxes with non-zero inline-axis margins, padding, or borders, and no other in-flow content (such as atomic inlines or ruby annotations), and do not end with a forced line break are phantom line boxes. Such boxes must be treated as zero-height line boxes for the purposes of determining the positions of any descendant content (such as absolutely positioned boxes), and both the line box and its in-flow content must be treated as not existing for any other layout or rendering purpose.
What’s invisible?
Such phantom line boxes, which can still contain unstyled empty inline boxes, out-of-flow boxes, and/or collapsed document white space, are ignored, for example, for:
-
finding the first formatted line
-
applying text-box-trim
-
etc.
Firefox allows the inline boxes within a phantom line box to accept outline,which allows it to make focus rings visible. As in other browsers, all other properties that could make the element visible (e.g. box-shadow) seem to be ignored.
2.4. Painting Order
Except as specified for positioned boxes (see [CSS-POSITION-3]) inline-level boxes are painted in document order; the z-index property does not generally apply.
3. Baselines and Alignment Metrics
3.1. Introduction to Baselines
A baseline is a line along the inline axis of a line box along which individual glyphs of text are aligned. Baselines guide the design of glyphs in a font (for example, the bottom of most alphabetic glyphs typically align with the alphabetic baseline), and they guide the alignment of glyphs from different fonts or font sizes when typesetting.
Different writing systems prefer different baselines.
A well-constructed font contains a baseline table, which indicates the position of one or more baselines within the font’s design coordinate space. (The design coordinate space is scaled with the font size.)
The baseline table is a property of the font, and the positions of the various baselines apply to all glyphs in the font.
Different baseline tables can be provided for alignment in horizontal and vertical text. UAs should use the vertical tables in vertical typographic modes and the horizontal tables otherwise.
Note: Fonts can have more than one baseline table in each axis; the UA is responsible for choosing the appropriate table in consideration of font-language-override and the content language.
3.2. Baselines and Metrics
CSS uses the following text-based metrics as baselines for inline layout functions such as alignment, box sizing, and initial letter layout.
The CSSWG would like to know which baseline values are necessary for each property that uses them (dominant-baseline, alignment-baseline, text-box-edge, line-fit-edge, initial-letter-align): if any can be dropped, or any need to be added. See Issue 859.
- alphabetic
-
Used in writing
Latin, Cyrillic, Greek, and many other scripts,
corresponds to the bottom of most, but not all, their characters,
(such as “m”, “Ш”, “Δ”).
Often represented as zero in font design coordinate systems;
assigned to
romnin OpenType and tobslnvalue zero in TrueType AAT. - cap-height
-
Corresponds to
the top of capital letters
(such as “T”, “Б”, “Σ”)
in Latin, Cyrillic, Greek, etc.
Calculated using
sCapHeightin OpenType. - x-height
-
Corresponds to
the top of short lowercase letters
(such as “m”, “л”, “α”)
in Latin, Cyrillic, Greek, etc.
Calculated using
sxHeightin OpenType. - x-middle
- Corresponds to halfway between the alphabetic and x-height baselines.
- ideographic-over
-
Corresponds to
the line-over design edge of CJK (Han/Hangul/Kana) text.
Assigned to
idtpin OpenType. - ideographic-under
-
Corresponds to
the line-under design edge of CJK (Han/Hangul/Kana) text.
Assigned to
ideoin OpenType. - central
-
Corresponds to the ideographic central baseline,
halfway between the ideographic-under and ideographic-over baselines.
Assigned to
bslnvalue 1 in TrueType AAT. - ideographic-ink-over
-
Corresponds to the line-over ink edge of CJK (Han/Hangul/Kana) text.
Assigned to
icftin OpenType. - ideographic-ink-under
-
Corresponds to the line-under ink edge of CJK (Han/Hangul/Kana) text.
Assigned
icfbin OpenType. - hanging
-
Corresponds to hanging baseline
from which characters in
Tibetan and similar unicameral scripts
with a strong but not absolute top edge seem to “hang”.
Assigned to
hangin OpenType and tobslnvalue 3 in TrueType AAT. - math
-
Corresponds to center baseline around which mathematical characters are designed.
Assigned to
mathin OpenType andbslnvalue 4 in TrueType AAT. - text-over
- Corresponds to the metric used as the line-over edge of an inline’s content box per [CSS2].
- text-under
- Corresponds to the metric used as the line-under edge of an inline’s content box per [CSS2].
- em-over
- Corresponds to a conceptual ascent normalized to ensure 1em between em-over and em-under. See A.1: Calculating Em-over and Em-under.
- em-under
- Corresponds to a conceptual descent normalized to ensure 1em between em-over and em-under. See A.1: Calculating Em-over and Em-under.
Note: These metrics are optical design metrics, and therefore do not necessarily correspond exactly to actual glyph outlines.
In general, these metrics are taken from the appropriate font, but if they are missing or need to be derived from a box rather than text, they must be synthesized, see § 3.3 Baselines of Glyphs and Boxes and Appendix A: Synthesizing Alignment Metrics.
3.2.1. Ascent and Descent Metrics
CSS assumes that every font has font metrics that specify a characteristic height above the baseline—called the ascent metric—and a characteristic depth below it—called the descent metric—which CSS uses for laying out text and boxes in an inline formatting context. Note that these are metrics of the font as a whole and need not correspond to the ascender and descender of any individual glyph.
Note: It is recommended that implementations that use OpenType or TrueType fonts
use the metrics sTypoAscender and sTypoDescender
from the font’s OS/2 table
(after scaling to the current element’s font size)
to find the ascent metric and descent metric for CSS layout.
In the absence of these metrics,
the "Ascent" and "Descent" metrics from the HHEA table should be used.
3.2.2. Line Gap Metrics
Font formats can allow for a font-recommended “line gap” or “external leading” metric. This metric is referred to as the line gap metric, and may be incorporated into the line box logical height calculations when line-height is normal as described in § 5.3 Calculating the Logical Height Contributions (“Layout Bounds”) of Inline Boxes.
Note: In OpenType, the line gap metric can be found
as sTypoLineGap or hhea.lineGap.
UAs must floor the line gap metric at zero.
3.3. Baselines of Glyphs and Boxes
Each font, glyph, and inline-level box is assumed to have a baseline coordinate for each baseline type indicating that baseline’s position on its block axis. The set of such baselines is called its baseline set. The baseline from this set that is used to align the box or glyph within its alignment context is called its alignment baseline; the baseline used to align its content within itself is called its dominant baseline.
For an individual glyph, the baseline set derives from the font’s baseline table. For an inline box, it derives from its first available font regardless of whether the box actually contains any glyphs from that font. If the requisite metrics are missing from a font, the UA must synthesize them, see A.2: Synthesizing Baselines (and Other Font Metrics) for Text.
For other boxes, its baseline set is nominally derived from its contents in accordance with baseline-source and the rules of the formatting context in which it participates. For an atomic inline box with no baseline set in the inline formatting context’s inline axis its alignment baselines are synthesized from its margin box, see A.3: Synthesizing Baselines for Atomic Inlines.
4. Baseline Alignment
While most CSS formatting contexts position content by aligning boxes with respect to their container’s edges, inline layout positions boxes in the block axis by aligning them with respect to each other using their baselines.
More specifically, (unless using a line-relative shift value) each glyph or inline-level box is aligned in the block axis by positioning its alignment baseline to match the corresponding baseline of its parent (which is its alignment context), and then is potentially shifted from that position according to its post-alignment shift.
Note: Baseline alignment always matches corresponding baselines: alphabetic to alphabetic, hanging to hanging, mathematical to mathematical, etc.
When aligning a box, the alignment baseline is chosen according to its alignment-baseline and baseline-source values (see shorthand vertical-align), and defaults to matching the parent’s dominant-baseline. For a glyph, the alignment baseline is always determined by the parent’s dominant baseline.
Given following sample markup:
< p >< span class = "outer" > Ap< span class = "inner" > ਜੀ</ span ></ span ></ p >
And the following style rule:
.inner{ font-size : 75 % ; }
The baseline sets of the parent (.outer) and the child (.inner)
will not match up due to the font size difference.
The child box is aligned to its parent
by matching up their alphabetic baselines.
The alphabetic baseline is used here because by default a box’s alignment baseline matches the dominant baseline of its parent, and in horizontal typographic mode, the dominant baseline itself defaults to the alphabetic baseline.
If we add vertical-align: super
to the .inner element from the example above,
the same rules are used to align the .inner child to its parent;
but in addition to the baseline alignment,
the child is shifted to the superscript position.
4.1. Dominant Baselines: the dominant-baseline property
| Name: | dominant-baseline |
|---|---|
| Value: | auto | <baseline-metric> |
| Initial: | auto |
| Applies to: | block containers, inline boxes, table rows, grid containers, flex containers, and SVG text content elements |
| Inherited: | yes |
| Percentages: | N/A |
| Computed value: | specified keyword |
| Canonical order: | per grammar |
| Animation type: | discrete |
This property specifies the dominant baseline, which is the default baseline type used to align content within the box.
For inline boxes, the dominant baseline is used to align the box’s text (and, unless otherwise specified by vertical-align, any inline-level child boxes) by aligning each glyph/box’s corresponding baseline to the box’s own dominant baseline. For other boxes, it indicates the default alignment baseline of any boxes participating in baseline alignment in the box’s alignment context; see (alignment-baseline: baseline and [CSS-ALIGN-3]).
The <baseline-metric> value, which identifies specific baseline metrics, expands to
<baseline-metric> = text-bottom | alphabetic | ideographic | middle
| central | mathematical | hanging | text-top
Values have the following meanings:
- auto
-
Equivalent to alphabetic in horizontal writing modes
and in vertical writing modes
when text-orientation is sideways.
Equivalent to central in vertical writing modes
when text-orientation is mixed or upright.
However, in SVG text, the origin point of glyphs (used for coordinate-based glyph positioning) is always handled as for central in vertical writing modes.
- text-bottom
- Use the text-under baselines.
- alphabetic
- Use the alphabetic baselines.
- ideographic
- Use the ideographic-under baselines.
- middle
- Use the x-middle baselines; except under text-orientation: upright (where the alphabetic and x-height baselines are essentially meaningless) use the central baseline.
- central
- Use the central baselines.
- mathematical
- Use the math baselines.
- hanging
- Use the hanging baselines.
- text-top
- Use the text-over baselines.
See [CSS-WRITING-MODES-3] for an introduction to dominant baselines.
Define behavior for mixed vertical orientations that isn’t nonsensical when specified baseline isn’t central.
4.2. Transverse Box Alignment: the vertical-align property
| Name: | vertical-align |
|---|---|
| Value: | [ first | last] || <'alignment-baseline'> || <'baseline-shift'> |
| Initial: | baseline |
| Applies to: | see individual properties |
| Inherited: | no |
| Percentages: | N/A |
| Computed value: | see individual properties |
| Animation type: | see individual properties |
| Canonical order: | per grammar |
This shorthand property specifies how an inline-level box is aligned within the line by specifying its alignment baseline type (alignment-baseline), baseline alignment preference (baseline-source), and post-alignment shift (baseline-shift) in a single declaration.
If first or last is specified, it sets baseline-source (which is otherwise reset to auto). Other values are as for the corresponding longhand properties, see below.
Authors should use this shorthand (vertical-align) instead of its longhands, unless specifically needing to cascade its longhands independently or (on SVG elements) to support legacy SVG implementations.
Note: vertical-align can also affect the alignment of table cells when align-content is normal. Specifically, top (baseline-shift: top) maps it to start, bottom (baseline-shift: bottom) to end, and otherwise middle (alignment-baseline: middle) to center. See CSS Box Alignment 3 § 5.1.1 Block Containers (Including Table Cells).
4.2.1. Alignment Baseline Source: the baseline-source longhand
| Name: | baseline-source |
|---|---|
| Value: | auto | first | last |
| Initial: | auto |
| Applies to: | inline-level boxes that establish an independent formatting context |
| Inherited: | no |
| Percentages: | N/A |
| Computed value: | specified keyword |
| Canonical order: | per grammar |
| Animation type: | discrete |
When an inline-level box has more than one possible source for baseline information (such as for a multi-line inline block or inline flex container) this property specifies whether the first baseline set or last baseline set is preferred for alignment, indicating the box’s baseline alignment preference. Values have the following meanings:
- auto
- Specifies last-baseline alignment for inline-block, first-baseline alignment for everything else.
- first
- Specifies first-baseline alignment.
- last
- Specifies last-baseline alignment.
See CSS Box Alignment 3 § 9.1 Determining the Baselines of a Box for how to find the baselines of boxes other than inline boxes.
4.2.2. Alignment Baseline Type: the alignment-baseline longhand
| Name: | alignment-baseline |
|---|---|
| Value: | baseline | <baseline-metric> |
| Initial: | baseline |
| Applies to: | inline-level boxes, flex items, grid items, table cells, and SVG text content elements |
| Inherited: | no |
| Percentages: | N/A |
| Computed value: | specified keyword |
| Canonical order: | per grammar |
| Animation type: | discrete |
This property specifies the box’s alignment baseline: the baseline used to align the box prior to applying its post-alignment shift (if applicable). Values have the same meanings as for dominant-baseline; the baseline keyword uses the dominant baseline choice of the parent.
When performing baseline alignment, these values specify which baseline of the box is aligned to the corresponding baseline of its alignment context. (In an inline formatting context, inline-level box fragments and glyphs share an established by their parent inline box fragment along its inline axis. For other formatting contexts, see CSS Box Alignment 3 § 9.2 Baseline Alignment Grouping.) In SVG text layout, these values instead specify the baseline that is aligned to the SVG current text position.
4.2.2.1. Legacy Values for SVG
SVG implementations may support the following aliases in order to support legacy content:
-
text-before-edge aliasing text-top
-
text-after-edge aliasing text-bottom
4.2.3. Post-Alignment Shift: the baseline-shift longhand
| Name: | baseline-shift |
|---|---|
| Value: | <length-percentage> | sub | super | top | center | bottom |
| Initial: | 0 |
| Applies to: | inline-level boxes and SVG text content elements |
| Inherited: | no |
| Percentages: | refer to the used value of line-height |
| Computed value: | the specified keyword or a computed <length-percentage> value |
| Canonical order: | per grammar |
| Animation type: | by computed value type |
This property specifies the box’s post-alignment shift. The baseline-relative shift values <length-percentage>, sub, super shift the box relative to its baseline-aligned position, whereas the line-relative shift values top, center, and bottom shift the inline box and its contents relative to the bounds of its line box.
Authors should use the vertical-align shorthand, which has existed since CSS1, instead of this baseline-shift longhand (except in SVG content, where conversely baseline-shift is more widely-supported in legacy user agents).
Values have the following meanings:
- <length>
- Raise (positive value) or lower (negative value) by the specified length.
- <percentage>
- Raise (positive value) or lower (negative value) by the specified percentage of the line-height.
- sub
- Lower by the offset appropriate for subscripts of the parent’s box. The UA may use the parent’s font metrics to find this offset; otherwise it defaults to dropping by one fifth of the parent’s used font-size.
- super
- Raise by the offset appropriate for superscripts of the parent’s box. The UA may use the parent’s font metrics to find this offset; otherwise it defaults to raising by one third of the parent’s used font-size.
- top
- Align the line-over edge of the aligned subtree with the line-over edge of the line box.
- center
- Align the center of the aligned subtree with the center of the line box.
- bottom
- Align the line-under edge of the aligned subtree with the line-under edge of the line box.
The aligned subtree of an inline box contains the layout bounds of that box and the aligned subtrees of all child inline boxes whose computed alignment-baseline value is not itself a line-relative shift value. The line-over edge of the aligned subtree is the highest over edge of the layout bounds in the subtree, and the line-under edge is analogously the lowest.
The line-relative shift values don’t fit perfectly in the dichotomy between alignment-baseline and baseline-shift. There’s decent arguments for either option. They’re currently drafted here, but if there’s a strong argument to move them, please file an issue for consideration.
4.2.3.1. Legacy Values for SVG
User agents may additionally support the keyword baseline as computing to 0 if is necessary for them to support legacy SVG content. This value is not allowed in the vertical-align shorthand.
We would prefer to remove the baseline value, and are looking for feedback from SVG user agents as to whether it’s necessary.
5. Logical Heights and Inter-line Spacing
The block-axis sizing of a line box depends on the sizes and alignment of its inline-level contents. This sizing is controlled by the line-height and line-fit-edge properties.
5.1. Line Spacing: the line-height property
| Name: | line-height |
|---|---|
| Value: | normal | <number [0,∞]> | <length-percentage [0,∞]> |
| Initial: | normal |
| Applies to: | non-replaced inline boxes and SVG text content elements |
| Inherited: | yes |
| Percentages: | computed relative to 1em |
| Computed value: | the specified keyword, a number, or a computed <length> value |
| Canonical order: | per grammar |
| Animation type: | by computed value type |
This property specifies the box’s preferred line height, which is used in calculating its “layout bounds”, i.e. its contribution to the logical height of its line box. (See § 5.3 Calculating the Logical Height Contributions (“Layout Bounds”) of Inline Boxes.)
Note: Because it applies to the root inline box when specified on a block container, line-height effectively establishes the minimum height of the block’s line boxes.
Values for this property have the following meanings:
- normal
- Determine the preferred line height automatically based on the metrics of the used font.
- <length [0,∞]>
- The specified length is used as the preferred line height. Negative values are illegal.
- <number [0,∞]>
- The preferred line height is this number multiplied by the element’s used font-size. Negative values are illegal. The computed value is the same as the specified value.
- <percentage [0,∞]>
- The preferred line height and computed value of the property is this percentage of the element’s computed font-size. Negative values are illegal.
Note: Metrics from fonts other than the first available font only impact the layout bounds of an inline box with line-height: normal.
div { line-height : 1.2 ; font-size : 10 pt } /* number */
div { line-height : 1.2 em ; font-size : 10 pt } /* length */
div { line-height : 120 % ; font-size : 10 pt } /* percentage */
However, they inherit differently: the first one inherits as a number, which will lead to different line heights if descendants have different font sizes; the last two as inherit as absolute lengths, which will not be influenced by the font size on descendants.
The fact that percentages compute to lengths is annoying. See also Issue 3118 and Issue 2165.
Note: When line-fit-edge is leading, the margins, borders, and padding of inline boxes do not affect the line box’s height calculation. However, they are still rendered around these boxes. This means that if the size specified by line-height is less than the size of the box, backgrounds and borders can “bleed” into adjoining line boxes, potentially obscuring earlier content.
5.2. Text Edge Metrics: the line-fit-edge property
| Name: | line-fit-edge |
|---|---|
| Value: | leading | <text-edge> |
| Initial: | leading |
| Applies to: | inline boxes |
| Inherited: | yes |
| Percentages: | N/A |
| Computed value: | the specified keyword |
| Canonical order: | per grammar |
| Animation type: | discrete |
This is an early draft of a proposal, and might change significantly as design critiques and use cases are registered and various details and interactions with other properties are worked out. Do not ship (yet).
Inline boxes, whose primary purpose is to contain text, are sized in the block axis based on their font metrics. The line-fit-edge property controls which metrics are used. These chosen metrics are used as the basis for the layout bounds of the inline box (if it is not the root inline box); and also, by default, are the metrics used for text-box-trim.
The <text-edge> value, which identifies specific font metrics, expands to
<text-edge> = [ text | ideographic | ideographic-ink ]
| [ text | ideographic | ideographic-ink | cap | ex ]
[ text | ideographic | ideographic-ink | alphabetic ]
The first value specifies the text over edge; the second value specifies the text under edge. If only one value is specified, both edges are assigned that same keyword if possible; else text is assumed as the missing value.
Do we need longhands or is this shorthand enough? [Issue #5236]
Values have the following meanings:
- leading
- Use the ascent/descent plus any positive half-leading. Margin/padding/border is ignored for the purpose of sizing the line box.
- text
- Use the text-over baseline/text-under baseline as the over/under edge.
- cap
- Use the cap-height baseline as the over edge.
- ex
- Use the x-height baseline as the over edge.
- ideographic
- Use the ideographic-over baseline/ideographic-under baseline as the over/under edge.
- ideographic-ink
- Use the ideographic-ink-over baseline/ideographic-ink-under baseline as the over/under edge.
- alphabetic
- Use the alphabetic baseline as the under edge.
Is text a reasonable name for the ascent/descent metrics, or can we think of something better? Ditto leading as a keyword. [Issue #8067]
Unless line-fit-edge is leading—in which case the box’s own line-height is used to add spacing—the box’s margin, padding, and border also contribute to the layout bounds.
Note: The leading and text values rely on the font ascent and descent to make sure the text fits. Other values are more likely to result in overlap or overflow caused by ascents above the specified metrics (such as for diacritics), so authors using these values need to be careful to provide sufficient spacing for the text, particularly in multi-lingual contexts.
This illustration doesn’t match actual font metrics, it’s actually illustrating the cap-height, not the ascent. [Issue #11364]
When line-fit-edge is leading, vertical rhythm can be broken any time there is a change in font metrics or vertical alignment within a paragraph.
Other values are more likely to give consistent line spacing—as long as there is enough leading added that the half-leading on the root inline is large enough to accommodate the specified metrics of any descendants. The line box will still grow, however, to accommodate content that would otherwise overflow, to avoid overlap between lines.
Note: Although only leading applies positive half-leading, in order to allow text to be set tightly, all values apply negative half-leading, see § 5.3 Calculating the Logical Height Contributions (“Layout Bounds”) of Inline Boxes. Half-leading is applied equally to both sides of the text; for more precise overlap control authors can use line-fit-edge: text together with negative margins on the affected text.
5.3. Calculating the Logical Height Contributions (“Layout Bounds”) of Inline Boxes
The contribution of an inline box to the logical height of its line box, here referred to as its layout bounds, is always calculated with respect to its own text metrics, as described below, and is controlled by line-fit-edge and line-height. The sizes and positions of child boxes do not influence its layout bounds (nor its own logical height, for that matter, see inline-sizing).
Note: The layout bounds need not correspond to the box’s edges.
To find the layout bounds of an inline box, the UA must first align all the glyphs directly contained in the inline box to each other by their dominant baselines. (See § 3.3 Baselines of Glyphs and Boxes.) If the inline box contains no glyphs at all, or if it contains only glyphs from fallback fonts, it is considered to contain a “strut” (an invisible glyph of zero width) with the metrics of the box’s first available font.
For each glyph (including the “strut”), A represents its ascent above the baseline; D represents its descent below. Unless line-fit-edge specifies a different metric to use, A refers to the ascent metric (for the given font at its given size) and D to the descent metric, each adjusted to account for the dominant baseline’s offset from zero. If line-height computes to normal and either line-fit-edge is leading or this is the root inline box, the font’s line gap metric may also be incorporated into A and D by adding half to each side as half-leading.
When its computed line-height is normal, the layout bounds of an inline box encloses all its glyphs, going from the highest A to the deepest D. (Note that glyphs in a single box can come from different fonts and thus might not all have the same A and D.)
When its computed line-height is not normal, its layout bounds are derived solely from metrics of its first available font (ignoring glyphs from other fonts), and leading is used to adjust the effective A and D to add up to the used line-height. Calculate the leading L as L = line-height - (A + D). Half the leading (its half-leading) is added above A of the first available font, and the other half below D of the first available font, giving an effective ascent above the baseline of A′ = A + L/2, and an effective descent of D′ = D + L/2. However, if line-fit-edge is not leading and this is not the root inline box, if the half-leading is positive, treat it as zero. The layout bounds exactly encloses this effective A′ and D′.
Note: L may be negative.
Additionally, when line-fit-edge is not leading, the layout bounds are inflated by the sum of the margin, border, and padding on each side. In order to allow negative margin values to have an actual effect, negative margins are also accumulated onto the layout bounds of any descendant inline boxes participating in the same inline formatting context.
In Quirks Mode [QUIRKS], any inline box fragment that has zero borders and padding and that does not directly contain text or preserved white space [CSS-TEXT-3] is ignored when sizing the line box.
6. Trimming Leading Over/Under Text
To ensure consistent spacing in the basic case of running text, CSS line layout introduces leading both above and below the text content of each line as needed to ensure its line-height. In addition, the ascent and descent font metrics themselves often include extra space above and below the most typical glyph shapes in order to accommodate occasional characters and diacritics that ascend or descend beyond the typical bounds. This prevents adjacent lines of text from overlapping each other. However, all this extra spacing interferes with visual alignment and with control over effective (visually-apparent) spacing.
The text-box property allows trimming this additional space above and below the first and last lines of a block, allowing more precise control over spacing around the glyphs. By relying on font metrics rather than hard-coded lengths, this feature allows content to be resized, rewrapped, and rendered in a variety of fonts while maintaining that precise spacing.
A common problem is vertical centering. It’s easy to vertically center the text container to an icon, but because the visual boundaries of Latin text are the cap height and the alphabetic baseline, rather than the ascent and descent, this often doesn’t yield the intended visual effect.
To center the text visually, it’s necessary to assume the cap height and alphabetic baseline as the top and bottom edges of the text, respectively.
By using text-box-trim to strip out the spacing above the cap height and below the alphabetic baseline, centering the box actually centers the text; and does so reliably, regardless of what font is used to render it.
6.1. Shorthand for Text Box Trimming: the text-box property
| Name: | text-box |
|---|---|
| Value: | normal | <'text-box-trim'> || <'text-box-edge'> |
| Initial: | normal |
| Applies to: | block containers, multi-column containers, and inline boxes |
| Inherited: | no |
| Percentages: | N/A |
| Computed value: | the specified keyword |
| Canonical order: | per grammar |
| Animation type: | discrete |
This property is a shorthand for setting the text-box-trim and text-box-edge properties in a single declaration.
If the single keyword normal is specified, it sets text-box-trim to none and text-box-edge to auto. Otherwise, omitting the text-box-trim value sets it to trim-both (not the initial value), while omitting the text-box-edge value sets it to auto (the initial value).
6.2. Trimming Over/Under Text: the text-box-trim property
| Name: | text-box-trim |
|---|---|
| Value: | none | trim-start | trim-end | trim-both |
| Initial: | none |
| Applies to: | block containers, multi-column containers, and inline boxes |
| Inherited: | no |