Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Properties applying to children although using direct descendant selector ">"

Tags:

css

I am using .class > ul > li.

Basically I would like to know why there are some properties that work as expected (like border) but others as text-transform, color or font-size affect the list inside the list.

Here's an example: FIDDLE

I find really annoying the need to use .class ul li ul li to "undo" the text style that I want just on the first direct <li> child.

Any links with docs that would allow me to learn how this selector really works will be greatly apreciated.

like image 942
Alvaro Menéndez Avatar asked Mar 16 '15 15:03

Alvaro Menéndez


People also ask

What is the difference between descendant and child selectors?

Descendant Selector :The descendant Selector selects all child elements of the selected parent element. In this example, the descendant selector selects all li of selected ul elements. 2. Direct Child Selector : This selector selects only the direct child of the selected element.

Are descendant selectors more specific than child selectors?

One important note is that a child selector is going to be faster than descendant selector, which can have a visible affect on pages with 1000's of DOM elements.

What is the purpose of descendant selectors?

The descendant selector is a way to select elements that are located somewhere underneath other elements, according to the tree structure of the webpage. This selector is actually multiple selectors combined together, but separated by a space.

Which selector will give all direct children?

The second selector above is a child combinator selector. This means it will only select list items that are direct children of an unordered list. In otherwords, it only looks one level down the markup structure, no deeper.


2 Answers

This is the normal behaviour it is called inheritance.
Some css properties are inherited from their parent whereas other aren't.

Inheritance

When no value for an inherited property has been specified on an element, the element gets the computed value of that property on its parent element. Only the root element of the document gets the initial value given in the property's summary. [MDN]

List of inherited properties

azimuth             border-collapse    border-spacing        caption-side
color               cursor             direction             elevation
empty-cells         font-family        font-size             font-style
font-variant        font-weight        font                  letter-spacing
line-height         list-style-image   list-style-position   list-style-type
list-style          orphans            pitch-range           pitch
quotes              richness           speak-header          speak-numeral
speak-punctuation   speak              speech-rate           stress
text-align          text-indent        text-transform        visibility
voice-family        volume             white-space           widows
word-spacing

Extracted from w3.org on 17/03/15


The all property

This property allows to control CSS inheritance with the initial | inherit | unset values :

The CSS all shorthand property resets all properties, apart from unicode-bidi and direction, to their initial or inherited value. [MDN]

This property currently has the status of "Candidate Recommendation" but the major modern borwsers (except safari) have implemented it.


For your example case :

The border property doesn't inherit so the child elements won't have a border event though the parent does.

But font-size, color and text-transform are properties that inherit to the child elements. Therefore the second level list item has the same font-size, color and text-transform as it's parent.

Conclusion :
The selector you are using is correct and targets only the first level list items but some properties like color are inherited to the second level list items.

like image 186
web-tiki Avatar answered Nov 16 '22 09:11

web-tiki


As you did not define style for your inner ul will have the same style as its parent.

What you have to do is wrap the text in a span

<div class="menu ">
  <ul>
    <li><span>This is uppercase red</span>
        <ul>
            <li>this should not be uppercase or red</li>
            <li>this should not be uppercase or red</li>
            <li>this should not be uppercase or red</li>                
        </ul>
    </li>
  </ul>
</div>

And apply your css definition to .menu>ul>li>span

like image 42
Adam Avatar answered Nov 16 '22 08:11

Adam