CSS选择具有特定类的第一个元素 [英] CSS select first element with a certain class

查看:202
本文介绍了CSS选择具有特定类的第一个元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

选择具有特定类的第一个元素的语法是什么?请指定该选择方法是CSS3还是CSS2.1的一部分。

解决方案

如果您需要第一个元素其兄弟姐妹类别中,您可以使用

  .myclass {
/ * styles第一个* /
}

.myclass〜.myclass {
/ *其他样式(必须取消第一条规则的样式)* /
}

不要尝试使用 .myclass:not(.myclass〜 .myclass)只能在一个规则中执行此操作,因为:not()只接受括号中的简单选择器。 / p>

如果你想在整个文档中使用第一个 .myclass ,就没有办法单独使用CSS。



:nth-​​of-type():nth-​​child



浏览器支持兄弟选择器(〜)。

:IE7 +和所有其他。


What is the syntax for selecting the first element with a certain class? Please specify whether that method of selection is part of CSS3 or CSS2.1.

解决方案

If you need the first element with a certain class among its siblings, you can use

.myclass {
    /* styles of the first one */
}

.myclass ~ .myclass {
    /* styles of the others (must cancel the styles of the first rule) */
}

Don't try to use .myclass:not(.myclass ~ .myclass) to do this in only one rule, it won't work since :not() only accepts simple selectors in the parentheses.

If you want the first .myclass in the whole document, there is no way to do it with CSS alone.

The :nth-of-type() or :nth-child() approaches posted are wrong, even if they coincidentally happen to match the elements you want in your page.

Browser support of sibling selector (~): IE7+ and all others.

这篇关于CSS选择具有特定类的第一个元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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