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

盒模型与box-sizing #131

Open
TieMuZhen opened this issue Mar 17, 2022 · 0 comments
Open

盒模型与box-sizing #131

TieMuZhen opened this issue Mar 17, 2022 · 0 comments
Labels

Comments

@TieMuZhen
Copy link
Owner

TieMuZhen commented Mar 17, 2022

盒模型一共分为两种:标准盒模型和怪异盒模型,只要在代码头部添加了doctype声明,浏览器就会默认该页面为标准盒模型,如果不添加doctype声明,就看浏览器想用哪一个了,但是基本上浏览器都默认使用标准盒模型,一般很少使用怪异盒模型。也可以通过box-sizing属性设置。

box-sizing: content-box(标准盒模型) \ border-box(怪异盒模型)

标准盒模型:(w3c盒子模型)是一般网页经常使用的一种。

一个盒子的总宽度 = width + padding(左右) + border(左右) + margin(左右)
image

2、怪异盒模型:(ie盒子模型)主要表现在IE内核的浏览器

当不对doctype进行定义时,会触发怪异模式。
一个盒子的总宽度 = width + margin(左右),(即width已经包含了paddingborder值)
image

标准盒模型

<!DOCTYPE html>
<html>
 <head>
  <meta charset="utf-8" />
  <title>标准盒模型</title>
  <style type="text/css">
   *{
      margin: 0;
      padding: 0;
   }
   .box{
      box-sizing: content-box;
      width: 100px;
      height: 100px;
      border: 2px solid green;
      padding: 5px;
      margin: 30px 0px 0px 300px;
      background:red;
    }
  </style>
 </head>
 <body>
  <div class="box"></div>
 </body>
</html>

怪异盒模型

<!DOCTYPE html>
<html>
 <head>
  <meta charset="UTF-8">
  <title>怪异盒模型</title>
  <style type="text/css">
   *{
      margin: 0;
      padding: 0;
   }
   .box{
      box-sizing: border-box;
      width: 100px;
      height: 100px;
      border: 2px solid red;
      padding: 5px;
      margin: 30px 0px 0px 300px;
      background:forestgreen;
   }
  </style>
 </head>
 <body>
  <div class="box"></div>
 </body>
</html>

其实它们主要就差在内容区域上。
左边是标准盒模型内容区宽度为100(width=100),在内容区以外添加其他属性。
右边是怪异盒模型内容区宽度为86(width=2+5+86+5+2),把boderpadding属性都包含在内容区中。
image

@TieMuZhen TieMuZhen added the CSS label Mar 17, 2022
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