Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CSS Box Model 盒子模型 #73

Closed
PolluxLee opened this issue Jul 29, 2018 · 0 comments
Closed

CSS Box Model 盒子模型 #73

PolluxLee opened this issue Jul 29, 2018 · 0 comments
Labels

Comments

@PolluxLee
Copy link
Owner

PolluxLee commented Jul 29, 2018

# 标准盒模型

实际宽度不包括 paddingborder

width = content
height = content

# 怪异盒子

IE6 及以下在 怪异模式 下,会把盒子解析为怪异盒子,实际宽度包括 paddingborder

width = content + padding + border
height= content + padding + border

# CSS3 box-sizing 属性

可以随意切换 box-sizing 来告诉浏览器如何解析盒子,该属性有三个值

  • content-box: width (或 height) = content 标准盒模型
  • padding-box: width (或 height) = content + padding
  • border-box: width (或 height) = content + padding + border 怪异盒模型

IE 7 及以下完全不支持 box-sizing 属性,需要 Polyfill

# 在 ResetCSS 中设置 border-box

  • 用继承根元素属性的方式设置 border-box 使得对于单独设置 content-boxpadding-box 变得无比方便,因为设置其他类型的盒模型之后,其子盒子都继承此属性
  • 当今主流的浏览器都支持 box-sizing 属性,但如果需要支持旧版本的浏览器, Safari (< 5.1), Chrome (< 10), and Firefox (< 29),就需要添加 -webkit-moz
html {
  -webkit-box-sizing: border-box;
  -moz-box-sizing: border-box;
  box-sizing: border-box;
}
*, *:before, *:after {
  -webkit-box-sizing: inherit;
  -moz-box-sizing: inherit;
  box-sizing: inherit;
}

# 参考

https://css-tricks.com/box-sizing/

https://developer.mozilla.org/zh-CN/docs/Web/CSS/box-sizing

https://github.com/Schepp/box-sizing-polyfill

https://developer.mozilla.org/zh-CN/docs/Web/CSS/CSS_Box_Model/Introduction_to_the_CSS_box_model

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant