This post (Why you should use semantic naming in CSS) was originally published on Sargalias.
Video version of this article: https://youtu.be/4Oo04...
For further actions, you may consider blocking this person and/or reporting abuse
I'm not convinced about the semantic-ness of "sidebar". Still seems presentational to me, since on some devices you might not want it at the side. The problem is of course that the sidebar often contains a bag of bits, and while each bit may have a semantic name, the bag doesn't. It's impossible to suggest a better name without knowing what bits a given bag contains, but in many cases, referring to the element as an
<aside>within the context of a given document element ancestry might work out better.Yeah perfectly good suggestion, thank you. You've got some good reasoning behind that.
At the end of the day you and your team will decide what makes the most sense to use.
There are two main motivations behind semantic class names:
color-redwhich could change all the time.You've got the right idea to challenge the name. If you can find one that you think is most appropriate then by all means go for it :).
EDIT: Clarified about label being non-surprising.
Semantic CSS is definitely important. I'm curious what you think of Tailwind CSS, isn't it doing the exact opposite?
Yes exactly, Tailwind CSS is doing the opposite. Personally I would never use it in a serious project for the reasons outlined in this article and more:
.text-indigo-600. If you suddenly want to change tofont-weight: 700for all headings on your website, or a different color, you'll have to go to a ton of HTML elements to make the change. However with normal CSS you can have a variable called color-primary and just change that and affect everything with a single change. You could technically change the value of theindigoclass to say yellow, but then the class name shows a different color so that will be confusing.Say for example you have an element representing the "page-heading" in multiple templates. With semantic class names, they all have a single class of
page-heading. With Tailwind CSS, they probably have multiple classes you'll need to re-copy every time. Then if you want to modify the styles, with a single class, you only modify the CSS in one place. With Tailwind CSS, you have to change classes in every single place.button button-primary, that's a single class. However with tailwind that's going to be an entireifstatement with multiple different classes:You cannot see all the styling at once for the relevant component unless you know what all the classes do off by heart. Otherwise you have to check documentation for every class. Whereas with a single class you can immediately open up the CSS file it's in and see it all in one place.
If you want a new custom style for your component, you now have to add it in two places: Create a new CSS class for it, and add it in HTML.
And so on.
... Damn I went all out on this one. Sorry my thoughts just grew as I was writing the thing.
But all of that is just my opinion of course.
There is one use case I see Tailwind CSS or other OOCSS frameworks being useful, and that's if non-technical people may need to change styles. E.g. if you have a CMS or something where a non-technical person can select things like "margin-large", or "border-radius-large". You would effectively be presenting OOCSS or atomic CSS options (like Tailwind CSS) to them, and then applying those classes to the resulting HTML.
I mean you could technically do it while staying semantic, but then you would basically be re-implementing Tailwind CSS for all those options in your own code. It's probably easier to just use Tailwind CSS in this case.
Also something like Tailiwind CSS is great for quickly trying out different styles in prototyping.
EDIT: Clarified about CMS use case.
Lot of good points, I've never really explored utility CSS libraries since I've always done semantic, but an article talking about the two approaches: utility and semantic would be very interesting to read.
I've long been a fan of Adam Silver's Maintainable CSS for all these reasons.
That's a nice article, thanks for linking it :). Some great points in there.