简体   繁体   English

CSS规则未按预期应用

[英]CSS rule not applied as expected

I have the below code. 我有以下代码。 The first 'ul' tag with id 'selected-plays' has 3 child 'li' tags (not descendants). id为'selected-plays'的第一个'ul'标签有3个子'li'标签(不是后代)。 I am trying to apply few CSS rules to these child tags. 我正在尝试将少量CSS规则应用于这些子标记。

My jQuery code adds the class 'horizontal'. 我的jQuery代码添加了“水平”类。 Notice that the selector points to only the child tags of the parent element with id #selected-plays 请注意,选择器仅指向id为#selected-plays的父元素的子标记

 $(document).ready(function() { $('#selected-plays > li').addClass('horizontal'); }); 
 .horizontal { margin: 10px; float: left; list-style: none; color: red; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script> <ul id="selected-plays"> <li>Comedies <ul> <li><a href="/asyoulikeit/">As You Like It</a></li> <li>All's Well That Ends Well</li> <li>A Midsummer Night's Dream</li> <li>Twelfth Night</li> </ul> </li> <li>Tragedies <ul> <li><a href="hamlet.pdf">Hamlet</a></li> <li>Macbeth</li> <li>Romeo and Juliet</li> </ul> </li> <li>Histories <ul> <li>Henry IV (<a href="mailto:henryiv@king.co.uk">email</a>) <ul> <li>Part I</li> <li>Part II</li> www.it-ebooks.info Chapter 2 [ 29 ] </ul> <li><a href="http://www.shakespeare.co.uk/henryv.htm"> Henry V</a></li> <li>Richard II</li> </ul> </li> </ul> 

The first 3 properties (margin, float, list-style) are applied to the three child 'li' tags as expected but the last property ie, color is applied to all the elements (descendants) within the parent element. 前3个属性(margin,float,list-style)按预期应用于三个子'li'标签,但最后一个属性即颜色应用于父元素中的所有元素(后代)。 Why is this happening? 为什么会这样?

This is expected behaviour as child elements will automatically inherit the color setting of their parent. 这是预期的行为,因为子元素将自动继承其父级的color设置。 If you do not want this behaviour you can set the color of the children in CSS: 如果您不想要这种行为,可以在CSS中设置子项的颜色:

#selected-plays li li {
  color: initial;
}

 $(document).ready(function() { $('#selected-plays > li').addClass('horizontal'); }); 
 .horizontal { margin: 10px; float: left; list-style: none; color: red; } #selected-plays li li { color: initial; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script> <ul id="selected-plays"> <li>Comedies <ul> <li><a href="/asyoulikeit/">As You Like It</a></li> <li>All's Well That Ends Well</li> <li>A Midsummer Night's Dream</li> <li>Twelfth Night</li> </ul> </li> <li>Tragedies <ul> <li><a href="hamlet.pdf">Hamlet</a></li> <li>Macbeth</li> <li>Romeo and Juliet</li> </ul> </li> <li>Histories <ul> <li>Henry IV (<a href="mailto:henryiv@king.co.uk">email</a>) <ul> <li>Part I</li> <li>Part II</li> www.it-ebooks.info Chapter 2 [ 29 ] </ul> <li><a href="http://www.shakespeare.co.uk/henryv.htm"> Henry V</a></li> <li>Richard II</li> </ul> </li> </ul> 

Inherited properties 继承的属性

FROM MDN 来自MDN

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. 只有文档的根元素才能获得属性摘要中给出的初始值。 A typical example of an inherited property is the color property. 继承属性的典型示例是color属性。

You can simply add color:initial style to the child elements to preserve the styles. 您可以简单地添加color:initial样式到子元素以保留样式。 The selector can be pre-specified in css or can be added via script as well. 选择器可以在css中预先指定,也可以通过脚本添加。 You can have different selectors based on the structure eg li > * , li > li or li li , etc. 您可以根据结构选择不同的选择器,例如li > *li > lili li等。

The child <li> tags are inheriting the color value from the parent <li> tags that have the .horizontal class. <li>标签继承了具有.horizontal类的父<li>标签的颜色值。 You can try over-riding the child tag color by adding a style rule such as: 您可以通过添加样式规则来尝试覆盖子标记颜色,例如:

.horizontal li { color: initial; }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM