How to structure CSS classes? By entities or by aspects? - css

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):
<table class="product">
<thead>
<tr><th>Name</th><th class="price">Price</th></tr>
</thead>
<tbody>
<tr><td>Product 1</td><td class="price">1</td></tr>
<tr><td>Product 2</td><td class="price">2</td></tr>
<tr><td>Product 3</td><td class="price">3</td></tr>
</tbody>
<tfoot>
<tr><td>Total</td><td class="price">6</td></tr>
</tfoot>
</table>
you can compose class styles with tag style to pin where the style will be aplied:
table { /* styles for all tables */ }
.product { /* styles for the product table */ }
.product thead { /* styles for the all product table header row */ }
.product thead th{ /* styles for header row generic column */ }
.product thead .price { /* styles for header row price column */ }
.product tbody { /* styles for the all product table data row */ }
.product tbody td { /* styles for data row generic column */ }
.product tbody .price { /* styles for data row price column */ }
.product tfoot { /* styles for the all product table summarize row */ }
.product tfoot td { /* styles for summarize row generic column */ }
.product tfoot .price { /* styles summarize row price column */ }
Or you can use only simple table tags (no th, thead, tbody and tfoot tags) like this:
<table class="product">
<tr class="header"><td>Name</td><td class="price">Price</td></tr>
<tr class="data"><td>Product 1</td><td class="price">1</td></tr>
<tr class="data"><td>Product 2</td><td class="price">2</td></tr>
<tr class="data"><td>Product 3</td><td class="price">3</td></tr>
<tr class="footer"><td>Total</td><td class="price">6</td></tr>
</table>
And the CSS would be
.product { /* styles for the product table */ }
.product .header { /* styles for the all product table header row */ }
.product .header td{ /* styles for header row generic column */ }
.product .header .price { /* styles for header row price column */ }
.product .data { /* styles for the all product table data row */ }
.product .data td { /* styles for data row generic column */ }
.product .data .price { /* styles for data row price column */ }
.product .footer { /* styles for the all product table summarize row */ }
.product .footer td { /* styles for summarize row generic column */ }
.product .footer .price { /* styles summarize row price column */ }
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:
<table class="product">
<tr class="header"><td>Name</td><td class="price">Price</td></tr>
<tr class="data"><td>Product 1</td><td class="price">1</td></tr>
<tr class="data" selected="selected"><td>Product 2</td><td class="price">2</td></tr>
<tr class="data"><td>Product 3</td><td class="price">3</td></tr>
<tr class="footer"><td>Total</td><td class="price">6</td></tr>
</table>
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:
.product tr[selected=selected] { /* styles for the selected row */ }

I would highly recommend tagging by entity (attribute, rather than individual element), so class='product price' (2 tags), rather than class='product-price'(1 tag). If you're careful, this will result in cleaner, easier to maintain code. If you're having trouble with !important's, try drawing a tree of how the tags will be structured. This will help decide at which level of the tree to declare certain properties, and result in minimal use of !important.

I don't think there's any right answer, but you want to favour the approach that results in the cleanest CSS and the least duplication overall. For me personally I'd say you want something in between your two options, but closer to option two.
If I'm understanding your descriptions correctly, with option one you'd have something like this:
<table>
<tr>
<td>Product A</td>
<td class="price">£1</td>
</tr>
<tr>
<td>Product B</td>
<td class="price">£2</td>
</tr>
<tr>
<td>Total</td>
<td class="price-sum">£3</td>
</tr>
</table>
and then the CSS:
.price {
text-align: right;
}
.price-sum {
text-align: right;
font-weight: bold;
}
and option two would be more like this:
<table>
<tr>
<td>Product A</td>
<td class="numeric currency">£1</td>
</tr>
<tr>
<td>Product B</td>
<td class="numeric currency">£2</td>
</tr>
<tr>
<td>Total</td>
<td class="numeric currency sum">£3</td>
</tr>
</table>
but why not have:
<table>
<tr>
<td>Product A</td>
<td class="price">£1</td>
</tr>
<tr>
<td>Product B</td>
<td class="price">£2</td>
</tr>
<tr>
<td>Total</td>
<td class="price sum">£3</td>
</tr>
</table>
and:
.price {
text-align: right;
}
.sum {
font-weight: bold;
}
There's no point giving a cell both 'numeric' and 'currency' if all currency fields will always have both classes - better to give currency the same styles as numeric in the stylesheet and then just use currency in the source:
.numeric, .currency {
// etc.
}
.currency {
// etc.
}
Using surrounding HTML tags for your exceptions is fine if you can do so in a semantic way. For example you might have some general styles on the currency class, but then a text-align: right on td.currency specifically.
The only other thing I'd say is that if you're having to use !important to override your own styles, you're doing something wrong!

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.
Isn't "addressing" like this the whole point of CSS rules and specificity? I think of it less as "addressing" and more like "namespacing" ;)
Personally, I prefer the second method; I tend to prefer using the id attribute to identify things ;)
... but the class attribute is nonetheless considered an element identifier.
In the end though, the purpose of the class attribute is used "for general purpose processing by user agents." I interpret this as meaning that the values you give it should cater to your application's needs.
So if your application needs to identify all the product prices - be it for CSS styling or for a jQuery script or a screen reader or whatever - identify them with a class attribute of product-prices.
If your application needs to describe all the numeric values - again, be it for CSS styling or for a jQuery script or a screen reader or whatever - identify them with a class attribute of numeric.
PS Please don't call it the CSS class; call it simply the "class attribute." Unless you're working in Asp.NET, there is no such thing as the CSS class, and even in Asp.NET - there shouldn't be a CSS class.
Sorry for multiple shameless links to my own web site.

I think its a bit of a personal approach.
I also like the hierarchi and use that alot.
But i use the entity variant most, but try to avoid as many markup as possible.
A rule i use is titles always go with h1 or h2. And you can style accordingly inside your entity.
So for example:
.product { }
.product h1 { }
.product span.price { }
.product span.discount { }
Same for lets say navigation:
ul.navigation { }
ul.navigation li { }
ul.navigation li.first { }
ul.navigation li.last { }
And with that said, i always would say, go for the option wich has the least amount of markup, without losing your overview. Clean code is nice.

Related

Css selector to get entire table row (tr)

I have html like this below - and do not have access to modify it beyond setting a class or id on the first "td" in the row. Is there a way to target the entire row, or get both "td" elements in the row?
<tr>
<td width="50%" valign="top"><font class="subheader"><span class="eField">membershipCode</span> </font></td>
<td width="50%" valign="top"><font class="text">Testing </font></td>
</tr>
Part of this goes out in Email, so I'd rather avoid Javascript if possible. I tried this css, but no luck so far:
<style type="text/css">
td span.eField {
display:none;
}
td span.eField+td {
display:none;
}
</style>
Is there any way to do this using pure css?
NOTE: I only want to target rows containing the "eField" elements - I can hide the element itself, but can't get the next or the entire row. So I don't want to hide all rows in the table, just a select few.
Thanks,
-Jim
Yes indeed You can do it using normal css like the following fiddle demonstrates:
using the following two methods
table tr td
or
table tr
http://jsfiddle.net/qLynh5n1/
I do not quite understand what you're trying to do.
But can't you just use:
tr,
td {
css:css !important;
}
?
I understand you are try to select the parent of the span with class eField.
CSS has the descendant selector but not the other way around.
a > img { border: none; } - valid - all img directly under a
a < img { border: none; } - not valid - all a directly above img
Refer this link

How to apply table css to this structure?

I have this html markup:
<div class="entry-content"><table>
<thead>
<tr>
<th></th>
<th><strong>32nd</strong> </th>
<th> <strong>Decimal</strong></th>
</tr>
...
How I can specify to apply a set of table style specifically to the table enclosed in a entry-content class width?
I have tried this in my .css like this:
#entry-content {
.table {
But it does not work.
EDIT
It has to be very specific to this structure. i.e. the css must apply to only
.entry-content table
not
.entry-content p table
Otherwise the css may stuff up layouts that also uses table
Use this CSS selector.
.entry-content table{
//styles here
}
If you want to apply styles to tr and th use this selector
.entry-content table tr{
//some styles
}
.entry-content table th{
//some styles
}
Edit
The above selector selects any table (not just immediate child) that is inside the .entry-content, even if the table is inside of another div, which is inside the .entry-content.
In order to select an immediate child/table of the .entry-content use this selector:
.entry-content > table{
//some style
}

overwriting css class

Little trouble in using css, in the below code, i have used odd and even classes of css to change the background color. But whenever there is a backup(as highlighted) it should display the values as italics along with the odd and even properties. how to achieve this ?
<c:forEach items="${as.value.connections}" var="circuit" varStatus="elements">
<c:set var="stylesheetclass" value="primaryLine"/>
<c:set var="icon" value="/images/primary.png"/>
<c:if test="${circuit.backup}">
> <c:set var="stylesheetclass" value="backupLine"/>
>
>
> <c:set var="icon" value="/images/backup.png"/>
</c:if>
> <tr class="${elements.index % 2 == 0 ? 'odd' : 'even'}">
<td >
First of all if you make the functionality as if that a new class is added in the <tr> when backup value is present. For example if you add a class named backup then the <tr> would look like this <tr class="odd backup"> or <tr class="even backup">. After doing this functionality add the styles like below :-
tr.odd {
/* your styles already used*/
}
tr.even {
/* your styles already used*/
}
tr.backup {
font-style: italic; /* This would take effect for the <tr> which has backup class added */
}
Edit:
If you place the styles which you want to keep for the backup <tr>inside the .backup class your requirement would be achieved. And please keep the .backup class below the .even and .odd class. As styles written above gets overwritten with the below ones.
used this one !important
tr.odd{
background:red !important;
}
tr.even{
background:yellow !important;
}
Try doing it just with css selectors, you don't need to specify the classes in the DOM.
tr:nth-child(even) {background: #CCC}
tr:nth-child(odd) {background: #FFF}
and then just add the class of backup when you want.
Just make sure you define the .backup class after the even and odd rules, so nothing becomes overwritten.

Select table rows without table headers using css selector

Using css selector how am i supposed to write a selector so that only table rows that have data display cursor as pointer. I tried below but nope
table tbody tr:not(tr>th)
is this cross browser and works event in IE6?
That is not a valid CSS selector; you can't put combinators inside :not().
Your best bet is to put header rows (i.e. <tr> elements that contain <th> elements) inside a <thead>, leave the rest of the rows in your table's <tbody>, and simply use this:
table tbody tr {
cursor: pointer;
}
If you can't modify your HTML, it's not possible with CSS selectors alone, especially not with your requirement of supporting IE6. Use JavaScript instead; here's an obligatory jQuery example:
$('table tbody tr:not(:has(th))').css('cursor', 'pointer');
Assuming that your header row will always be the first row, you could do this:
table tr:not(:first-child) {
background:red;
}​
This selects all tr elements except the first-child (as in, the first of the matched elements).
This does not work in IE6 or any other version of IE except IE9.
But yes, if you do require IE6 support, Javascript must be used.
you can do it using <thead> tag.
In my case table looks like:
<table>
<thead> <tr><th>... </thead>
<tbody> <tr><td>... </tbody>
</table>
this will be applied for all tables:
tbody tr{
background: yellow;
}
this is only for tables which has header:
thead+tbody tr{
background: red;
}
Basically I tried solutions above but in my case I wrote something different to make it works. I don't know if it's better to use :hover or not for cursor.
table tbody>tr:hover {
cursor: pointer;
}
In my case table is coded like that
<table>
<thead>
<tr>...</tr>
....
</thead>
<tbody>
<tr>...</tr>
...
</table>
At all, I wanted to add style to entire line and not tr themselves

CSS different color for table each line

I have this table with the following CSS formatting:
<table cellspacing="2">
<tbody>
<tr>
<th>Name</th>
<th>Area</th>
</tr>
<tr>
<td>${it.conference}</td>
<td>${it.accepted}</td>
</tr>
</tbody>
</table>
And CSS:
table {
padding-left: 10px;
width:90%;
font-family:Arial, Helvetica, sans-serif;
font-size:11px;
text-align:left;
}
th, td {
padding:5px 10px;
}
th {
color:#666666;
border-top:2px solid #b7ddf2;
background-color:#ebf4fb;
}
How can i apply individual css modifications for each line (for example, I would like to change the color of 'Name', without messing up with the other lines formatting, which means, only modify that one. Is that possible to do?
Are you looking for something similar to the nth-child CSS pseudo-class?
If you want a more fine grain control over each individual one you might want to consider applying classes to them and styling them differently.
Edit: Here are a few examples of nth-child.
With a CSS only method you'll need to add some class to the line you would like to style, like this:
<table cellspacing="2">
<tbody><tr>
<th class="color1">Name</th>
<th>Area</th>
</tr>
<td>${it.conference}</td>
<td>${it.accepted}</td>
</tr></tbody>
</table>
and then style it:
.color1 {
background-color: (somecolor);
}
To style "Even" & "Odd" rows then use CSS3
like:
tr:nth-child(odd){
background:#999;}
tr:nth-child(even){
background:#f5f5f5;}
If you can get away without support for IE 7 and 8, you can do...
th:nth-of-type(1) {
color: #c00;
}
Otherwise, add a class such as th class="whatever" and then...
th.whatever {
color: #c00;
}
See a live demo:
http://jsfiddle.net/GFPgB/
If you want to apply CSS style based on the content of the element, that is not possible with CSS. If on the other hand you want to apply CSS styles based on their position, you can use the :nth-child(N) pseudo classes. For example
th:nth-child(1) /*for name*/
{
color: blue;
}
th:nth-child(2) /*for area*/
{
color: red;
}
apply a class to whatever element you want, and CSS style it. http://jsfiddle.net/robx/wzXAJ/
IE: apply <th class="name">Name</th>.
I know this is an old answer, but I made a fun example in Jquery, and maybe it will help somebody with their question.
JSFIDDLE
It'll get all <p> elements from the document and will loop through them, as jquery does that, it will add a CSS style to every <p> element on the page.

Resources