处理CSS中的不可见孩子 [英] Dealing with invisible children in CSS

查看:103
本文介绍了处理CSS中的不可见孩子的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在处理容器对象的CSS。我有它大多工作。具体来说,我正在查看测试用例11和12.我想忽略隐形的孩子,当处理:first-child :last-child 。

I'm working on the CSS for a container object. I have it mostly working. Specifically I'm looking at test case 11 and 12. I would like to ignore invisible children when dealing with :first-child and :last-child.

使用:visible:first-child 我本来希望。和google失败了我...:visible是不是一个真正的选择器,它的一个jQuery的东西。

Using :visible:first-child doesn't seem to work as I would have expected. And google failed me... :visible isn't really a selector at all, its a jQuery thing.

有没有反正与CSS选择第一个/最后可见

Is there anyway with CSS to select the first/last visible child?

以下是程式码

code, p, quote {
  display: block;
  position: relative;
  margin: 0;
  padding: 1em;
  border: 1px solid black;
  box-sizing: border-box;
}

code {
  background-color: #ccc;
}

p {
  background-color: #0df;
}

quote {
  background-color: #fd0;
}

quote::after {
  display: table;
  clear: both;
  content: "";
}

.hidden {
  display: none;
}

.third {
  height: 100%;
  width: 33%;
  float: left;
  border: 1px solid black;
}

.container {
  display: block;
  position: relative;
  margin-right: 0;
  margin-left: 0;
  margin-top: 1.5em;
  margin-bottom: 1.5em;
  padding: 0.5rem;
  border: 1px solid black;
  box-sizing: border-box;
  border-radius: 10px;
  box-shadow: none;
}

.container > :first-child {
  border-top-left-radius: 10px;
  border-top-right-radius: 10px;
  border-bottom-left-radius: 0;
  border-bottom-right-radius: 0;
  border-width: 0;
  margin-top: -0.5rem;
  margin-bottom: -0.5rem;
  margin-left: -0.5rem;
  margin-right: -0.5rem;
}

.container > :last-child {
  border-top-left-radius: 0;
  border-top-right-radius: 0;
  border-bottom-left-radius: 10px;
  border-bottom-right-radius: 10px;
  border-bottom-width: 0;
  border-left-width: 0;
  border-right-width: 0;
  margin-top: 0.5rem;
  margin-bottom: -0.5rem;
  margin-left: -0.5rem;
  margin-right: -0.5rem;
}

.container > :not(:first-child):not(:last-child) {
  margin-left: -0.5rem;
  margin-right: -0.5rem;
  margin-bottom: -0.5rem;
  border-right-width: 0;
  border-left-width: 0;
  border-bottom-width: 0;
}

.container > :only-child {
  border-radius: 10px;
  border-width: 0;
  margin-top: -0.5rem;
  margin-bottom: -0.5rem;
  margin-left: -0.5rem;
  margin-right: -0.5rem;
}

<p>p</p>
<br />
<code>code</code>
<br />
<quote>quote</quote>

<div id="0" class="container">
  text
</div>

<div id="1" class="container">
  <p>first child</p>
  text
  <code>last child</code>
</div>

<div id="2" class="container">
  <p>first child</p>
  text
</div>

<div id="3" class="container">
  text
  <p>last child</p>
</div>

<div id="4" class="container">
  <p>first child</p>
  <code>last child</code>
</div>

<div id="5" class="container">
  <code>first child</code>
  <p>last child</p>
</div>

<div id="6" class="container">
  <code>first child</code>
  <code>last child</code>
</div>

<div id="7" class="container">
  <p>first child</p>
  <p>last child</p>
</div>

<div id="8" class="container">
  <code>only child</code>
</div>

<div id="9" class="container">
  <p>first child</p>
  <quote>middle child</quote>
  <quote>middle child</quote>
  <p>last child</p>
</div>

<div id="10" class="container">
  <quote>
    <div class="third">1</div>
    <div class="third">2</div>
    <div class="third">3</div>
  </quote>
</div>

<div id="11" class="container">
  <quote class="hidden">hidden child</quote>
  <p>first child</p>
  <p>last child</p>
</div>

<div id="12" class="container">
  <p>first child</p>
  <p>last child</p>
  <quote class="hidden">hidden child</quote>
</div>

推荐答案

没有:visible selector,但你可以使用一些类来选择隐藏的元素(如你已经做的那样),所以应该是这样: / p>

There is no :visible selector, but you may use some class to select hidden elements (as you already do), so this should look like this:

.hidden + :not(.hidden) {
    //code you want to set to first visible child goes here
}
.hidden + :not(.hidden) ~ :not(.hidden) {
    //code to reset stuff what you've set before
}

这篇关于处理CSS中的不可见孩子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆