I wonder what fits better into the CSS philosophy:
- CSS classes mark entities
– OR – - CSS classes mark aspects
For example let’s take the cell for a product price sheet and a footer cell which contains the sum of all prices.
In the first case each of the cells would only have one class: product-price resp. product-price-sum (or price and the row for example has a product class)
In other words: Classes identify things.
In case two the cells would have many classes, which define the properties/aspects of a product price, for example numeric and currency and an additional sum class for the footer. numeric would define the text to be right aligned, sum would mark the cell bold.
In other words: Classes describe things.
I can’t decide which approach is better. In the past I used a mixture of both which quickly led to an ugly pile of unstructured CSS classes with conflicting styles and some ugly !important hacks.
The first approach obviously has some redundancy, because one would have many classes (product-*) and most of them would share common CSS properties.
The second one has problems when it comes to format just one place differently, let’s say the product price sum. It can be possible that there are other places which also have the exact same three classes assigned, but don’t have anything to do with a product price. In that case one would have to use the surrounding HTML tags to somehow “address” the specific place in the HTML file.
Are there any rules of thumb, guidelines, proven concepts, etc on how to handle this problem?
Just an observation… People tend to forget that CSS is hierarchical. Let me give you a very simple sample (please the tags are reduced to the minimum):
you can compose class styles with tag style to pin where the style will be aplied:
Or you can use only simple table tags (no th, thead, tbody and tfoot tags) like this:
And the CSS would be
This is not a final solution. Just a new approach to the problem.
Remember also that you can indicate some states or additional information to the CSS using custom attributes. See this sample:
See that the “selected” attribute at the “tr” tag has no effect in the standard renderization of the table since it is not a recognized attribute of the tag, but it can be identified by the CSS (and also by the javascript which is not the case here). Like this: