CSS中的border-box和content-box有什么区别?

时间:2017-06-09 08:50:36

标签: css html5 css3

CSS中的border-box和content-box有什么区别? 我不清楚这两个盒子之间的关系。例如,  box-sizing:border-box;box-sizing:content-box; 这两种风格的输出看起来很相似。

1 个答案:

答案 0 :(得分:11)

大小调整 border-box ;使用人们已经与Internet Explorer关联的盒子模型,其中填充和边框的尺寸包含在元素的尺寸中。 enter image description here (图片source

示例:

enter image description here (图片source

添加了演示。



$("#content").on("click", function() {
  $("*").css("box-sizing", $(this).text());
});

$("#border").on("click", function() {
  $("*").css("box-sizing", $(this).text());
});

.parent {
  width: 50%;
  border: 5px solid #E18728;
  float: left;
}

.child {
  width: 90%;
  padding: 20%;
  border: 4px solid black;
  margin: .5em auto;
}

.twins {
  width: 50%;
  padding: 1em;
  border: 4px solid black;
  float: left;
}


/* styling for Pen, not related to box-sizing or layout */

body {
  font-family: sans-serif;
  font-size: 1.1em;
}

.buttons {
  margin-bottom: .5em;
}

p:not(.intro) {
  text-align: center;
}

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="buttons">
  <p class="intro">Click the <code>border-box</code> button to fix the layout with the power of Box Sizing!</p>
  <button id="content">content-box</button>
  <button id="border">border-box</button>
</div>
<div class="parent">
  <p>Parent div with 50% width.</p>
  <div class="child">
    <p>Child div with 90% width, 4px black border, and 20% padding </p>
  </div>
  <div class="twins">
    <p>Child div with 50% width, 4px black border, and 1em padding</p>
  </div>
  <div class="twins">
    <p>Child div with 50% width, 4px black border, and 1em padding</p>
  </div>
</div>
&#13;
&#13;
&#13;