0

I'm not sure if my question title accurately displays what I'm trying to ask, but this is pretty much my first exposure to CSS3 and have been exploring various projects people have done in order to gain some hands on experience.

In searching through these projects I cam across something I'm having some trouble understanding. What exactly is the difference between the following two lines:

#random_ID > ul > li > a { ... }

#random_ID ul li a { ... }

Are these just two ways of writing the same thing? Any help would be greatly appreciated!

Shawn Taylor
  • 1,434
  • 5
  • 26
  • 36

2 Answers2

3

The greater than symbol limits the lookup to just first-level descendants: children of the selector on the left. Without the symbol, it can be any descendent at any level.

So the first example, it's "random_ID with a child ul with a child li with a child a" and the second is "random_ID with any descendant ul with any descendent li with any descendent a"

Matt
  • 10,434
  • 1
  • 36
  • 45
1

> means "direct child". This will only style it if the element (one on the right of >) is a direct child of the parent (one on the left of >)

So say if I have the following layout:

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

div > li { background: red; } Would not work, because li is not a direct child of it, whereas div > ul > li> or div li would work.

It should be noted that not every browser supports the direct child tag, specifically older versions of internet explorer, so don't rely on it, or have some fallback if you do use it.

Andy
  • 14,427
  • 3
  • 52
  • 76
  • It's 2012. Unless you're in a stubborn enterprise, [you generally don't have to worry about browser support](http://stackoverflow.com/questions/10543780/how-reliable-is/10543791#10543791). – BoltClock Nov 08 '12 at 17:37
  • @BoltClock I appreciate that, I'm sure whoever uses that information can decide whether they need to support IE6 or not, so was merely mentioning it so they are aware. A significant amount of my work is for one customer who refuses to upgrade past IE6. – Andy Nov 08 '12 at 17:39