How do I add :hover on the nth child of an element using CSS2 selectors?

Saahir Foux

I'm trying to give each menu li a unique background color on hover, and I want to do it using css2.

<ul>
    <li>
    <li>
    <li>
    <li>
</ul>

I was able to add a background onto the first element, using the following:

nav ul > li:hover:first-child a { background-color:red !important; }

When I add a different background onto the 2ndelement, the hover effect for the next element appears only when I place my cursor over the first element.

This is my code so far:

nav ul > li:hover:first-child a { background-color:red !important; }
nav ul > li:hover:first-child + li a { background-color:blue !important; }

Is there a proper way to do this with CSS2?

I'm tried to get some insight into this Boltclock's answer in the link below.

How do I get the nth child of an element using CSS2 selectors?

Any help would be greatly appreciated.

BoltClock

The nth child is represented by the last element in the + chain (before the combinator immediately following it, if any), so you would attach the :hover selector to that element like so:

nav ul > li:hover:first-child a { background-color:red !important; }
nav ul > li:first-child + li:hover a { background-color:blue !important; }

If you wish to make this clearer for the first child, you can reorder the pseudo-classes, placing :hover at the end (i.e. after :first-child):

nav ul > li:first-child:hover a { background-color:red !important; }
nav ul > li:first-child + li:hover a { background-color:blue !important; }

Lastly, as others have mentioned, you should remove the !importants unless you have a specific reason to use them.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How do I add :hover on the nth child of an element using CSS2 selectors?

From Dev

How do I select the nth child of an element?

From Dev

How do I select the nth child of an element?

From Dev

How to get different class selectors using nth-child in css

From Dev

How do I keep a CSS hover rule applied when the mouse moves to the child element?

From Dev

How to find the child element on hover using jQuery?

From Dev

How do i add an element to body using angular2

From Dev

How do I get the nth element of a Seq?

From Dev

Using CSS selectors to get first child from a nested element

From Dev

Using CSS or jQ, how can I show this section element on :hover?

From Dev

How do I add a child object to parent using REST APIs?

From Dev

javascript how do i use the hover element on a div-element using a script with namespace?

From Dev

How do I get the Nth largest element from a list with duplicates, using LINQ?

From Dev

How do I get the Nth largest element from a list with duplicates, using LINQ?

From Dev

How can I select an element by an attribute set on its parent using CSS 2.0 selectors?

From Dev

How can I select an element by an attribute set on its parent using CSS 2.0 selectors?

From Dev

How often should I use child/sibling selectors in CSS?

From Dev

CSS nth-child(odd): hover when hover:not used

From Dev

Why aren't these CSS nth-child selectors working as expected?

From Dev

CSS first child hover with a element

From Dev

Advanced element selection using nth-child()

From Dev

In Fortran, how do I remove Nth element from an array?

From Dev

How to do CSS nth-child in span tags?

From Dev

How do I access the child element of a Frame?

From Dev

How do I style the content in a child element?

From Dev

CSS Selectors with Nth Childs

From Dev

Can not select child element using JQuery/CSS :nth-child selector

From Dev

How can I drop nth element of a list using foldl?

From Dev

How can I use the child selectors here?

Related Related

  1. 1

    How do I add :hover on the nth child of an element using CSS2 selectors?

  2. 2

    How do I select the nth child of an element?

  3. 3

    How do I select the nth child of an element?

  4. 4

    How to get different class selectors using nth-child in css

  5. 5

    How do I keep a CSS hover rule applied when the mouse moves to the child element?

  6. 6

    How to find the child element on hover using jQuery?

  7. 7

    How do i add an element to body using angular2

  8. 8

    How do I get the nth element of a Seq?

  9. 9

    Using CSS selectors to get first child from a nested element

  10. 10

    Using CSS or jQ, how can I show this section element on :hover?

  11. 11

    How do I add a child object to parent using REST APIs?

  12. 12

    javascript how do i use the hover element on a div-element using a script with namespace?

  13. 13

    How do I get the Nth largest element from a list with duplicates, using LINQ?

  14. 14

    How do I get the Nth largest element from a list with duplicates, using LINQ?

  15. 15

    How can I select an element by an attribute set on its parent using CSS 2.0 selectors?

  16. 16

    How can I select an element by an attribute set on its parent using CSS 2.0 selectors?

  17. 17

    How often should I use child/sibling selectors in CSS?

  18. 18

    CSS nth-child(odd): hover when hover:not used

  19. 19

    Why aren't these CSS nth-child selectors working as expected?

  20. 20

    CSS first child hover with a element

  21. 21

    Advanced element selection using nth-child()

  22. 22

    In Fortran, how do I remove Nth element from an array?

  23. 23

    How to do CSS nth-child in span tags?

  24. 24

    How do I access the child element of a Frame?

  25. 25

    How do I style the content in a child element?

  26. 26

    CSS Selectors with Nth Childs

  27. 27

    Can not select child element using JQuery/CSS :nth-child selector

  28. 28

    How can I drop nth element of a list using foldl?

  29. 29

    How can I use the child selectors here?

HotTag

Archive