by pan69 · 11y ago · 0 comments
<ul class="collection">
<li class="collection-item">Alvin</li>
<li class="collection-item">Alvin</li>
<li class="collection-item">Alvin</li>
<li class="collection-item">Alvin</li>
</ul>
Rather than assigning a "collection-item" class to each "li", would you not simply style the "li" in the context of "collection"?Bootstrap does a similar thing. Drives me nuts...
There are two problems with your suggested approach -- specificity and coupling. The rules of CSS are such that .collection > li is a more specific selector than .collection-item. So if you want a particular item in the list to be red, you can't just give it a class name .warning-item and style against that -- you have to match or exceed the existing specificity. In the simple case, this isn't too bad, but it's surprisingly easy to end up representing deeply nested structures in your CSS which are very difficult to override.
The coupling problem is really just a way of saying that it might not be a good idea to describe the specifics of your HTML implementation in CSS. Class names are like an interface. One refactoring I've actually done a lot is switching out lists like the above for a combination of nav and anchor elements. It's great to be able to do that without needing to rewrite all the corresponding CSS, too.
These are tools designed to help you manage complexity and increase flexibility, not dogma. If you don't find your HTML needing to change much, or you don't need to make styles overrideable, then YAGNI. Hopefully it makes sense why framework authors, whose work is explicitly designed to be overridden, would choose this approach.
I'm just provided the link here in case anyone else was wondering the same thing.
I tend to agree with you, but I was heavily chastised by lead the front end dev at my last gig for ever directly applying design styles to tag entities as doing it as above is considered best practice at the moment (until the cycle comes around again to another best practice).
You want one collection li's to not have the style? Use an alternative class on the ul: "collection collection-alt".
You want nested li's to not inherit the styles? Use ".collection > li" in your CSS.
In any case, assigning a class on each li goes exactly against CSS best practices, because it prevents taking advantage of the inheritance of property values or of advanced selectors.
I think the whole community went to separating the HTML tag names from the styles and work only with class names [1].
If you start styling your tags directly it imminently increase the code debt if you scale. Let's say you just want to add another element inside the tag. I can give you example :
<ul class='collection'><li></li></ul>
.collection > li { color: red; }
and later on you add a anchor as a child you would need to force the specificy to this element <ul class='collection'><li><a class='collection-link'></a></li></ul>
.collection-link { color: blue; } // this will not work
.collection > li .collection-link { color: blue; } // increased complexity
I would almost always advice against styling tag names, except when you are doing css reset.As to this specific case, though, I was surprised to see your "this will not work" comment and tested it. It worked: http://jsfiddle.net/brlewis/p0Locwgp/
I don't usually nitpick but I don't think one link is representative of the `whole community`. Just this thread seems to prove it's pretty divided on the issue.
What is the recommendation, then? .collection .collection-item .collection-item-anchor? Genuinely curious here.
A class denotes something special. You don't need to repeat yourself here. The best way to eliminate side affects is by doing explicit targeting so your CSS only does what you say it can and nothing else.
html > body > div.header > h1 { color: #FFFFFF; }
I guess it's come about from trying to be more semantic within the documents.
Declaring Headings with classes is a good example as well.
h1,
.h1{
font-size:20px;
}
h2,
.h2{
font-size:18px;
}
etc... This allows you to apply <h1> styles to <h3> or even <p>'s while still keeping the content semantically correct and designers happy.Another few other common ones are
strong,
.strong{
font-weight:600;
}
small,
.small{
text-size:10px;
}
The same can go with .collection, somebody may want to use the styles on something thats not a list for example <div>'s.It also does not really go against inheritance, themes like .collection-alt can still work.
Sometimes its an overkill and I don't always do it, but in some cases (heavily responsive sites) it has worked really well. Its also easier to work in a team with one approach, rather then just doing it for headings etc why not take the principle/standard across all styles.
<ul class="list-reset">
<li class="inline-block mr1 h4 border border-red">Half-Smoke</li>
<li class="inline-block mr1 h4 border border-green">Kielbasa</li>
<li class="inline-block mr1 h4 border border-blue">Bologna</li>
<li class="inline-block mr1 h4 border border-yellow">Prosciutto</li>
</ul>...don't exist. There are many competing schools of thought. In particular, BEM is a school of thought with widespread support which disagrees with your comment.
https://developer.mozilla.org/en-US/docs/Web/Guide/CSS/Writi...
Using nesting and tags is less performant than classes. So using more classes is better than less.
> Note: This document was originally written in 2000. Much has changed when it comes to writing CSS that is fast.
<ul class="collection"> <li class="collection-item"> <ul> <li>Bulleted label</li> <li>Bulleted label 2</li> </ul> </li> </ul>
This case must be considered when setting coding standards for a framework.
You want nested li's to not inherit the styles? Use ".collection > li" in your CSS.
I'd say, keep it DRY [1].
https://github.com/suitcss/suit/blob/master/doc/naming-conve...
Using element names instead of class names can be problematic. In some frameworks you may need to use a different element or an element that wraps another element to achieve certain functionality.
Some use generic names like "button large" as class names, but, again in my personal experience, it often causes problems. You might have other elements that need to be "large" and may need to be inside a button - but which "large" do you mean.
So to keep things predictable, I've adopted the following practices:
- Always namespace components.
- Always ensure that selectors work even if the child has a wrapper around it.
- Whenever possible use classnames instead of element names.
And to ensure I don't get tendonitis (not sure if everyone agrees with this):
- If possible provide a shorthand for at least the most used components (i.e. "col"/"col-i" for "collection"/"collection-item"