Fireship · @fireship

CSS Pseudo-elements :: in 100 Seconds

Uploaded June 8, 2020 · Length 1:57

CSS Pseudo-elements :: in 100 Seconds

Summary

Learn how to use CSS pseudo-elements to style HTML parts without markup duplication, improving code efficiency and maintainability.

  • css pseudo-elements
  • html styling
  • css efficiency
  • web development

Transcript preview

The first 2 minutes of the transcript. Select a timestamp to watch that moment on YouTube.

  1. 0:00

    pseudo elements there's more to your

  2. 0:02

    HTML than meets the eye there are parts

  3. 0:04

    of the UI that you can style with CSS

  4. 0:05

    that don't actually exist in your HTML

  5. 0:07

    markup take for example a form input

  6. 0:09

    that has some placeholder text the

  7. 0:11

    placeholder value is defined as an

  8. 0:13

    attribute on the element so how do we

  9. 0:14

    get ahold of the placeholder to style it

  10. 0:16

    the answer is we target a pseudo element

  11. 0:18

    in our CSS they always have a double

  12. 0:20

    colon to distinguish them from pseudo

  13. 0:22

    classes the difference is that a pseudo

  14. 0:24

    element represents an actual part of the

  15. 0:26

    Dom whereas a pseudo class represents

  16. 0:28

    the state of an element based on the

  17. 0:30

    user's interaction with it pseudo

  18. 0:31

    elements can make your code much more

  19. 0:33

    efficient take for example this HTML

  20. 0:35

    where the first letter in each paragraph

  21. 0:37

    is being wrapped in a span so it can be

  22. 0:39

    styled a far more efficient way to solve

  23. 0:41

    this problem is by targeting the first

  24. 0:42

    letter on the paragraph element the end

  25. 0:44

    result is much cleaner semantic HTML

  26. 0:46

    that's cool but by far the most powerful

  27. 0:48

    pseudo elements are before and after

  28. 0:50

    many but not all HTML elements can have

  29. 0:53

    content inserted before or after their

  30. 0:55

    inner content handled entirely from your

  31. 0:57

    CSS code for example we can target

  32. 0:59

    before and provide an empty string as

  33. 1:01

    the content property now that we've

  34. 1:03

    targeted this element you'll notice that

  35. 1:04

    it shows up in the browser's inspector

  36. 1:06

    we can style this element as if it were

  37. 1:08

    an empty span or div in our HTML and

  38. 1:10

    that can be very useful for creating

  39. 1:11

    advanced animations without polluting

  40. 1:13

    your HTML markup imagine we have a big

  41. 1:15

    unordered list and before each list item

  42. 1:17

    we have an emoji character instead of

  43. 1:19

    duplicating it in the markup we can

  44. 1:21

    simply add it as the content property to

  45. 1:23

    the before selector and once again we're

  46. 1:25

    left with code that is far more

  47. 1:26

    maintainable there's a few other pseudo

  48. 1:28

    elements that you should know about

  49. 1:29

    selection allows you to change the

  50. 1:31

    background and color of highlighted text

  51. 1:33

    in some browsers like chrome but not

  52. 1:34

    Firefox you can customize the scroll bar

  53. 1:36

    on scrollable elements before using the

  54. 1:38

    pseudo element always check for browser

  55. 1:40

    compatibility ones like spelling error

  56. 1:42

    might sound cool but they don't really

  57. 1:43

    have any browser support yet this has

  58. 1:45

    been pseudo elements in 100 seconds make

  59. 1:47

    sure to also check out pseudo classes

  60. 1:48

    thanks for watching and I will see you

  61. 1:50

    in the next one

  62. 1:52

    you