Select all 'tr' except the first one

CssCss Selectors

Css Problem Overview


How can I select all tr elements except the first tr in a table with CSS?

I tried using this method, but found that it did not work.

Css Solutions


Solution 1 - Css

By adding a class to either the first tr or the subsequent trs. There is no crossbrowser way of selecting the rows you want with CSS alone.

However, if you don't care about Internet Explorer 6, 7 or 8:

tr:not(:first-child) {
    color: red;
}

Solution 2 - Css

I'm surprised nobody mentioned the use of sibling combinators, which are supported by IE7 and later:

tr + tr /* CSS2, adjacent sibling */
tr ~ tr /* CSS3, general sibling */

They both function in exactly the same way (in the context of HTML tables anyway) as:

tr:not(:first-child)

Solution 3 - Css

ideal solution but not supported in IE

tr:not(:first-child) {css}

second solution would be to style all tr's and then override with css for first-child:

tr {css}
tr:first-child {override css above}

Solution 4 - Css

sounds like the 'first line' you're talking of is your table-header - so you realy should think of using thead and tbody in your markup (click here) which would result in 'better' markup (semantically correct, useful for things like screenreaders) and easier, cross-browser-friendly possibilitys for css-selection (table thead ... { ... })

Solution 5 - Css

Another option:

tr:nth-child(n + 2) {
    /* properties */
}

Solution 6 - Css

Though the question has a decent answer already, I just want to stress that the :first-child tag goes on the item type that represents the children.

For example, in the code:

<div id"someDiv">
     <input id="someInput1" /> 
     <input id="someInput2" />
     <input id="someInput2" />
</div

If you want to affect only the second two elements with a margin, but not the first, you would do:

#someDiv > input {
     margin-top: 20px;
}
#someDiv > input:first-child{
     margin-top: 0px;
}

that is, since the inputs are the children, you would place first-child on the input portion of the selector.

Solution 7 - Css

Sorry I know this is old but why not style all tr elements the way you want all except the first and the use the psuedo class :first-child where you revoke what you specified for all tr elements.

Better descriped by this example:

http://jsfiddle.net/DWTr7/1/

tr {
    border-top: 1px solid;
}
tr:first-child {
    border-top: none;   
}

/Patrik

Solution 8 - Css

Since tr:not(:first-child) is not supported by IE 6, 7, 8. You can use the help of jQuery. You may find it here

Solution 9 - Css

You could also use a pseudo class selector in your CSS like this:

.desc:not(:first-child) {
    display: none;
}

That will not apply the class to the first element with the class .desc.

Here's a JSFiddle with an example: http://jsfiddle.net/YYTFT/, and this is a good source to explain pseudo class selectors: http://css-tricks.com/pseudo-class-selectors/

Solution 10 - Css

You could create a class and use the class when you define all of your future 's that you want (or don't want) to be selected by the CSS.

This would be done by writing

<tr class="unselected">

and then in your css having the lines (and using the text-align command as an example) :

unselected {
  text-align:center;
}



selected {
  text-align:right;
}

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionSteven SpielbergView Question on Stackoverflow
Solution 1 - CssMagnarView Answer on Stackoverflow
Solution 2 - CssBoltClockView Answer on Stackoverflow
Solution 3 - CssMark StegglesView Answer on Stackoverflow
Solution 4 - CssoeziView Answer on Stackoverflow
Solution 5 - CssArman YeghiazaryanView Answer on Stackoverflow
Solution 6 - CssCodyBugsteinView Answer on Stackoverflow
Solution 7 - CssPatrikView Answer on Stackoverflow
Solution 8 - CssTaposView Answer on Stackoverflow
Solution 9 - CssJamesView Answer on Stackoverflow
Solution 10 - Cssuser476033View Answer on Stackoverflow