94
Css Css Css Css 继承 CSS CSS CSS CSS

Inheritance css继承

  • Upload
    paulguo

  • View
    688

  • Download
    1

Embed Size (px)

DESCRIPTION

 

Citation preview

Page 1: Inheritance css继承

CssCssCssCss继承

CSSCSSCSSCSS

Page 2: Inheritance css继承

偶也,偶也,偶也,偶也,我们从文档树我们从文档树我们从文档树我们从文档树

((((DOMDOMDOMDOM treetreetreetree))))

开始开始开始开始

Page 3: Inheritance css继承

在我们研究继承性之前我们需要弄清楚documentdocumentdocumentdocument treetreetreetree

Page 4: Inheritance css继承

我们认为所有的HTML结构为一棵树一棵树一棵树一棵树

Page 5: Inheritance css继承

文档树结构是由HTMLHTMLHTMLHTML元素元素元素元素组成

Page 6: Inheritance css继承

文档树我们可以把他理解成家庭关系家庭关系家庭关系家庭关系

Page 7: Inheritance css继承

一个祖先元素(暂且这么说吧,发现翻译成根元素是不对的)

连接并牵引出所有的元素

祖先

Page 8: Inheritance css继承

一个后代元素后代元素后代元素后代元素牵引出所有元素

但他处于文档树低一级

后代元素

Descendants

Page 9: Inheritance css继承

我们亦可将bodybodybodybody元素元素元素元素

理解为父元素

父元素

Page 10: Inheritance css继承

Also,低一级的ulululul元素元素元素元素

理解为子元素

子元素

Page 11: Inheritance css继承

兄弟元素(LI)和他的兄弟姐妹们

有同一个爸爸同一个爸爸同一个爸爸同一个爸爸(UL)

相对父元素

兄弟姐妹们

Page 12: Inheritance css继承

Next,Next,Next,Next,我们得看我们得看我们得看我们得看CSSCSSCSSCSS规则规则规则规则

Page 13: Inheritance css继承

在我们研究继承性之前,应该需要理解

CSS规则的基本原理.

Page 14: Inheritance css继承

CSS规则会告诉浏览器如何去表现

HTML页面中的每个元素的特点

Page 15: Inheritance css继承

CSSCSSCSSCSS规则规则规则规则由由由由5555个要素个要素个要素个要素构成构成构成构成

Page 16: Inheritance css继承

通过通过通过通过选择器选择器选择器选择器来选择来选择来选择来选择HTMLHTMLHTMLHTML页面中的页面中的页面中的页面中的

元素元素元素元素

p { color: red; }

选择选择选择选择 选择器选择器选择器选择器

Page 17: Inheritance css继承

声明块声明块声明块声明块包含所有的东西当然也包含方括号

p { color: red; }

声明块声明块声明块声明块

Page 18: Inheritance css继承

声明声明声明声明告诉浏览器如何在页面中表现已

被选择的元素

p { color: red; }

声明声明声明声明

Page 19: Inheritance css继承

属性属性属性属性

p { color: red; }

属性属性属性属性

Page 20: Inheritance css继承

对应属性的属性值

p { color: red; }

属性值属性值属性值属性值

Page 21: Inheritance css继承

NowNowNowNow…………什么是什么是什么是什么是

继承继承继承继承????

Page 22: Inheritance css继承

当有特定的CSS属性传承传承传承传承给后代

(儿子,孙子and so on)元素就是继承继承继承继承

Page 23: Inheritance css继承

我们在实践中来看看继承,来看下面的HTMLHTMLHTMLHTML codecodecodecode

<p>Lorem <em>ipsum</em> dolorsit amet consect etuer.

</p>

Page 24: Inheritance css继承

em元素是包含在p元素里.

Page 25: Inheritance css继承

看下面的CSS代码Em元素并没有

单独特定的去定义他

p { color: red; }

Page 26: Inheritance css继承

在浏览器中查看P与em元素表现是一样的

<em><em><em><em> 元素元素元素元素

Page 27: Inheritance css继承

但是为什么em元素也是红色的红色的红色的红色的????

问题是我并没有给他任何属性, 囧 。。。

Page 28: Inheritance css继承

因为em元素从P那里继承了继承了继承了继承了color:red属性(做儿子还有点儿好处的)

Page 29: Inheritance css继承

WhyWhyWhyWhy????

为什么要有继承为什么要有继承为什么要有继承为什么要有继承

Page 30: Inheritance css继承

继承被设计是用来简化开发简化开发简化开发简化开发

Page 31: Inheritance css继承

否则,我们需要去单独定义

所有的所有的所有的所有的后代元素,look,

p { color: red; }p em { color: red; }

Page 32: Inheritance css继承

css文件将会变得非常大非常大非常大非常大,开发和维护将极为困难

同时 下载速度下载速度下载速度下载速度也将受到很大影响

Page 33: Inheritance css继承

ALLALLALLALL????所有的所有的所有的所有的csscsscsscss

属性都会继承吗?属性都会继承吗?属性都会继承吗?属性都会继承吗?

Page 34: Inheritance css继承

AbsolutelyAbsolutelyAbsolutelyAbsolutely no!!!no!!!no!!!no!!!

Page 35: Inheritance css继承

如果所有CSS属性都会被继承,

那对开发者来说是非常严重的问题!!非常严重的问题!!非常严重的问题!!非常严重的问题!!

Page 36: Inheritance css继承

开发者需要去掉去掉去掉去掉

后代元素并不需要的属性。

Page 37: Inheritance css继承

What?What?What?What?例如:

如果border属性会被继承的话,

将会发生什么将会发生什么将会发生什么将会发生什么????

Page 38: Inheritance css继承

Okey,我们来给P元素一个borderborderborderborder属性

p { border: 1px solid red; }

Page 39: Inheritance css继承

在P元素里的em元素也会有红色的边框?红色的边框?红色的边框?红色的边框?

<em><em><em><em> elementelementelementelement

Page 40: Inheritance css继承

.幸运的是,

.边框并没有被继承,

.em元素并没有红色边框

<em><em><em><em> elementelementelementelement

Page 41: Inheritance css继承

通俗的讲,有利于有利于有利于有利于简化开发的属性都会被继承

Page 42: Inheritance css继承

So,So,So,So,哪些属性会被继承哪些属性会被继承哪些属性会被继承哪些属性会被继承

Page 43: Inheritance css继承

下面已列出会被继承会被继承会被继承会被继承的属性…

Page 44: Inheritance css继承

azimuth, border-collapse, border-spacing,caption-side, color, cursor, direction, elevation,

empty-cells, font-family, font-size, font-style,font-variant, font-weight, font, letter-spacing,

line-height, list-style-image, list-style-position,list-style-type, list-style, orphans, pitch-range,pitch, quotes, richness, speak-header, speak-numeral, speak-punctuation, speak, speech-

rate, stress, text-align, text-indent, text-transform, visibility, voice-family, volume, white-

space, widows, word-spacing

Page 45: Inheritance css继承

啊,啊,啊!会有这么多属性哦

最讨厌背东西了

Page 46: Inheritance css继承

如此简单,我们来个排列组合,惊喜的发现,

是有几个主要的属性系列属性系列属性系列属性系列组成

Page 47: Inheritance css继承

TextTextTextText相关相关相关相关属性都会被继承

Page 48: Inheritance css继承

azimuth, border-collapse, border-spacing,caption-side, color, cursor, direction, elevation,

empty-cells, font-family, font-size, font-style,font-variant, font-weight, font, letter-spacing,

line-height, list-style-image, list-style-position,list-style-type, list-style, orphans, pitch-range,pitch, quotes, richness, speak-header, speak-numeral, speak-punctuation, speak, speech-

rate, stress, text-align, text-indent, text-transform, visibility, voice-family, volume, white-

space, widows, word-spacing

Page 49: Inheritance css继承

ListListListList相关相关相关相关属性都会被属性都会被属性都会被属性都会被

继承继承继承继承

Page 50: Inheritance css继承

azimuth, border-collapse, border-spacing,caption-side, color, cursor, direction, elevation,

empty-cells, font-family, font-size, font-style,font-variant, font-weight, font, letter-spacing,

line-height, list-style-image, list-style-position,list-style-type, list-style, orphans, pitch-range,pitch, quotes, richness, speak-header, speak-numeral, speak-punctuation, speak, speech-

rate, stress, text-align, text-indent, text-transform, visibility, voice-family, volume, white-

space, widows, word-spacing

Page 51: Inheritance css继承

更重要的是,color属性会被继承

Page 52: Inheritance css继承

azimuth, border-collapse, border-spacing,caption-side, color, cursor, direction, elevation,

empty-cells, font-family, font-size, font-style,font-variant, font-weight, font, letter-spacing,

line-height, list-style-image, list-style-position,list-style-type, list-style, orphans, pitch-range,pitch, quotes, richness, speak-header, speak-numeral, speak-punctuation, speak, speech-

rate, stress, text-align, text-indent, text-transform, visibility, voice-family, volume, white-

space, widows, word-spacing

Page 53: Inheritance css继承

font-sizefont-sizefont-sizefont-size????Font-sizeFont-sizeFont-sizeFont-size会被继承吗会被继承吗会被继承吗会被继承吗

Page 54: Inheritance css继承

absolutely “yes”.然而font-size被继承不同于其他的属性

Page 55: Inheritance css继承

被计算出来的值会被继承而不是其初始值

Page 56: Inheritance css继承

在解释font-size如何来继承之前,我们需要知道font-size为什么为什么为什么为什么不是直接继承?

Page 57: Inheritance css继承

让我们来用之前的一个简单的htm代码实例开始

<p>Lorem <em>ipsum</em> dolorsit amet consect etuer.

</p>

Page 58: Inheritance css继承

来看下面HTML结构

Page 59: Inheritance css继承

这里我仅仅仅仅仅仅仅仅只给p元素定义了font-size属性,而em并没有单独定义

p { font-size: 80%; }

Page 60: Inheritance css继承

Font-size的属性值为80%,那么em元素的font-size属性值应该为PPPP的的的的80%80%80%80%

Page 61: Inheritance css继承

所以解释成文档应该像下面这样下面这样下面这样下面这样::::

<em><em><em><em> elementelementelementelement

Page 62: Inheritance css继承

然而事实却不是这样的,em元素与P元素的大小是一样

<em><em><em><em> 元素元素元素元素

Page 63: Inheritance css继承

????

那么font-size属性的继承到底怎样执行的呢

Page 64: Inheritance css继承

我们来看看三个实例三个实例三个实例三个实例

Page 65: Inheritance css继承

我们还是使用与之前一样的

HTML代码:

<p>Lorem <em>ipsum</em> dolorsit amet consect etuer.

</p>

Page 66: Inheritance css继承

欧克,这里的文档结构也是一样的.

Page 67: Inheritance css继承

ExampleExampleExampleExample 1:1:1:1:PixelsPixelsPixelsPixels(单位)(单位)(单位)(单位)

Page 68: Inheritance css继承

我们给p元素的font-size属性的值为 14px14px14px14px.

p { font-size: 14px; }

Page 69: Inheritance css继承

PxPxPxPx属性值属性值属性值属性值(14px)(14px)(14px)(14px)会会会会重写重写重写重写浏览器默认的浏览器默认的浏览器默认的浏览器默认的font-sizefont-sizefont-sizefont-size的值(约为的值(约为的值(约为的值(约为16px16px16px16px)。)。)。)。

新的属性值会被继承。新的属性值会被继承。新的属性值会被继承。新的属性值会被继承。

Page 70: Inheritance css继承

元素 属性值 计算后的属性值

默认font size 约16px

<body> 未单独定义 约 16px

<p> 14px 14px

<em> 未单独定义 继承值 = 14px

所以em元素继承了14px属性值.

Page 71: Inheritance css继承

ExampleExampleExampleExample 2:2:2:2:百分比百分比百分比百分比

Page 72: Inheritance css继承

给p 元素 font-size属性值为 85%85%85%85%.

p { font-size: 85%; }

Page 73: Inheritance css继承

浏览器默认的浏览器默认的浏览器默认的浏览器默认的font-sizefont-sizefont-sizefont-size值为值为值为值为16px16px16px16px,,,,那么计算出值为那么计算出值为那么计算出值为那么计算出值为16px x 85% =13.6px

Page 74: Inheritance css继承

元素 属性值 计算后的属性值

默 认 的 fontsize

约16px

<body> 未单独定义 约16px

<p> 85% 16px x 85% = 13.6px

<em> 未单独定义 继承属性值 = 13.6px

所以em元素会继承13.6px这个计算出来的值

Page 75: Inheritance css继承

ExampleExampleExampleExample 3:3:3:3:EMsEMsEMsEMs

Page 76: Inheritance css继承

给<p>元素 font-size属性值为 .85em.85em.85em.85em

p { font-size: .85em; }

Page 77: Inheritance css继承

欧克,我们再来算一算哦欧克,我们再来算一算哦欧克,我们再来算一算哦欧克,我们再来算一算哦

16px x .85em = 13.6px

Page 78: Inheritance css继承

元素 属性值 计算后的属性值

默认 font size 约 16px

<body> 未单独定义 约16px

<p> .85em 16px x .85em = 13.6px

<em> 未单独定义 继承属性值 = 13.6px

所以所以所以所以emememem元素继承属性值为元素继承属性值为元素继承属性值为元素继承属性值为13.6px13.6px13.6px13.6px .

Page 79: Inheritance css继承

这些实例都太简单了。在有比较多的标签

而相对复杂的实例里是这样吗?

Page 80: Inheritance css继承

ExampleExampleExampleExample 4:4:4:4:

Page 81: Inheritance css继承

所有的标签都定义了font-size单位为百分比的

属性值.

body { font-size: 85%; }h1 { font-size: 200%; }h2 { font-size: 150%; }

Page 82: Inheritance css继承

浏览器默认的font-size为16pxbody元素font-size属性值为85%

计算出16px x 85% =13.6px这个值将被后代继承后代继承后代继承后代继承除非后代元素重新定义

Page 83: Inheritance css继承

元素 属性值 计算font-size

默认font size 约16px

<body> 85% 16px x 85% = 13.6px

<h1> 200% 继承值为 13.6px x 200% = 27.2px

<h2> 150% 继承值为 13.6px x 150% = 20.4px

<p> 未单独定义 继承值为 = 13.6px

<em> 未单独定义 继承值为 = 13.6px

font-sizefont-sizefont-sizefont-size属性继承情况如下

Page 84: Inheritance css继承

使用继承来使用继承来使用继承来使用继承来提高提高提高提高我们的效率我们的效率我们的效率我们的效率

Page 85: Inheritance css继承

开发者能用继承性来写出高效的CSS文件

Page 86: Inheritance css继承

For example,我们可以对body元素设置color, font-size and font-family 属性

body {color: #222;font-family: arial,helvetica, sans-serif;font-size: 90%;

}

Page 87: Inheritance css继承

所有的后代元素都会继承来自上面对body的属性设置.

Page 88: Inheritance css继承

如果需要,您可以重写重写重写重写新的属性。

Page 89: Inheritance css继承

body {color: #222;font-family: arial,helvetica, sans-serif;font-size: 90%;

}

h1, h2, h3 { color: green; }h4, h5, h6 { color: black; }

Page 90: Inheritance css继承

And,新的font-familyfont-familyfont-familyfont-family属性

Page 91: Inheritance css继承

body {color: #222;font-family: arial,helvetica, sans-serif;font-size: 90%;

}

h1, h2, h3 { color: green; }h4, h5, h6 { color: black; }

h1, h2, h3, h4, h5, h6 {font-family: georgia,times, serif;

}

Page 92: Inheritance css继承

And,新的font-sizefont-sizefont-sizefont-size属性

Page 93: Inheritance css继承

}

h1, h2, h3 { color: green; }h4, h5, h6 { color: black; }

h1, h2, h3, h4, h5, h6 {font-family: georgia,times, serif;

}

h1 { font-size: 200%; }h2 { font-size: 150%; }h3 { font-size: 125%; }#footer { font-size: 90%; }

Page 94: Inheritance css继承

WeWeWeWe’’’’rererere done!done!done!done!Connect::::Name:AlexE-mail:[email protected]:http://ued.iciba.com/QQ:303683080

声明:

本文翻译自http://www.slideshare.net/maxdesign/css-inheritance-a-simple-stepbystep-tutorial?from=ss_embed作者:Russ Weakley(http://www.slideshare.net/maxdesign/)本译文仅供技术交流使用,转载请说明出处,禁止从事商业用途