30秒学会 CSS 片段 · 2019年9月25日

30秒学会 CSS 片段 – Box-sizing reset

Resets the box-model so that width and height are not affected by border or padding.

  • box-sizing: border-box makes the addition of padding or borders not affect an element’s width or height.
  • box-sizing: inherit makes an element respect its parent’s box-sizing rule.

预览

border-box
content-box



HTML

<div class="box">border-box</div>
<div class="box content-box">content-box</div>

CSS

div {
  box-sizing: border-box;
}

*,
*:before,
*:after {
  box-sizing: inherit;
}

.box {
  display: inline-block;
  width: 120px;
  height: 120px;
  padding: 8px;
  background: #F24333;
  color: white;
  border: 1px solid #BA1B1D;
  border-radius: 4px;
}

.content-box {
  box-sizing: content-box;
}

翻译自:https://www.30secondsofcode.org/css/s/box-sizing-reset