CSS Cascade [译]

111

Click here to load reader

Transcript of CSS Cascade [译]

Page 1: CSS Cascade [译]

CSSCASCADE

[ 译 ]

Page 2: CSS Cascade [译]

CSS 规则的背景资料

Page 3: CSS Cascade [译]

CSS 规则 告诉 HTML 如何渲染元素

h2{

color: blue;margin: 1em;

}

Page 4: CSS Cascade [译]

选择器 " 选择 " HTML 中将 被定义样式的元素 .

h2 选择器{

color: blue;margin: 1em;

}

Page 5: CSS Cascade [译]

声明 告诉浏览器

如何定义元素的样式 .

}

color: blue;margin: 1em;

声明

h2{

Page 6: CSS Cascade [译]

属性 color: blue;margin: 1em;

}

属性 是元素样式的各个角度

h2{

Page 7: CSS Cascade [译]

属性值 是属性对应的具体样式

}

color: blue;margin: 1em;

属性值

h2{

Page 8: CSS Cascade [译]

样式表的类型

Page 9: CSS Cascade [译]

HTML 文档可以应用三种样式

Page 10: CSS Cascade [译]

浏览器样式浏览器给所有页面应用的样式 , 也称浏览器 " 默认 " 样式 .

Page 11: CSS Cascade [译]

用户定义样式大部分现代浏览器允许用户在浏览器中应用他们自定义的样式

Page 12: CSS Cascade [译]

作者样式站点作者可以对 HTML 文档应用一个或多个样式

Page 13: CSS Cascade [译]

作者样式

Page 14: CSS Cascade [译]

作者可以通过三种方法给HTML 文档添加样式

Page 15: CSS Cascade [译]

内联样式 通过 HTML 代码的style 属性应用到元素

使用 style 属性的内联样式<body><h2 style=“color: red;”>

Heading here</h2><p>

Page 16: CSS Cascade [译]

头部样式 位于文档的头部的 style 元素里

<style> 元素里的头部样式<head><title>Document title</titl<style type="text/css" medih2 { color: blue; }</style>

Page 17: CSS Cascade [译]

外部样式 通过 link 或者@import 实现

使用 link 元素的外部样式<title>Document title</titl<link rel="stylesheet"href=”my-styles.css”type=”text/css"media="screen” />

Page 18: CSS Cascade [译]

CSS 规则超负荷了 !

Page 19: CSS Cascade [译]

浏览器需要处理来自浏览器 , 用户和作者样式的 CSS 规则 .

Page 20: CSS Cascade [译]

浏览器还需要处理来自各种作者样式 ( 外部 , 头部和内联 )

的 CSS 规则 .

Page 21: CSS Cascade [译]

某些时候 , 浏览器还需要处理冲突的 CSS 规则 .

Page 22: CSS Cascade [译]

“ 冲突”是什么意思 ?

Page 23: CSS Cascade [译]

冲突指的是多个 CSS 规则作用在相同的元素和属性上 .

h2 { color: blue; }h2 { color: red; }

冲突的 CSS 规则

Page 24: CSS Cascade [译]

冲突可以发生在不同类型样式的 CSS 规则上 .

浏览器样式h2 { color: blue; }

作者样式h2 { color: red; }

Page 25: CSS Cascade [译]

冲突也可以发生在单个或多个作者样式的 CSS 规则里 .

作者样式 1

h2 { color: blue; }

作者样式 2

h2 { color: red; }h2 { color: green; }

Page 26: CSS Cascade [译]

那哪条 CSS 规则“ 赢”了呢 ?

Page 27: CSS Cascade [译]

需要四个步骤来决定哪条 CSS 规则

“ 赢了” ( 即被应用到 HTML 文档 )

Page 28: CSS Cascade [译]

第一步

Page 29: CSS Cascade [译]

收集所有应用到元素和属性的来自于浏览器 , 作者

和用户的样式声明

Page 30: CSS Cascade [译]

比如 , 找到所有符合以下条件的声明:

元素 = h2

属性 = color

Page 31: CSS Cascade [译]

收集到的声明

Browser style sheet

User style sheet

Author style sheets

h2 { color: black; }

h2 { color: green; }

h2 { color: blue; }#nav h2 { color: lime; }

Page 32: CSS Cascade [译]

如果中有声明的来源超过三个来源中的一个 , 进入步骤二 .

Page 33: CSS Cascade [译]

第二步

Page 34: CSS Cascade [译]

将收集到的声明按来源 ( 浏览器 , 作者 , 用户样式 ) 和重要性

(normal 或 !important) 分类 .

Page 35: CSS Cascade [译]

什么是!important?

Page 36: CSS Cascade [译]

作者可以指定任何声明为“!important” .

h2 { color: red !important;}

!important

Page 37: CSS Cascade [译]

“!important” 声明可以覆盖普通声明

( 普通声明指的是不包含!important 的声明 ).

Page 38: CSS Cascade [译]

那么 , 声明如何分类 ?

Page 39: CSS Cascade [译]

优先级由低到高1 浏览器样式2 用户样式里的普通声明3 作者样式里的普通声明4 作者样式里的 !important 声明5 用户样式里的 !important 声明

Page 40: CSS Cascade [译]

1. 浏览器样式

Browser style sheet

User style sheet

Author style sheets

h2 { color: black; }

如果不存在其他声明 ,浏览器声明获胜

Page 41: CSS Cascade [译]

2. 普通用户样式

Browser style sheet h2 { color: black; }

普通用户声明胜过浏览器声明

User style sheet

Author style sheets

h2 { color: green; }

Page 42: CSS Cascade [译]

Browser style sheet h2 { color: black; }

普通作者声明胜过浏览器声明和普通用户声明

User style sheet

Author style sheets

h2 { color: green; }

h2 { color: blue; }

3. 普通作者样式

Page 43: CSS Cascade [译]

Browser style sheet h2 { color: black; }

!important 作者声明胜过所有普通声明

User style sheet

Author style sheets

h2 { color: green; }

h2 { color: blue; }h2 { color: lime !important; }

4. !important 作者样式

Page 44: CSS Cascade [译]

Browser style sheet h2 { color: black; }

!important 用户声明胜过 !important 作者声明和所有普通声明

User style sheet

Author style sheets

h2 { color: green; }h2 { color: red !important;}

h2 { color: blue; }h2 { color: lime !important; }

5. !important 用户样式

Page 45: CSS Cascade [译]

那如果两条声明的来源或重要性相同,该怎么办呢?

Page 46: CSS Cascade [译]

有两条匹配的声明

Browser style sheet

User style sheet

h2 { color: black; }

h2 { color: green; }

两条来源和重要性相同的声明

Author style sheets h2 { color: blue; }h2 { color: lime; }

Page 47: CSS Cascade [译]

如果声明的来源或重要性相同 , 进入步骤三 .

Page 48: CSS Cascade [译]

第三步

Page 49: CSS Cascade [译]

如果声明的来源和重要性都相同,需要对声明的选择器进行评分,

来决定是哪条声明“赢了” .

Page 50: CSS Cascade [译]

选择器

#nav h2 { color: blue; }h2.intro { color: red; }

选择器

Page 51: CSS Cascade [译]

四个分数连在一起 ( 像链条一样 ) 得到最终的评分 .

a,b,c,d

Page 52: CSS Cascade [译]

得分取决于选择器的优先级 .

Page 53: CSS Cascade [译]

那优先级是如何计算的?

Page 54: CSS Cascade [译]

inline styles

A. 是不是内联样式 ?

<h2 style=“color: red;”>This is a heading

a = 1 x </h2>b = 0 x ID<p>c = 0 x classes Here is a paragraph of

Specificity = 1,0,0,0d = 0 x element

Page 55: CSS Cascade [译]

B. 统计选择器中 ID 的数量 .

#nav { color: red; }

a = 0 x inline stylesb = 1 x IDc = 0 x classesd = 0 x elementSpecificity = 0,1,0,0

Page 56: CSS Cascade [译]

C. 统计 class ,属性 和伪类的数量 .

.main { color: red; }

a = 0 x inline stylesb = 0 x IDc = 1 x classesd = 0 x elementSpecificity = 0,0,1,0

Page 57: CSS Cascade [译]

D. 统计元素名和伪元素的数量 .

h2 { color: red; }

a = 0 x inline stylesb = 0 x IDc = 0 x classesd = 1 x elementSpecificity = 0,0,0,1

Page 58: CSS Cascade [译]

优先级连接笔记

Page 59: CSS Cascade [译]

“A” 始终优先于“ B”, “B” 始终优先于“ C”, “C” 始终优先于“ D”.

Page 60: CSS Cascade [译]

不管选择器中有多少个 ID , 只要一个内联样式就可以轻松获胜 .

( 除非 ID 的声明中使用了 !important)

Page 61: CSS Cascade [译]

External style sheetsand header styles

(Author styles)

HTML document withinline styles

(Author styles)

#one #two #three #four #five#six #seven #eight #nine #ten{ color: green; }

<h2 style=“color: purple;”>

高亮的样式胜出因为优先级1,0,0,0 胜过 0,10,0,0

Page 62: CSS Cascade [译]

不管选择器中有多少个 class , 只要一个 ID 就可以轻松获胜 .

Page 63: CSS Cascade [译]

External style sheetsand header styles

(Author styles)

.one .two .three .four .five

.six .seven .eight .nine .ten{ color: green; }

#nav { color: lime; }

高亮的样式胜出因为优先级0,1,0,0 胜过 0,0,10,0

Page 64: CSS Cascade [译]

不管选择器中有多少个 元素名 , 只要一个 class 就可以轻松获胜 .

Page 65: CSS Cascade [译]

External style sheetsand header styles

(Author styles)

div div div div div formfieldset div label span{ color: green; }

.intro { color: lime; }

高亮的样式胜出因为优先级0,0,1,0 胜过 0,0,0,10

Page 66: CSS Cascade [译]

优先级的复杂案例

Page 67: CSS Cascade [译]

ID 和元素

#nav h2 { color: red; }

a = 0 x inline stylesb = 1 x ID (#nav)c = 0 x classesd = 1 x element (h2)Specificity = 0,1,0,1

Page 68: CSS Cascade [译]

元素和 class

h2.intro { color: red; }

a = 0 x inline stylesb = 0 x IDc = 1 x classes (.intro)d = 1 x element (h2)Specificity = 0,0,1,1

Page 69: CSS Cascade [译]

ID, 元素和伪类

#nav ul li a:hover { color:

a = 0 x inline stylesb = 1 x ID (#nav)c = 1 x pseudo-class (:hover)d = 3 x elements (ul, li, a)Specificity = 0,1,1,3

Page 70: CSS Cascade [译]

元素和伪元素

p:first-line { color: green

a = 0 x inline stylesb = 0 x IDc = 0 x classesd = 2 x element (p) and pseudo-element (:first-line)Specificity = 0,0,0,2

Page 71: CSS Cascade [译]

元素和属性选择器

h2[title=“intro”] { color:

a = 0 x inline stylesb = 0 x IDc = 1 x attribute selector ([title=“intro”])d = 1 x element (h2)Specificity = 0,0,1,1

Page 72: CSS Cascade [译]

如果还没 分出胜负?

Page 73: CSS Cascade [译]

优先级相同的选择器

#nav h2 { color: red; }#nav h2 { color: green; }

优先级 = 0,1,0,1

Page 74: CSS Cascade [译]

如果还没分出胜负 , 进入步骤四 .

Page 75: CSS Cascade [译]

第四步

Page 76: CSS Cascade [译]

如果两条声明有相同的重要性、来源和优先级 , 则后面的声明赢 .

Page 77: CSS Cascade [译]

同等的声明

#nav h2 { color: green; }#nav h2 { color: red; }

第二条声明获胜,因为他写在第一条后面 .

Page 78: CSS Cascade [译]

现在…来些猜谜游戏

Page 79: CSS Cascade [译]

习题一浏览器 , 用户 , 作者

Page 80: CSS Cascade [译]

Part 1: 哪个赢了 ?

Page 81: CSS Cascade [译]

Browser style sheet

User style sheet

External style sheetsand header styles

(Author styles)

HTML document withinline styles

(Author styles)

h2 { color: black; }

h2 { color: green; }

Page 82: CSS Cascade [译]

External style sheets

Browser style sheet

User style sheet

h2 { color: black; }

h2 { color: green; }

普通用户声明从来源上胜过浏览器声明

and header styles(Author styles)

HTML document withinline styles

(Author styles)

Page 83: CSS Cascade [译]

Part 2: 哪个赢了 ?

Page 84: CSS Cascade [译]

Browser style sheet

User style sheet

External style sheetsand header styles

(Author styles)

HTML document withinline styles

(Author styles)

h2 { color: black; }

h2 { color: green; }

h2 { color: blue; }

Page 85: CSS Cascade [译]

HTML document with

Browser style sheet

User style sheet

External style sheetsand header styles

(Author styles)

h2 { color: black; }

h2 { color: green; }

h2 { color: blue; }

作者声明从来源上胜过浏览器和用户声明

inline styles(Author styles)

Page 86: CSS Cascade [译]

Part 3: 哪个赢了 ?

Page 87: CSS Cascade [译]

Browser style sheet

User style sheet

External style sheetsand header styles

(Author styles)

HTML document withinline styles

(Author styles)

h2 { color: black; }

h2 { color: green; }

h2 { color: blue; }

<h2 style=“color: purple;”>

Page 88: CSS Cascade [译]

User style sheet h2 { color:

Browser style sheet h2 { color: black; }

普通内联声明胜过普通外部和头部声明,因为优先级 1,0,0,0 胜过 0,0,0,1

External style sheetsand header styles

(Author styles)

HTML document withinline styles

(Author styles)

h2 { color: blue; }

<h2 style=“color: purple;”>

Page 89: CSS Cascade [译]

Part 4: 哪个赢了 ?

Page 90: CSS Cascade [译]

Browser style sheet

User style sheet

External style sheetsand header styles

(Author styles)

HTML document withinline styles

(Author styles)

h2 { color: black; }

h2 { color: green; }

h2 { color: blue; }h2 { color: lime !important; }

<h2 style=“color: purple;”>

Page 91: CSS Cascade [译]

Browser style sheet

User style sheet

h2 { color: black; }

h2 { color: green; }

!important 作者样式 胜过普通浏览器、用户和作者样式

External style sheetsand header styles

(Author styles)

HTML document withinline styles

(Author styles)

h2 { color: blue; }h2 { color: lime !important; }

<h2 style=“color: purple;”>

Page 92: CSS Cascade [译]

Part 5: 哪个赢了 ?

Page 93: CSS Cascade [译]

Browser style sheet

User style sheet

External style sheetsand header styles

(Author styles)

HTML document withinline styles

(Author styles)

h2 { color: black; }

h2 { color: green; }

h2 { color: blue; }h2 { color: lime !important; }

<h2 style=“color: purple!important;”>

Page 94: CSS Cascade [译]

User style sheet

Browser style sheet h2 { color: black; }

!important 内联作者声明胜过 !important 外部作者声明和头部声明,因为优先级 1,0,0,0 胜过 0,0,0,1

External style sheetsand header styles

(Author styles)

HTML document withinline styles

(Author styles)

h2 { color: blue; }h2 { color: lime !important; }

<h2 style=“color: purple!important;”>

Page 95: CSS Cascade [译]

Part 6: 哪个赢了 ?

Page 96: CSS Cascade [译]

Browser style sheet

User style sheet

External style sheetsand header styles

(Author styles)

HTML document withinline styles

(Author styles)

h2 { color: black; }

h2 { color: green; }h2 { color: gray !important; }

h2 { color: blue; }h2 { color: lime !important; }

<h2 style=“color: purple!important;”>

Page 97: CSS Cascade [译]

Browser style sheet h2 { color: black;!important 用户声明胜过 !important 作者声明 ( 不管他们是外部、头部还是内联 )

User style sheet

External style sheetsand header styles

(Author styles)

HTML document withinline styles

(Author styles)

h2 { color: green; }h2 { color: gray !important; }

h2 { color: blue; }h2 { color: lime !important; }

<h2 style=“color: purple!important;”>

Page 98: CSS Cascade [译]

习题二作者的外部 , 头部和内联 CSS

Page 99: CSS Cascade [译]

Part 1: 哪个赢了 ?

Page 100: CSS Cascade [译]

External style sheetsand header styles

(Author styles)

h2.news { color: #eee; }h2 { color: blue; }

Page 101: CSS Cascade [译]

高亮的声明胜出,因为优先级0,0,1,1 胜过 0,0,0,1

External style sheetsand header styles

(Author styles)

h2.news { color: #eee; }h2 { color: blue; }

Page 102: CSS Cascade [译]

Part 2: 哪个赢了 ?

Page 103: CSS Cascade [译]

External style sheetsand header styles

(Author styles)

h2.news { color: #eee; }h2 { color: blue; }h2.news { color: green; }

Page 104: CSS Cascade [译]

高亮的声明和第一个声明优先级相同 (0,0,1,1).但是他写在后面,所以他胜出 !

External style sheetsand header styles

(Author styles)

h2.news { color: #eee; }h2 { color: blue; }h2.news { color: green; }

Page 105: CSS Cascade [译]

Part 3: 哪个赢了 ?

Page 106: CSS Cascade [译]

External style sheetsand header styles

(Author styles)

#nav h2 { color: lime; }h2.news { color: #eee; }h2 { color: blue; }h2.news { color: green; }

Page 107: CSS Cascade [译]

高亮的选择器胜出 , 因为优先级0,1,0,1 胜过 0,0,1,1 和 0,0,0,1

External style sheetsand header styles

(Author styles)

#nav h2 { color: lime; }h2.news { color: #eee; }h2 { color: blue; }h2.news { color: green; }

Page 108: CSS Cascade [译]

Part 4: 哪个赢了 ?

Page 109: CSS Cascade [译]

External style sheetsand header styles

(Author styles)

#nav h2 { color: lime; }h2.news { color: #eee; }h2 { color: blue; }h2.news { color: green; }div#nav h2 { color: lime; }

Page 110: CSS Cascade [译]

External style sheetsand header styles

(Author styles)

#nav h2 { color: lime; }h2.news { color: #eee; }h2 { color: blue; }h2.news { color: green; }div#nav h2 { color: lime; }

高亮的选择器胜出 , 因为优先级0,1,0,2 胜过 0,1,0,1 和 0,0,1,1 和 0,0,0,1

Page 111: CSS Cascade [译]

搞定 !