Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hover not working with nth-child

He I am exploring the features of CSS 3 and I walked into some trouble:

For a table I have made this CSS:

table.sortable tbody tr td {
    border-bottom:1px solid;
    height: 20px;
}

table.sortable tbody tr:hover {
    background-color:#BCD2E5 !important;
}

table.sortable tbody tr:nth-child(odd) td {
    background-color: #F3FAFF;
}
table.sortable tbody tr:nth-child(even) td {
    background-color: #FFFFFF;
}

table.new{
    background-color: rgb(255, 255, 187);
}

table.reaction{
    background-color: rgb(255, 128, 64);
}

table.send{
    background-color: rgba(154, 211, 109, 0.470588);
}

The problem is that the hover is not working but if I comment the nth-child selector out it does actually work. Also in some cases I have to give some rows different background color's. This is for the users so they can see the status of some stuf really easy. So if I assign a class like class="send" to a row it has to get the background color form the class send.

Why is this not working out?! or did I miss something!?

like image 569
botenvouwer Avatar asked Apr 24 '13 13:04

botenvouwer


People also ask

How do you target the nth child in SCSS?

You can pass in a positive number as an argument to :nth-child() , which will select the one element whose index inside its parent matches the argument of :nth-child() . For example, li:nth-child(3) will select the list item with an index value 3; that is, it will select the third list item.

How do you use the nth of a child?

Definition and UsageThe :nth-child(n) selector matches every element that is the nth child of its parent. n can be a number, a keyword (odd or even), or a formula (like an + b). Tip: Look at the :nth-of-type() selector to select the element that is the nth child, of the same type (tag name), of its parent.

What is the nth child () selector used for?

The :nth-child selector allows you to select one or more elements based on their source order, according to a formula. It is defined in the CSS Selectors Level 3 spec as a “structural pseudo-class”, meaning it is used to style content based on its relationship with parent and sibling elements.

How do I enable hover in HTML?

The :hover selector is used to select elements when you mouse over them. Tip: The :hover selector can be used on all elements, not only on links. Tip: Use the :link selector to style links to unvisited pages, the :visited selector to style links to visited pages, and the :active selector to style the active link.


1 Answers

You are applying the background-color for the nth-child rows to the td. The background-color on the td is showing above the background-color of the tr.

Changing your CSS to the following worked for me (remove the td from the end nth-child selectors):

table.sortable tbody tr:hover {
    background-color:#BCD2E5 !important;
}

table.sortable tbody tr:nth-child(odd) {
    background-color: #F3FAFF;
}
table.sortable tbody tr:nth-child(even) {
    background-color: #FFFFFF;
}

OR

Add td to the end of your hover selector, like so:

table.sortable tbody tr:hover td {
    background-color:#BCD2E5 !important;
}

See this codepen: http://codepen.io/keithwyland/pen/woLmh


ALSO

If you move the hover selector after the nth-child selectors in the CSS, then you shouldn't need the !important. So, like this:

table.sortable tbody tr:nth-child(odd) {
    background-color: #F3FAFF;
}
table.sortable tbody tr:nth-child(even) {
    background-color: #FFFFFF;
}

table.sortable tbody tr:hover {
    background-color:#BCD2E5;
}
like image 146
keithwyland Avatar answered Sep 27 '22 23:09

keithwyland