微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

CSS选择器时:目标为空

当:target等于元素的id(easy)或者:target为空(不可能?)时,我需要一个css3选择器来定位元素.这很难解释,所以让我举一个简单的例子.
div {
  background: blue;
}
div:target,div:no-target {
  background: red;
}

但当然:no-target伪类不存在;).有没有办法解决这个问题而不使用Javascript?提前致谢!

解决方法

叹.我觉得我正在复活一个死的话题,但它需要一个真正的答案.

仅使用CSS就可以做到这一点,只需使用:last-child和general sibling combinator,形式为:target~:last-child:

.pages > .page:target ~ .page:last-child,.pages > .page {
    display: none;
}

/* :last-child works,but for some reason .page:last-child will not */
.pages > :last-child,.pages > .page:target {
    display: block;
}

(live example)

编辑:显然这与an older,previously mentioned,related post中接受的答案非常相似.

原文地址:https://www.jb51.cc/css/217473.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。