The CSS formatting model provides for a flow of elements and text inside of a container to be wrapped into lines. This module describes box model for this inline layout model and defines the block-axis alignment and sizing of inline-level content, extending the model in [CSS2]. It also adds a special layout mode for drop-caps.
CSS is a language for describing the rendering of structured documents
(such as HTML and XML)
on screen, on paper, etc.
Status of this document
This section describes the status of this document at the time of its publication.
A list of current W3C publications
and the latest revision of this technical report
can be found in the W3C technical reports index at https://www.w3.org/TR/.
This document was published
by the CSS Working Group as a Working Draft using the Recommendation
track.
Publication as a Working Draft
does not imply endorsement by W3C and its Members.
This is a draft document
and may be updated, replaced
or obsoleted by other documents at any time.
It is inappropriate to cite this document as other than work in progress.
Please send feedback
by filing issues in GitHub (preferred),
including the spec code "css-inline" in the title, like this:
"[css-inline] ...summary of comment...".
All issues and comments are archived.
Alternately, feedback can be sent to the (archived) public mailing list www-style@w3.org.
This document was produced by a group operating under the W3C Patent Policy.
W3C maintains a public list of any patent disclosures made in connection with the deliverables of the group;
that page also includes instructions for disclosing a patent.
An individual who has actual knowledge of a patent which the individual believes
contains Essential Claim(s) must disclose the information in accordance with section 6 of the W3C Patent Policy.
The following features are at-risk, and may be dropped during the CR period:
"At-risk" is a W3C Process term-of-art, and does not necessarily imply that the feature is in danger of being dropped or delayed. It means that the WG believes the feature may have difficulty being interoperably implemented in a timely manner, and marking it as such allows the WG to drop the feature if necessary when transitioning to the Proposed Rec stage, without having to publish a new Candidate Rec without the feature first.
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.
The block container also generates
a root inline box,
which is an anonymousinline 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.
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:
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.
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.
Picture of alphabetic text in two font sizes with the baseline and em-boxes
Alphabetic text in two font sizes with the baseline and em-boxes
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.)
In a well-designed mixed-script font,
the glyphs are positioned in the coordinate space
to harmonize with one another when typeset together.
The baseline table is then constructed
to match the shape of the glyphs,
each baseline positioned to match the glyphs
from its preferred scripts.
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.
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 romn in OpenType
and to bsln value zero in TrueType AAT.
cap-height
Corresponds to
the top of capital letters
(such as "T", "Б", "Σ")
in Latin, Cyrillic, Greek, etc.
Calculated using sCapHeight in OpenType.
x-height
Corresponds to
the top of short lowercase letters
(such as "m", "л", "α")
in Latin, Cyrillic, Greek, etc.
Calculated using sxHeight in OpenType.
Corresponds to
the line-over design edge of CJK (Han/Hangul/Kana) text.
Assigned to idtp in OpenType.
ideographic-under
Corresponds to
the line-under design edge of CJK (Han/Hangul/Kana) text.
Assigned to ideo in OpenType.
central
Corresponds to the ideographic central baseline,
halfway between the ideographic-under and ideographic-over baselines.
Assigned to bsln value 1 in TrueType AAT.
ideographic-ink-over
Corresponds to the line-over ink edge of CJK (Han/Hangul/Kana) text.
Assigned to icft in OpenType.
ideographic-ink-under
Corresponds to the line-under ink edge of CJK (Han/Hangul/Kana) text.
Assigned icfb in 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 hang in OpenType
and to bsln value 3 in TrueType AAT.
math
Corresponds to center baseline around which mathematical characters are designed.
Assigned to math in OpenType
and bsln value 4 in TrueType AAT.
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.
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.
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.
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.
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.
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.
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.
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:
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).
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.
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.
The three rules in the example below have the same used line height:
div {line-height:1.2;font-size:10pt}/* number */
div {line-height:1.2em;font-size:10pt}/* length */
div {line-height:120%;font-size:10pt}/* 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.
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.
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.
Three different values of the line-fit-edge property.
The line-fit-edge property, showing values for leading, cap, and ex.
The red lines indicate the layout bounds of the inline box.
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.
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).
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 leadingL 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′.
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.
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.
Even though different fonts have different cap heights,
by using the font’s metric rather than a magic number,
the layout intention is met even as the font is changed.
6.1. Shorthand for Text Box Trimming: the text-box property
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 both (not the initial value),
while omitting the text-box-edge value sets it to auto (the initial value).
Add examples.
6.2. Trimming Over/Under Text: the text-box-trim property
For block containers and column boxes:
trim the block-end side of the last formatted line
to the specified metric of its root inline box.
If there is no such line,
or if there is intervening non-zero padding or borders,
there is no effect.
If multiple ancestors specify trimming on the same line box,
the metric used is that of the innermost block container that requests trimming on that side of the line box.
Note: Content and ink overflowing a box
due to non-initial values of text-box-trim is handled the same as content that would overflow the box or line box otherwise.
Unlike ::first-line,
when applying to the first (or last) formatted line of a multi-column container,
this property applies to the first (or last) formatted lines
of every column in the multi-column container.
What happens if the column is split by a spanner? [Issue #11363]
This property specifies the metrics to use for text-box-trim effects.
Values have the same meanings as for line-fit-edge;
the auto keyword
uses the value of line-fit-edge,
interpreting leading (the initial value) as text.
This has a confusing name. We need a new name.
Alternatively, incorporate this into text-box-trim? [Issue #5189]
This property specifies how the logical height of the content area of an inline box is measured in relation to its contents.
It has no effect on the size or position of the box’s contents,
the line box, or any other content.
Values have the following meanings:
normal
The content area of the inline box is sized and positioned to fit (possibly hypothetical) text
from its first available font.
If text-box-trim indicates trimming,
then the specified metric must be used.
Otherwise, this specification does not specify how.
A UA may, e.g., use the maximum ascender and descender of the font.
(This would ensure that glyphs with parts above or below the em-box
still fall within the content area,
but leads to differently sized boxes for different fonts.)
The editors would appreciate any examples of drop initials in non-western scripts, especially Indic scripts.
7.1. An Introduction to Initial Letters
This section is non-normative.
Large, decorative letters have been used to start new sections of text since before the invention of printing.
In fact, their use predates lowercase letters entirely.
7.1.1. Drop Initial
A dropped initial (or "drop cap")
is a larger-than-usual letter at the start of a paragraph,
with a baseline at least one line lower than the first baseline of the paragraph.
The size of the drop initial is usually indicated by how many lines it occupies.
Two- and three-line drop initials are very common.
3-line drop cap with E Acute
Three-line drop initial with E acute.
Since the cap-height of the drop initial aligns with the cap-height of the main text,
the accent extends above the paragraph.
The exact size and position of a dropped initial depends on the alignment of its glyph.
Reference points on the drop cap must align precisely
with reference points in the text.
The alignment constraints for drop initials depend on the writing system.
In Western scripts, the top reference points are
the cap height of the initial letter and of the first line of text.
The bottom reference points are
the alphabetic baseline of the initial letter
and the baseline of the Nth line of text.
The figure below shows a simple two-line drop cap, with the relevant reference lines marked.
drop cap showing alignment
Two-line drop cap showing baselines (green lines), cap-height (red line), and ascender (cyan line).
In Han-derived scripts, the initial letter extends
from the block-start edge of the glyphs on the first line
to the block-end edge of the glyphs on the Nth line.
Japanese Vertical Initial
Two-line drop initial in vertical writing mode
In certain Indic scripts,
the top alignment point is the hanging baseline,
and the bottom alignment point is the text-after-edge.
Devanagari initial letter
Devanagari initial letter aligned with hanging baseline. Alignment points shown in red.
7.1.2. Sunken Initial Letters
Some styles of drop initials do not align with the first line of text.
A sunken initial (or "sunken cap")
both sinks below the first baseline,
and extends above the first line of text.
sunken drop initial
Sunken cap. The letter drops two lines, but is the size of a three-line initial letter.
7.1.3. Raised Initial Letters
A raised initial (often called a "raised cap" or "stick-up cap") "sinks" to the first text baseline.
Note: A proper raised initial has several advantages over
simply increasing the font size of a first letter.
The line spacing in the rest of the paragraph will not be altered,
but text will still be excluded around large descenders.
And if the size of raised initial is defined to be an integral number of lines,
implicit baseline grids can be maintained.
raised cap
Raised cap. The initial letter is the size of a 3-line initial, but does not drop.
7.2. Selecting Initial Letters
This section is non-normative.
Initial letters are typically a single letter, although
they may include punctuation or a sequence of characters which
are perceived by the user to be a single typographic unit.
The ::first-letter pseudo-element,
defined in [SELECT] and [CSS-PSEUDO-4],
can be used to select the character(s) to be formatted as initial letters.
Authors who need more control over which characters are included in an initial letter,
or who want to apply initial-letter formatting to replaced elements or multiple words
can alternately apply the initial-letter property to the first inline-level child of a block container.
<p>This paragraph has a dropped "T".
<p><img alt="H" src="illuminated-h.svg">ere we have an illuminated "H".
<p><span>Words may also</span> be given initial letter styling at the beginning of a paragraph.
::first-letter, /* style first paragraph’s T */
img, /* style illuminated H */
span /* style phrase inside span */
{ initial-letter: 2; }
Note that
since ::first-letter selects punctuation before or after the first letter,
these characters are included in the initial letter when ::first-letter is used.
Paragraph showing both opening quote and first letter set as three-line drop cap
The ::first-letter pseudo-element selects the quotation mark as well as the "M".
Should there be a way to opt out of this behavior? See GitHub Issue 310.
7.3. Creating Initial Letters: the initial-letter property
This property specifies
the size and sink for dropped, raised, and sunken initial letters
as the number of lines spanned.
For example, the following code will create
a 2-line dropped initial letter
at the beginning of each paragraph
that immediately follows a second-level heading:
h2 + p::first-letter { initial-letter: 2; }
It takes the following values:
normal
No special initial letter effect. Text behaves as normal.
This optional second argument
defines the number of lines the initial letter should sink.
A value of 1 indicates a raised initial;
values greater than 1 indicate a sunken initial.
Values less than one are invalid.
The size of the initial letter does not have to be an integral number of lines.
In this case only the top aligns.
Non-integral initial letter that only aligns at base
In conjunction with other CSS properties, initial-letter can be used to create
"adjacent initial letters," where the initial letter is adjacent to the text:
To give authors more control over which characters can be styled as an initial letter and to allow the possibility of multi-character initial letters (such as for first word or first phrase styling),
the initial-letter property applies not just
to the CSS-defined ::first-letter pseudo-element,
but also to inside-positioned ::marker pseudo-elements and
to inline-level boxes that are placed at the start of the first line.
Specifically, initial-letter applies to
any inline-level box—including any such ::first-letter or ::marker box—that is the first child of its parent box
and whose ancestors (if any) that are descendants of its containing block are all first-child inline boxes that have a computedinitial-letter value
of normal.
For example,
the <span>, <em>, and <b> elements
in the following example are
"first-most inline-level descendants" of the <p>,
but the <strong> element
is not:
<p><span><em><b>This</b> phrase</em> is styled
<strong>specially</strong>.</span> ...
The initial-letter property will take effect only on the <em>.
The styling on <b> is ignored,
as it has an ancestor already styled as an initial letter;
and the styling on <strong> is ignored
because it is a second sibling.
As mentioned earlier, the alignment of initial letters
depends on the script used.
The initial-letter-align property can be used to specify the proper alignment.
This property specifies the alignment points
used to size and position an initial letter.
Two sets of alignment points are necessary:
the over and under alignment points of the initial letter are matched to corresponding over and under points
of the root inline box.
Note: This will essentially match the edges of the initial letter to middle of the line gap above/below the first/last impacted lines,
which is an effect sometimes used in certain types of Indic typesetting[ILREQ].
Correct alignment of initial letter in scripts such as Hebrew and Thai
is currently not possible because OpenType lacks corresponding metrics.
(Issue 5244)
Hebrew 2-line drop-letter alignment using the hebrew-top and alphabetic baselines
Note: The ordering of keywords in this property is fixed in case border-box is expanded to [ border-box | alphabetic | ideographic | hanging ] to allow explicitly specifying the initial letter’s alignment points.
For the non-atomic inline initial letter,
the box and its contents
participate in the same inline formatting context as the line on which it occurs,
and a lot of special rules apply
to give the expected sizing and alignment.
For an atomic initial letter,
however,
which is either a replaced element or which establishes an independent formatting context for its contents,
the sizing of the box
(aside from its automatic size in the block axis)
and layout of the contents within the box
follows the usual rules:
it is primarily the positioning of the box
which is special.
When padding and borders are zero,
the initial letter may be kerned;
see below.
7.5.3. Font Sizing of Initial Letters
For an inline initial letter,
the font size used for sizing the initial letter contents
is calculated to fulfill its specified size (see initial-letter)
as anchored by its specified alignment points (see initial-letter-align).
Note that no layout is required in this calculation:
it is based only on computed values and font metrics.
These used font size calculations do not affect the computedfont-size,
and therefore have no effect on the computation of em length values, etc.
What about inheritance to descendants? [Issue #4988]
The line height used in these calculations
is the line-height of the containing block
(or, in the case where a baseline grid is in use,
the baseline-to-baseline spacing required by the baseline grid [CSS-LINE-GRID-1]).
The contents of the lines spanned,
and therefore any variation in their heights and positions,
is not accounted for.
Text underlay shows how initial letter alignment is not affected by the content of the spanned lines.
For an N-line drop initial in a Western script,
the cap-height of the letter needs to be (N – 1) times the line-height,
plus the cap-height of the surrounding text.
Note this height is not the font size of the drop initial.
Actually calculating this font size is tricky.
For an N-line drop initial,
we find the drop initial font size to be:
Font size of drop cap = ((N-1) * line-height + [cap-height of para] * [font size of paragraph])/[cap-height ratio of drop initial font]
Update this calculation to be a) generic across writing systems / alignment points and b) handle non-integer sizes.
A three-line drop initial in Adobe Minion Pro
would have a font size of 61.2pt given
12pt text, 16pt line-height, and a cap-height of 651/1000 (from the font’s OS/2 table).
For an atomic initial letter,
the used font size is the computed font size as usual.
7.5.4. Shaping and Glyph Selection
When initial-letter is not normal,
an inline initial letter is isolated for glyph shaping;
however the text after it should shape across
the inline initial letter box’s boundaries,
assuming its presence as part of the first line’s text content.
(See CSS Text 3 § 7.3 Shaping Across Element Boundaries.)
For example, if the first letter of the word "يحق"
were styled with initial-letter: 2 1,
the first letter would be styled in its isolated form "ي",
as the initial letter,
followed by the medial/final-form "حق",
which assumes it is preceded by the initial letter’s contents
as normal text.
The glyph(s) of an initial letter do not always fit within the specified sink.
For example, if an initial letter has a descender,
it could crash into the (n+1)th line of text.
This is not desirable.
3-line drop cap with J, with descender crashing into fourth line of text
Incorrect: three-line initial letter
(initial-letter: drop 3) with descender.
In this font, the capital "J" extends well below the baseline (shown in red).
3-line drop cap with J, but four-line exclusion
Correct: text excluded around glyph bounding box
However, if its block-startpadding and border are both zero,
then its block-startcontent edge instead coincides
with its over alignment point exactly,
and any content overflowing above that point is ignored for the purpose of layout.
Note: If an inline initial letter has ascenders above its over alignment point,
and the author has not provided sufficient margin on either the initial letter itself or its containing block,
then those ascenders might collide with preceding content.
Note: It might be nice to automatically provide the necessary spacing
by treating such ascenders as a margin that can collapse
with the margin of the containing block,
and thus guarantee the requisite spacing without imposing any additional space
unless it becomes actually necessary.
Depending on implementation complexity,
this option may be explored in the future;
but in the meantime,
authors need to be careful to provide the requisite spacing explicitly.
Should the hanging punctuation be included in the box instead
(so that the box is drawn around the punctuation when it is made visible through borders/background),
but rather only excluded when positioning the box
(so that the initial letter remains flush,
with the hanging punctuation properly hanging)?
See discussion.
If the inline size is definite, text-align is honored
for aligning the contents of the initial letter within its box in the inline axis (using its inline-axis bearings as usual,
not the bounding box of its glyph outlines).
Note: An initial letter is essentially positioned
such that it would sink the number of lines
specified by initial-letter’s second argument
and align to the requisite under alignment point
if it was assumed that its containing block held only
the initial letter itself
followed by an infinite sequence of plain text
as the direct contents of its root inline box.
Its position is not affected by line height inconsistencies
introduced by the contents of the impacted line boxes.
Constant-sized text underlay shows how initial letter alignment is not affected by the content of the spanned lines.
If the initial letter is a non-atomic inline
with an automaticinline size and
zero padding and borders,
its margin box is kerned (negatively inset)
by the distance from the start edge of its content box
to the point in the content that would have been placed
at the start edge of the line box if it were not an initial letter (i.e. the distance between its glyph bounding box
and its start side bearing).
This inset is effectively
an additional inline-startmargin on the box.
This property specifies whether lines impacted by an initial letter are shortened to fit the rectangular shape of the initial letter box
or the contour of its glyph outline.
Behaves as none if the first typographic character unit after the initial letter belongs to Unicode General Category Zs.
Otherwise behaves as for all on the first line of the block containing the initial letter
and as none on the rest.
This example shows why contour-fitting the first line is necessary,
and why it is dropped when the initial letter is followed by a space:
optical kerning in the presence or absence of a space after the initial letter
In the top paragraph, the initial letter "A" has a word space after it:
the gap between the top of the "A" and the next letter
provides the necessary word separation.
In the next paragraph, the initial letter "A"
is part of the first word,
and leaving a gap between the top of the "A" and the next letter
would create a jarring visual break within the word.
In this case, the first line of text
should be kerned into the initial letter’s area,
as shown in the bottom paragraph.
Do we need an unconditional first?
(I.e. Should we rename this value to auto and add a first value
that does not check for spaces?) See GitHub issue 410
all
For each line of text impacted by the initial letter,
the line box adjacent to the initial letter starts
at the start-most point that does not overlap
the initial letter’s glyph outline.
If the value of shape-outside is not none, shape-outside is used instead of the glyph outline.
This value is the same as none,
except that the exclusion area of the impacted lines
is increased as necessary for its end-edge to land on the character grid,
i.e. to be a multiple of (1ic + letter-spacing)
as computed on the containing block.
The justify-self property can then be used to align
the initial letter box within the exclusion area.
Diagram of Japanese initial letter in vertical writing mode
Diagram of Japanese initial letter in vertical writing mode
Note: In this example, the exclusion area for the drop initial
is larger than its glyph in order to preserve inline-axis alignment.
This value behaves the same as first except that the adjustment to the first line is given explicitly
instead of being inferred from the glyph shape.
This really needs font-relative lengths to be relative to the used size.
Note: This value exists because it is easier to implement.
Authors are encouraged to use the first value
and to set margins to control spacing,
and to use this as a fallback for glyph detection if necessary.
In the following example, UAs that support first will use the glyph outline
plus the specified margin in order to place the first line,
whereas UAs that only support <length> or <percentage> values
will pull in the first line by 40% of the initial letter’s width
(and then add the margin to that point).
h1 + p:first-letter {
initial-letter: 3; /* 3-line drop-cap */
initial-letter-wrap: first;
margin-right: 0.1em;
}
@supports (not (initial-letter-wrap: first)) {
/* Classes auto-generated on paragraphs to match first letter. */
p.A:first-letter {
initial-letter-wrap: -40%; /* Start of glyph outline, assuming correct font. */
}
}
These values and related annoyance is likely unnecessary if someone submits a patch to Blink to support first.
Edit figure to show how auto behaves in varying contexts
However, to ensure consistent alignment of all the impacted lines, collapsible white space between a sunken initial and subsequent content on its originating line is collapsed away,
and any letter-spacing or justification opportunity that would normally be introduced by
the juxtaposition of the contents of a sunken initial and the subsequent contents of the line
is suppressed.
(Note that this does not affect word-spacing or
the justification opportunity introduced by a word separator because that space is provided by the typographic character unit alone
and not by its juxtaposition with an adjacent character.)
7.8.2. Edge Effects: Indentation and Hanging Punctuation
text-indent and hanging-punctuation apply to an initial letter’s originating line box as usual,
and cause a shift in the start of the line’s contents
including the initial letter itself.
Subsequent lines affected by the exclusion are shortened as usual,
possibly more or less than otherwise depending on the resulting position
of the initial letter.
initial letter with text indent
Initial letter with text indent.
If the initial letter box is contained by inline box ancestors,
the boundaries of those inline boxes are drawn
to exclude the initial letter box,
as if it were outside their startmost margin edge.
This is a purely geometric operation:
it does not affect e.g.
property inheritance or
the effective letter-spacing between the initial letter box and subsequent content.
7.8.4. Multi-line Initial Letters
If an initial letter is too long to fit on one line,
it wraps (according to the usual text-wrapping rules),
each line filled and formatted exactly as if it were the first line
and the initial letter too long to fit any subsequent normal text.
Any normal text after the initial letter starts on its last line,
affected exactly as if that line were the first line.
multi-line drop cap
Drop cap extends to two lines.
7.9. Clearing Initial Letters
7.9.1. Raised and sunken caps
The margin box of an initial letter contributes to the size
of its containing element.
Initial letters that extend above the first line of text,
known as "raised caps" or "sunken caps,"
do not extend up into previous elements.
Since the content box for an initial letter includes all glyph ink,
this also means that accents or other ink
above the cap height of an initial letter
will not impinge on previous elements.
raised cap para after normal para
Raised cap (initial-letter: 3 1) on right;
note that the position of the "C" is the same in both cases,
but on the right all text is moved down relative to the initial letter.
Handle glyph ink above cap height of font.
Proposal: Make it an exclusion area for line boxes and border boxes. Include margin specified on initial-letters as part of exclusion area in order to control spacing.
Draw a box model diagram here. Does the margin of the initial letter collapse with its container?
7.9.2. Short paragraphs with initial letters
A paragraph with an initial letter can have fewer lines of text
than the initial letter occupies.
In this case, the initial letter’s top alignment is still honored,
and its exclusion area continues into any subsequent blocks.
This forces the subsequent inline-level content to wrap around the initial letter—exactly as if that block’s text were part of its own containing block.
(This is similar to how floats exclude content in subsequent block boxes.)
short para with initial letter
The red text is a short paragraph with an initial letter.
Note the subsequent paragraph wraps around the initial letter
just as text in the paragraph with the initial letter does.
If the subsequent block starts with an initial letter,
establishes an independent formatting context,
or specifies clear in the initial letter’s containing block’s start direction,
then it must clear the previous block’s initial letter.
short para with initial letter followed by para with initial
The red text is a short paragraph with an initial letter.
The subsequent paragraph clears because it also has an initial letter.
The clear property does not care about initial letters:
it neither applies to initial letters nor clears them when applied to nearby floats.
Like line boxes or floats, initial letter boxes must not overlap the margin boxes of any floats participating in the same block formatting context.
An overlapping initial letter box is shifted inward or downward
until either it fits without overlapping
or there are no more floats present.
If a line box’s start edge shifts or moves down to clear a float,
an initial letteroriginating in it moves with it;
likewise if an initial letter shifts inward or moves downward
to clear a float,
its originating line box and subsequent line boxes
shorten and/or move accordingly.
If an inline-start float
originates in the first line of content
adjacent to an initial letter,
then it moves past the initial letter towards the containing block edge,
exactly as if the initial letter were any other inline-level content.
However, if such a float originates
in subsequent lines of content adjacent to a (sunk) initial letter,
then that float must clear the initial letter.
initial letter interacting with floats
In the absence of an initial letter, the first line of text could abut the blue float.
But the presence of the initial letter requires that the text move over.
See CSS2§9.5 for more information about the layout of floats
and adjacent content. [CSS2]
Whether an inline-end float originating in subsequent lines
must clear the initial letter (as inline-start floats do)
is still under discussion.
There is no aesthetic reason to require it;
however it’s yet unclear how the underlying layout model
would distinguish between the two cases.
7.9.4. Interaction with Fragmentation (Pagination)
Since a single glyph cannot be fragmented across
pages (or columns or other fragmentation containers),
an initial letter is considered monolithic[CSS-BREAK-3] for the purpose of block-axis fragmentation (breaking across pages, columns, regions, etc.).
Additionally,
breaks between the in-flow lines alongside an initial letter box are avoided,
much as breaks between line boxes affected be widows and orphans are avoided.
However,
if there is a forced break alongside the initial letter box,
then it takes precedence;
but has no effect on the initial letter box itself.
As with other monolithic objects,
if an initial letter box occurs
at the top of a fragmentation container and that fragmentation container is too short to contain it,
it may be either truncated or sliced.
Adjacent content, however, must be fragmented according to its own rules,
not truncated or sliced along with the initial letter.
Note: The em-over and em-under baselines are not used by CSS.
Their definitions are included in this module
for consistency with the other metrics used by Canvas TextMetrics API.
If any one of central, ideographic-over, or ideographic-under is defined by the font,
then em-over is 0.5em over the central baseline,
and em-under is 0.5em under it,
with the central baseline derived from the others
if missing or undefined
(see below).
Otherwise, the ascent and descent are both proportionally augmented or reduced
to add up to exactly 1em,
and these normalized metrics are taken
as the em-over and em-under metrics, respectively.
Note: This calculation ensures that em-over and em-under are always exactly 1em apart
while trying to center the glyph outlines’ "center of gravity" between them.
A.2: Synthesizing Baselines (and Other Font Metrics) for Text
Some fonts might not contain the metrics information necessary
to align text properly as described in this module.
User agents may use the following strategies
in the absence of a required metric:
Use related metrics
Certain metrics are typically related,
and this relationship can be used
to at least heuristically derive the missing metric.
If the font format itself does not define any specific calculations,
the following rules may be used:
The ideographic-over and ideographic-under baselines
are typically 1em apart,
so if only one of the ideographic-over/ideographic-under/central baselines are provided,
this relation can be used to calculate the other two.
Metrics may be derived from the glyph shapes.
For example,
The center of the minus sign (U+2212)
can be taken as the mathematical baseline.
The amount by which the lowercase "o"
descends below the alphabetic baseline
can be subtracted from its highest point
to measure the x-height.
measuring the x height of the letter o
Measuring the x height.
The amount by which the uppercase "O"
descends below the alphabetic baseline
can be subtracted from its highest point
to measure the cap-height.
The bounding box of 永 (U+6C38) can be used to find
the ideographic character face edges.
The top edge of the center of the Hebrew He (U+05D4 "ה")
can be taken as the Hebrew hanging baseline.
The top edge of the center of the letter Ka
can be taken as the hanging baseline.
Which Ka is used should depend on the content language:
Language
Script
Letter
Devanagari
क U+0915 KA
Bengali
ক U+0995
Gurmukhi
ਕ U+0A15
Tibetan
ཀ U+0F40
Pick a default.
finding the position of the hanging baseline of the letter ka
The hanging baseline is at the top edge of the character ink.
Note: Authors can use margins (positive or negative)
to adjust the alignment of replaced content within a line.
In this example, the author is using a set of images
to display characters that don’t exist.
img[src^="/text/"] {
height: 1em; /* Size to match adjacent text */
margin-bottom: -0.2em; /* Baseline at 20% above bottom */
}
...
<p>This is some text with words written in an unencoded script:
<img src="/text/ch3439.png" alt="...">
<img src="/text/ch3440.png" alt="...">
<img src="/text/ch3442.png" alt="...">
Note: A future level of CSS may include a way of specifying a full baseline table for replaced elements.
(This will probably look like a baseline-table property
that accepts [<baseline-keyword> <percentage>]+.)
Reworked the relationship of the earlier line-sizing and text-box-trim proposals
to create text-edge and a differently-structured leading-trim.
(Issue 5168)
Special thanks goes to the initial authors,
Eric A. Meyer and Michel Suignard.
In additions to the authors, this specification would not have been possible without the help from:
David Baron,
Mike Bremford,
David M Brown,
Oriol Brufau,
John Daggett,
Stephen Deach,
Sylvain Galineau,
David Hyatt,
Myles Maxfield,
Shinyu Murakami,
Jan Nicklas,
Tess O’Connor,
Sujal Parikh,
Florian Rivoal,
Alan Stearns,
Weston Thayer,
Bobby Tung,
Chris Wilson,
Grzegorz Zygmunt.
Privacy Considerations
No new privacy considerations have been reported on this specification.
Security Considerations
No new security considerations have been reported on this specification.
Conformance
Document conventions
Conformance requirements are expressed with a combination of
descriptive assertions and RFC 2119 terminology. The key words "MUST",
"MUST NOT", "REQUIRED", "SHALL", "SHALL NOT", "SHOULD", "SHOULD NOT",
"RECOMMENDED", "MAY", and "OPTIONAL" in the normative parts of this
document are to be interpreted as described in RFC 2119.
However, for readability, these words do not appear in all uppercase
letters in this specification.
All of the text of this specification is normative except sections
explicitly marked as non-normative, examples, and notes. [RFC2119]
Examples in this specification are introduced with the words "for example"
or are set apart from the normative text with class="example",
like this:
This is an example of an informative example.
Informative notes begin with the word "Note" and are set apart from the
normative text with class="note", like this:
Note, this is an informative note.
Advisements are normative sections styled to evoke special attention and are
set apart from other normative text with <strong class="advisement">, like
this: UAs MUST provide an accessible alternative.
Conformance classes
Conformance to this specification
is defined for three conformance classes:
A style sheet is conformant to this specification
if all of its statements that use syntax defined in this module are valid
according to the generic CSS grammar and the individual grammars of each
feature defined in this module.
A renderer is conformant to this specification
if, in addition to interpreting the style sheet as defined by the
appropriate specifications, it supports all the features defined
by this specification by parsing them correctly
and rendering the document accordingly. However, the inability of a
UA to correctly render a document due to limitations of the device
does not make the UA non-conformant. (For example, a UA is not
required to render color on a monochrome monitor.)
An authoring tool is conformant to this specification
if it writes style sheets that are syntactically correct according to the
generic CSS grammar and the individual grammars of each feature in
this module, and meet all other conformance requirements of style sheets
as described in this module.
Partial implementations
So that authors can exploit the forward-compatible parsing rules to
assign fallback values, CSS renderers must treat as invalid (and ignore
as appropriate) any at-rules, properties, property values, keywords,
and other syntactic constructs for which they have no usable level of
support. In particular, user agents must not selectively
ignore unsupported component values and honor supported values in a single
multi-value property declaration: if any value is considered invalid
(as unsupported values must be), CSS requires that the entire declaration
be ignored.
Implementations of Unstable and Proprietary Features
Once a specification reaches the Candidate Recommendation stage,
non-experimental implementations are possible, and implementors should
release an unprefixed implementation of any CR-level feature they
can demonstrate to be correctly implemented according to spec.
To establish and maintain the interoperability of CSS across
implementations, the CSS Working Group requests that non-experimental
CSS renderers submit an implementation report (and, if necessary, the
testcases used for that implementation report) to the W3C before
releasing an unprefixed implementation of any CSS features. Testcases
submitted to W3C are subject to review and correction by the CSS
Working Group.
[ first | last] || <'alignment-baseline'> || <'baseline-shift'>
baseline
see individual properties
no
N/A
see individual properties
per grammar
see individual properties
Issues Index
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. ↵
Define what to do for top/bottom/center aligned boxes that are taller than the rest of the content. ↵
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. ↵
We would prefer to remove the baseline value, and are looking for feedback from SVG user agents as to whether it’s necessary. ↵
The fact that percentages compute to lengths is annoying.
See also Issue 3118 and Issue 2165. ↵
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).↵
What happens if the column is split by a spanner? [Issue #11363]↵
This has a confusing name. We need a new name.
Alternatively, incorporate this into text-box-trim? [Issue #5189]↵
The editors would appreciate any examples of drop initials in non-western scripts, especially Indic scripts. ↵
Should there be a way to opt out of this behavior? See GitHub Issue 310. ↵
Correct alignment of initial letter in scripts such as Hebrew and Thai
is currently not possible because OpenType lacks corresponding metrics.
(Issue 5244)
Hebrew 2-line drop-letter alignment using the hebrew-top and alphabetic baselines ↵
This only covers the most common cross-linguistic transcription systems.
Should we include any other / all script tags in the UA style sheet? ↵
Update this calculation to be a) generic across writing systems / alignment points and b) handle non-integer sizes. ↵
Should the hanging punctuation be included in the box instead
(so that the box is drawn around the punctuation when it is made visible through borders/background),
but rather only excluded when positioning the box
(so that the initial letter remains flush,
with the hanging punctuation properly hanging)?
See discussion. ↵
Do we need an unconditional first?
(I.e. Should we rename this value to auto and add a first value
that does not check for spaces?) See GitHub issue 410↵
This really needs font-relative lengths to be relative to the used size. ↵
These values and related annoyance is likely unnecessary if someone submits a patch to Blink to support first. ↵
Edit figure to show how auto behaves in varying contexts ↵
Handle glyph ink above cap height of font.
Proposal: Make it an exclusion area for line boxes and border boxes. Include margin specified on initial-letters as part of exclusion area in order to control spacing. ↵
Draw a box model diagram here. Does the margin of the initial letter collapse with its container? ↵
Whether an inline-end float originating in subsequent lines
must clear the initial letter (as inline-start floats do)
is still under discussion.
There is no aesthetic reason to require it;
however it’s yet unclear how the underlying layout model
would distinguish between the two cases. ↵