CSS选择器、优先级和常用的选择器

优先级(由高到低):

1. id选择器(#myid

2. 类选择器(.myclassname

3. 标签选择器(div,h1,p

4. 相邻选择器(h1+p

5. 子选择器(ul < li

6. 后代选择器(li a

7. 通配符选择器(*

8. 属性选择器(a[rel="external"]

9. 伪类选择器(a:hover,li:nth-child

上面九种选择器中ID选择器的效率是最高,而伪类选择器的效率则是最低。

element>element 选择器

定义父元素是 <div> 元素的每个 <p> 元素的样式。

注释:如果元素不是父元素的直接子元素,则不会被选择。

<!DOCTYPE html>
<html>
<head>
<style>
div > p {
width: 100px;
height: 50px;
line-height: 50px;
border-bottom: 1px solid #000;
}
</style>
</head>

<body>

<div>
  <p>1</p>
  <a><p>2</p></a>
</div>

</body>
</html>

element+element 选择器

定义div同一层级,并且为与div下面的p元素的样式。

 1 <!DOCTYPE html>
 2 <html>
 3 <head>
 4 <style>
 5 div + p {
 6 width: 100px;
 7 height: 50px;
 8 line-height: 50px;
 9 border-bottom: 1px solid #000;
10 }
11 </style>
12 </head>
13
14 <body>
15
16 <p>1</p><!-- 失败 -->
17 <div></div>
18 <p>2</p><!-- 成功 -->
19 <p>3</p><!-- 失败 -->
20 <a><p>4</p></a><!-- 失败 -->
21
22 </body>
23 </html>

CSS [attribute] 选择器

定义p中有class标签的样式

 1 <!DOCTYPE html>
 2 <html>
 3 <head>
 4 <style>
 5 p[class]{
 6   border-bottom: 1px solid #000;
 7 }
 8 </style>
 9 </head>
10
11 <body>
12
13 <p class="class1">1</p><!-- 失败 -->
14 <p>2</p><!-- 成功 -->
15 <p class="class3">3</p><!-- 失败 -->
16
17 </body>
18 </html>

CSS [attribute=value] 选择器

定义了p中class等于class1的元素样式

 1 <!DOCTYPE html>
 2 <html>
 3 <head>
 4 <style>
 5 p[class="class1"]{
 6   border-bottom: 1px solid #000;
 7 }
 8 </style>
 9 </head>
10
11 <body>
12
13 <p class="class1">1</p><!-- 成功 -->
14 <p>2</p><!-- 失败 -->
15 <p class="class3">3</p><!-- 失败 -->
16
17 </body>
18 </html>

CSS [attribute~=value] 选择器

定义了p中class包含class1的元素样式

 1 <!DOCTYPE html>
 2 <html>
 3 <head>
 4 <style>
 5 p[class~=class1]{
 6   border-bottom: 1px solid #000;
 7 }
 8 </style>
 9 </head>
10 <body>
11
12 <p class="class1">1</p><!-- 成功 -->
13 <p class="class3">2</p><!-- 失败 -->
14 <p class="class1 class3">3</p><!-- 成功-->
15
16 </body>
17 </html>

CSS [attribute|=value] 选择器

定义了p中class以cla1开头(cla11不是,但cla1-1就是) 的元素样式

 1 <!DOCTYPE html>
 2 <html>
 3 <head>
 4 <style>
 5 p[class|=cla1]{
 6   border-bottom: 1px solid #000;
 7 }
 8 </style>
 9 </head>
10 <body>
11
12 <p class="cla1">1</p><!-- 成功 -->
13 <p class="cla11">2</p><!-- 失败 -->
14 <p class="cla1-1">3</p><!-- 成功 -->
15
16 </body>
17 </html>

CSS3 element1~element2 选择器

定义div同一层级,并且在div下面的所有p元素的样式

 1 <!DOCTYPE html>
 2 <html>
 3 <head>
 4 <style>
 5 div~p{
 6   border-bottom: 1px solid #000;
 7 }
 8 </style>
 9 </head>
10 <body>
11 <div></div>
12 <p>1</p><!-- 成功 -->
13 <a><p>2</p></a><!-- 失败 -->
14 <p>3</p><!-- 成功 -->
15 </body>
16 </html>

CSS3 [attribute^=value] 选择器

定义p中class已cla1开头的元素样式(与CSS [attribute|=value] 不同)

 1 <!DOCTYPE html>
 2 <html>
 3 <head>
 4 <style>
 5 p[class^="cla1"]{
 6   border-bottom: 1px solid #000;
 7 }
 8 </style>
 9 </head>
10 <body>
11 <p class="cla1">1</p><!-- 成功 -->
12 <p class="cla11">2</p><!-- 成功 -->
13 <p class="cla1-1">3</p><!-- 成功 -->
14 <p class="cla21">4</p><!-- 失败 -->
15 </body>
16 </html>

CSS3 [attribute$=value] 选择器

定义和CSS3 [attribute^=value] 一样,只不过是定义的结尾的元素

CSS3 [attribute*=value] 选择器

定义和CSS3 [attribute^=value] 一样,只不过是定义的包含value的元素

CSS3 :nth-child() 选择器

定义html中第2个p元素的样式。

odd【p:nth-child(odd)】奇数行 和 even【p:nth-child(even)】偶数行 是可用于匹配下标是奇数或偶数的子元素的关键词(第一个子元素的下标是 1)。

 1 <!DOCTYPE html>
 2 <html>
 3 <head>
 4 <style>
 5 p:nth-child(2){
 6   border-bottom: 1px solid #000;
 7 }
 8 </style>
 9 </head>
10 <body>
11 <p>1</p><!-- 失败 -->
12 <p>2</p><!-- 成功 -->
13 <p>3</p><!-- 失败 -->
14 <p>4</p><!-- 失败 -->
15 </body>
16 </html>

CSS3 :nth-last-child() 选择器

同上,只不过跟最后一个开始选择。

CSS :first-child 选择器

第一个元素的样式

CSS :last-child 选择器

最后一个元素的样式

CSS3 :root 选择器

选择文档的根元素。

 1 <!DOCTYPE html>
 2 <html>
 3 <head>
 4 <style>
 5 :root{
 6   background-color: #000;
 7 }
 8 </style>
 9 </head>
10 <body>
11 </body>
12 </html>

CSS3 :disabled 选择器

 1 <!DOCTYPE html>
 2 <html>
 3 <head>
 4 <style>
 5 input[type="text"]:disabled
 6 {
 7 background-color: #dddddd;
 8 }
 9 </style>
10 </head>
11 <body>
12 <input type="text" value="1" disabled /><!-- 成功 -->
13 <input value="2" disabled /><!-- 成功 -->
14 <input type="text" value="3" /><!-- 失败 -->
15 <input type="text" value="4" /><!-- 失败 -->
16 <input type="password" value="5" /><!-- 失败 -->
17 </body>
18 </html>

CSS3 :not 选择器

 1 <!DOCTYPE html>
 2 <html>
 3 <head>
 4 <style>
 5 p {
 6 color:#000000;
 7 }
 8 :not(p)
 9 {
10 color:#ff0000;
11 }
12 </style>
13 </head>
14 <body>
15 <h1>1</h1><!-- 成功 -->
16 <p>2</p><!-- 失败 -->
17 <p>3</p><!-- 失败 -->
18 <div>4</div><!-- 成功 -->
19 </body>
20 </html>
时间: 2024-08-01 22:41:58

CSS选择器、优先级和常用的选择器的相关文章

我给女朋友讲编程CSS系列(2)- CSS语法、3大选择器、选择器优先级

首先看一下使用Css设置h1标签字体颜色和大小的例子,效果图如下: 新建一个网页test.html,然后复制粘贴下面的内容: <html> <head> <style type="text/css"> h1 {color:red; font-size:14px;} </style> </head> <body > <h1>使用Css让h1标签字体变红</h1> </body> &

CSS常用的选择器和优先级的权重问题

CSS注释 CSS修改页面中的所有标签,必须借助选择器选中.选择器中,可以写多对CSS属性,用{}包裹:每个属性名与属性值之间用:分隔,多对属性之间,必须用;来分隔 选择器{ 属性1:属性值1; 属性2:属性值2; [选择器的命名规范] 1.只能有字母.数字.下划线.减号组成.2.开头不能是数字,也不能是只有一个减号.一般,起名要求有语义,使用英文单词与数字的组合. 1.标签选择器 写法:HTML标签名{}作用:可以选中页面中,所有与选择器同名的HTML标签. 例如: li{ color:red

导航栏布局时遇到的问题以及解决办法 css选择器优先级

得到的导航栏效果 添加#menu ul li{width:30px;} 效果如图 将会使列表项和分隔区域的宽度同时改变因为id选择器的优先级高于类选择器,此时应该为列表项添加内联样式如图 才能得到如下效果 或者可以使用第二种方法 #menu ul li{ float:left; margin-left:10px;text-align:center;padding-left:10px;实现列表项文字的居中 line-height:28px; height:28px; width:40px; bor

css选择器优先级深入理解

转载自:http://www.jb51.net/css/67029.html 一.基础选择器 css基础选择器有标签选择器.类选择器.id选择器.通用选择器 1.标签选择器 每个html页面都由很多个标签组成,通过标签选择器可以对某类标签应用相应的样式,如对p标签应用下面的样式,则页面中所有的p标签都会生效 p{ font-size:12px; color:red; background:blue; } 2.类选择器 类选择器是css非常常用的选择器,在html中可以为某个标签增加class属性

CSS的三种使用方式以及常用的选择器

一.CSS的三种使用方式: 1. 内联样式              * 在标签内使用style属性指定css代码              * 如:<div style="color:red;">hello css</div>2. 内部样式             * 在head标签内,定义style标签,style标签的标签体内容就是css代码             * 如:                 <style>           

CSS选择器优先级【转】

样式的优先级 多重样式(Multiple Styles):如果外部样式.内部样式和内联样式同时应用于同一个元素,就是使多重样式的情况. 一般情况下,优先级如下: (外部样式)External style sheet <(内部样式)Internal style sheet <(内联样式)Inline style 有个例外的情况,就是如果外部样式放在内部样式的后面,则外部样式将覆盖内部样式. 示例如下: <head>     <style type="text/css&

CSS中选择器优先级顺序实战讲解

我们有些程序猿在给一个元素(比如div)应用样式的时候,会有一些疑问,为什么我写在后面的样式不能覆盖前面的样式呢,不是说CSS是层叠样式表吗? 如果你在开发中也遇到同样的问题,即在给某一元素应用样式的时候(比如写在外部样式表中),写在后面的样式不能覆盖前面定义的样式.这个时候,你可能就得考虑是不是没考虑优先级问题了. 当我们在给一个标签,比如div应用样式的时候,我们得考虑优先级问题. 下面列出的就是是选择器的优先级: 行内样式 > ID选择器样式 > 类别选择器样式 > 标记选择器样式

css选择器 优先级的问题

最近写代码时发现,有时候写出的样式不会被引用.看了一些其他人写的网站源码后,我发现了这样写可以解决问题,如下: 比如说<div class="abc">  <div class="edf">     <div class="fgg">    </div>  </div></div>确定class="fgg"时,写成:.abc .efg .fgg{/* 代码

CSS选择器命名及常用命名

CSS选择器命名及常用命名 CSS选择器命名及常用命名 规范的命名也是Web标准中的重要一项,标准的命名可以使代码更加易读,而且利于搜索引擎搜索,比如定义了两个div,一个 id 命名为"div1",另外一个命名为"News",肯定第二个比较易读,而且搜索引擎抓取率要高,在团队合作中还可以大大提高工作效率.为了达到这种效果我们就要规范化命名(语义化命名)! 说个题外话,规范化命名的代码,会显着你更加专业! 关于CSS命名法,和其他的程序命名差不多,主要有三种:骆驼命