我试图隐藏块 .container 内具有类 .row 的前 3 个元素.
I am trying to hide the first 3 elements having the class .row inside the block .container.
我正在做的是首先隐藏所有 .row,然后我尝试使用 .row 显示前 3 个 .row:nth-child(-n+3)
What I'm doing is hiding all the .row first, and then I am trying to display the first 3 .row by using .row:nth-child(-n+3)
jsfiddle 在这里:http://jsfiddle.net/z8fMr/1/
jsfiddle here: http://jsfiddle.net/z8fMr/1/
.row {
display: none;
}
.row:nth-child(-n+3) {
display: block;
}
<div class="content">
<div class="notarow">I'm not a row and I must remain visible</div>
<div class="row">Row 1</div>
<div class="row">Row 2</div>
<div class="row">Row 3</div>
<div class="row">Row 4</div>
<div class="row">Row 5</div>
<div class="row">Row 6</div>
</div>
我这里有两个问题:
.row 然后隐藏所有其他 .row ?.row and then hide all the other .row ?谢谢.
您有一个 .notarow 作为第一个孩子,因此您必须在 :nth-child() 公式中考虑这一点.由于那个 .notarow,你的第一个 .row 成为了父级的第二个孩子,所以你必须从第二个到第四个开始计数:
You have a .notarow as the first child, so you have to account for that in your :nth-child() formula. Because of that .notarow, your first .row becomes the second child overall of the parent, so you have to count starting from the second to the fourth:
.row:nth-child(-n+4) {
display: block;
}
更新小提琴
.row {
display: none;
}
.row:nth-child(-n+4) {
display: block;
}
<div class="content">
<div class="notarow">I'm not a row and I must remain visible</div>
<div class="row">Row 1</div>
<div class="row">Row 2</div>
<div class="row">Row 3</div>
<div class="row">Row 4</div>
<div class="row">Row 5</div>
<div class="row">Row 6</div>
</div>
你做的很好.
这篇关于如何在css中显示块的前N个元素并隐藏其他元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!
每隔三个元素设置样式?Style every third element?(每隔三个元素设置样式?)
为什么我不应该在 CSS 中使用 ID 选择器?Why shouldn#39;t I use ID selectors in CSS?(为什么我不应该在 CSS 中使用 ID 选择器?)
CSS 中的 img[class*=“align"] 是什么意思?What does img[class*=quot;alignquot;] mean in CSS?(CSS 中的 img[class*=“align] 是什么意思?)
CSS:最后一个元素CSS: Last element on line(CSS:最后一个元素)
如何仅使用 CSS(无 js)选择所有其他 div 类元素How do I select every other div class element using just CSS (no js)(如何仅使用 CSS(无 js)选择所有其他 div 类元素)
检查未使用的 CSS 选择器的工具?Tool for checking unused CSS selectors?(检查未使用的 CSS 选择器的工具?)