Select table rows without table headers using css selector - css

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

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

Targeting odd even style to specific table with id

I have a few tables of which I would only like to target the even and odd rows:
<table class="record">
<tbody>
<tr>
<th scope="col">Score</th>
<th scope="col">Time</th>
</tr>
<tr>
<td>A</td>
<td>1.20</td>
</tr>
<tr>
<td>B</td>
<td>1.35</td>
</tr>
<tr>
<td>C</td>
<td>1.39</td>
</tr>
</tbody>
</table>
I tried to use the following code which I found online, but it works but on all of the tables around the site:
tr:nth-child(even) { background: #666; }
tr:nth-child(odd) { background: #CCC; }
Any suggestions how can I target on tables only with a class of "record" ?
Thank you for your suggestions.
Edit:
And what if this tables is under another table as td? :)
First things first: your question title implies/states that you want to use an id, whereas in your question-code you're using a class, to select the relevant table element. These are not equivalent; an element may have only one id, but multiple classes. That said, to use a class, the posted answer has you covered.
If, on the other hand, you want to use an id (as stated in your title), then replace .record with #idOfTable (and remember to pass an id to your HTML: <table id="idOfTable"><!-- other stuff --></table>).
You can, of course, combine an id with a class selector:
Just pass the ancestor as part of the selector:
.record tr:nth-child(even) {background: #666;}
.record tr:nth-child(odd) {background: #CCC;}
JS Fiddle demo.
On the grounds you may only want this to work within the tbody, you can also pass that as part of the selector:
.record tbody tr:nth-child(even) {background: #666;}
.record tbody tr:nth-child(odd) {background: #CCC;}
JS Fiddle demo.
You can, of course, combine an id with a class selector:
#idOfTable.classNameOfTable {
/* CSS */
}
Just add table.record before the CSS code you posted like so:
table.record tr:nth-child(even) { background: #666; }
table.record tr:nth-child(odd) { background: #CCC; }
The table part is to delimit this only to tables as there might be other elements with the class record and you don't this to interfere with them.
The .record just specifies the class (<elem>.<class> is the syntax and <elem> is not necessary).
And the nesting is simple to understand too: it looks for matching elements within the outer elements. Here is the relevant W3S documentation.

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.

Conditional alternative table row styles in HTML5

Is there any changes regarding this questions Conditional alternative table row styles since HTML5 came in?
Here is a copy of original question:
Is it possible to style alternate table rows without defining classes on alternate tags?
With the following table, can CSS define alternate row styles WITHOUT having to give the alternate rows the class "row1/row2"? row1 can be default, so row2 is the issue.
<style>
.altTable td { }
.altTable .row2 td { background-color: #EEE; }
</style>
<table class="altTable">
<thead><tr><td></td></tr></thead>
<tbody>
<tr><td></td></tr>
<tr class="row2"><td></td></tr>
<tr><td></td></tr>
<tr class="row2"><td></td></tr>
</tbody>
</table>
CSS3 supports the "nth-child" property on the tr tag. Something like the following should work:
tr:nth-child(odd) { background-color: white; }
tr:nth-child(even) { background-color: green; }
Jacob R's solution to your original posting still applies.

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

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.

Resources