Estelle Weyl | @estellevw | Github | Press key to advance.

Welcome back!

Go Back ➹

Pseudo Elements

  • Pseudo elements
  • Generated Content
    with ::before and ::after
  • ::first-letter & ::first-line
  • ::selection
  • Other pseudo-elements
  • Browser specific pseudo-elements

estelle.github.io/CSS/selectors/pseudo.html

Pseudo elements

Pseudo elements

::after (:after)
::before (:before)
::first-letter (:first-letter)
::first-line (:first-line)
::selection
::inactive-selection
::spelling-error 
::grammar-error
::marker 
::placeholder
::backdrop
::cue  
::part()
::slotted()

Pseudo elements

Pseudo-classes select elements that already exist.
Pseudo-elements create "faux" elements you can style.

Original pseudo-elements

::after (:after)
::before (:before)
::first-letter (:first-letter)
::first-line (:first-line)

The double colon notation was introduced to “establish a discrimination between pseudo-classes and pseudo-elements.”

For compatibility, browsers accept the one-colon notation for pseudo-elements introduced in CSS levels 1 and 2, but not for newer pseudo-elements

::selection
::inactive-selection
::spelling-error 
::grammar-error
::marker 
::placeholder
::backdrop
::cue  
::part()
::slotted()

::first-letter

You can even pretty drop-caps with :first-letter, but make sure to apply it to p:first-of-type so your site not too ugly. Code for drop cap.

p:first-of-type::first-letter {
	position: relative;
	top: 8px;
	float: left;
	font-size: 3em;
	line-height: 1;
	color: hsl(205, 87%, 50%);
	padding: 0 4px 2px 0;
	font-weight: bold;
}
Drop Cap

::first-line

You can ident the first line or otherwise make it stand out. How about making the first line blue and bold? What happens if you change the font size?

There is the text-indent property, so maybe it doesn't make sense to use for indentation. Nor does it make sense to do it for every paragraph! Code for drop cap.

p:first-of-type::first-line {
  color: blue;
  font-size: 120%;
  font-weight: bold;
}
First Line

Generated Content
with ::before and ::after

Just an intro. We'll deep dive in the next section

::before and ::after

<p>the content</p>

the content

<p>
    <before>before content - </before>
        the content
    <after> - after content</after>
</p>
Try it out

Generated Content: UI selectors

UI Selectors

Test them out for yourselves

Open in new page

Newer pseudo-elements

::after (:after)
::before (:before)
::first-letter (:first-letter)
::first-line (:first-line)
::selection
::inactive-selection
::spelling-error 
::grammar-error
::marker 
::placeholder
::backdrop
::cue  
::part()
::slotted()

::selection


Matches the part of a document highlighted by user.

limited stylability: color, background-color, cursor, caret-color, outline, text-decoration, text-emphasis-color, and text-shadow can be styled. background-image can not.

Disabling ::selection

For mobile but impacts desktop too:

.noSelection { /* pre */
  -webkit-tap-highlight-color: #bada55;
  -webkit-user-select: none;
  -webkit-touch-callout: none;
}

::inactive-selection

Similar to ::selection, but ::selection applies to active selections. ::inactive-selection applies to inactive selections

Example: selected area when the document window is inactive and therefore not receiving events.

::spelling-error

The portions of text that have been flagged by the user agent as misspelled.

::grammar-error

The portions of text that have been flagged by the user agent as grammatically incorrect.

::spelling-error and ::grammar-error

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 their address book!) UAs that implement ::spelling-error and ::grammar-error must prevent pages from being able to read the styling of such highlighted segments.

limited stylability:

  • color
  • background-color
  • cursor
  • caret-color
  • outline
  • text-decoration
  • text-emphasis-color
  • text-shadow

background-image can not.

Introduced in spec June 2016

::marker

The bullet, or marker, of a list item.

Currently, only limited CSS properties apply:

  • color and content
  • all font properties
  • some writing mode properties: text-combine-upright, unicode-bidi, and direction properties

::placeholder

The placeholder text in an input field

More pseudo-elements

::after (:after)
::before (:before)
::first-letter (:first-letter)
::first-line (:first-line)
::selection
::inactive-selection
::spelling-error 
::grammar-error
::marker 
::placeholder
::backdrop
::cue  
::part()
::slotted()

::backdrop

Backdrop is displayed when dialog is opened with dialog.showModal()

dialog::backdrop {
  background: rgba(255,0,0,.25);
}
video::backdrop {
  background-color: #448;
}

Fullscreen API

::cue

Part of the VTT - video text tracks Specification

WebVTT Node Objects that are in the past or future

:future
:past

Matches WebVTT Internal Node Objects

::cue
::cue(s1)

Matches any list of WebVTT region objects

::cue-region
::cue-region()

::cue / ::cue(s1)

Style captions and other cues in media with text tracks

Limited stylability:

  • color
  • opacity
  • visibility
  • text-decoration
  • text-shadow
  • background /* applied to the WebVTT cue background box */
  • outline
  • font
  • line-height
  • white-space
  • text-combine-upright
  • ruby-position

::part()

Matches element within a shadow tree that has a matching part attribute.

custom-element-name::part(partvalue) {
  /* Styles for node that has part="partvalue" set */
}
<custom-element-name>
  <div part="partvalue">
    Foo Bar
  </div>
</custom-element-name>

CSS shadow parts

::slotted()

Element placed into slot in an HTML template

<x-bar>
   <div id="one" slot="foo" class="foo">...</div>   
   <div id="two" slot="foo">...</div>   
   <div id="three" class="foo">
     <div id="four" slot="foo">...</div>
   </div>
</x-bar>
::slotted(*) { /* #one and #two only. 
    #three has no slot attribute
    #four is not a direct children of shadow host so can't have a slot */
}
::slotted(.foo) { /* matches #one. */ }

CSS Scoping

DevTools

You can inspect pseudo-elements like::first-letter, ::first-line, ::before, ::after, ::cue, ::marker, and ::backdrop, and browser specific pseudo-elements like::-moz-progress-bar in DevTools

  • pseudo elements appear in the element inspector
  • styles appear in the pseudo-element pane in style rules

Browser specific pseudo-elements

IE Specific

::-ms-browse
::-ms-value
::-ms-check
::-ms-clear
::-ms-expand
::-ms-fill
::-ms-fill-lower
::-ms-fill-upper
::-ms-reveal
::-ms-thumb
::-ms-ticks-after
::-ms-ticks-before
::-ms-tooltip
::-ms-track

Mozilla specific

::-moz-anonymous-block
::-moz-anonymous-positioned-block
::-moz-canvas
::-moz-color-swatch
::-moz-cell-content
::-moz-focus-inner
::-moz-focus-outer
::-moz-inline-table
::-moz-page
::-moz-page-sequence
::-moz-pagebreak
::-moz-pagecontent
::-moz-progress-bar
::-moz-range-progress
::-moz-range-thumb
::-moz-range-track
::-moz-scrolled-canvas
::-moz-scrolled-content
::-moz-scrolled-page-sequence
::-moz-svg-foreign-content
::-moz-table
::-moz-table-cell
::-moz-table-column
::-moz-table-column-group
::-moz-table-outer
::-moz-table-row
::-moz-table-row-group
::-moz-viewport
::-moz-viewport-scroll
::-moz-xul-anonymous-block

-webkit scrolling pseudo-elements

::-webkit-progress-bar
::-webkit-progress-value
::-webkit-meter-even-less-good-value
::-webkit-inner-spin-button
::-webkit-outer-spin-button
::-webkit-scrollbar
::-webkit-scrollbar-button
::-webkit-scrollbar-thumb
::-webkit-scrollbar-track

Pseudo Elements (open in -webkit- browser)

Specificity

Next ➹