DEV Community

Stop Using IDs and Classes in Every HTML Element!

Puput Tallulah on July 24, 2022

Ola! So, this is something I've just come to a realisation recently and if you're a newbie in the world of web development then it's easy for you t...
Collapse
 
atinypixel profile image
Aziz Kaukawala

In my early days, my mentor and my boss taught me 2 specific things:

  1. Don't use IDs unnecessary. Use only if an element needs to be accessed in Javascript.
  2. In CSS, don't use selector & tag together.
h1.some-class {} // a big no!

h1 {}
// or
.some-class {}
Enter fullscreen mode Exit fullscreen mode

7+ years down, I still find those tips helpful in every project!

Happy Coding ❤

Collapse
 
geforcesong profile image
George Guo

why for #2?

Collapse
 
lolcabanon profile image
lolcabanon • Edited

Adding on Aziz answer :

If for examble you use h2.nice-heading { color: red; font-size: 69px; }, but later on another page you want to style an h3 the same (you have to put an h3 to follow the hierarchy of content, but you would like to have the same style) you cannot use your class .nice-heading cause it is not an h2. Your selector would be over specific.

You should simply do a class .nice-heading so you can then apply it to any element you want.

Thread Thread
 
geforcesong profile image
George Guo

In fact, this should not happen or very rare. Same thing should be extracted to be a single component which can be used anywhere. The big con of this is, after CSS bundled, and some part of CSS could be affect by another piece of CSS, you have to add a lot of dirty stuff to fix, like !improtant

Thread Thread
 
lolcabanon profile image
lolcabanon

Depends on you tech stack indeed... But i'm more talking about a classic vanilla (no web component) setup with some naming convention like BEM or CUBE.

Also my example is just an answer to a prior question to demonstrate that you should not use uselessly specifics selectors like h1.nice-header or div.box.

PS: improtant is not a word ;)

Thread Thread
 
atinypixel profile image
Aziz Kaukawala

No, rather than using !important, I tend to control specific/scoped styling via parent hierarchy.

Its not like I create everything generic. However, trying to keep everything modular is my main goal. When I get the design, I study it thoroughly so that I can figure out which styles needs to be generic, modular, single use or limited.

And yes, recently I got a change to work with tailwind and because of my usual practices, I kinda loved it (but only with Next.js or similar frameworks! 😅)!

Collapse
 
atinypixel profile image
Aziz Kaukawala

Because its irrelevant and loses its reusability. I tend to write modular styles so that it could be reused anywhere without dependency of the element. So I either use tag to style the element or class, never both.

Thread Thread
 
geforcesong profile image
George Guo

you are creating tailwind stuff. that is fine. but i don't think h1.some-class is the case.

Collapse
 
syeo66 profile image
Red Ochsenbein (he/him)

Now let's talk to Tailwind users... :-D

Collapse
 
taufik_nurrohman profile image
Taufik Nurrohman

With WAI-ARIA, you can utilize the role attribute to style the elements. For example, a[role=button] instead of a.button, div[role=alert] instead of .alert, etc.

Collapse
 
rillus profile image
Riley

I definitely agree that you shouldn't ever use an ID (reserve these for JS hooks or anchors - their specificity will make it harder to overwrite in the wild).

For me, pseudo selectors are mainly useful if you're trying to style something where you don't have control over the markup, or if the rules you're imposing would still work should the elements themselves be reordered or changed.

For example, an unordered list you're using to display some navigation, and the last item needs to have no margin, regardless of if the navigation order changes.

If in this navigational example, you wanted to highlight the 3rd item (due to it's content, not simply because it's the third item), I'd recommend adding a modifier class - this future-proofs your CSS in case the order changes, or if you later wanted to highlight another item you could just add the class elsewhere.

Pseudo classes are a great trick to give you more power over your lists and flex items, but it's no substitute for a good class naming convention and structure (check out BEM or SUIT CSS if you've not already).

Happy coding!

Collapse
 
naveennamani profile image
naveennamani

Title: Stop Using IDs and Classes in Every HTML Element!

Example given: consists of 5 classes

Me: whhaaat?

Collapse
 
mrmobin profile image
Mobin

"xD" Yea , me too

Collapse
 
darkwiiplayer profile image
𒎏Wii 🏳️‍⚧️

What if I told you this example only needs one class, if even that? 😉

Collapse
 
puputtallulah profile image
Puput Tallulah

you can elaborate :) I am all ears for suggestions

Collapse
 
darkwiiplayer profile image
𒎏Wii 🏳️‍⚧️
.flex-container-column > * { /* Rules */ }
Enter fullscreen mode Exit fullscreen mode

for the one class approach, or just an element selector if you want no classes at all.