--- title: box-sizing slug: Web/CSS/box-sizing tags: - CSS - CSS Box Model - CSS Property - Experimental - Reference - border-box - box model - content-box translation_of: Web/CSS/box-sizing ---
The box-sizing
property is used to alter the default CSS box model used to calculate width and height of the elements. It is possible to use this property to emulate the behavior of browsers that do not correctly support the CSS box model specification.
box-sizing
屬性 用於更改預設 CSS 盒子模型 中所計算的寬度和高度。可以使用此屬性來模擬不正確支持CSS盒子模型規範的瀏覽器的行為。
{{cssinfo}}
/* Keyword values */ box-sizing: content-box; box-sizing: border-box; /* Global values */ box-sizing: inherit; box-sizing: initial; box-sizing: unset;
content-box
.box {width: 350px}; 而且 {border: 10px solid black;}
,那麼在瀏覽器中的渲染該容器的實際寬度將是370px,;border-box
.box {width: 350px; border: 10px solid black;}
,渲染出的容器寬度會固定在 350px,而內容(content)的寬度就會變成 330px, 因為邊框(border)佔了20px。內容框不能為負,並且進位到 0,使得不可能使用 border-box 使元素消失。padding-box
{{non-standard_inline}} {{deprecated_inline}}{{csssyntax}}
/* support Firefox, Chrome, Safari, Opera, IE8+ and old Android */ .example { -moz-box-sizing: border-box; box-sizing: border-box; }
Specification | Status | Comment |
---|---|---|
{{SpecName('CSS3 Basic UI', '#box-sizing', 'box-sizing')}} | {{Spec2('CSS3 Basic UI')}} | Initial definition |
{{Compat("css.properties.box-sizing")}}