CSS, Selector, table - css

I have a HTML <table>.
I want to have columns 2 and 4 of that table be hidden by CSS. Is there a method to have CSS detect the column number of a td element?

Use jQuery if you can:
$("table td:nth-child(2)").addClass("col2");
$("table td:nth-child(4)").addClass("col4");
CSS:
.col2, .col4 { display: none }

Some CSS 3 answers have been given. A CSS 2-compatible solution would be the following, assuming you can identify the table somehow (pretend it has the class "foo"):
table.foo > tbody > tr > td:first-class + td, /* column 2 */
table.foo > tbody > tr > td:first-class + td + td + td /* column 4 */
{display: none;}
Note that this doesn't select th elements or headings in thead or tfoot. You could copy the two selectors to read "th", but if you can trust the integrity of your markup to not have anything but th and td inside a tr and tr only in thead, tfoot, or tbody (the only valid possibilities); you could do something like this:
table.foo > * > tr > :first-class + *, /* column 2 */
table.foo > * > tr > :first-class + * + * + * /* column 4 */
{display: none;}
This works fine in browsers newer than IE6, generally, which is almost always acceptable.
If IE6 support is mandatory -- and be utterly sure it is before bothering to go down this road -- a combination of valid CSS2/3 and Javascript in a conditional comment is the simplest solution (avoid using Javascript for layout when the job doesn't require it).

Add classes to the appropriate rows, and then you can use that to hide those rows.
There are nth-child selectors, but those will only work on more recent browsers.

Take a look at the nth-child and nth-of-type psudo classes.
Something like
td:nth-of-type(2) { visible: false; }
td:nth-of-type(4) { visible: false; }

You can do that with CSS 3 selectors - specifically :nth-child. Note that this solution won't work on most browsers at present (it basically only works properly in Firefox 3.1b and higher).
If you don't have access to change the classes, and you can't use JavaScript, then I'm afraid you're out of luck.

You can, but since that is CSS3, it won't work on IE and older versions of Firefox and some other browsers. Check out :nth-child.
IMHO, the best you could do is add a class to each row you want to target.

Tanks to everybody, I had to use jquery, I could not find how to do that from pure CSS2 , IE doesn't support CSS3 :(

Related

CSS: applying class to tbody cells (in the same column) based on header class

couldn't find anything so here's my Markup:
<style>
table {
width:300px;
border-collapse:collapse;
}
th.price
{
text-align:right;
background:yellow;
}
th, td
{
border:1px solid #aaa;
}
</style>
<table>
<thead><tr><th>Item</th><th class="price">Price</th></tr></thead>
<tbody>
<tr><td>Item1</td><td>12.30</td></tr>
<tr><td>Item2</td><td>23.40</td></tr>
<tr><td>Item2</td><td>45.60</td></tr>
</tbody>
</table>
https://jsfiddle.net/2b67rw5o/
Desired output:
So I don't want to apply .price to each table cell or use :nth-child or jQuery .. would it be possible with css only?
I don’t think you can apply a class to td elements based on the class applied to a th element, in css.
You don’t want to use jQuery, but you can use vanilla javascript:
const cssClass = "price";
const th = document.getElementsByClassName(cssClass)[0];
const thead = th.parentElement;
const idx = Array.prototype.indexOf.call(thead.children, th);
const tbody = th.parentElement.getElementsByTagName("tbody")[0];
Array.prototype.forEach(tbody.getElementsByTagName("tr"), tr => {
tr.children[idx].classList.add(cssClass)
})
I don't think what you want to do is possible in CSS today. Although it was often requested, you can't travel (at least now) over parents with CSS selectors because CSS cannot pass information upwards in the DOM hierarchy. But this specific feature would be the minimum requirement to determine the index of the children in the following rows that need to be styled.
For more on that see the answer of "Is there a CSS parent selector?", which is stating "There is currently no way to select the parent of an element in CSS. (...) That said, the Selectors Level 4 Working Draft includes a :has() pseudo-class that will provide this capability."
With the currently drafted :has() you could at least build a repetitive CSS solution with a finite column count like this:
/* For a column of three columns maximum: */
/* if price is first column */
table:has(thead > th.price:first-child) tbody > td:first-child,
/* if price is second column */
table:has(thead > :first-child+th.price) tbody > :first-child+td,
/* if price is third column */
table:has(thead > :first-child+*+th.price) tbody > :first-child+*+td {
...
}
Crappy, I know... but currently the only native CSS solution in a possible foreseeable future.
But for now depending on what you need, you could also "cheat": If the background and/or border of the column should be changed you can use styling of the th header cell only (e.g. by abusing :before and :after). But text content specific changes would be quite impossible without JavaScript.

Style the first <td> column of a table differently

If I have a table with two columns, how do I specify a padding or any other css so that it is applied just for the first column of <td>s. Also how do I style an n-th column similarly?
You could use the n-th child selector.
to target the nth element you could then use:
td:nth-child(n) {
/* your stuff here */
}
(where n starts at 1)
The :nth-child() and :nth-of-type() pseudo-classes allows you to select elements with a formula.
The syntax is :nth-child(an+b), where you replace a and b by numbers of your choice.
For instance, :nth-child(3n+1) selects the 1st, 4th, 7th etc. child.
td:nth-child(3n+1) {
/* your stuff here */
}
:nth-of-type() works the same, except that it only considers element of the given type ( in the example).
For more information about nth-child
https://developer.mozilla.org/es/docs/Web/CSS/:nth-child
If you've to support IE7, a more compatible solution is:
/* only the cells with no cell before (aka the first one) */
td {
padding-left: 20px;
}
/* only the cells with at least one cell before (aka all except the first one) */
td + td {
padding-left: 0;
}
Also works fine with li; general sibling selector ~ may be more suitable with mixed elements like a heading h1 followed by paragraphs AND a subheading and then again other paragraphs.
This should help. Its CSS3 :first-child where you should say that the first tr of the table you would like to style. http://reference.sitepoint.com/css/pseudoclass-firstchild
To select the first column of a table you can use this syntax
tr td:nth-child(1n + 2){
padding-left: 10px;
}

CSS3: Reset alternate table row coloring after a certain class of tr

Here's my fiddle.
http://jsfiddle.net/cnLDF/
I want every .content row after each .heading row to start with gray, in this case, "content 5".
How do I do it by modifying the CSS only.
Using the adjacent sibling selector (i used red color for demo purposes):
table tr.heading + tr.content {
background: #f00;
}
But note that the nth-child(even) will win, then you will colour only the first .content, preceeded by an .header, that is odd (and not even).
Demo: http://jsfiddle.net/cnLDF/1/
EDIT:
I've tried a bit, and I don't think it's possible with pure CSS (but i'll be happy to be proven wrong): pseudo-elements will always refer to all tr elements in the table (with odd and even), and the last declaration will override the previous... the only way with CSS only imho is to create a static declaration for every case with adjacent selector after every heading, that is doable only if you have a small, known, fixed number of content for every heading (let's say 5, or 10...), like this:
table tr.heading + tr.content > td {/* stuff */ }
table tr.heading + tr.content + tr.content > td {/* stuff */ }
table tr.heading + tr.content + tr.content + tr.content > td {/* stuff */ }
/* and so on... */
If instead you could handle the input, you could easily calculate the output serverside, applying "odd" and "even" classes to trs, and coloring them accordingly.

CSS collect similar properties?

How can I "collect" CSS statements? I have several th classes that I want to align the same like table.t-data-grid thead tr th.*
How can I collect eg the following?
table.t-data-grid thead tr th.depot, table.t-data-grid thead tr th.amount ... {
}
Do you really need that complex a selector? The less complex your selector is, the better is its performance. So just put in just as much as is necessary to target the correct elements.
It looks like, you would be better of with just
.t-data-grid .depot, .t-data-grid .amount { ... }

advanced :hover deeper nested

I don't know exactly how I can describe this? I think its better if you look at the jsfiddle I have made..
As you can see there is a hover on some TR elements and if the TD already has another bgcoler it has to change to an alternative bgcolor..
It works fine in the first 3 rows, but if there is nested a new table deeper in the DOM the green TD's in the new table does always have the :hover class
jsfiddle
http://jsfiddle.net/VvZuV/1/
Change this:
tr:hover td.green, tr.deep:hover td.green {
background:#7bcf81;
}
To this:
tr:hover > td.green, tr.deep:hover > td.green {
background:#7bcf81;
}
No new class needed.
http://jsfiddle.net/rCztp/
Explanation
As soon as you hovered over the <tr> that contained the <table>, all children, grand-children, and etc, were affected by your css rule. Using > means that only children will be affected.

Resources