Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why :first-child select all children?

I want to select only the very first links from "dropdown menu" (the ones with "ONE" text), but :first-child selects them all.

Link:

http://jsfiddle.net/773Xd/1/

Sorry for the mess in the HTML part, but I'm customizing Wordpress theme and it produces so many classes and ids.

The most important thing is at the end of CSS file.

like image 929
fomicz Avatar asked Nov 17 '10 00:11

fomicz


People also ask

What selector to use if we want to select the first child of its parent that is of its type?

The :first-child selector is used to select the specified selector, only if it is the first child of its parent.

What is the difference between first child and first-of-type?

The :first-child: The :first-child selector is used to select those elements which are the first-child elements. For :first-child selector the <! DOCTYPE> must be declared for IE8 and earlier versions. The :first-of-type: The :first-of-type Selector is used to targeting the first child of every element of it's parent.

What is the difference between Div first child and Div first child what will each select?

Using :first-child is very similar to :first-of-type but with one critical difference: it is less specific. :first-child will only try to match the immediate first child of a parent element, while first-of-type will match the first occurrence of a specified element, even if it doesn't come absolutely first in the HTML.


1 Answers

Change your CSS selector to this...

#page-navigation ul li .sub-menu li:first-child a

(i.e. put pseudo class :first-child on the li, not the a).

See it on jsfiddle.net

The old selector didn't work because a is always the first child of the li elements.

But in the new selector, the li is the first child of the ul elements.

like image 145
alex Avatar answered Oct 15 '22 16:10

alex