简体   繁体   English

定位ol li ol li ol li,使用CSS嵌套列表

[英]Targeting ol li ol li ol li, nested list with css

I have this html 我有这个HTML

<div class="multiple">
<ol>
    <li>Ordered List Style</li>
    <li>Here’s another, they shouldn’t be too terribly long, but might wrap a line or three
    <ol>
        <li>nested</li>
        <li>nested</li>
          <ol>
             <li>nested</li>
             <li>nested</li>
          </ol>
    </ol>
    </li>
    <li>Let’s talk about the benefits of this here product!</li>
    <li>More feature talk! This thing is super useful, you should totally buy it!</li>
</ol>

<ol>
    <li>Ordered List Style</li>
    <li>Here’s another, they shouldn’t be too terribly long, but might wrap a line</li>
    <li>Let’s talk about the benefits of this here product!</li>
</ol>

and the below css, I can not seem to target anything below ol li ol li, my styles for ol li ol li get applied to all nested lists under it. 而在下面的CSS中,我似乎无法定位到ol li ol li以下的任何对象,我的ol li ol li样式将应用于其下的所有嵌套列表。 The last rule shown below never gets applied 下面显示的最后一条规则永远不会应用

ol li { list-style-type: upper-roman; }
ol li ol li { list-style-type: upper-alpha; }
ol li ol li ol li { list-style-type: decimal; }

Your html is bad, you need to put the ol in the <li> 您的html不好,您需要将ol放入<li>

<ol>
    <li>Ordered List Style</li>
    <li>Here’s another, they shouldn’t be too terribly long, but might wrap a line or three
    <ol>
        <li>nested</li>
        <li>nested
          <ol>
             <li>nested</li>
             <li>nested</li>
          </ol>
        </li>
    </ol>
    </li>
    <li>Let’s talk about the benefits of this here product!</li>
    <li>More feature talk! This thing is super useful, you should totally buy it!</li>
</ol>

Try to use child selector > instead of space. 尝试使用子选择器>代替空格。

ol li { list-style-type: upper-roman; }
ol li > ol li { list-style-type: upper-alpha; }
ol li > ol li > ol li { list-style-type: decimal; }

A space between selectors will get all matching elements below it, no matter how far down in the tree, so your ol li selector will select all li elements, at all three levels. 选择器之间的空格将使所有匹配的元素都位于其下方,无论在树中到底有多远,因此ol li选择器将在所有三个级别上选择所有 li元素。

Use the > selector to get direct children. 使用>选择器可以获取直接子级。

You also need to anchor them to something at the top level, say a div . 您还需要将它们锚定在顶层,例如div

This should work for you: 这应该为您工作:

div>ol>li { list-style-type: upper-roman; }
div>ol>li>ol>li { list-style-type: upper-alpha; }
div>ol>li>ol>li>ol>li { list-style-type: decimal; }

There is a fault in your HTML. 您的HTML中有错误。 Below is an example of a better way. 下面是一个更好方法的示例。

<ol>
  <li>Ordered List Style
  <ol>
   <li>First nest
   <li>First nest
    <ol>
     <li>Second nest
     <li>Second nest
      <ol>
         <li>Inline
         <ol>
           <li>More nests
         </ol>
      </ol>
    </ol>
   </ol>
</ol>

It will look like this: 它看起来像这样:

  1. Ordered List Style 有序列表样式
    1. First nest 第一窝
    2. First nest 第一窝
      1. Second nest 第二窝
      2. Second nest 第二窝
        1. Inline 排队
          1. More nests 更多巢

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

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