简体   繁体   English

造型父母<li>而不是孩子 CSS</li>

[英]Styling parent <li> and not children CSS

I am trying to make the title, that has it's own CSS class bold.我正在尝试制作标题,它有自己的 CSS class 粗体。 However, also the children are getting the style.然而,孩子们也得到了这种风格。 Code:代码:

   <ul class="product-categories">
    <li class="cat-item cat-parent">
<ul class="children">
    <li class="cat-item">...</li>
    <li class="cat-item">...</li>
    <li class="cat-item">...</li>
</ul>
</li>
    <li class="cat-item">...</li>
    <li class="cat-item">...</li>
    <li class="cat-item">...</li>
    </ul>

So i tried to add .cat-parent {font-weight: bold;} however, all children are getting bold too.所以我尝试添加.cat-parent {font-weight: bold;}然而,所有的孩子也变得粗体了。 Same if i tried adding :first-of-type .如果我尝试添加:first-of-type一样。

1: Why are the children getting the style, even though they don't have the css class "cat-parent" 2: What should I do to make it work? 1:为什么孩子们会得到这种风格,即使他们没有 css class "cat-parent" 2:我应该怎么做才能让它工作?

There's a couple ways to solve this.有几种方法可以解决这个问题。 You can add styling to the .children class by either doing font-weight: normal;您可以通过执行font-weight: normal; .children class 添加样式。 or even font-weight: initial;甚至font-weight: initial; . .

 .cat-parent { font-weight: bold; }.children { font-weight: normal; font-weight: initial; }
 <ul class="product-categories"> <li class="cat-item cat-parent">parent <ul class="children"> <li class="cat-item">...</li> <li class="cat-item">...</li> <li class="cat-item">...</li> </ul> </li> <li class="cat-item">...</li> <li class="cat-item">...</li> <li class="cat-item">...</li> </ul>

In CSS the child element gets the style of the parent element until a styling is specified for the child element.在 CSS 中,子元素获取父元素的样式,直到为子元素指定样式。 Example例子

 .parent{ color: red; }
 <div class='parent'> Parent <div class='child'>Hello<div> </div>

To handle this, you must specify a style for the child;要处理这个问题,您必须为孩子指定一个样式; see example below.请参见下面的示例。

 .parent{ color: red; }.child{ color: blue; }
 <div class='parent'> Parent <div class='child'> Child </div> </div>

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

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