<pre class='metadata'>
Title: CSS Pseudo-Elements Module Level 4
Shortname: css-pseudo
Level: 4
Status: ED
Work Status: Exploring
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/2016/WD-css-pseudo-4-20160607/
Previous Version: https://www.w3.org/TR/2015/WD-css-pseudo-4-20150115/
!Issues List: <a href="https://drafts.csswg.org/css-pseudo/#issues-index">Tracked in Editor's Draft</a>
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
</pre>
<pre class="link-defaults">
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
</pre>

<h2 id="intro">Introduction</h2>

  <em>This section is informative.</em>

  <a>Pseudo-elements</a> 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
  <em>after</em> text wrapping,
  allowing just that line to be styled differently
  from the rest of the paragraph.

  Each pseudo-element is associated with an <a>originating element</a>
  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 <a>selectors</a>,
  see [[!SELECTORS-4]].

<h2 id="typographic-pseudos">
Typographic Pseudo-elements</h2>

<h3 id="first-line-pseudo">
The ::first-line pseudo-element</h3>

  The <dfn>::first-line</dfn> pseudo-element describes the contents of
  the <a>first formatted line</a> of its <a>originating element</a>.

  <div class="example">
    The rule below means
    “change the letters of the first line of every <code>p</code> element to uppercase”:

    <pre>p::first-line { text-transform: uppercase }</pre>

    The selector ''p::first-line''
    does not match any real document element.
    It does match a pseudo-element that conforming user agents
    will insert at the beginning of every <code>p</code> element.
  </div>

  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.

  <div class="example">
    For example, given an ordinary HTML [[HTML5]] paragraph such as:

    <pre>
      &lt;P&gt;This is a somewhat long HTML
      paragraph that will be broken into several
      lines. The first line will be identified
      by a fictional tag sequence. The other lines
      will be treated as ordinary lines in the
      paragraph.&lt;/P&gt;
    </pre>

    The lines might be broken as follows:

    <pre>
      THIS IS A SOMEWHAT LONG HTML PARAGRAPH THAT
      will be broken into several lines. The first
      line will be identified by a fictional tag
      sequence. The other lines will be treated as
      ordinary lines in the paragraph.
    </pre>

    This paragraph might be “rewritten” by user agents
    to include a <dfn>fictional tag sequence</dfn> to represent ''::first-line''.
    This <a>fictional tag sequence</a> helps to show how properties are inherited.

    <pre>
      &lt;P&gt;<b>&lt;P::first-line&gt;</b> This is a somewhat long HTML
      paragraph that <b>&lt;/P::first-line&gt;</b> will be broken into several
      lines. The first line will be identified
      by a fictional tag sequence. The other lines
      will be treated as ordinary lines in the
      paragraph.&lt;/P&gt;
    </pre>
  </div>

  If a pseudo-element breaks up a real element,
  the desired effect can often be described by a <a>fictional tag sequence</a>
  that closes and then re-opens the element.

  <div class="example">
    Thus, if we mark up the previous paragraph with a <code>span</code>
    element encompassing the first sentence:

    <pre>
      &lt;P&gt;<b>&lt;SPAN class="test"&gt;</b> This is a somewhat long HTML
      paragraph that will be broken into several
      lines.<b>&lt;/SPAN&gt;</b> The first line will be identified
      by a fictional tag sequence. The other lines
      will be treated as ordinary lines in the
      paragraph.&lt;/P&gt;
    </pre>

    the user agent could simulate start and end tags for <code>span</code>
    when inserting the <a>fictional tag sequence</a> for ''::first-line''
    to get the correct inheritance behavior.

    <pre>
      &lt;P&gt;&lt;P::first-line&gt;<b>&lt;SPAN class="test"&gt;</b> This is a somewhat long HTML
      paragraph that will <b>&lt;/SPAN&gt;</b>&lt;/P::first-line&gt;<b>&lt;SPAN class="test"&gt;</b> be broken into several
      lines.<b>&lt;/SPAN&gt;</b> The first line will be identified
      by a fictional tag sequence. The other lines
      will be treated as ordinary lines in the
      paragraph.&lt;/P&gt;
    </pre>
  </div>

<h4 id="first-text-line">
Finding the First Formatted Line</h4>

  In CSS, the ''::first-line'' pseudo-element
  can only have an effect when attached to a <a>block container</a>.
  The <dfn export>first formatted line</dfn> of an element
  must occur inside a block-level descendant in the same flow
  (i.e., a block-level descendant that is not out-of-flow due to floating or positioning).

  <div class="example">
    For example, the first line of the <code>DIV</code> in <code>&lt;DIV&gt;&lt;P&gt;This line...&lt;/P&gt;&lt;/DIV&gt;</code>
    is the first line of the <code>P</code>
    (assuming that both <code>P</code> and <code>DIV</code> are blocks).
  </div>

  The first line of a table-cell or inline-block
  cannot be the first formatted line of an ancestor element.
  Thus, in <code>&lt;DIV&gt;&lt;P STYLE="display: inline-block"&gt;Hello&lt;BR&gt;Goodbye&lt;/P&gt; etcetera&lt;/DIV&gt;</code>
  the first formatted line of the <code>DIV</code> is not the line "Hello".

  Note: Note that the first line of the <code>p</code> in this fragment:
  <code>&lt;p&gt;&lt;br&gt;First...</code>
  doesn't contain any letters (assuming the default style for <code>br</code>).
  The word "First" is not on the first formatted line.

  A user agent must act as if the fictional start tags of a ''::first-line'' pseudo-element
  were nested just inside the innermost enclosing block-level element.

  <div class="example">
    For example, the <a>fictional tag sequence</a> for
    <pre>
      &lt;DIV&gt;
        &lt;P&gt;First paragraph&lt;/P&gt;
        &lt;P&gt;Second paragraph&lt;/P&gt;
      &lt;/DIV&gt;
    </pre>
    is
    <pre>
      &lt;DIV&gt;
        &lt;P&gt;&lt;DIV::first-line&gt;&lt;P::first-line&gt;First paragraph&lt;/P::first-line&gt;&lt;/DIV::first-line&gt;&lt;/P&gt;
        &lt;P&gt;&lt;P::first-line&gt;Second paragraph&lt;/P::first-line&gt;&lt;/P&gt;
      &lt;/DIV&gt;
    </pre>
  </div>

<h4 id="first-line-styling">
Styling the First Line Pseudo-element</h4>

  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:
  <ul>
    <li>all font properties (see [[CSS-FONTS-3]])
    <li>the 'color' and 'opacity' properties (see [[CSS-COLOR-3]])
    <li>all background properties (see [[CSS-BACKGROUNDS-3]])
    <li>any typesetting properties that apply to inline elements (see [[CSS-TEXT-3]])
    <li>all text decoration properties (see [[CSS-TEXT-DECOR-3]])
    <li>the 'ruby-position' property (see [[CSS-RUBY-1]])
    <li>any inline layout properties that apply to inline elements (see [[CSS-INLINE-3]])
    <li>any other properties defined to apply to ''::first-line''
        by their respective specifications
  </ul>
  User agents may apply other properties as well.

<h4 id="first-line-inheritance">
Inheritance and the ''::first-line'' Pseudo-element</h4>

  During CSS inheritance, the portion of a child element that occurs on the first line
  only inherits properties applicable to the ''::first-line'' pseudo-element
  from the ''::first-line'' pseudo-element.
  For all other properties inheritance is
  from the non-pseudo-element parent of the first line pseudo element.
  (The portion of a child element that does not occur on the first line
  always inherits from the parent of that child.)

<h3 id="first-letter-pseudo">
The ::first-letter pseudo-element</h3>

  The <dfn>::first-letter</dfn> pseudo-element represents
  the first <a>typographic letter unit</a> [[!CSS-TEXT-3]]
  on the <a>first formatted line</a> of its <a>originating element</a>,
  if it is not preceded by any other content
  (such as images or inline tables) on its line.
  The ''::first-letter'' pseudo-element can be used
  to create “initial caps” and “drop caps”,
  which are common typographic effects.

  <div class="example">
    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]]:

    <pre>h2 + p::first-letter { initial-letter: 2; }
    </pre>
  </div>

  Punctuation (i.e, characters that belong to the Punctuation (<code>P*</code>) <a>Unicode general category</a> [[!UAX44]])
  that precedes or follows the first <a>typographic letter unit</a> must also be included
  in the ''::first-letter'' pseudo-element.

  <div class="figure">
      <img alt="Quotes that precede the first letter should be included." src="images/first-letter2.gif">
  </div>

  As explained in [[!CSS-TEXT-3]],
  a <a>typographic letter unit</a> 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]]
  The UA should tailor its definition of <a>typographic letter unit</a>
  to reflect the first-letter traditions of the ''::first-letter'' pseudo-element’s <em>containing block</em>’s content language.

  Note: Note that the first <a>typographic letter unit</a> may in fact
  be a digit, e.g., the “6” in “67 million dollars is a lot of money.”

  If the characters that would form the ''::first-letter''
  are not in the same element, such as <code>‘T</code> in <code>&lt;p&gt;‘&lt;em&gt;T...</code>,
  the user agent may create a ''::first-letter'' pseudo-element
  from one of the elements, both elements, or simply not create a pseudo-element.</code>
  Additionally, if the first letter(s) of the block
  are not at the start of the line (for example due to bidirectional reordering),
  then the user agent need not create the pseudo-element(s).

  The ''::first-letter'' pseudo-element is contained within any ''::first-line''
  pseudo-elements, and thus inherits from ''::first-line''.

<h4 id="first-letter-application">
Finding the First Letter</h4>

  The first letter must occur on the <a>first formatted line</a>.
  For example, in this HTML fragment: <code>&lt;p&gt;&lt;br&gt;First...</code>
  the first line doesn't contain any letters
  and ''::first-letter'' doesn't match anything.
  In particular, it does not match the “F” of “First”.

  In CSS, the ''::first-letter'' pseudo-element
  only applies to block containers.
  <span class="note">A future version of this specification
  may allow this pseudo-element to apply to more display types.</span>
  The ''::first-letter'' pseudo-element can be used
  with all such elements that contain text,
  or that have a descendant in the same flow that contains text.
  A user agent should act as if the fictional start tag
  of the ::first-letter pseudo-element
  is just before the first text of the element,
  even if that first text is in a descendant.

  <div class="example">
    Example:
    The <a>fictional tag sequence</a> for this HTML fragment:
    <pre>
      &lt;div&gt;
      &lt;p&gt;The first text.
    </pre>
    is:
    <pre>
      &lt;div&gt;
      &lt;p&gt;&lt;div::first-letter&gt;&lt;p::first-letter&gt;T&lt;/...&gt;&lt;/...&gt;he first text.
    </pre>
  </div>

  In CSS the first letter of a table-cell or inline-block
  cannot be the first letter of an ancestor element.
  Thus, in <code>&lt;DIV&gt;&lt;P STYLE="display: inline-block"&gt;Hello&lt;BR&gt;Goodbye&lt;/P&gt; etcetera&lt;/DIV&gt;</code>
  the first letter of the <code>DIV</code> is not the letter "H".
  In fact, the <code>DIV</code> doesn't have a first letter.
  If an element is a list item (''display: list-item''),
  the ''::first-letter'' applies
  to the first letter in the principal box <em>after</em> the marker.
  User-Agents may ignore ''::first-letter''
  on list items with ''list-style-position: inside''.
  If an element has ''::before'' or ''::after'' content,
  the ''::first-letter'' applies to the first letter of the
  element <em>including</em> that content.

  <div class="example">
    Example:
    After the rule ''p::before {content: "Note: "}'', the
    selector ''p::first-letter'' matches the "N" of "Note".
  </div>

<h4 id="first-letter-styling">
Styling the ''::first-letter'' Pseudo-element</h4>

  In CSS a ::first-letter pseudo-element is similar to an inline-level element.
  The following properties that apply to ''::first-letter'' pseudo-elements:

  <ul>
    <li>all font properties (see [[CSS-FONTS-3]])
    <li>the 'color' and 'opacity' properties (see [[CSS-COLOR-3]])
    <li>all background properties (see [[CSS-BACKGROUNDS-3]])
    <li>any typesetting properties that apply to inline elements (see [[CSS-TEXT-3]])
    <li>all text decoration properties (see [[CSS-TEXT-DECOR-3]])
    <li>any inline layout properties that apply to inline elements (see [[CSS-INLINE-3]])
    <li>margin and padding properties (see [[CSS2]])
    <li>border properties and 'box-shadow' (see [[CSS-BACKGROUNDS-3]])
    <li>any other properties defined to apply to ''::first-letter''
        by their respective specifications
  </ul>

  User agents may apply other properties as well.

  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,
  approximate font sizes,
  or to take the glyph outline into account when formatting.
  This possibility has been intentionally removed,
  as it proved to be a poor solution for the intended use case (Drop Caps),
  yet caused interoperability problems.

  <div class="example">
    Example:
    This CSS and HTML example shows a possible rendering of an initial cap.
    Note that the fictional start tag of the first letter
    is inside the <span>span</span>,
    and thus the font weight of the first letter is normal,
    not bold as the <span>span</span>:

    <pre>
      p { line-height: 1.1 }
      p::first-letter { font-size: 3em; font-weight: normal }
      span { font-weight: bold }
      ...
      &lt;p&gt;&lt;span&gt;Het hemelsche&lt;/span&gt; gerecht heeft zich ten lange lesten&lt;br&gt;
      Erbarremt over my en mijn benaeuwde vesten&lt;br&gt;
      En arme burgery, en op mijn volcx gebed&lt;br&gt;
      En dagelix geschrey de bange stad ontzet.
    </pre>

    <div class="figure">
        <img alt="Image illustrating the ::first-letter pseudo-element" src="images/initial-cap.png">
    </div>
  </div>

  <div class="example">
    The following CSS will make a drop cap initial letter span about two lines:
    <pre>
      &lt;!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"&gt;
      &lt;HTML&gt;
       &lt;HEAD&gt;
        &lt;TITLE&gt;Drop cap initial letter&lt;/TITLE&gt;
        &lt;STYLE type="text/css"&gt;
         P               { font-size: 12pt; line-height: 1.2 }
         P::first-letter { font-size: 200%; font-weight: bold; float: left }
         SPAN            { text-transform: uppercase }
        &lt;/STYLE&gt;
       &lt;/HEAD&gt;
       &lt;BODY&gt;
        &lt;P&gt;&lt;SPAN&gt;The first&lt;/SPAN&gt; few words of an article
          in The Economist.&lt;/P&gt;
       &lt;/BODY&gt;
      &lt;/HTML&gt;
    </pre>

    This example might be formatted as follows:

    <div class="figure">
        <img alt="Image illustrating the combined effect of the ::first-letter
   and ::first-line pseudo-elements" src="images/first-letter.gif">
    </div>

    The <a>fictional tag sequence</a> is:

    <pre>
      &lt;P&gt;
        &lt;SPAN&gt;
          &lt;P::first-letter&gt;
            T
          &lt;/P::first-letter&gt;he first
        &lt;/SPAN&gt;
        few words of an article in the Economist.
      &lt;/P&gt;
    </pre>

    Note that the ''::first-letter'' pseudo-element tags abut the
    content (i.e., the initial character), while the ::first-line
    pseudo-element start tag is inserted right after the start tag of the
    block element.
  </div>

<h2 id="highlight-pseudos">
Highlight Pseudo-elements</h2>

<h3 id="highlight-selectors">
Selecting Highlighted Content: the ''::selection'',  ''::inactive-selection'', ''::spelling-error'', and ''::grammar-error'' pseudo-elements</h3>

  The <dfn export lt="highlight pseudo-element">highlight pseudo-elements</dfn>
  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 <a>highlight pseudo-elements</a> are defined:

  <dl export>
    <dt><dfn>::selection</dfn>
    <dt><dfn>::inactive-selection</dfn>
    <dd>
      The ''::selection'' and ''::inactive-selection'' pseudo-elements represent
      the portion of a document that has been selected
      as the target or object of some possible future user-agent operation(s).
      They apply, for example, to selected text within an editable text field,
      which would be copied by a copy operation or replaced by a paste operation.

      ''::selection'' applies to active selections,
      whereas ''::inactive-selection'' applies to inactive selections
      (e.g. when the document window is inactive and therefore not receiving events).

    <dt><dfn>::spelling-error</dfn>
    <dd>
      The ''::spelling-error'' pseudo-element represents
      a portion of text that has been flagged by the user agent as misspelled.

    <dt><dfn>::grammar-error</dfn>
    <dd>
      The ''::grammar-error'' pseudo-element represents
      a portion of text that has been flagged by the user agent as grammatically incorrect.
  </dl>

  The <a>highlight pseudo-elements</a>
  do not necessarily fit into the element tree,
  and can arbitrarily cross element boundaries without honoring its nesting structure.

    The following addition is made to the default UA stylesheet:
	<pre><code class="lang-css">
		/* 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; }
		/* Ensure contrast on text with shadows */
		:root::selection      { text-shadow: none; }
	</code></pre>

  Note: A future level of CSS may introduce ways to create
  custom highlight pseudo-elements.

<h3 id="highlight-styling">
Styling Highlights</h3>

  The <a>highlight pseudo-elements</a> can only be styled
  by a limited set of properties that do not affect layout.
  <!-- ALSO: are not dependent on the exact bounds of the selection painting area,
  and can be applied performantly in a highly dynamic environment-->
  The following properties apply to the <a>highlight pseudo-elements</a>:

  <ul>
    <li>'color'
    <li>'background-color'
    <li>'cursor'
    <li>'caret-color'
    <li>'text-decoration' and its associated properties
    <li>'text-shadow'
    <li>'stroke-color', 'fill-color', and 'stroke-width'
  </ul>

  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 <a>originating element</a> and its ancestors and descendants.

<!-- Add this back if for some reason someone wants to implement 'outline'?
  The outline, if supported, must be drawn
  around the union of the active portions of the <a>highlight overlay</a>
  precisely along the boundaries of those portions
  and not between congruent parts.
  The UA may use the outline styling of the nearest common ancestor
  of any continuous or discontinuous range
  rather than piecing together varying styles of outline
  into a single shape.
-->

  Note: Historically (and at the time of writing)
  only 'color' and 'background-color' have been interoperably supported.

<h3 id=highlight-bounds>
Area of a Highlight</h3>

  <p>For each type of highlighting (see [[#highlight-selectors]])
  there exists a single <dfn>highlight overlay</dfn> for the entire document,
  the active portions of which are represented
  by the corresponding <a>highlight pseudo-element</a>.
  Each box owns the piece of the overlay corresponding to any text or replaced content
  directly contained by the box.

  <ul>
    <li>
      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.
    <li>
      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.
    <li>
      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.
  </ul>

  Issue: See
  <a href="http://lists.w3.org/Archives/Public/www-style/2008Nov/0022.html">F2F minutes</a>,
  <a href="http://lists.w3.org/Archives/Public/www-style/2008Oct/0268.html">dbaron's message</a>,
  <a href="http://lists.w3.org/Archives/Public/www-style/2010May/0247.html">Daniel's thread</a>,
  <a href="http://lists.w3.org/Archives/Public/www-style/2010May/0261.html">Gecko notes</a>,
  <a href="http://lists.w3.org/Archives/Public/www-style/2010May/0366.html">Opera notes</a>,
  <a href="http://lists.w3.org/Archives/Public/www-style/2010May/0280.html">Webkit notes</a>

  Issue: Not sure if this is the correct way of describing the way things work.

<h3 id=highlight-cascade>
Cascading and Per-Element Highlight Styles</h3>

  Each element draws its own active portions of the <a>highlight overlays</a>,
  which receives the styles specified by
  the corresponding <a>highlight pseudo-element</a> styles
  for which that element is the <a>originating element</a>.
  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 <a>highlight pseudo-element</a>
  of its <a>originating element</a>'s parent element
  (regardless of whether that property is an <a>inherited property</a>).

  <div class="example">
    For example, if the following rules were applied:
    <pre>
      p::selection      { color: yellow; background: green; }
      p > em::selection { color: orange; }
      em::selection     { color:    red; }
    </pre>
    to the following markup:
    <pre>
      &lt;p>Highlight this &lt;em>and this&lt;/em>.&lt;/p>
    </pre>
    The selection highlight would be green throughout,
    with yellow text outside the <code>&lt;em></code> element
    and orange text inside it.
  </div>

  <p class="advisement">
  Authors wanting multiple selections styles should use
  <strong>'':root::selection''</strong>
  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.

  <div class="example">
    For example, if an author specified
    <pre>
      ::selection          { background: blue; }
      p.warning::selection { background:  red; }
    </pre>
    and the document included
    <pre>
      &lt;p class="warning">Some &lt;strong>very important information&lt;/strong>&lt;/p>
    </pre>
    The highlight would be blue over “very important information”
    because the <code>&lt;strong></code> 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 <code>p.warning</code>, blue elsewhere) are
    <pre>
      :root::selection     { background: blue; }
      p.warning::selection { background:  red; }
    </pre>
  </div>

  The UA should use the OS-default highlight colors for ''::selection''
  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 <a>UA style sheet</a>)
  to represent the OS default selection colors.
  However it has been interoperably implemented in browsers
  and is thus probably a Web-compatibility requirement.

<h3 id="highlight-painting">
Painting the Highlight</h3>

  Each <a>highlight pseudo-element</a> draws its background
  over the corresponding portion of the <a>highlight overlay</a>,
  painting it
  immediately below any positioned descendants
  (i.e. just before step 8 in <a href="https://www.w3.org/TR/CSS21/zindex.html">CSS2.1&sect;E.2</a>).
  <!-- Its outline, if any, is painted immediately over its background. -->
  The ''::selection'' overlay is drawn
  over the ''::spelling-error'' overlay
  which is drawn over the ''::grammar-error'' overlay.

  A <a>highlight pseudo-element</a>
  also suppresses the drawing of any selected text
  (and any text decorations applied to that text).
  Instead the topmost active <a>highlight overlay</a>
  redraws that text
  (and its decorations)
  over the <a>highlight overlay</a> backgrounds
  <!-- (and outlines, if any) -->
  using its own 'color',
  with ''currentColor'' on its 'color' property representing the 'color'
  of the next <a>highlight pseudo-element</a> layer below,
  falling back finally to that of the <a>originating element</a>
  (the colors that would otherwise be used).
  Any text decorations introduced by each <a>highlight pseudo-element</a>
  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 <a href="https://www.w3.org/TR/css-text-decor/#line-decoration">line decorations</a>
  and <a href="https://www.w3.org/TR/css-text-decor/#emphasis-marks">emphasis marks</a>)
  are thus drawn in the pseudo-element’s own 'color'
  when that is not ''currentColor'',
  regardless of their original color or fill specifications.

  Issue: What should happen with text shadows?
  Drawing them in their original color is disconcerting if that color is not a shade of gray.
  Maybe if the overlay has a background, suppress any text shadows below it?

  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.

<h3 id="highlight-security">
Security and Privacy Considerations</h3>

  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.

<h2 id="treelike">
Tree-Abiding Pseudo-elements</h2>

  <dfn export lt="tree-abiding|tree-abiding pseudo-element">Tree-abiding pseudo-elements</dfn> always fit within the box tree.
  They <a lt="inheritance">inherit</a> any inheritable properties from their <a>originating element</a>;
  non-inheritable properties take their <a>initial values</a> as usual.
  [[CSS-CASCADE-4]]

<h3 id="generated-content">
Generated Content Pseudo-elements: ''::before'' and ''::after''</h3>

  When their computed 'content' value is not ''content/none'',
  these pseudo-elements generate boxes
  as if they were immediate children of their <a>originating element</a>,
  and can be styled exactly like any normal document-sourced element in the document tree.

  <dl>
    <dt><dfn>::before</dfn>
    <dd>Represents a styleable child pseudo-element
    immediately before the <a>originating element</a>'s actual content.

    <dt><dfn>::after</dfn>
    <dd>Represents a styleable child pseudo-element
    immediately after the <a>originating element</a>'s actual content.
  </dl>

  <div class="example">
    For example, the following rule inserts the string “Note: ”
    before the content of every <code>&lt;p></code> element
    whose <code>class</code> attribute has the value <code>note</code>:

    <pre>p.note::before { content: "Note: " }</pre>

    Since the initial value of 'display' is ''display/inline'',
    this will generate an inline box.
    Like other inline children of <code>&lt;p></code>,
    it will participate in <code>&lt;p></code>’s inline formatting context,
    potentially sharing a line with other content.
  </div>

  As with the content of regular elements,
  the generated content of ''::before'' and '':after'' pseudo-elements
  may be included in any ''::first-line'' and ''::first-letter'' pseudo-elements
  applied to its <a>originating element</a>.

<h3 id="marker-pseudo">
List Markers: the ''::marker'' pseudo-element</h3>

  The <dfn>::marker</dfn> pseudo-element represents
  the automatically generated [=marker box=] of a [=list item=].
  (See [[CSS-DISPLAY-3]] and [[CSS-LISTS-3]].)

  The following CSS properties apply to a ''::marker'' pseudo-element:
  <ul>
    <li>all font properties (see [[CSS-FONTS-3]])
    <li>the 'white-space' property (see [[CSS-TEXT-3]])
    <li>the 'color' property (see [[CSS-COLOR-3]])
    <li>the 'text-combine-upright', 'unicode-bidi', and 'direction' properties (see [[CSS-WRITING-MODES-3]])
    <li>the 'content' property (see [[CSS-LISTS-3]])
  </ul>

  <p class="note">
    It is expected that future specifications will extend this list of properties;
    however at the moment marker box layout is not fully defined,
    so only these properties are allowed.

  UAs must add the following rule to their default style sheet:
  <pre>
    ::marker, ::before::marker, ::after::marker {
      unicode-bidi: isolate;
      font-variant-numeric: tabular-nums;
    }
  </pre>

  Note: Although the ''::marker'' pseudo-element can represent
  the [=marker box=] of a ''::before'' or ''::after'' pseudo-element,
  the [=compound selector=] ''::marker'',
  which expands to ''*::marker'' [[SELECTORS-4]],
  will not select these markers--
  an [=originating element=] that is a [=pseudo-element=]
  needs to be explicitly specified in the [=selector=],
  e.g. ''::before::marker''.

<h3 id="placeholder-pseudo">
Placeholder Input: the ''::placeholder'' pseudo-element</h3>

  The <dfn>::placeholder</dfn> 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-<em>class</em>,
  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 <em>text</em>,
  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. <a href="http://css-tricks.com/almanac/selectors/p/placeholder/">comments here</a>.

  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 <a href="https://github.com/w3c/csswg-drafts/issues/2517">Issue 2417</a>.


<h2 id="interactions">
Overlapping Pseudo-element Interactions</h2>

  <div class="example" id="overlapping-example">
    Recall that
    <ul>
      <li>
        the contents of ''::before'' and ''::after'' are selected
        exactly as if they were normal elements in the document source tree
      <li>
        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.
      <li>
        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
    </ul>

    The following CSS and HTML example
    illustrates how overlapping pseudo-elements interact:

    <xmp class="lang-html">
      <style>
      p { color: red; font-size: 12pt }
      p::first-letter { color: green; font-size: 200% }
      p::first-line { color: blue }
      </style>

      <p>Some text that ends up on two lines</p>
    </xmp>

    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 <a>fictional tag sequence</a> for this fragment might be:
    <xmp class="lang-html">
      <p>
        <p::first-line>
          <p::first-letter>
            S
          </p::first-letter>
          ome text that
        </p::first-line>
        ends up on two lines
      </p>
    </xmp>
  </div>

<h2 id="cssom">
Additions to the CSS Object Model</h2>

<h3 id="CSSPseudoElement-interface">
{{CSSPseudoElement}} Interface</h3>

  The {{CSSPseudoElement}} interface
  allows pseudo-elements to be event targets.

  <pre class="idl">
    [Exposed=Window]
    interface CSSPseudoElement : EventTarget {
        readonly attribute CSSOMString type;
        readonly attribute Element element;
    };
  </pre>

  The <dfn attribute for=CSSPseudoElement>type</dfn> attribute
  is a string representing the type of the pseudo-element.
  This can be one of the following values:

  <dl>
    <dt><code>"::before"</code>
    <dd>Represents the ''::before'' pseudo-element.
    <dt><code>"::after"</code>
    <dd>Represents the ''::after'' pseudo-element.
    <dt><code>"::marker"</code>
    <dd>Represents the ''::marker'' pseudo-element.
  </dl>

  The <dfn attribute for=CSSPseudoElement>element</dfn> 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}} <code>style</code> attribute.
  The current functionality is limited
  to that which is needed to support [[web-animations-1]].

<h3 id="window-interface">
{{pseudo()}} method of the {{Element}} interface</h3>

  A new method is added to the {{Element}} interface to retrieve
  pseudo-elements created by a given element for a given type:
  <pre class="idl">
    partial interface Element {
      CSSPseudoElement? pseudo(CSSOMString type);
    };
  </pre>

  <div algorithm>
    The <dfn method for=Element title="pseudo(type)" id="dom-element-pseudo">pseudo(CSSOMString type)</dfn> 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 <<pseudo-element-selector>>,
      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=].
  </div>

  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 <a href="https://github.com/w3c/csswg-drafts/issues/3607">Issue 3607</a>
  and <a href="https://github.com/w3c/csswg-drafts/issues/3603">Issue 3603</a>.

<h2 id="css2-compat">
Compatibilitiy Syntax</h2>

  <p>For compatibility with existing style sheets written against CSS Level 2 [[CSS2]],
  user agents must also accept the previous one-colon notation
  (<code>:before</code>, <code>:after</code>, <code>:first-letter</code>, <code>:first-line</code>)
  for the ''::before'', ''::after'', ''::first-letter'', and ''::first-line'' pseudo-elements.

<h2 class="no-num" id="changes">
Changes</h2>

  Changes since the <a href="https://www.w3.org/TR/2019/WD-css-pseudo-4-20190225/">25 February 2019 Working Draft</a> include:

  <ul>
    <li>Added ''::marker'' to types allowed for {{CSSPseudoElement}} interface.
    <li>Clarified return value of {{pseudo()}}.
    <li>Added 'content', 'unicode-bidi', and 'direction' to properties allowed on ''::marker''.
    <li>Added 'ruby-position' to properties allowed on ''::first-line'',
    analogous with 'text-emphasis-position'.
    (<a href="https://github.com/w3c/csswg-drafts/issues/2998">Issue 2998</a>)
  </ul>

  Changes since the <a href="https://www.w3.org/TR/2016/WD-css-pseudo-4-20160607/">7 June 2016 Working Draft</a> include:

  <ul>
    <li>Specified ''spelling-error'' and ''grammar-error'' with ''::spelling-error'' and ''::grammar-error'' in the UA stylesheet.
    <li>Redefined value propagation between parent/child highlight pseudo-elements to use inheritance rather than cascading. See discussion in and linked from <a href="https://github.com/w3c/csswg-drafts/issues/2474">Issue 2474</a>.
    <li>Refined list of supported properties for highlight pseudo-elements, e.g. adding 'stroke-color'/'fill-color', removing 'outline', etc.
    <li>Clarified how text and text decoration colors are painted for highlight pseudo-elements.
    <li>Added the {{CSSPseudoElement/element}} attribute to the {{CSSPseudoElement}} interface.
    <li>Changed the values of the {{CSSPseudoElement/type}} attribute on the {{CSSPseudoElement}} interface to match the corresponding pseudo-elements.
    <li>Reduced supported types of {{CSSPseudoElement}} and removed unimplemented <code>style</code> attribute.
    <li>Changed <code>window.getPseudoElements(elem,type)</code> to <code>Element.pseudo(type)</code>.
    <li>Miscellaneous minor clarifications and fixes.
  </ul>

<h2 class="no-num" id="acknowledgements">
Acknowledgements</h2>

  The editors would like to thank the following individuals for their
  contributions, either during the conception of the specification or during
  its development and specification review process:
  Tab Atkins,
  David Baron,
  Razvan Caliman,
  Chris Coyier,
  Anders Grimsrud,
  Vincent Hardy.
