热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

如何跳过n-child中的元素?-Howtoskipanelementinnth-child?

Iwanttoskipcountingonanelementinnth-childwith:notattribute,butitdoesnotworkorits

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. Is there any other way to do this?

我想跳过对nth-child中的元素的依赖:not attribute,但它不起作用或者甚至不可能。有没有其他方法可以做到这一点?

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;
}

Even though the element with class .hidden is not visible, it's still counted with nth-child

即使具有类.hidden的元素不可见,它仍然与nth-child一起计算

2 个解决方案

#1


0  

I don't think the :nth-child can be applied to the result of evaluation of :not. But I suggest you a walkaround to get the same effect:

我不认为:nth-​​child可以应用于评估结果:not。但我建议你走一走来获得同样的效果:

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

    

  1. CSS:
  2. 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.

要准确计算容器的精确宽度,首先应该去除布局DIV之间的换行符,因为它们在渲染时会添加任意数量的空间。

#2


-1  

HTML

HTML

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;
}

推荐阅读
author-avatar
fo切為祢
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有