Fireship · @fireship

7 ways to deal with CSS

Uploaded February 15, 2022 · Length 6:23

7 ways to deal with CSS

Summary

7 ways to handle CSS in React apps, including global CSS, CSS modules, preprocessors, CSS-in-JS libraries, and utility class libraries.

  • css in react
  • css modules
  • scss
  • css-in-js
  • styled components
  • tailwind css
  • bootstrap
  • react css

Transcript preview

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

  1. 0:00

    when you get started with web

  2. 0:01

    development they say you gotta learn the

  3. 0:03

    css fundamentals i agree but when you

  4. 0:05

    get out into the real world you'll find

  5. 0:06

    that one does not just use plain css to

  6. 0:09

    build an app

  7. 0:10

    the left side's getting lower than the

  8. 0:11

    right side i can see what's happening

  9. 0:13

    now twist it all right

  10. 0:15

    it's just too painful there are

  11. 0:17

    literally hundreds of libraries that

  12. 0:18

    attempt to make css better but so far

  13. 0:20

    there's no universal solution today

  14. 0:22

    we'll take a look at seven different

  15. 0:24

    ways to write css specifically in a

  16. 0:26

    react app using next.js but most

  17. 0:28

    importantly we'll look at the trade-offs

  18. 0:30

    of each approach so you can decide

  19. 0:32

    what's best for your project if you're

  20. 0:33

    new here like and subscribe and check

  21. 0:35

    out my full courses on fireship io to

  22. 0:37

    learn even more by building complete

  23. 0:39

    projects now let's talk about css here i

  24. 0:41

    have a pristine next.js app and the most

  25. 0:43

    primitive way to style it is to create

  26. 0:45

    global css that applies to the entire

  27. 0:47

    application that might work okay for a

  28. 0:49

    small application but it doesn't scale

  29. 0:51

    well at all the first problem you'll run

  30. 0:53

    into is that naming things gets really

  31. 0:54

    hard because css cascades to address

  32. 0:57

    this you'll likely need to use a naming

  33. 0:59

    convention like bem but it's not going

  34. 1:01

    to be very fun it's hard to do perfectly

  35. 1:03

    and you'll probably end up using

  36. 1:04

    important everywhere because you've got

  37. 1:06

    more important things to do than figure

  38. 1:07

    out how to not use important the other

  39. 1:09

    problem is that it generally results in

  40. 1:11

    a very large and inefficient css bundle

  41. 1:14

    the larger the bundle size the slower

  42. 1:16

    the app loads luckily next.js supports a

  43. 1:18

    tool called css modules out of the box

  44. 1:20

    to address some of these concerns a css

  45. 1:23

    module file looks just like regular css

  46. 1:25

    but it can be scoped to an individual

  47. 1:27

    component and that means you don't have

  48. 1:28

    to worry about name collisions when

  49. 1:30

    writing your code in other words you can

  50. 1:32

    have two different module files that

  51. 1:33

    define the same class name but because

  52. 1:35

    they're scoped locally you don't have to

  53. 1:37

    worry about them interfering with each

  54. 1:38

    other in addition it tells next.js

  55. 1:40

    exactly which css to load for a given

  56. 1:42

    page which can reduce the bundle size

  57. 1:44

    now that may sound inefficient because

  58. 1:46

    you may want to share styles between

  59. 1:47

    multiple components but modules have a

  60. 1:49

    special composes property that can

  61. 1:51

    import and override code from a

  62. 1:53

    different module overall modules will

  63. 1:55

    make your life much easier but we're

  64. 1:57

    still dealing with plain css here and

  65. 1:58

    one thing it lacks is programmatic