Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to combine :first-child and :hover?

Tags:

html

css

I have an unordered list I'm using for a menu. Each item has a background image and a :hover image. The background image on the first element is different that the rest, so I use the following to style it, which works fine:

#prodNavBar ul:last-child li:first-child {...} 

Since I want a roll-over image on this element as well, I've tried adding :hover, like so:

#prodNavBar ul:last-child li:first-child:hover {...} 

...but this doesn't work. What's the syntax to combine :first-child and :hover?

like image 904
Diodeus - James MacFarlane Avatar asked Nov 25 '09 21:11

Diodeus - James MacFarlane


People also ask

How do I use first child and last child in CSS?

The :first-child pseudo class means "if this element is the first child of its parent". :last-child means "if this element is the last child of its parent". Note that only element nodes (HTML tags) count, these pseudo-classes ignore text nodes. This is the div id="test".

How do I not include my first child in CSS?

Use the :not(selector) Selector Not to Select the First Child in CSS. We can use the :not(selector) selector to select every other element that is not the selected element. So, we can use the selector not to select the first child in CSS.

What does first child do in CSS?

The :first-child selector allows you to target the first element immediately inside another element. 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 content.


2 Answers

Chaining :first-child and :hover as you do here should work just fine. If you're using IE6, however, only the last pseudo-class in the chain will be recognized.

In other words, you're doing it right.

like image 174
stephenhay Avatar answered Sep 20 '22 14:09

stephenhay


li:first-child:hover should work. Which browser are you testing with? IE doesn't support last-child

Here is a sample test case.

like image 41
Chetan S Avatar answered Sep 20 '22 14:09

Chetan S