DEV Community

Cover image for CSS GUIDE: The Art of Naming (Save hrs for debugging)
Rahul
Rahul

Posted on

CSS GUIDE: The Art of Naming (Save hrs for debugging)

I have heard lots of developers say they hate CSS. In my experience, this comes as a result of not taking the time to learn CSS. So here is my latest post, I'll tell you about a few naming conventions that will save you a bit of stress and countless hours down the line.

Why only CSS

CSS isn’t the prettiest β€˜language,’ but it has successfully powered the styling of the web for over 20 years now.

However, as you write more CSS, you quickly see one big downside.
It is darn difficult to maintain CSS.

Poorly written CSS will quickly turn into a nightmare.

Conventions To Use πŸ‘Š

Use hyphen Delimited strings

-> If you write a lot of JavaScript then writing variables in camel case is a common practice.
Eg:-

var blueBox = document.getElementById('......')
Enter fullscreen mode Exit fullscreen mode

This is correct, right?

The problem is that this type of naming is **not **well-suited to CSS.

.blueBox {
    border: 3px solid blue;
   }
/* Wrong way. Don't Do This */
Enter fullscreen mode Exit fullscreen mode
.blue-box{
    border: 3px solid blue;
   }
/* Do This. The correct way*/
Enter fullscreen mode Exit fullscreen mode

This is a pretty standard CSS naming convention.


Teams have different approaches to writing CSS selectors. Some teams use hyphen delimiters, while others prefer to use the more structured naming convention called BEM.

There are 3 problems that CSS naming conventions try to solve

  • To know what selector does, just by looking at its name
  • To have an idea where selector can be used by just looking at it
  • To know the relationships between class names, just by looking at them

Have you ever seen class names written like this:

.nav--secondary{
      ....
    }

.nav__header{
      ....
   }
/* This is called BEM naming convention. */
Enter fullscreen mode Exit fullscreen mode

Real life explanation πŸ˜…πŸ‘Š

Today is John's first day at work.
He is handed over an HTML code that looks like this.

<div class="siteNavigation">

</div>
Enter fullscreen mode Exit fullscreen mode

John realizes this may not be the best way to name things in CSS. So he goes ahead and refractors like the codebase like so.

<div class="site-navigation">

</div>
Enter fullscreen mode Exit fullscreen mode

Poor John, he is unknown that he had broken the codebase 😩😩.

Somewhere in JavaScript code, there was a relationship with the previous class name, siteNavigation:

const nav = document.querySelector('siteNavigation')
Enter fullscreen mode Exit fullscreen mode

So the change in the class name the nav variable became null.

So SAD 😩



To prevent cases like this Developer has come with some amazing and very different strategies.

Use js-class names

One way to mitigate such bugs is to use a js-* class name to denote a relationship with the DOM element in question.

for example:-

<div class="site-navigation js-site-navigation">

<div>
Enter fullscreen mode Exit fullscreen mode

and in javascript code

const nav = document.query.Selector('.js-site-navigation')   
Enter fullscreen mode Exit fullscreen mode

As a convention, anyone who sees the .js-site-navigation class name would understand that there is relationship with that DOM element in the JavaScript code.

Some developer use data attributes as JavaScript hooks. This isn't right.
By definition, data attributes are used to store custom data.

If people use the rel attribute, then it's perhaps okay to use data attributes in certain cases. It's your call after all.


This has nothing to do with name convention but will save your time too.

-> You don't need to write a comment to say color: red; will give the color as red.

But, if you're using a CSS trick that is less obvious, feel free to write a comment.


Don't forget to get daily.dev extension.

Thanks For Reading.

Hope this article is helpful for you. 😁

Top comments (7)

Collapse
 
sparkalow profile image
Brian M.

I have started using Tailwindcss and had to do zero css class naming on my last project. I was skeptical of Tailwind at first, but am a convert now. It's not for everyone, but it does solve some of CSS's annoying problems.

Collapse
 
isarisariver profile image
Marian

Silly question, but why are your code blocks colored and mine are all white? πŸ˜„

Collapse
 
rahxuls profile image
Rahul

Brother try this.

```javascript (you coding language)


Enter fullscreen mode Exit fullscreen mode
Collapse
 
isarisariver profile image
Marian
a {
  color: white;
/*It works, thanks so much ❀️ */
}
Enter fullscreen mode Exit fullscreen mode
Collapse
 
isarisariver profile image
Marian

Great tip thanks for sharing :)

Collapse
 
bernardbaker profile image
Bernard Baker

I like the idea of the is prefix.

Collapse
 
rahxuls profile image
Rahul

Now this is something important. Helpful sir.