Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Select all except first and last td element in one selector

I wrote something like this in CSS:

tr.red > td:not(:last-of-type):not(:first-of-type)
{
    color: #E53B2C;
    border-bottom: 4px solid #E53B2C;
}

I'm trying to apply this to table cells, which are not first and not last in the row with .red class.

It seems to work as expected, but is this really the right way to do it?

like image 919
Kamil Avatar asked Nov 10 '12 04:11

Kamil


People also ask

How to select all items except the first in a list?

The trick is very easy, in CSS we have the sibling selector ("+"), if i will make selector that choose "li + li" it will select all list-items except the first . That's all! I am using it all the time, for unwanted last/first separator. Note: Works in all Browsers (Internet Explorer 7 And Above).

How to select all the child elements except the last element?

Sometimes, you may need to select all the child elements except the last element. It’s quite easy to do this using the :notand :last-childpseudo-classes. The :not pseudo-class specifies elements that do not match a list of selectors and the :last-child selects the element if it is the last child among the other elements.

How to select all HTML elements except the first with CSS?

Learn how to select all HTML elements except the first with the CSS `:not (:first-child) selector. If you add the following rule-set to your CSS stylesheet, every h2 element on your entire website will get a 64px top margin.

When to use the “not” selector?

By using the :not (:first-child) selector, you remove that problem. You can use this selector on every HTML element. Another common use case is if you use an unordered list <ul> for your menu.


3 Answers

May be this would help

tr.red  td:not(:first-child):not(:last-child)
{
//your styles
}

Sample

like image 179
Priyank Patel Avatar answered Oct 07 '22 04:10

Priyank Patel


As some have mentioned, td:first-child selects the first child element of the parent TR if it is a TD, whereas td:first-of-type selects the first TD regardless of any TH that may come before it.

As a result, if you have any TH elements in your rows, your code won't give you all the cells that aren't first or last.

tr.red > *:not(:last-child):not(:first-child)
    { /* ... your css code ... */ }

I think it should work well this way. Even if you wanted to have separate code for TH and TD, you could do it like th:not(:first-child) and td:not(:first-child).

Typically, :first-of-type is more intuitive when you want to style the first instance of a tag name, (like p:first-of-type).

like image 41
Vlasec Avatar answered Oct 07 '22 05:10

Vlasec


It is syntactically correct, as you can quickly check using the W3C CSS Validator. The validator is known to have errors, so in principle, you should check the rule against CSS specifications, especially Selectors Level 3. The result is still that yes, it is correct.

It also has the desired meaning, since this is how selectors combine. You can use :not(...) selectors to express a condition of type “not ... and not ...”.

This applies provided that all children of tr elements are td. If there are header cells, i.e. th elements as well, then the selector applies to those data cells (td elements) that are not the first data cell or the last data cell in a row with .red class.

like image 4
Jukka K. Korpela Avatar answered Oct 07 '22 06:10

Jukka K. Korpela