简体   繁体   English

如何跳过第n个子元素?

[英]How to skip an element in nth-child?

I want to skip counting on an element in nth-child with :not attribute, but it does not work or it's not even possible to do. 我想跳过对带有:not属性的nth-child中的一个元素的计数,但是它不起作用或者甚至无法执行。 Is there any other way to do this? 还有其他方法吗?

Here is the code: 这是代码:

 .layout { float: left; width: 100px; height: 60px; border: 2px solid red; margin-right: 10px; margin-bottom: 10px; } .hidden { display: none; } .layout:not(.hidden):nth-child(2n+1) { clear: both; } 
 <div class="layout"></div> <div class="layout"></div> <div class="layout hidden"></div> <div class="layout"></div> <div class="layout"></div> <div class="layout"></div> <div class="layout"></div> <div class="layout"></div> 

Even though the element with class .hidden is not visible, it's still counted with nth-child 即使类.hidden的元素不可见,仍将其计为nth-child

I don't think the :nth-child can be applied to the result of evaluation of :not . 我认为:nth-child不能应用于:not的评估结果。 But I suggest you a walkaround to get the same effect: 但我建议您先行一步以达到相同的效果:

  1. Wrap the "layout" DIVs within a container DIV: 将“布局” DIV包装在container DIV中:

  <div id="container"> <div class="layout"></div> <div class="layout"></div> <div class="layout hidden"></div> <div class="layout"></div> </div> 

  1. CSS: CSS:

 .layout { /*float: left;*/ display: inline-table; width: 100px; height: 60px; border: 2px solid red; margin-right: 10px; margin-bottom: 10px; } div#container { width: 232px; /* See the note below */ } div.layout:first-child { display: block; clear: both; } .hidden { display: none; } /* .layout:not(.hidden):nth-child(2n+1) { clear: both; } */ 

To calculate accurately the container's exact width, you should first strip off the newlines between the layout DIVs, because they add an arbitrary amount of space when being rendered. 为了精确计算容器的确切宽度,您应该首先去除layout DIV之间的换行符,因为它们在渲染时会添加任意数量的空间。

HTML 的HTML

<div class="content">
<div class="layout"></div>
<div class="layout"></div>
<div class="layout hidden"></div>
<div class="layout"></div>
<div class="layout"></div>
<div class="layout"></div>
<div class="layout"></div>
<div class="layout"></div>
</div>

CSS 的CSS

.content{
  display: flex;
  flex-wrap: wrap;
  width: 224px
}
.layout {
  width: 100px;
  height: 60px;
  border: 2px solid red;
  margin-right: 10px;
  margin-bottom: 10px;
  flex: 1 0 30%;
}

.hidden {
  display:none;
}

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

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