简体   繁体   English

使用css为父列表设置样式

[英]Styling the parent list with css

For the below list, i want to style the parent list only like ( first parent ) i tried to use : 对于下面的列表,我想样式父列表只像(第一个父)我试图使用:

  ul < li {
    background-color: yellow;
  }

put this doesn't work , how can i do that ? 把这个不起作用,我怎么能这样做?

    <ul>
  <li>first parent <!-- style this -->
    <ul>
      <li>second</li>
      <li>second</li>
      <li>second</li>
      <li>second</li>
      <li>second</li>
    </ul>
  </li>
  <li>first parent</li> <!-- style this -->
  <li>first parent</li> <!-- style this -->
  <li>first parent</li> <!-- style this -->
  <li>first parent</li> <!-- style this -->
</ul>

You can't target a parent in CSS. 您无法在CSS中定位父级。 You should just add a class to it and use it that way. 你应该只添加一个类并以这种方式使用它。

<li class="highlight">

- -

.highlight {
    background-color: yellow;
}

Or, designate that specific li by it's position in its parent. 或者,通过它在父级中的位置指定特定的li。

li:nth-child(5) { /* assuming it's the 5th <li> in that list */
    background-color: yellow;
}

Let's be clear here, just in case someone is finding this from a search engine: there are no parent selectors in CSS , not even in CSS3. 让我们在这里清楚,以防万一有人从搜索引擎中发现这一点: CSS中没有父选择器 ,甚至在CSS3中也没有

check this tutorial . 查看本教程

Here is the discuss about it: Is there a CSS parent selector? 以下是对它的讨论: 是否有CSS父选择器?

Hmm, you could use a class. 嗯,你可以用一堂课。

Or style both like this: 或者样式都是这样的:

ul li { background-color: yellow; }
ul li ul li { background-color: none; }

Id probably use a class 我可能会使用一个班级

you need to apply a class to identify your "first level" like this : 你需要申请一个类来识别你的“第一级”,如下所示:

<ul class="firstLevel">
  <li>first parent <!-- style this -->
    <ul>
      <li>second</li>
      <li>second</li>
      <li>second</li>
      <li>second</li>
      <li>second</li>
    </ul>
  </li>
  <li>first parent</li> <!-- style this -->
  <li>first parent</li> <!-- style this -->
  <li>first parent</li> <!-- style this -->
  <li>first parent</li> <!-- style this -->
</ul>

Then apply css : 然后申请css:

/* ">" means only first level childs "li" will be concerned */
ul.firstLevel > li { 
  background-color: yellow;
}

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

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