Fireship · @fireship

3 More Life-Changing CSS Tips

Uploaded April 14, 2021 · Length 2:59

3 More Life-Changing CSS Tips

Summary

learn 3 simple yet powerful css techniques to improve your web design: using relative units, hsl colors, and scroll padding.

  • css tips
  • responsive design
  • hsl colors
  • scroll padding
  • web development

Transcript preview

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

  1. 0:00

    a few days ago i shared 10 of my

  2. 0:01

    favorite css techniques

  3. 0:03

    and you guys responded with nearly a

  4. 0:04

    thousand comments of your own pro tips

  5. 0:06

    tips that are so good that they deserve

  6. 0:08

    their own video

  7. 0:09

    the first one from skylark murphy is

  8. 0:11

    really simple yet really powerful

  9. 0:13

    basically it boils down to use relative

  10. 0:15

    units when possible over static pixel

  11. 0:17

    values for example

  12. 0:18

    when designing a responsive layout you

  13. 0:20

    might have headings or containers with

  14. 0:22

    fixed pixel values for margin and

  15. 0:24

    padding then you might have a media

  16. 0:25

    query to change those values on smaller

  17. 0:27

    screens

  18. 0:28

    not only is this difficult to maintain

  19. 0:29

    but it doesn't work very well on devices

  20. 0:31

    that scale fonts automatically

  21. 0:33

    instead of fixed units like pixels we

  22. 0:35

    can use relative units like

  23. 0:36

    m or rem m will calculate the size

  24. 0:39

    automatically

  25. 0:40

    based on the font size of its nearest

  26. 0:41

    parent if the parent has a font size of

  27. 0:43

    16 pixels

  28. 0:45

    then 2m would compute to 32 pixels as an

  29. 0:48

    alternative

  30. 0:48

    you could use rem which is relative to

  31. 0:50

    the root font size i generally find that

  32. 0:52

    one more useful when working with margin

  33. 0:54

    and padding

  34. 0:55

    but most importantly notice how that got

  35. 0:56

    rid of the media query and one thing

  36. 0:58

    i'll add is that there are more relative

  37. 0:59

    units than just

  38. 1:00

    m and rem one of which is ch or

  39. 1:03

    character width

  40. 1:04

    now if you're really serious about

  41. 1:05

    design you may have read the elements of

  42. 1:07

    typographic style by robert bringhurst

  43. 1:09

    which says that the optimal size for a

  44. 1:11

    paragraph is between

  45. 1:12

    45 and 75 characters a cool trick is

  46. 1:15

    that we can enforce that rule using the

  47. 1:17

    clamp function i showed you in the

  48. 1:18

    previous video

  49. 1:19

    use it to make the min width 45

  50. 1:21

    characters the preferred width 50

  51. 1:23

    and the max width 75 characters and now

  52. 1:25

    every paragraph will be the absolute

  53. 1:27

    perfect width now our next tip is also

  54. 1:29

    very simple but also one that will

  55. 1:30

    change your life

  56. 1:31

    dimitra says to use hsl colors over hex

  57. 1:34

    or rgb

  58. 1:35

    rgb stands for red green blue where each

  59. 1:37

    mix of the color can be a value from 0

  60. 1:39

    to

  61. 1:40

    255. hexadecimal contains the same

  62. 1:42

    information but in a more compact format

  63. 1:44

    that's even harder to interpret

  64. 1:46

    hsl on the other hand stands for hue

  65. 1:48

    saturation and lightness

  66. 1:50

    and that format makes it way easier to

  67. 1:52

    compute a nice looking color palette if

  68. 1:54

    you have a hex color in vs code

  69. 1:55

    go ahead and hover over it then click on

  70. 1:57

    the top to bring up hsl mode on the

  71. 1:59

    right you'll notice we have our cue