23

Possible Duplicate:
What does “>” mean in CSS rules?

I came across many many websites and I saw many of them use this type of notation in their css file for creating navigation bar like :

#navigation ul li > ul {
  /* some code in between */
}

but when i omit the > sign as

#navigation ul li ul {
  /* some code in between */
}

this still works the same way.

what is the difference and when to use > sign ?

Community
  • 1
  • 1
monk
  • 4,727
  • 13
  • 45
  • 67

8 Answers8

46

> Means the direct child of a selector, so

li > a will ONLY match an <a> which is directly inside an <li> for example.

If the html was <li><span><a> the <a> would not be matched.

Removing the > will match any <a> nested inside an <li>, irrespective of other things around it, so li a would match the <a> in

<li><a> but also in <li><span><a>, for example.

Here's more information on direct Child selectors: https://developer.mozilla.org/en-US/docs/Web/CSS/Child_selectors

Steve Schrab
  • 375
  • 5
  • 15
dougajmcdonald
  • 19,231
  • 12
  • 56
  • 89
  • 8
    That's a very bad example as it's invalid markup (`p` cannot be a child of `ul`). It's so bad, in fact, that the selector `ul > li` **will** match the `li` in your example in Firefox. – BoltClock Jan 09 '12 at 10:52
  • 5
    Yeah I wasn't thinking with the example in terms of valid, it was just to prove a point and avoid using many nested li's / ul's for simplicities sake – dougajmcdonald Jan 09 '12 at 11:48
7

The > means a child element - it is the child selector. That is, directly / immediately nested after.

So, in your first example, the ul at the end of the selector must be directly descending from the li.

Oded
  • 489,969
  • 99
  • 883
  • 1,009
3

The "li > ul" syntax specifies that the ul must be a child of li. "li ul" instead says that the the styled ul is descendant of li, no matter how many levels below.

BoltClock
  • 700,868
  • 160
  • 1,392
  • 1,356
Aldo Stracquadanio
  • 6,167
  • 1
  • 23
  • 34
2
selector[1] > selector[2]{ 
[property]: value 
}

This is called the child selector. In browsers that support it, it applies the styles to selector2 child elements of selector1.

Edit:
The second one you use I believe is called the Descendant selectors.

They should work identically, but it's there's a little difference. The decendant selector will apply to ALL decendants ,whereas the child selector applies only to the direct children of the parent.

T23
  • 582
  • 2
  • 11
1

You would use > when you want to target a direct descendant.

For example, .foo > .bar would target .bar only if it is the direct child, while .foo .bar would target any descendant of .foo that has the class .bar.

Nix
  • 5,746
  • 4
  • 30
  • 51
0

> is to be used when the second operand/element is a child of the first operand/element. When it's omitted; descendants are matched, which includes children.


Therefore, if your HTML is structured as you suggested (#navigation ul li ul) then if you have the following in your CSS:

#navigation ul {color:red;}

Then both #navigation ul AND #navigation ul li ul will be coloured red (the text) as they BOTH are descendants of #navigation ul.

But if you had the following in your CSS:

#navigation > ul {color:red;}

Then only #navigation ul would be coloured red as it is the only ul which is a direct child of #navigation

Adam Lynch
  • 3,341
  • 5
  • 36
  • 66
0

> is the child selector.

It will only select immediate children of the previous element. If it there is a

#navigation ul li ul li ul

element it will not be affected by the

#navigation ul li > ul

selector. But the

#navigation ul li ul

will be.


EDIT: @Nix is right, but he isn't telling the whole truth it seems. *Why isn't the p-enclosed ul ignored but only the span-enclosed? display: block vs inline perhaps? Who knows?

Community
  • 1
  • 1
joar
  • 15,077
  • 1
  • 29
  • 54
  • This is not exactly true in this instance, as in this example using `>` it will target any `
      ` that is a direct descendant of `
    • ` — even if it is buried in ten other `ul li`'s. It just means it will not target `li span ul`. – Nix Jan 09 '12 at 10:52
  • Regarding your edit, @Nix *is* telling the whole truth. The behavior you're seeing is caused by the fact that the opening `
      ` tag happens to close the opening `

      ` tag just before it. This makes the second nested `ul` really a child of the `li` above it, as well as the adjacent sibling of that `p`. It has nothing to do with `display: block` vs `inline`.

    – BoltClock Jan 09 '12 at 14:44
  • This does not happen with the `` tag because the closing tag is required, unlike `

    `, so the DOM is constructed with the `ul` being actually inside the `span`.

    – BoltClock Jan 09 '12 at 14:50
  • @BoltClock, It has to do with the tag type. XHTML( strict)? would not close the ´

    ` tag without complaining, is that correct?

    – joar Jan 09 '12 at 15:13
  • 1
    @jwandborg: Yes, so if you serve XHTML (and not just with the strict XHTML doctype), the `
      ` will be contained within the `

      ` in XHTML-compliant browsers. The implicit closing tag rule is part of HTML, not XHTML. (This is where things get complicated and confusing :)

    – BoltClock Jan 09 '12 at 15:20