首页 > 设计 > WEB开发 > 正文

CSS3 选择器

2019-11-02 18:21:19
字体:
来源:转载
供稿:网友

转自 http://www.Cuoxin.com.cn/CSSref/css_selectors.asp

在 CSS 中,选择器是一种模式,用于选择需要添加样式的元素。

“CSS” 列指示该属性是在哪个 CSS 版本中定义的(CSS1、CSS2 还是 CSS3)

选择器 例子 例子描述 CSS
.class .intro 选择class=”intro”的所有元素 1
id firstname 选择id=”firstname”的所有元素 1
* * 选择所有元素 2
element p 选择所有<p>元素 2
element,element div,p 选择所有<div>元素和所有<p>元素 1
element element div p 选择<div>元素内部的所有<p>元素 1
element>element div>p 选择父元素为<div>的所有<p>元素 2
element+element div+p 选择紧接在<div>元素之后的所有<p>元素 2
[attribute] [target] 选择带有target属性的所有元素 2
[attribute=value] [target=_blank] 选择target=”_blank”的所有元素 2
[attribute~=value] [title~=flower] 选择title属性包含单词”flower”的所有元素 2
[attribute|=value] [lang|=en] 选择lang属性值以”en”开头的所有元素 2
:link a:link 选择所有未被访问的链接 2
:visited a:visited 选择所有已被访问的链接 2
:active a:active 选择活动链接(鼠标按下未释放的状态) 2
:hover a:hover 选择鼠标指针位于其上的链接 2
:focus input:focus 选择获得焦点的<input>元素 2
:first-letter p:first-letter 选择每个<p>元素的首字母 1
:first-line p:first-line 选择每个<p>元素的首行 1
:first-child p:first-child 选择属于父元素的第一个子元素的每个<p>元素 2
:before p:before 在每个<p>元素的内容之前插入内容 2
:after p:after 在每个<p>元素的内容之后插入内容 2
:lang p:lang(it) 选择带有以”it”开头的lang属性值的每个<p>元素 2
element1~element2 p~ul 选择前面有<p>元素的每个<ul>元素 3
[attribute^=value] a[src^=”https”] 选择其src属性值以”https”打头的每个<a>元素 3
[attribute$=value] a[src$=”.pdf”] 选择其src属性以”.pdf”结尾的所有<a>元素 3
[attribute*=value] a[src*=”abc”] 选择其src属性中包含”abc”子串的每个<a>元素 3
:first-of-type p:first-of-type 选择属于其父元素的首个<p>元素的每个<p>元素 3
:last-of-type p:last-of-type 选择属于其父元素的最后<p>元素的每个<p>元素 3
:only-of-type p:only-of-type 选择属于其父元素唯一的<p>元素的每个<p>元素 3
:only-child p:only-child 选择属于其父元素唯一的<p>元素的每个<p>元素 3
:nth-child(n) p:nth-child(2) 选择属于其父元素的第二个子元素的每个<p>元素 3
:nth-last-child(n) p:nth-last-child(2) 选择属于其父元素的倒数第二个子元素的每个<p>元素 3
:nth-of-type(n) p:nth-of-type(2) 选择属于其父元素的第二个<p>元素的每个<p>元素 3
:nth-of-last-type(n) p:nth-of-last-type(n) 选择属于其父元素的倒数第二个<p>元素的每个<p>元素 3
:last-child p:last-child 选择属于其父元素最后一个子元素的每个<p>元素 3
:root :root 选择文档的根元素 3
:empty p:empty 选择没有子元素的每个<p>元素(包括文本节点) 3
:target news:target 选择当前活动的#news元素 3
:enabled input:enabled 选择每个启用的<input>元素 3
:disabled input:disabled 选择每个启用的<input>元素 3
:checked input:checked 选择每个被选中的<input>元素 3
:not(selector) :not(p) 选择非<p>元素的每个元素 3
::selection ::selection 选择被用户选取的元素部分 3

:before

<!DOCTYPE html><html><head> <style> p:before { content:"台词:"; } </style></head><body> <p>我是唐老鸭。</p> <p>我住在 Duckburg。</p> <p><b>注释:</b>对于在 IE8 中工作的 :before,必须声明 DOCTYPE。</p></body></html>

这里写图片描述


发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表