一:一些普通的选择器

<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<title></title>
<link href="MyCss.css" type="text/css" rel="stylesheet">
</head>
<body>
<!-- 派生选择器实例 -->
<a>这是一个div外部a标签的样式</a>
<div>
<a>这是一个div内部a标签的样式</a>
<h2>css基础语法</h2>
</div> <!--id选择器与派生选择器实例 -->
<a href="https://www.baidu.com/" > 百度不变色</a>
<div id="divid">
<a href="https://www.baidu.com/" >百度变色</a>
</div>
<br /> <!--类选择器-->
<div id="divclass" class="divclss">
div类选择器实例
</div>
<br /> <!--属性选择器-->
<p title="t1"> 属性选择器</p>
<!--和body指定的颜色一样-->
<p title="t2"> 属性选择器</p>
</body>
</html>
h2,a,h1{
color:red;
}
body{
color:yellow; }
div a{
color: blue;
}
#divid a{
color:darkred;
}
.divclss{
color:chartreuse;
}
[title = t1]
{
color: mediumvioletred;
}
/*这个要按顺序来*/
div a:link {color:blue;}
div a:visited {color:blue;}
div a:hover {color:red;}
div a:active {color:yellow;}
/*id选择器的优先级比类型选择器优先级高*/

效果截图:

aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAtgAAAE2CAIAAADUF3I9AAAbx0lEQVR4nO3dT4hd150n8LN7Cy2EFsUshBFBmDAURgtrIWxCsAjphUHYEAgGFw1jFJwwWsRMMuAM1gTG4350k0WPDOqEiQfCTONEMcXEznRn7LSNGHnw2J4YFyjtiSMUi27hhRhCLapXv17Uq6r33v137vtTp1T1+XAxr+4795zzSobzrXPOvS/9v8//yeFwOBwOh6PIkYr3wOFwOBwOx5E90k/f/L8Oh8PhcDgcRY4UAACFCCIAQDGCCABQjCACABQjiAAAxQgiAEAxgggAUIwgAgAUI4gAAMX0DyKp65KUGo+FW0ad+W21t76ovvWtZ/5+5pfve+1Umf385wPgQMpIFZknO989akGk6ZK+yWxq4O+Md/sZRDp/7KxBFgE42noOG01nct5d+JCzXWFutRsxXF9Ac+1nds83JYYZxuBev7emRpuyy/xBc85PJIgAHG3ZQSR/w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g2KxMyLVOnMuyQwi1cvz5zZqLwQAClvsHpGWAjkzIjmJZIbKe02uAAD7JydV9N1gcUCCSGbOEEcAoJj2Mbh2U0Vqfbepzs4gkrMWM9V6TjCqfctOVQA4EPKDSNOQP2cQ6buBdObplpgMIjnXAgDL1XfFpFqgJYh0Tlp07jytJh5BBAAOj8xdFL3eWt7Q3h6Jmt6yNAMAB5RhGAAoRhABAIoRRACAYgQRAKAYQQQAKEYQAQCKEUQAgGIEEQCgGEEEAChGEAEAihFEAIBiBBEAoBhBBAAoRhABAIoRRACAYgQRAKAYQQQAKEYQAQCKEUQAgGIEEQCgGEEEACjmnwEJzcTegDKIxQAAAABJRU5ErkJggg==" alt="" />

二:优先级实例

<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<title></title>
<style>
/*标签选择器*/
div { color: blue } /*绿色*/
/*类选择器*/
.a1 { color:yellow } /*黄色*/
/*类派生选择器*/
.a1 div {color:purple} /*紫色*/
/*id选择器*/
#id1{ color: cyan} /*青色*/
#id2{ color: cyan} /*青色*/
#id3{ color: cyan} /*青色*/
/*id派生选择器*/
#id2 div{ color: red} /*红色*/ </style>
</head>
<body>
<div id="id1" class="a1"> 我是青色的 id选择器优先级高于类选择器 </div> <div class="a1">
<div class="a1">我是紫色的 类派生选择器优先级高于所有类选择器</div>
<div id="id2">我是青色的 id选择器优先级高于类派生选择器
<div id="id3">
我是红色的而不是青色的 id派生选择器优先级高于id选择器
</div>
</div>
</div>
</body>
</html>

 效果图:

aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAApAAAABXCAIAAAA8peIiAAAQdUlEQVR4nO2awXHmuA6ElZGz2CycytReHYmvk4UTmAQcifYwO15aBBoNkJKorf7KtaWfAkGQBNHUe7PtQgghhFie7e4AhBBCCBEjwRZCCCEeACXYm/NsWnp/c0JJXjF647tuKGDca0IyN3HWvpSncMHca0Pcvl9CCHEgLj4Hxd1gt2x71oY384wvVghGGlsb5q5jvg2vR96tK2U8awreWJ4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5BgCyGEEA9Agi2EEEI8AAm2EEII8QAk2EIIIcQD+AfztYnhO4oxtQAAAABJRU5ErkJggg==" alt="" />

3:更加专业点的优先级判断

(原博客地址   : http://www.cnblogs.com/aaronjs/p/3156809.html#_h2_1  )

计算指定选择器的优先级:重新认识CSS的权重

  1. 通配选择符的权值 0,0,0,0
  2. 标签的权值为 0,0,0,1
  3. 类的权值为 0,0,1,0
  4. 属性选择的权值为 0,0,1,1 0,0,1,0
  5. 伪类选择的权值为 0,0,1,0
  6. 伪对象选择的权值为 0,0,0,1
  7. ID的权值为 0,1,0,0
  8. important的权值为最高 1,0,0,0

使用的规则也很简单,就是 选择器的权值加到一起,大的优先;如果权值相同,后定义的优先 。虽然很简单,但如果书写的时候没有注意,很容易就会导致CSS的重复定义,代码冗余。

从上面我们可以得出两个关键的因素:

  1. 权值的大小跟选择器的类型和数量有关
  2. 样式的优先级跟样式的定义顺序有关

一篇文章 256个class类名选择器干掉一个id选择器实例页面

http://www.zhangxinxu.com/study/201208/256-class-fire-an-id.html

给出的答案是:所有的类名(classes)都是以8字节字符串存储的。8字节所能hold的最大值就是255. 当同时出现256个class, 势必会越过其边缘,溢出到id区域。

总结:

比较同一级别的个数,数量多的优先级高,如果相同即比较下一级别的个数 ,至于各级别的优先级,大家应该已经很清楚了,就是:

important > 内联 > ID > 类 > 标签 | 伪类 | 属性选择 > 伪对象 > 继承 > 通配符 通配符 > 继承

这也就解释了为什么11个标签的定义会比不上1个类的定义,1个类加11个标签会比不上2个类的权重高。

选择器种类

查阅资料归纳下大概有如下几种:

  • 通配选择器

  • 类型选择器

  • ID选择器

  • 类选择器

  • 包含选择器

  • 子元素选择器

  • 相邻兄弟选择器

  • 属性选择器

css1-css3提供非常丰富的选择器,但是由于某些选择器被各个浏览器支持的情况不一样,所以很多选择器在实际css开发中很少用到

关于CSS的执行效率:

  1. 样式系统从最右边的选择符开始向左进行匹配规则。只要当前选择符的左边还有其他选择符,样式系统就会继续向左移动,直到找到和规则匹配的元素,或者因为不匹配而退出
  2. 如果你非常在意页面的性能那千万别使用CSS3选择器。实际上,在所有浏览器中,用 class 和 id 来渲染,比那些使用同胞,后代选择器,子选择器(sibling, descendant and child selectors)对页面性能的改善更值得关注。

Google 资深web开发工程师Steve Souders对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选择器的效率是最高,而伪类选择器的效率则是最低、

详细的介绍大家还可以点击Writing efficient CSS selectors

CSS选择器及其优先级的更多相关文章

  1. CSS选择器以及优先级与匹配原理

    最常用的五类CSS选择器 准确而简洁的运用CSS选择器会达到非常好的效果.我们不必通篇给每一个元素定义类(class)或ID,通过合适的组织,可以用最简单的方法实现同样的效果.在实际工作中,最常用的选 ...

  2. CSS选择器、优先级与匹配原理(转)

    CSS选择器.优先级与匹配原理 导航 为了分析Bootstrap源码,所以的先把CSS选择器相关的东东给巩固好 废话就不多说了 CSS 2.1 selectors, Part 1 计算指定选择器的优先 ...

  3. 详解CSS选择器、优先级与匹配原理

    原文链接:http://polaris1119.javaeye.com/blog/764428 作为一个Web开发者,掌握必要的前台技术也是很重要的,特别是在遇到一些实际问题的时候.这里给大家列举一个 ...

  4. 转载:详解CSS选择器、优先级与匹配原

    转载网址:http://polaris1119.javaeye.com/blog/764428 文章就CSS选择器的优先级问题做了一些总结,严格来讲,选择器的种类可以分为三种:标签名选择器.类选择器和 ...

  5. CSS选择器、优先级和匹配原理

    作为一个Web开发者,掌握必要的前台技术也是很重要的,特别是在遇到一些实际问题的时候.这里给大家列举一个例子: 给一个p标签增加一个类(class),可是执行后该class中的有些属性并没有起作用.通 ...

  6. 详解CSS选择器、优先级与匹配原理【转】

    作为一个Web开发者,掌握必要的前台技术也是很重要的,特别是在遇到一些实际问题的时候.这里给大家列举一个例子: 给一个p标签增加一个类(class),可是执行后该class中的有些属性并没有起作用.通 ...

  7. (转)css选择器及其优先级

    文章主要介绍什么是CSS选择器,CSS选择器的分类以及CSS选择器的优先级三部分内容,希望能够帮助到正在学习CSS的童鞋,有什么不足的地方欢迎大家批评指正. 一.什么是CSS选择器? CSS选择器又被 ...

  8. CSS 选择器及优先级

    CSS 选择器及优先级 1.根据权值计算 div .class1 #people的权值等于1+10+100=111 .class2 li #age的权值等于10+1+100=111 2.权值相同,那么 ...

  9. CSS选择器、优先级与匹配原理

    为了分析Bootstrap源码,所以的先把CSS选择器相关的东东给巩固好 废话就不多说了 CSS 2.1 selectors, Part 1 计算指定选择器的优先级:重新认识CSS的权重 标签的权值为 ...

随机推荐

  1. c# Start/Stop/Check Status远程计算机的Windows Service

    static void Main(string[] args) { ConnectionOptions op = new ConnectionOptions(); // 登陆远程计算机的远程, op. ...

  2. CentOS6.5 安装HAProxy 1.5.20

    [在CentOS下安装haproxy] sudo yum install haproxy 编辑配置文件 vim /etc/haproxy/haproxy.cfg #------------------ ...

  3. ViewController 的代码规范

    1.#pragma mark - life cycle viewDidLoad viewWillAppear 2.#pragma mark - delegate #pragma mark collec ...

  4. scrollView中的pagingEnabled的功能

    pagingEnabled是分页设置,以页为单位滑动,即自动到下一页的开始边界

  5. zip格式jdk在Linux环境下的安装过程

    1下载jdk [root@localhost opt]# cd soft_bak/[root@localhost soft_bak]# lsjdk1.7.0_25.zip postgresql-9.4 ...

  6. [Reprint] C++函数模板与类模板实例解析

    这篇文章主要介绍了C++函数模板与类模板,需要的朋友可以参考下   本文针对C++函数模板与类模板进行了较为详尽的实例解析,有助于帮助读者加深对C++函数模板与类模板的理解.具体内容如下: 泛型编程( ...

  7. Fedora20 MATE Destop 环境下安装Sougoupinyin输入法+皮肤

    卸载ibus # yum erase ibus* 选择性安装 fcitx # yum install fcitx fcitx-configtool $ ls -a $ vi .bashrc  .bas ...

  8. .NET: WPF 路由事件

    (一)使用WPF内置路由事件 xaml: <Window x:Class="WpfApplication1.MainWindow" xmlns="http://sc ...

  9. CCF真题之相反数

    201403-1 问题描述 有 N 个非零且各不相同的整数.请你编一个程序求出它们中有多少对相反数(a 和 -a 为一对相反数). 输入格式 第一行包含一个正整数 N.(1 ≤ N ≤ 500).   ...

  10. struts_22_xwork校验器列表使用说明

    系统提供的校验器列表如下: required (必填校验器,要求field的值不能为null) requiredstring (必填字符串校验器,要求field的值不能为null,并且长度大于0,默认 ...