CSS - 如何定位第二个子列表项?

时间:2014-11-19 20:07:41

标签: css css-selectors

我无法使用CSS儿童组合子选择器<li>定位<ul> <li>的另一个>的孩子。

我需要为这些列表项添加list_style_type:SomethingElse,为所有其他列表添加list-style-type: none;

我已使用以下内容删除列表样式:

.snap-drawer ul {
    padding: 0;
    margin: 0;
    list-style-type: none;
}

以下内容没有改变:

.snap-drawer ul > li {
    list-style-type: disc !important;
}

HTML:

<div class="snap-drawer">
    <ul>
        <li class="li-item">...</li>
        <li class="li-item">...</li>
        <li class="li-item">
            <ul>
                <li class="li-item">...</li> <==== Target all these only
                <li class="li-item">...</li> <==== Target all these only
            </ul>
        </li>
    </ul>
</div>

2 个答案:

答案 0 :(得分:3)

选项1

您要删除padding,然后bullet不可见。

同时定位ulli项。尝试:

.snap-drawer ul > li > ul {
    padding-left:25px;
    list-style-type: disc !important;
}

.snap-drawer ul {
  padding: 0;
  margin: 0;
  list-style-type: none;
}
.snap-drawer ul > li > ul {
  padding-left: 25px;
  list-style-type: disc !important;
}
<div class="snap-drawer">
    <ul>
        <li class="li-item">...</li>
        <li class="li-item">...</li>
        <li class="li-item">
            <ul>
                <li class="li-item">...</li> <==== Target all these only
                <li class="li-item">...</li> <==== Target all these only
            </ul>
        </li>
    </ul>
</div>


选项2

您也可以尝试>选择器仅用于第一个ul

.snap-drawer > ul {
    padding: 0;
    margin: 0;
    list-style-type: none;
}

.snap-drawer > ul {
    padding: 0;
    margin: 0;
    list-style-type: none;
}
<div class="snap-drawer">
    <ul>
        <li class="li-item">...</li>
        <li class="li-item">...</li>
        <li class="li-item">
            <ul>
                <li class="li-item">...</li> <==== Target all these only
                <li class="li-item">...</li> <==== Target all these only
            </ul>
        </li>
    </ul>
</div>

答案 1 :(得分:0)

你需要的是:

.snap-drawer ul ul > li {
    list-style-type: disc !important;
}

或更简单:

.snap-drawer ul ul li {
    list-style-type: disc;
}