Your Web News in One Place

Help Webnuz

Referal links:

Sign up for GreenGeeks web hosting
August 25, 2019 03:20 am GMT

IDs vs Classes: a CSS Specificity Chapter

Who will win? IDs or classes?

We may be able to answer this question by looking at the context, and the differences between each one.

Lets jump right in!

What is CSS Specificity?

CSS Specificity is like a rank, that determines which set of rules will be applied to an element instead of other sets of rules.

If the browser finds two conflicting styles pointing to the same element, it will apply the rules with higher ranking.

When writing CSS, you should always remember this:

inline style > #id > .class > element

Thats the CSS Specificity Hierarchy between the CSS Selectors.

The all-mighty styles that would be applied no matter what, are the inline styles. Next, if there are no inline styles, the ID selector takes the lead. Among the other two, the class styles have a higher ranking than the element type ones.

Benefits of using them wisely

  • Youll get less conflicting styles
  • Youll find and correct conflicting styles faster
  • Youll understand better whats happening in your stylesheet
  • Youll end up with a better CSS code (cleaner, shorter, more readable)

Differences between each CSS Selector

Inline styles

Most of the times these styles are never used. Why? Well, to write better code more humanly-readable, its better to have the CSS code all in one place: an external file.

Using inline styles is often seen as a bad practice. You are mixing HTML code with CSS so your code will be larger and harder to read and often harder to write too.

Try to avoid it.

IDs

IDs are unique, meaning that:

  • theres only one ID per element
  • theres only one element with that particular ID on the page

IDs are often used when adding JavaScript code, for performance sakes. But we are talking about CSS here! When adding styles, IDs are usually avoided because the code wont end up being scalable nor modular. You cant reuse styles, and if you are working on a big site, that will slow down the development process.

Classes

Classes are not unique, meaning that:

  • the same class can be used on different elements
  • the same element can have different classes

The common practice is to use mostly (if not only) classes for CSS.

Like said before, you can apply the same styles to different elements, ending up with less and easier to read code.

This approach works better with big websites when you dont want to write over and over again styles to each element, just give a set of rules for a class and give all the desired elements the same class. And if you want to add some different styles, add another class and problem solved.

Element type

By using these you are styling the element (tag) that has the specified name, like:

div {    font-family: Roboto;    border-radius: 4px;}

Its not really specific, right?

If you use div as the selector, youll apply those styles to all the divs of your website.

Why would you do that?

There are plenty of divs on your site, ones on the footer, others on the navbar, on the header, some may contain text, others images

There is no good rule of thumb on when to use type selectors. Just use classes a lot and add type selectors whenever possible, to keep specificity levels low, preventing styling conflicts.

Summary

You should keep the specificity levels low, so when a styling conflict shows up, you can easily solve it. The way to do it is using element type selectors and classes most of the times, along with pseudo-classes and attributes, avoiding IDs and inline styles.

Also, as told before, classes have several advantages over IDs.

So in the war between IDs and classes, classes win. But hey! You know the differences now, and keep in mind that there may be cases when using an ID selector would be better than using a class, so keep your eyes open!

There are some other good posts on the topic that I encourage for further reading:

I hope that this post has somehow helped you. Thanks for reading!


Original Link: https://dev.to/lautarolobo/ids-vs-classes-a-css-specificity-chapter-340p

Share this article:    Share on Facebook
View Full Article

Dev To

An online community for sharing and discovering great ideas, having debates, and making friends

More About this Source Visit Dev To