Fireship · @fireship
7 ways to deal with CSS
Uploaded February 15, 2022 · Length 6:23
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.
- 0:00
when you get started with web
- 0:01
development they say you gotta learn the
- 0:03
css fundamentals i agree but when you
- 0:05
get out into the real world you'll find
- 0:06
that one does not just use plain css to
- 0:09
build an app
- 0:10
the left side's getting lower than the
- 0:11
right side i can see what's happening
- 0:13
now twist it all right
- 0:15
it's just too painful there are
- 0:17
literally hundreds of libraries that
- 0:18
attempt to make css better but so far
- 0:20
there's no universal solution today
- 0:22
we'll take a look at seven different
- 0:24
ways to write css specifically in a
- 0:26
react app using next.js but most
- 0:28
importantly we'll look at the trade-offs
- 0:30
of each approach so you can decide
- 0:32
what's best for your project if you're
- 0:33
new here like and subscribe and check
- 0:35
out my full courses on fireship io to
- 0:37
learn even more by building complete
- 0:39
projects now let's talk about css here i
- 0:41
have a pristine next.js app and the most
- 0:43
primitive way to style it is to create
- 0:45
global css that applies to the entire
- 0:47
application that might work okay for a
- 0:49
small application but it doesn't scale
- 0:51
well at all the first problem you'll run
- 0:53
into is that naming things gets really
- 0:54
hard because css cascades to address
- 0:57
this you'll likely need to use a naming
- 0:59
convention like bem but it's not going
- 1:01
to be very fun it's hard to do perfectly
- 1:03
and you'll probably end up using
- 1:04
important everywhere because you've got
- 1:06
more important things to do than figure
- 1:07
out how to not use important the other
- 1:09
problem is that it generally results in
- 1:11
a very large and inefficient css bundle
- 1:14
the larger the bundle size the slower
- 1:16
the app loads luckily next.js supports a
- 1:18
tool called css modules out of the box
- 1:20
to address some of these concerns a css
- 1:23
module file looks just like regular css
- 1:25
but it can be scoped to an individual
- 1:27
component and that means you don't have
- 1:28
to worry about name collisions when
- 1:30
writing your code in other words you can
- 1:32
have two different module files that
- 1:33
define the same class name but because
- 1:35
they're scoped locally you don't have to
- 1:37
worry about them interfering with each
- 1:38
other in addition it tells next.js
- 1:40
exactly which css to load for a given
- 1:42
page which can reduce the bundle size
- 1:44
now that may sound inefficient because
- 1:46
you may want to share styles between
- 1:47
multiple components but modules have a
- 1:49
special composes property that can
- 1:51
import and override code from a
- 1:53
different module overall modules will
- 1:55
make your life much easier but we're
- 1:57
still dealing with plain css here and
- 1:58
one thing it lacks is programmatic


