DEV Community

Cover image for 6 Powerful CSS Techniques You Can Use Instead of Javascript

6 Powerful CSS Techniques You Can Use Instead of Javascript

Diona Rodrigues on July 20, 2020

There are a lot of discussions about CSS vs. JavaScript and the battle is getting seriously once CSS is adding a bunch of amazing features and it's...
Collapse
 
maxart2501 profile image
Massimo Artizzu

About scroll-behavior: I'm sure most of us are excited about it, but remember that it doesn't come without problems. Specifically:

  • users may be annoyed by long scrolling waiting time: use only for small jumps;
  • some other users perceive motion sickness when large areas move around. That's why there's also a prefers-reduced-motion media query to meet their needs.

In short, consider doing something like that:

@media screen and (prefers-reduced-motion: no-preference) {
  .scrolling-box {
    scroll-behavior: smooth;
  }
}

(Written like this, it's safe for those browsers that don't support prefers-reduced-motion.)

Collapse
 
dionarodrigues profile image
Diona Rodrigues

I'm with you Massimo, good tips. There are a lot of interesting CSS features, but we need to think about when and how to use them correctly. 🀩

Collapse
 
mrakonja profile image
Mladen Milosavljevic

Most of the stuff i used but darkmode was new for me and a good tool for my arsenal. It was good read. Kudos

Collapse
 
dionarodrigues profile image
Diona Rodrigues

Awesome! Using just CSS we are able to handle Dark mode easily! πŸ‘Œ

Collapse
 
mrakonja profile image
Mladen Milosavljevic

Yes!

Collapse
 
dionarodrigues profile image
Diona Rodrigues

I feel happy that this article was useful for you! There are a lot of solutions for handling Dark theme, but just using CSS you can do a lot of thing easily! βœ¨πŸŽ‰πŸ˜Ž

Collapse
 
kendalmintcode profile image
Rob Kendal {{β˜•}}

Great article, Diogo, I love little tips like this that get lost or that you never knew existed.

Collapse
 
dionarodrigues profile image
Diona Rodrigues

Thank you Rob! πŸ‘

Collapse
 
mehdico profile image
Mehdi Mousavi

scroll-behavior: smooth; is not supported in Safari, so you have to use JS ;)

Collapse
 
dionarodrigues profile image
Diona Rodrigues • Edited

I agree with you that for some browsers JS is necessary depending on the feature πŸ˜’πŸ€·β€β™‚οΈ. But for most of modern browsers we no longer need JS anymore for a lot of new CSS features. πŸŽ‰

Collapse
 
maxart2501 profile image
Massimo Artizzu

... or let it gracefully degrade in Safari.
It's not something that would break the UI πŸ˜‰

Collapse
 
dionarodrigues profile image
Diona Rodrigues

Thank you for sharing this solution. It's really helpful!! ✌

Collapse
 
bowiechang profile image
bowiechang

Lovely, but just wanna point out on

position: sticky

you cannot have a parent that has attr overflow hidden to be applied otherwise it doesn't work...

Collapse
 
iskin profile image
Andrey Sitnik

postcss-dark-theme-class can make even a dark/light switcher with CSS-only dark theme (and 5 lines JS for switcher).

Collapse
 
pris_stratton profile image
pris stratton

Lovely stuff. CSS is amazing. I always feel like the less JS a page uses the quicker it’ll be to load and use, but I have no idea whether that has any basis in fact.

Collapse
 
dionarodrigues profile image
Diona Rodrigues

I think you're right. In general, CSS can increase the performance a lot when compared with JS. So, every time you can, go ahead with CSS. :)

Collapse
 
kadonwills profile image
Kadon • Edited

Thanks, great article, Diogo, I love the tips and i really find it helpfull in increasing web performance (since it implies less js scripts)

Collapse
 
dionarodrigues profile image
Diona Rodrigues

You get it. That's the idea: less JS and better performance! 🀩

Collapse
 
timhuang profile image
Timothy Huang

Thanks! It's really helpful.

Collapse
 
dionarodrigues profile image
Diona Rodrigues

You're welcome! Thank you for your feedback! :)