我遇到了一些CSS wordpres样式。我想对这一段进行设计,但我怎样才能选择前两个" li" (功能,featured2)?
<div class="categories-wrp" data-type="cliparts">
<ul class="smooth">
<li data-act"item" data-id="{featured}"></li>
<li data-act"item" data-id="{featured2}"></li>
<li data-act"item" data-id="110"></li>
<li data-act"item" data-id="111"></li>
</ul>
</div>
感谢您的帮助!
答案 0 :(得分:2)
两个选项:1。first-child
和nth-child(2)
。 2.按属性选择器[data-id="{featured}"]
li {
display: block;
height: 5px;
margin-bottom: 1px;
background-color: #ddd;
}
.o1 li:first-child,
.o1 li:nth-child(2) {
background-color: blue;
}
.o2 li[data-id="{featured}"],
.o2 li[data-id="{featured2}"] {
background-color: blue;
}
<div class="categories-wrp o1" data-type="cliparts">
<ul class="smooth">
<li data-act"item" data-id="{featured}"></li>
<li data-act"item" data-id="{featured2}"></li>
<li data-act"item" data-id="110"></li>
<li data-act"item" data-id="111"></li>
</ul>
</div>
<div class="categories-wrp o2" data-type="cliparts">
<ul class="smooth">
<li data-act"item" data-id="{featured}"></li>
<li data-act"item" data-id="110"></li>
<li data-act"item" data-id="{featured2}"></li>
<li data-act"item" data-id="111"></li>
</ul>
</div>
答案 1 :(得分:1)
您可以使用nth-child
:
ul.smooth li:nth-child(1),
ul.smooth li:nth-child(2) {
color:red;
}
&#13;
<div class="categories-wrp" data-type="cliparts">
<ul class="smooth">
<li data-act"item" data-id="{featured}">Test</li>
<li data-act"item" data-id="{featured2}">Test</li>
<li data-act"item" data-id="110">Test</li>
<li data-act"item" data-id="111">Test</li>
</ul>
</div>
&#13;
如果您要格式化所有精选<li>
元素,您还可以使用attribute selectors使用以下解决方案:
ul.smooth li[data-id*="featured"] {
color:red;
}
&#13;
<div class="categories-wrp" data-type="cliparts">
<ul class="smooth">
<li data-act"item" data-id="{featured}">Test</li>
<li data-act"item" data-id="{featured2}">Test</li>
<li data-act"item" data-id="110">Test</li>
<li data-act"item" data-id="111">Test</li>
</ul>
</div>
<div class="categories-wrp" data-type="cliparts">
<ul class="smooth">
<li data-act"item" data-id="{featured}">Test</li>
<li data-act"item" data-id="110">Test</li>
<li data-act"item" data-id="{featured2}">Test</li>
<li data-act"item" data-id="{featured3}">Test</li>
<li data-act"item" data-id="111">Test</li>
</ul>
</div>
&#13;
注意:仅当{featured}
和{featured2}
未被模板引擎(如Smarty或Blade)替换时,此功能才有效。
答案 2 :(得分:0)