box model

51
2011-04-19 Box Model

Upload: jay-li

Post on 28-Jan-2015

1.790 views

Category:

Entertainment & Humor


7 download

DESCRIPTION

盒模型_兼容性问题_hasLayout

TRANSCRIPT

Page 1: box model

2011-04-19

Box Model

Page 2: box model

• 拔赤淘宝前端开发工程师

http://jayli.github.com

Page 3: box model

Hack !

Page 4: box model

#box {color:black; /*firefox*/color:red\9; /*所有ie*/*color:blue; /*ie7*/_color:green; /*ie6*/

}

Page 5: box model

CSS Basic Box Model

Page 6: box model
Page 7: box model

#box {border:20px solid red;padding:20px;width:100px;height:100px;

}#box p {width:100%;height:100%;

}

<div id=“box”><p>#box</p>

</div>

Page 8: box model

Firefox with W3C box layout

Page 9: box model

IE with W3C box layout

Page 10: box model

IE with W3C box layout

Page 11: box model

What about IE with Quirks Mode?

Page 12: box model

IE with Quirks box layout

Page 13: box model

指定 Doctype!

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" " http://www.w3.org/TR/html4/strict.dtd">

<!DOCTYPE HTML>

Page 14: box model

IE6 Double Margin

Page 15: box model

float:leftmargin-left:100px

IE6

!IE6

float:leftmargin-left:100px

Page 16: box model

This is a bug!

Page 17: box model

float:leftmargin-left:100pxdisplay:inline

Fix this bug

Page 18: box model

IE hasLayout

Page 19: box model

div#box {background:red;filter:alpha(opacity=50);

}

<!DOCTYPE html>…<div id=“box”>text

</div>

Page 20: box model

Filter dosn’t work in IE6/7

Filter is ok in IE8

Page 21: box model

div#box {background:red;filter:alpha(opacity=50);

}

<!DOCTYPE html>…<div id=“box”>text

</div>

在IE6/7下没有hasLayout

Page 22: box model

没有 hasLayout 的元素上Filter 无作用

Page 23: box model

Why?

Page 24: box model

降低渲染压力

Page 25: box model

• div• span

默认不具有 hasLayout

Page 26: box model

激活元素的 hasLayout• position• float• width(!auto)• height(!auto)• zoom• overflow

Page 27: box model

div#box {background:red;filter:alpha(opacity=50);height:19px;

}

<!DOCTYPE html>…<div id=“box”>text

</div>

让div具有hasLayout

Page 28: box model

Filter is ok in IE6/7

Filter is ok in IE8

Page 29: box model

Warning !!!

Page 30: box model

hasLayout 元素呈现为”矩形”

Page 31: box model

I want this !

Page 32: box model

#pic {float:left;width:50px;height:50px;

}#content {border:1px solid blue;background:yellow;height:100%;

}

<div><div id=“pic”></div><div id=“content”>long text…</span>

</div>

Page 33: box model

It’s correct in Firefox 3.6

It’s correct in IE8

Page 34: box model

It’s incorrect in IE7

It’s incorrect in IE6

Page 35: box model

#pic {float:left;width:50px;height:50px;

}#content {border:1px solid blue;background:yellow;height:100%;

}

<div><div id=“pic”></div><div id=“content”>long text…</span>

</div>

height触发了#content的hasLayout

Page 36: box model

#pic {float:left;width:50px;height:50px;

}#content {border:1px solid blue;background:yellow;height:100%;

}

<div><div id=“pic”></div><div id=“content”>long text…</span>

</div>

height触发了#content的hasLayout

IE6/7�下 #content�显示为矩形

Page 37: box model

#pic {float:left;width:50px;height:50px;

}#content {border:1px solid blue;background:yellow;height:100%;

}

<div style=“background:yellow”><div id=“pic”></div><div id=“content”>long text…</span>

</div>

样式定义在wrapper上

Page 38: box model

It’s correct in IE7

It’s correct in IE6

Page 39: box model

Margin Overlap

Page 40: box model

Rules

• 水平margin不重叠

• 浮动元素上下margin不重叠(float)• 飘动元素上下margin不重叠(position)• 常规折行上下margin重叠

Page 41: box model

<div id=“wrapper”><p></p><p></p>…

</div>

#wrapper {width:500px;

}#wrapper p{width:100px;margin:50px;float:left;

}

Page 42: box model

margin:50px

margin:50px100px

浮动元素上下margin不重叠

Page 43: box model

<div id=“wrapper”><p></p><p></p>…

</div>

#wrapper {width:500px;

}#wrapper p{width:100px;margin:50px;float:left;

}

如果去掉浮动?

Page 44: box model

margin:50px

无浮动/飘动元素上下margin重叠

Page 45: box model

More & More…

Page 46: box model

秘籍?

Page 47: box model

知己知彼,百战不殆!

Page 48: box model

战争是持久的,胜利是我们的!

Page 49: box model

鉴定信念,战胜 IE6!

Page 51: box model