.block-grid > li { display: block; height: auto; float: left; }
Can you explain what the ">" is doing in the CSS above?
| SitePoint Sponsor |





.block-grid > li { display: block; height: auto; float: left; }
Can you explain what the ">" is doing in the CSS above?

In CSS terms, > means "direct child of".
If you just had .block-grid li {...} then any <li> that was a descendant of any element with class="block-grid" would take those styles, eg
So how do you target only the first and second one? You quite possibly don't want the same styles applied to the sub-list and sub-sub-list. That's where the > selector comes in. Because any of the <li>s that start say "and this one" are not direct children of <li class="block-grid">, they're grandchildren or great-grandchildren. So by using the direct child selector, you ensure that you don't hit other elements nested inside.HTML Code:<ul class="block-grid"> <li>This first one</li> <li>This second one</li> <ul><li>and this one</li> <li>and this one <ul><li>and this one</li></ul> </li></ul></li></ul>
Any posts I write in Arial are on my mobile phone, so please excuse typos etc.
Any posts I write in Verdana are on a PC, so feel free to berate me mercilessly for any mistakes





Very clear explanation. Thank you!





I tested this:
onCode:ul>li { text-decoration: blink; }
and ALL of the li elements blink! That can't be right.Code:<ul> <li>testing ul>li text-decoration (chosen)</li> <li> <ol> <li>testing ul>li text-decoration (not chosen)</li> <li>testing ul>li text-decoration (not chosen)</li> </ol> </li> </ul>

The problem there is that some properties are inherited by their children. A lot of formatting is like that. If you have
the text in the <p> will be red (unless it's overridden by CSS unseen), even though there is nothing in the CSS that says paragraphs have red text.HTML Code:<div class="color:red;"> <p>Some text here</p> </div>
A better way to demonstrate it would be to use font-size:2em;. If you include the > you should see that all the list items are double the size of regular text. But if you leave the > out, the 'not chosen' items will be quadruple the size of normal text (because they are doubled and doubled again as they are contained within two <li>s).
Any posts I write in Arial are on my mobile phone, so please excuse typos etc.
Any posts I write in Verdana are on a PC, so feel free to berate me mercilessly for any mistakes





Then I'm not sure exactly when > will apply in CSS. How do I use it and it NOT affect the other li's?

Any posts I write in Arial are on my mobile phone, so please excuse typos etc.
Any posts I write in Verdana are on a PC, so feel free to berate me mercilessly for any mistakes





So the example given of using ul>li { text-decoration: blink; } to modify
was a poor example of the usage of >. So exactly how would > be used to make a specific line in a code example of your choosing be styled text-decoration:line-through? I'd still like to see a full example. I am just going deeper into CSS theory and testing them for myself to gain more understanding and appreciation.Code:<ul> <li>testing ul>li text-decoration (chosen)</li> <li> <ol> <li>testing ul>li text-decoration (not chosen)</li> <li>testing ul>li text-decoration (not chosen)</li> </ol> </li> </ul>

Here's another example.
Like Steve said, text properties like font-size / color are always inherited by their children elements so you can't ever apply those styles to one element and not have their children inherit them. You need to override the children styles if you want them to be different.HTML Code:<!DOCTYPE html> <html> <head> <style> li > ul { display: none } li:hover > ul { display: block } </style> <body> <ul> <li><a href="home.html">Main</a> <ul> <li><a href="home.html">Sub1</a> <ul> <li><a href="home.html">Sub2</a></li> </ul> </li> </ul> </li> </ul> </body> </html>





Mark,
That was a helpful example. Thank you! So this tag just doesn't work well in isolation. I get it now.
Bookmarks