Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CSS selectors ul li a {...} vs ul > li > a {...}

  1. What is the difference between ul > li > a {...} and ul li a {...} in CSS?
  2. Which one is more efficient and why?
like image 252
Inquisitive Avatar asked Jun 27 '12 13:06

Inquisitive


People also ask

What is UL LI A in CSS?

The <ul> tag defines an unordered (bulleted) list. Use the <ul> tag together with the <li> tag to create unordered lists. Tip: Use CSS to style lists. Tip: For ordered lists, use the <ol> tag.

What are the 3 different kinds of selectors in CSS?

Simple selectors (select elements based on name, id, class) Combinator selectors (select elements based on a specific relationship between them) Pseudo-class selectors (select elements based on a certain state)

Which selector should be used in order to only select the Li inside of the UL?

The selector “ ol li ” will select any <li> that is inside an <ol> element but not other list items.

What is the difference between Li and UL?

The <li> HTML element is used to represent an item in a list. It must be contained in a parent element: an ordered list ( <ol> ), an unordered list ( <ul> ), or a menu ( <menu> ). In menus and unordered lists, list items are usually displayed using bullet points.


2 Answers

">" is the child selector

"" is the descendant selector

The difference is that a descendant can be a child of the element, or a child of a child of the element or a child of a child of a child ad inifinitum.

A child element is simply one that is directly contained within the parent element:

<foo> <!-- parent -->   <bar> <!-- child of foo, descendant of foo -->     <baz> <!-- descendant of foo -->     </baz>   </bar> </foo> 

for this example, foo * would match <bar> and <baz>, whereas foo > * would only match <bar>.

As for your second question:

Which one is more efficient and why?

I'm not actually going to answer this question as it's completely irrelevant to development. CSS rendering engines are so fast that there is almost never* a reason to optimize CSS selectors beyond making them as short as possible.

Instead of worrying about micro-optimizations, focus on writing selectors that make sense for the case at hand. I often use > selectors when styling nested lists, because it's important to distinguish which level of the list is being styled.

* if it genuinely is an issue in rendering the page, you've probably got too many elements on the page, or too much CSS. Then you'll have to run some tests to see what the actual issue is.

like image 184
zzzzBov Avatar answered Oct 11 '22 11:10

zzzzBov


ul>li selects all li that are a direct child of ul whereas ul li selects all li that are anywhere within (descending as deep as you like) a ul

For HTML:

<ul>   <li><span><a href='#'>Something</a></span></li>   <li><a href='#'>or Other</a></li> </ul> 

And CSS:

li a{ color: green; } li>a{ color: red; } 

The colour of Something will remain green but or Other will be red

Part 2, you should write the rule to be appropriate to the situation, I think the speed difference would be incredibly small, and probably overshadowed by the extra characters involved in writing more code, and definitely overshadowed by the time taken by the developer to think about it.

However, as a rule of thumb, the more specific you are with your rules, the faster the CSS engines can locate the DOM elements you want to apply it to, so I expect li>a is faster than li a as the DOM search can be cut short earlier. It also means that nested anchors are not styled with that rule, is that what you want? <~~ much more pertinent question.

like image 40
Billy Moon Avatar answered Oct 11 '22 10:10

Billy Moon