Title: CSS Pseudo-Elements Module Level 4
Shortname: css-pseudo
Level: 4
Status: ED
Work Status: Refining
Group: csswg
ED: https://drafts.csswg.org/css-pseudo-4/
TR: https://www.w3.org/TR/css-pseudo-4/
Previous Version: https://www.w3.org/TR/2020/WD-css-pseudo-4-20201231/
Previous Version: https://www.w3.org/TR/2019/WD-css-pseudo-4-20190225/
Previous Version: https://www.w3.org/TR/2016/WD-css-pseudo-4-20160607/
Previous Version: https://www.w3.org/TR/2015/WD-css-pseudo-4-20150115/
!Issues List: Tracked in Editor's Draft
Editor: Daniel Glazman, Disruptive Innovations, w3cid 13329
Editor: Elika J. Etemad / fantasai, Invited Expert, http://fantasai.inkedblade.net/contact, w3cid 35400
Editor: Alan Stearns, Adobe Systems Inc., stearns@adobe.com, w3cid 46659
Abstract: This CSS module defines pseudo-elements, abstract elements that represent portions of the CSS render tree that can be selected and styled.
Ignored Terms: initial-letter, PseudoElement, pseudo(), selectors
Default Highlight: css
Indent: 2
spec:css-color-3; type:value; text:currentcolor
spec:css-color-3; type:property; text:color
spec:fill-stroke-3; type:property; text:stroke-width
Introduction
This section is informative.
Pseudo-elements represent abstract elements of the document
beyond those elements explicitly created by the document language.
Since they are not restricted to fitting into the document tree,
they can be used to select and style portions of the document
that do not necessarily map to the document's tree structure.
For instance, the ''::first-line'' pseudo-element can
select content on the first formatted line of an element
after text wrapping,
allowing just that line to be styled differently
from the rest of the paragraph.
Each pseudo-element is associated with an originating element
and has syntax of the form ''::name-of-pseudo''.
This module defines the pseudo-elements that exist in CSS
and how they can be styled.
For more information on pseudo-elements in general,
and on their syntax and interaction with other selectors,
see [[!SELECTORS-4]].
Typographic Pseudo-elements
The ::first-line pseudo-element
The ::first-line [=pseudo-element=] represents
the contents of the [=first formatted line=]
of its [=originating element=].
The rule below means
“change the letters of the first line of every
p
element to uppercase”:
p::first-line { text-transform: uppercase }
The selector ''p::first-line''
does not match any real document element.
It instead matches a pseudo-element
that the user agent will automatically insert
at the beginning of every
p
element.
Note: Note that the length of the first line depends on a number of factors,
including the width of the page, the font size, etc.
For example, given an ordinary HTML [[HTML5]] paragraph such as:
<P>This is a somewhat long HTML paragraph
that will be broken into several lines.
The first line will be styled
by the ‘::first-line’ pseudo-element.
The other lines will be treated
as ordinary lines in the paragraph.</P>
Depending on the width of the element,
its lines might be broken as follows:
THIS IS A SOMEWHAT LONG HTML PARAGRAPH THAT
will be broken into several lines. The
first line will be by the ‘::first-line’
pseudo-element. The other lines will be
treated as ordinary lines in the paragraph.
or alternately as follows:
THIS IS A SOMEWHAT LONG
HTML paragraph that will
be broken into several
lines. The first line will
be by the ‘::first-line’
pseudo-element. The other
lines will be treated as
ordinary lines in the
paragraph.
Finding the First Formatted Line
In CSS, the ''::first-line'' pseudo-element
can only have an effect when attached to a block container:
* The first formatted line of
a [=block container=] that establishes an [=inline formatting context=]
contains the [=inline-level=] content of its first [=line box=].
* The [=first formatted line=] of
a [=block container=] or [=multi-column container=]
that contains [=block-level=] content
(and is not a [=table wrapper box=])
is the [=first formatted line=] of
its first [=in-flow=] [=block-level=] child.
If no such line exists,
it has no [=first formatted line=].
Note: The [=first formatted line=] can be an empty line.
For example, the first line of the p
in
<p><br>First…
doesn't contain any letters.
Thus the word “First” is not on the first formatted line,
and will not be affected by ''p::first-line''.
Note: The first line of a [=block container=]
that does not participate in a [=block formatting context=]
cannot be the first formatted line of an ancestor element.
Thus, in <DIV><P STYLE="display: inline-block">Hello<BR>Goodbye</P> etcetera</DIV>
the first formatted line of the DIV
is not the line “Hello”,
but rather the (otherwise empty) line that contains that entire inline block.
When a [=first formatted line=] is represented
by multiple ''::first-line'' pseudo-elements,
they are nested in the same order as their [=originating elements=].
Consider the following markup:
<DIV>
<P>First paragraph</P>
<P>Second paragraph</P>
</DIV>
If we assume a [=fictional tag sequence=] to represent
the elements’ ''::first-line'' pseudo elements,
it would be something like:
<DIV>
<P><DIV::first-line><P::first-line>First paragraph</P::first-line></DIV::first-line></P>
<P><P::first-line>Second paragraph</P::first-line></P>
</DIV>
Styling the First Line Pseudo-element
The ''::first-line'' pseudo-element’s generated box
behaves similar to that of an inline-level element, but with certain restrictions.
The following CSS properties apply to a ''::first-line'' pseudo-element:
- all font properties (see [[CSS-FONTS-3]])
- the 'color' and 'opacity' properties (see [[CSS-COLOR-3]])
- all background properties (see [[CSS-BACKGROUNDS-3]])
- any typesetting properties that apply to inline elements (see [[CSS-TEXT-3]])
- all text decoration properties (see [[CSS-TEXT-DECOR-3]])
- the 'ruby-position' property (see [[CSS-RUBY-1]])
- any inline layout properties that apply to inline elements (see [[CSS-INLINE-3]])
- any other properties defined to apply to ''::first-line''
by their respective specifications
User agents may apply other properties as well except for
the following excluded properties:
- 'writing-mode'
- 'direction'
- 'text-orientation'
Inheritance and the ''::first-line'' Pseudo-element
During CSS [=inheritance=],
the [=box fragment|fragment=] of a child that occurs on the first line
inherits any standard [=inherited properties=]--
except the properties excluded above--
from the ''::first-line'' pseudo-element.
For all other properties,
including all [=custom properties=] [[!CSS-VARIABLES-1]],
inheritance is
from the non-pseudo parent.
(The portion of a child element that does not occur on the first line
always inherits from the non-pseudo parent.)
In the common case (of standard inherited CSS properties),
[=inheritance=] into and from a ''::first-line'' pseudo-element
can be understood by writing out a
fictional tag sequence
to represent ''::first-line''.
Consider the
earlier example;
in case of the first rendering, the fictional tag sequence would be:
<P><p::first-line>This is a somewhat long HTML paragraph
that</p::first-line> will be broken into several lines.
The first line will be styled
by the ‘::first-line’ pseudo-element.
The other lines will be treated
as ordinary lines in the paragraph.</P>
And in the case of the second rendering:
<P><p::first-line>This is a somewhat long</p::first-line> HTML paragraph
that will be broken into several lines.
The first line will be styled
by the ‘::first-line’ pseudo-element.
The other lines will be treated
as ordinary lines in the paragraph.</P>
If a pseudo-element breaks up a real element,
the effect can often be described
by a
fictional tag sequence
that closes and then re-opens the element.
Suppose we mark up the earlier example
with a
span
element encompassing the first sentence:
<P><span>This is a somewhat long HTML paragraph
that will be broken into several lines.</span>
The first line will be styled
by the ‘::first-line’ pseudo-element.
The other lines will be treated
as ordinary lines in the paragraph.</P>
The effect of the first rendering
would be similar to the following [=fictional tag sequence=]:
<P<P><p::first-line><span>This is a somewhat long HTML paragraph
that</span></P><p::first-line><span> will be broken into several lines.</span>
The first line will be styled
by the ‘::first-line’ pseudo-element.
The other lines will be treated
as ordinary lines in the paragraph.</P>
The ::first-letter pseudo-element
The ::first-letter pseudo-element represents
the first typographic letter unit [[!CSS-TEXT-3]]
on the first formatted line of its originating element,
its first-letter text.
The ''::first-letter'' pseudo-element can be used
to create “initial caps” and “drop caps”,
which are common typographic effects.
For example, the following rule creates a 2-line drop-letter
on every paragraph following a level-2 header,
using the 'initial-letter' property defined in [[CSS-INLINE-3]]:
h2 + p::first-letter { initial-letter: 2; }
As explained in [[!CSS-TEXT-3]],
a typographic letter unit can include more than one Unicode codepoint.
For example, combining characters must be kept with their base character.
Also, languages may have additional rules
about how to treat certain letter combinations.
In Dutch, for example, if the letter combination "ij" appears at the beginning of an element,
both letters should be considered within the ''::first-letter'' pseudo-element. [[UAX29]]
When selecting the [=first-letter text=],
the UA should tailor its definition of typographic letter unit
to reflect the first-letter traditions of the ''::first-letter'' pseudo-element’s containing block’s content language.
Note: Note that the first typographic letter unit may in fact
be a digit, e.g., the “6” in “67 million dollars is a lot of money.”
Punctuation (i.e, characters that belong to the Punctuation (P*
) Unicode general category [[!UAX44]])
that precedes or follows the first typographic letter unit
and any intervening space separators
(characters that belong to the Zs
Unicode general category [[!UAX44]])
must also be included as part of the [=first-letter text=]
in the ''::first-letter'' pseudo-element.
Finding the First Letter Text
As with ''::first-line'',
the ''::first-letter'' pseudo-element
can only have an effect when attached to a [=block container=].
Its [=first-letter text=] is the first such [=inline-level content=]
participating in the [=inline formatting context=]
of its [=originating element=]’s [=first formatted line=],
if it is not preceded by any other in-flow content
(such as images or inline tables) on its line.
For this purpose, any [=marker boxes=] are ignored,
as if they were out-of-flow.
However, if an element has in-flow ''::before'' or ''::after'' content,
the [=first-letter text=] is selected from the content of the element
including that generated content.
Example:
After the rule p::before {content: "Note: "}
, the
selector ''p::first-letter'' matches the "N" of "Note".
If no such text exists,
then there is no [=first-letter text=]
and no ''::first-letter'' pseudo-element.
Note: When the [=first formatted line=] is empty,
''::first-letter'' will not match anything.
For example, in this HTML fragment:
<p><br>First...
the first line doesn't contain any letters,
so ''::first-letter'' doesn't match anything.
In particular, it does not match the “F” of “First”,
which is on the second line.
Note: As with ''::first-line'',
the [=first-letter text=] of a [=block container=]
that does not participate in a [=block formatting context=]
cannot be the [=first-letter text=] of an ancestor element.
Thus, in <DIV><P STYLE="display: inline-block">Hello<BR>Goodbye</P> etcetera</DIV>
the first letter of the DIV
is not the letter “H”.
In fact, the DIV
doesn't have a first letter,
as its [=first formatted line=] contains no content besides the inline block.
Inheritance and Box Tree Structure of the ''::first-letter'' Pseudo-element
The ''::first-letter'' pseudo-element is wrapped
immediately around the [=first-letter text=] it represents,
even if that text is in a descendant.
When a [=first-letter text=] is represented by multiple ''::first-letter'' pseudo-elements,
they are nested in the same order as their [=originating elements=].
[=Inheritance=] behaves accordingly.
Consider the following markup:
<div>
<p><span>The first few words</span>
and the rest of the paragraph.
</div>
If we assume a [=fictional tag sequence=]
to represent the elements’ ''::first-letter'' pseudo-elements,
it would be something like:
<div>
<p><span><div::first-letter><p::first-letter>T</…></…>he first few words</span>
and the rest of the paragraph.
</div>
If the characters that would form the [=first-letter text=]
are not all in the same element
(as the ‘T
in <p>‘<em>T...
),
the user agent may create a ''::first-letter'' pseudo-element
from one of the elements, or all elements,
or simply not create a pseudo-element.
Additionally, if the [=first-letter text=]
is not at the start of the line
(for example due to bidirectional reordering,
or due to a [=list item=] [=marker=] with ''list-style-position: inside''),
then the user agent is not required to create the pseudo-element(s).
A ''::first-letter'' pseudo-element is contained within
any ''::first-line'' pseudo-elements,
and thus inherits (potentially indirectly) from ''::first-line'',
the same as any [=inline box=] on the same line.
Styling the ''::first-letter'' Pseudo-element
In CSS a ::first-letter pseudo-element is similar to an [=inline box=].
The following properties that apply to ''::first-letter'' pseudo-elements:
- all font properties (see [[CSS-FONTS-3]])
- the 'color' and 'opacity' properties (see [[CSS-COLOR-3]])
- all background properties (see [[CSS-BACKGROUNDS-3]])
- any typesetting properties that apply to inline elements (see [[CSS-TEXT-3]])
- all text decoration properties (see [[CSS-TEXT-DECOR-3]])
- any inline layout properties that apply to inline elements (see [[CSS-INLINE-3]])
- margin and padding properties (see [[CSS2]])
- border properties and 'box-shadow' (see [[CSS-BACKGROUNDS-3]])
- any other properties defined to apply to ''::first-letter''
by their respective specifications
User agents may apply other properties as well.
However,
in no case may the application of such unlisted properties to ''::first-letter'' change
what [=first-letter text=] is represented by that ''::first-letter''.
Note: In previous levels of CSS,
User Agents were allowed to choose a line height, width, and height
based on the shape of the letter,
to approximate font sizes;
and to take the glyph outline into account when performing layout.
The possibility of such loosely-defined magic has been intentionally removed,
as it proved to be a poor solution for the intended use case (drop caps and raised caps),
yet caused interoperability problems.
See 'initial-letter' in [[CSS-INLINE-3]]
for explicitly handling drop caps and raised caps.
Highlight Pseudo-elements
Selecting Highlighted Content: the ''::selection'', ''::target-text'', ''::spelling-error'', and ''::grammar-error'' pseudo-elements
The highlight pseudo-elements
represent portions of a document that have been given a particular status
and are typically styled differently to indicate that status to the user.
For example,
selected portions of the document are typically highlighted
(given alternate background and foreground colors, or a color wash)
to indicate their selected status.
The following highlight pseudo-elements are defined:
- ::selection
css/css-pseudo/active-selection-063.html
css/css-pseudo/textpath-selection-011.html
-
The ''::selection'' pseudo-element represents
the portion of a document that has been selected
as the target or object of some possible future user-agent operation(s).
It applies, for example, to selected text within an editable text field,
which would be copied by a copy operation or replaced by a paste operation.
- ::target-text
-
The ''::target-text'' pseudo-element represents text
directly targetted by the document URL’s [=url/fragment=], if any.
Note: When a [=url/fragment|URL fragment=] targets an element,
the '':target'' pseudo-element can be used to select it,
but ''::target-text'' does not match anything.
It only matches text that is itself targetted by the [[=url/fragment=]].
- ::spelling-error
-
The ''::spelling-error'' pseudo-element represents
a portion of text that has been flagged by the user agent as misspelled.
css/css-pseudo/spelling-error-001.html
css/css-pseudo/spelling-error-002-manual.html
css/css-pseudo/spelling-error-003-manual.html
- ::grammar-error
-
The ''::grammar-error'' pseudo-element represents
a portion of text that has been flagged by the user agent as grammatically incorrect.
css/css-pseudo/grammar-error-001.html
The highlight pseudo-elements
do not necessarily fit into the element tree,
and can arbitrarily cross element boundaries without honoring its nesting structure.
Note: A future level of CSS may introduce ways to create
custom highlight pseudo-elements.
Styling Highlights
The highlight pseudo-elements can only be styled
by a limited set of properties that do not affect layout
and can be applied performantly in a highly dynamic environment--
and additionally (to ensure interoperability)
whose rendering within the [[#highlight-bounds|required area]]
is not dependent on the exact (UA-determined) bounds
of the [=highlight overlay=].
The following properties apply to the highlight pseudo-elements:
- 'color'
- 'background-color'
- 'text-decoration' and its associated properties
css/css-pseudo/grammar-error-001.html
css/css-pseudo/spelling-error-001.html
css/css-pseudo/spelling-error-002-manual.html
css/css-pseudo/spelling-error-003-manual.html
- 'text-shadow'
css/css-pseudo/selection-text-shadow-006-manual.html
css/css-pseudo/selection-text-shadow-016.html
- 'stroke-color', 'fill-color', and 'stroke-width'
css/css-pseudo/textpath-selection-011.html
Issue: Are there any other properties that should be included here?
The 'color' property specifies the color of both the text
and all line decorations (underline, overline, line-through)
and emphasis marks ('text-emphasis')
applied to the text
by the originating element and its ancestors and descendants.
Note: Historically (and at the time of writing)
only 'color' and 'background-color' have been interoperably supported.
Default UA Styles
The following additions are recommended for the default UA stylesheet:
/* Represent default spelling/grammar error styling in an adjustable way */
:root::spelling-error { text-decoration-line: spelling-error; }
:root::grammar-error { text-decoration-line: grammar-error; }
/* Highlight targetted text */
:root::target-text { color: MarkText; background: Mark; }
UAs may apply additional effects to enhance the presentation
of highlighted content,
for example dimming content other than the highlighted text
or transitioning out a highlight style based on user interactions or timing.
These are not controlled by CSS.
Area of a Highlight
For each type of highlighting (see [[#highlight-selectors]])
there exists a single highlight overlay for the entire document,
the active portions of which are represented
by the corresponding highlight pseudo-element.
Each box owns the piece of the overlay corresponding to any text or replaced content
directly contained by the box.
-
For text, the corresponding overlay must cover at least the entire em box
and may extend further above/below the em box to the line box edges.
Spacing between two characters may also be part of the overlay area,
in which case it belongs to the innermost element that contains both characters
and is selected when both characters are selected.
-
For replaced content, the associated overlay must cover at least the entire replaced object,
and may extend outward to include the element's entire content box.
-
The overlay may also include other other areas within the border-box of an element;
in this case, those areas belong to the innermost such element that contains the area.
-
For an [=inline-level box=], the overlay may extend outside its border edges
in the [=block axis=] as far as the edges of its [=line box=].
Issue: See
F2F minutes,
dbaron's message,
Daniel's thread,
Gecko notes,
Opera notes,
Webkit notes
Issue: Not sure if this is the correct way of describing the way things work.
Cascading and Per-Element Highlight Styles
Each element draws its own active portions of the highlight overlays,
which receives the styles specified by
the corresponding highlight pseudo-element styles
for which that element is the originating element.
When multiple styles conflict,
the winning style is determined by the cascade.
When any supported property is not given a value by the cascade,
it's value is determined by inheritance from
the corresponding highlight pseudo-element
of its originating element's parent element
(regardless of whether that property is an inherited property).
css/css-pseudo/cascade-highlight-004.html
For example, if the following rules were applied:
p::selection { color: yellow; background: green; }
p > em::selection { color: orange; }
em::selection { color: red; }
to the following markup:
<p>Highlight this <em>and this</em>.</p>
The selection highlight would be green throughout,
with yellow text outside the
<em>
element
and orange text inside it.
css/css-pseudo/cascade-highlight-001.html
Authors wanting multiple selections styles should use
'':root::selection''
for their document-wide selection style,
since this will allow clean overriding in descendants.
''::selection'' alone applies to every element in the tree,
overriding the more specific styles of any ancestors.
For example, if an author specified
::selection { background: blue; }
p.warning::selection { background: red; }
and the document included
<p class="warning">Some <strong>very important information</strong></p>
The highlight would be blue over “very important information”
because the
<strong>
element´s ''::selection''
also matches the ''::selection { background: blue; }'' rule.
(Remember that ''*'' is implied when a tag selector is missing.)
The style rules that would give the intended behavior
(red highlight within
p.warning
, blue elsewhere) are
:root::selection { background: blue; }
p.warning::selection { background: red; }
css/css-pseudo/cascade-highlight-002.html
The UA must use its own highlight colors for ''::selection'' only
when neither 'color' nor 'background-color' has been specified by the author.
Note: This paired-cascading behavior
does not allow using the normal cascade
(i.g. '':root::selection'' rules in the UA style sheet)
to represent the OS default selection colors.
However it has been interoperably implemented in browsers
and is thus probably a Web-compatibility requirement.
Painting the Highlight
Backgrounds
Each highlight pseudo-element draws its background
over the corresponding portion of the highlight overlay,
painting it
immediately below any positioned descendants
(i.e. just before step 8 in CSS2.2§E.2).
The ''::selection'' overlay is drawn
over the ''::target-text'' overlay which is drawn
over the ''::spelling-error'' overlay
which is drawn over the ''::grammar-error'' overlay.
css/css-pseudo/highlight-z-index-001.html
css/css-pseudo/highlight-z-index-002.html
Shadows
Any 'text-shadow' applying to a [=highlight pseudo-element=]
is drawn over its corresponding [=highlight overlay=] background.
Such text shadows also stack over each other
(and over any original 'text-shadow' applied to the text and its decorations,
which continues to apply).
Note: Since each [=highlight overlay=] background
is drawn over any shadows belonging to the layer(s) below,
a [=highlight overlay=] background can obscure lower-level shadows.
Text and Text Decorations
A highlight pseudo-element
suppresses the normal drawing of any associated text
(and any text decorations applied to that text).
Instead the topmost active highlight overlay
redraws that text
(and its decorations)
over the highlight overlay backgrounds
using its own 'color'.
For this purpose,
''currentColor'' on a [=highlight pseudo-element=]’s 'color' property represents
the 'color' of the next highlight pseudo-element layer below,
falling back finally to the colors that would otherwise have been used
(those applied by the [=originating element=] or
an intervening [=pseudo-element=] such as ''::first-line'' or ''::first-letter'').
Any text decorations introduced by each highlight pseudo-element
are stacked in the same order as their backgrounds
over the text’s original decorations
and are all drawn, in their own colors.
Note: The element’s own text decorations
(both line decorations
and emphasis marks)
are thus drawn in the pseudo-element’s own 'color'
when that is not ''currentColor'',
regardless of their original color or fill specifications.
Replaced Elements
For non-replaced content, the UA must honor the 'color' and 'background-color'
(including their alpha channels) as specified.
However, for replaced content, the UA should create a semi-transparent wash
to coat the content so that it can show through the selection.
This wash should be of the specified 'background-color' if that is not ''transparent'',
else of the specified 'color';
however the UA may adjust the alpha channel.
Security and Privacy Considerations
Because the styling of spelling and grammar errors
can leak information about the contents of a user's dictionary
(which can include the user's name and even includes the contents of his/her address book!)
UAs that implement ''::spelling-error'' and ''::grammar-error''
must prevent pages from being able to read
the styling of such highlighted segments.
Tree-Abiding Pseudo-elements
Tree-abiding pseudo-elements always fit within the box tree.
They inherit any inheritable properties from their originating element;
non-inheritable properties take their initial values as usual.
[[CSS-CASCADE-4]]
Generated Content Pseudo-elements: ''::before'' and ''::after''
When their computed 'content' value is not ''content/none'',
these pseudo-elements generate boxes
as if they were immediate children of their originating element,
with content as specified by 'content'.
- ::before
- Represents a styleable child pseudo-element
immediately before the originating element's actual content.
- ::after
- Represents a styleable child pseudo-element
immediately after the originating element's actual content.
These pseudo-elements can be styled
exactly like any normal document-sourced element in the document tree;
all properties that apply to a normal element
likewise apply to ''::before'' and ''::after''.
For example, the following rule inserts the string “Note: ”
before the content of every
<p>
element
whose
class
attribute has the value
note
:
p.note::before { content: "Note: " }
Since the initial value of 'display' is ''display/inline'',
this will generate an inline box.
Like other inline children of
<p>
,
it will participate in
<p>
’s inline formatting context,
potentially sharing a line with other content.
As with the content of regular elements,
the generated content of ''::before'' and '':after'' pseudo-elements
can form part of any ''::first-line'' and ''::first-letter'' pseudo-elements
applied to its originating element.
List Markers: the ''::marker'' pseudo-element
The ::marker pseudo-element represents
the automatically generated [=marker box=] of a [=list item=].
(See [[CSS-DISPLAY-3]] and [[CSS-LISTS-3]].)
The contents of a ''::marker'' are ignored (not selected) by ''::first-letter''.
ISSUE: Interaction of ''::marker'' and ''::first-line'' is currently under discussion in Issue 4506.
Only a limited set of properties can be used on the ''::marker'' pseudo-element.
This list is defined in [[css-lists-3#marker-properties]].
The ''::before::marker'' or ''::after::marker'' selectors
are valid and
can be used to represent the [=marker boxes=]
of ''::before'' or ''::after'' pseudo-elements that happen to be [=list items=].
However ''::marker::marker'' is invalid,
and the computed value of 'display' on ''::marker''
loses its ''display/list-item'' aspect.
Placeholder Input: the ''::placeholder'' pseudo-element
The ::placeholder pseudo-element represents
placeholder text in an input field:
text that represents the input
and provides a hint to the user on how to fill out the form.
For example, a date-input field
might have the placeholder text “YYYY/MM/DD”
to clarify that numeric dates are to be entered in year-month-day order.
Note: There also exists a '':placeholder-shown'' pseudo-class,
which applies to (real) elements while they are showing placeholder text,
and can be used to style such elements specially.
''::placeholder'' specifically selects
a pseudo-element representing the placeholder text,
and is thus relatively limited in its abilities.
All properties that apply to the ''::first-line'' pseudo-element
also apply to the ''::placeholder'' pseudo-element.
In interactive media, placeholder text is often hidden once the user has entered input;
however this is not a requirement, and both the input value and the placeholder text may be visible simultaneously.
The exact behavior is UA-defined.
Note that in static media (such as print)
placeholder text will be present even after the user has entered input.
Issue: Authors seem to want 'text-align' on the list of supported properties.
See e.g. comments here.
Note: It's been requested that ''::placeholder'' also refer to
a placeholder which has a corresponding element in the element tree.
It's not clear how this should work, but it may be worth doing.
See Issue 2417.
The ::file-selector-button pseudo-element targets the ''<button>''
inside an ''<input>'' element with type=file
, if the UA
renders such a button.
There is no restriction on which properties apply to the
''::file-selector-button'' pseudo-element.
Overlapping Pseudo-element Interactions
Recall that
-
the contents of ''::before'' and ''::after'' are selected
exactly as if they were normal elements in the document source tree
-
the ''::first-letter'' boundaries are tightly wrapped around the first letter text,
and ''::first-letter'' is constrained to exist solely on the first formatted line.
-
the ''::first-line'' start is inserted just inside the containing block's element boundary,
and its end after the close of all content on the line
The following CSS and HTML example
illustrates how overlapping pseudo-elements interact:
Some text that ends up on two lines
The first letter of each P element will be green with a font size of ’24pt'.
The rest of the first formatted line will be blue
while the rest of the paragraph will be red.
Assuming that a line break will occur before the word "ends",
the
fictional tag sequence for this fragment might be:
S
ome text that
ends up on two lines
Additions to the CSS Object Model
{{CSSPseudoElement}} Interface
The {{CSSPseudoElement}} interface
allows pseudo-elements to be event targets.
[Exposed=Window]
interface CSSPseudoElement : EventTarget {
readonly attribute CSSOMString type;
readonly attribute Element element;
};
The type attribute
is a string representing the type of the pseudo-element.
This can be one of the following values:
"::before"
- Represents the ''::before'' pseudo-element.
"::after"
- Represents the ''::after'' pseudo-element.
"::marker"
- Represents the ''::marker'' pseudo-element.
The element attribute is the
[=originating element=] of the pseudo-element.
Note: This interface may be extended in the future
to other pseudo-element types
and/or to allow setting style information
through a {{CSSStyleDeclaration}} style
attribute.
The current functionality is limited
to that which is needed to support [[web-animations-1]].
{{pseudo()}} method of the {{Element}} interface
A new method is added to the {{Element}} interface to retrieve
pseudo-elements created by a given element for a given type:
partial interface Element {
CSSPseudoElement? pseudo(CSSOMString type);
};
The
pseudo(CSSOMString type) method
is used to retrieve the {{CSSPseudoElement}} instance
of the type matching {{type!!argument}}
associated with the element.
When it is called,
execute the following steps:
1. [=CSS/Parse=] the {{type!!argument}} argument as a <
>,
and let |type| be the result.
2. If |type| is failure,
return null.
3. Otherwise, return the {{CSSPseudoElement}} object
representing the pseudo-element
that would match the selector |type|
with [=this=] as its [=originating element=].
Return values that represent the same [=pseudo-element=]
on the same [=originating element=]
must be, insofar as observable,
always the same {{CSSPseudoElement}} object.
(The UA may drop or regenerate the object for convenience or performance
if this is not observable.)
ISSUE: The identity, lifetime, and nullness of the return value
(and potential error cases)
of the {{pseudo()}} method is still under discussion.
See Issue 3607
and Issue 3603.
Compatibility Syntax
For compatibility with existing style sheets written against CSS Level 2 [[CSS2]],
user agents must also accept the previous one-colon notation
(:before
, :after
, :first-letter
, :first-line
)
for the ''::before'', ''::after'', ''::first-letter'', and ''::first-line'' pseudo-elements.
Changes
Significant changes since the 25 February 2019 Working Draft include:
- Added ''::target-text'' pseudo-element.
(Issue 5522)
- Removed ''::inactive-selection''.
(Issue 4579)
- Added ''::file-selectors-button'' pseudo-element.
(Issue 5049)
- Adjusted rules for inheritance of properties from ''::first-line''
to handle inheritable vs non-inheritable properties differently,
specifically excluded applicability of properties that control [=writing mode=] because they affect the cascade,
and defined interaction with custom properties.
(Issue 1097)
- Added 'ruby-position' to properties allowed on ''::first-line'',
analogous with 'text-emphasis-position'.
(Issue 2998)
- Included spaces between the first letter and its surrounding punctuation in ''::first-letter''.
(Issue 5154)
- Defined interaction of ''::marker'' and ''::first-letter''.
- Made ''::before::marker'' and ''::after::marker'' valid.
(Issue 1793)
- Moved the list of properties applying to ''::marker'' to [[CSS-LISTS-3]]
(also adding 'content', 'unicode-bidi', and 'direction').
- Added ''::marker'' to types allowed for {{CSSPseudoElement}} interface.
(Issue 3763)
- Clarified return value of {{pseudo()}}.
(Issues 3603 and 3607)
- Allowed highlight overlay of inline-level boxes to extend to the line box edges.
(Issues 5395 and 4624)
- Clarified that ''currentColor'' on a [=highlight pseudo-element=]
uses the color that would be used if the text were not “highlighted”,
even if that color was provided by a pseudo-element like ''::first-line''.
(Issue 4625)
- Defined how 'text-shadow' interacts with [=highlight pseudo-elements=].
(Issue 3932)
- Improved the [[#highlight-ua-styles|default UA style sheet]] rules applying to highlight pseudo-elements.
Changes since the 7 June 2016 Working Draft include:
- Specified ''spelling-error'' and ''grammar-error'' with ''::spelling-error'' and ''::grammar-error'' in the UA stylesheet.
- Redefined value propagation between parent/child highlight pseudo-elements to use inheritance rather than cascading. See discussion in and linked from Issue 2474.
- Refined list of supported properties for highlight pseudo-elements, e.g. adding 'stroke-color'/'fill-color', removing 'outline', etc.
- Clarified how text and text decoration colors are painted for highlight pseudo-elements.
- Added the {{CSSPseudoElement/element}} attribute to the {{CSSPseudoElement}} interface.
- Changed the values of the {{CSSPseudoElement/type}} attribute on the {{CSSPseudoElement}} interface to match the corresponding pseudo-elements.
- Reduced supported types of {{CSSPseudoElement}} and removed unimplemented
style
attribute.
- Changed
window.getPseudoElements(elem,type)
to Element.pseudo(type)
.
- Miscellaneous minor clarifications and fixes.
Acknowledgements
The editors would like to specifically thank the following individuals for their
contributions to this specification:
Tab Atkins,
David Baron,
Oriol Brufau,
Razvan Caliman,
Chris Coyier,
Anders Grimsrud,
Vincent Hardy,
François Remy.