CSS Pseudoelements

Made with by

::before

::after

::first-line

::first-character

We're going to look at these pseudoelements

double vs single colon?

CSS3 distinguishes pseudoelements and pseudoclasses

     ::double-colon for pseudoelements

     :single colon for pseudoclasses

.link:hover {
  /* pseudoclass */
  background-color: #bada55;
}

.link::after {
  /* pseudoelement */
  content: "*_*";
}
  • a magic keyword added to a selector

  • a virtual element that is not really in the DOM

  • accessibility problem

  • one element can only have one pseudoelement

What are they?

  • they style some part of the selected element

  • they inject virtual element to the DOM

  • we can do lots of tricks with them

What do they do?

  • add double colon to the selector

How do we use them?

.header::before {
    content: "Before the header";
    font-size: 2rem;
}

.header::after {
    content: "After the header";
    font-size: 2rem;
}

.title::first-character {
    color: #292929;
    font-size: 3rem;
}

.title::first-line {
   background-color: blue;
}

::first-line

  • selects the first line in the block-level element
  • doesn't add a new element to the DOM (selects the parts of an existing element)
  • can only have some properties
  • responsive
  • doesn't work with display:inline

First line example (responsive):

First line with different display values

::first-letter

  • selects the first letter in the element
  • doesn't add a new element to the DOM (selects the parts of an existing element)
  • can only have some properties, mostly related to fonts
  • doesn't work with inline elements

First letter:

First letter and display property

::after

  • injected element after the selected element
  • will not be shown if there is no content property
  • not accessible => screen readers will not see the after and before
  • can have any CSS property

::before

  • injected element before the selected element
  • will not be shown if there is no content property
  • not accessible => screen readers will not see the after and before

::after and ::before

  • not all elements can have ::after  and ::before
  • elements that are replaced in the DOM can not have pseudoelements
    • image
    • iframe, video, audio
    • input, textarea, select

JS manipulation

pseudoelements are playing hard to get

Get the styles of pseudoelements with JS

  • Js has better solutions than jQuery
  • pseudoelements can be read with getComputedStyle

Style pseudoelements with JS

  • Js has better solutions than jQuery
  • pseudoelements can be styled with insertRule(rule, rulesIndex)

Examples from every slide:

https://codepen.io/collection/XvJmMY/

So what is the big deal with pseudoelements?

  • pseudoelements insert the content that is not present in DOM
  • more dynamic markup
  • plenty of cool tricks

Scss libraries made only with after and before

Other cool stuff from the internet

Demo time

content with attr()

  • attr returns value of the attribute in an element
  • we can do string interpolations in CSS 😍

HAML&SCSS enhanced with pseudoelements

Pure CSS github graph with contributions

Thank you for coming

.meetup::after {
    content: "beer";
}

CSS Pseudoelements

By tonkec palonkec