
- CSS 教程
- CSS - 首頁
- CSS - 路線圖
- CSS - 簡介
- CSS - 語法
- CSS - 選擇器
- CSS - 匯入
- CSS - 度量單位
- CSS - 顏色
- CSS - 背景
- CSS - 字型
- CSS - 文字
- CSS - 圖片
- CSS - 連結
- CSS - 表格
- CSS - 邊框
- CSS - 塊級邊框
- CSS - 行內邊框
- CSS - 外邊距
- CSS - 列表
- CSS - 內邊距
- CSS - 游標
- CSS - 輪廓
- CSS - 尺寸
- CSS - 捲軸
- CSS - 行內塊
- CSS - 下拉選單
- CSS - 可見性
- CSS - 溢位
- CSS - 清除浮動
- CSS - 浮動
- CSS - 箭頭
- CSS - 調整大小
- CSS - 引號
- CSS - 順序
- CSS - 定位
- CSS - 連字元
- CSS - 懸停
- CSS - 顯示
- CSS - 焦點
- CSS - 縮放
- CSS - 位移
- CSS - 高度
- CSS - 連字元字元
- CSS - 寬度
- CSS - 透明度
- CSS - z-index
- CSS - 底部
- CSS - 導航欄
- CSS - 覆蓋層
- CSS - 表單
- CSS - 對齊
- CSS - 圖示
- CSS - 圖片庫
- CSS - 註釋
- CSS - 載入器
- CSS - 屬性選擇器
- CSS - 組合器
- CSS - 根元素
- CSS - 盒模型
- CSS - 計數器
- CSS - 剪裁
- CSS - 書寫模式
- CSS - Unicode-bidi
- CSS - min-content
- CSS - all
- CSS - inset
- CSS - isolation
- CSS - overscroll
- CSS - justify-items
- CSS - justify-self
- CSS - tab-size
- CSS - pointer-events
- CSS - place-content
- CSS - place-items
- CSS - place-self
- CSS - max-block-size
- CSS - min-block-size
- CSS - mix-blend-mode
- CSS - max-inline-size
- CSS - min-inline-size
- CSS - offset
- CSS - accent-color
- CSS - user-select
- CSS 高階
- CSS - 網格
- CSS - 網格佈局
- CSS - Flexbox
- CSS - 可見性
- CSS - 定位
- CSS - 層
- CSS - 偽類
- CSS - 偽元素
- CSS - @規則
- CSS - 文字效果
- CSS - 分頁媒體
- CSS - 列印
- CSS - 佈局
- CSS - 驗證
- CSS - 圖片精靈
- CSS - !important
- CSS - 資料型別
- CSS3 教程
- CSS3 - 教程
- CSS - 圓角
- CSS - 邊框圖片
- CSS - 多重背景
- CSS - 顏色
- CSS - 漸變
- CSS - 盒陰影
- CSS - box-decoration-break
- CSS - caret-color
- CSS - 文字陰影
- CSS - 文字
- CSS - 2D 變換
- CSS - 3D 變換
- CSS - 過渡
- CSS - 動畫
- CSS - 多列
- CSS - 盒大小
- CSS - 工具提示
- CSS - 按鈕
- CSS - 分頁
- CSS - 變數
- CSS - 媒體查詢
- CSS - 函式
- CSS - 數學函式
- CSS - 遮罩
- CSS - 形狀
- CSS - 樣式圖片
- CSS - 特效性
- CSS - 自定義屬性
- CSS 響應式
- CSS RWD - 簡介
- CSS RWD - 視口
- CSS RWD - 網格檢視
- CSS RWD - 媒體查詢
- CSS RWD - 圖片
- CSS RWD - 影片
- CSS RWD - 框架
- CSS 工具
- CSS - px到em轉換器
- CSS - 顏色選擇器和動畫
- CSS 資源
- CSS - 有用資源
- CSS - 討論
CSS - box-sizing 屬性
CSS box-sizing 屬性用於定義元素的總寬度和高度是如何計算的。預設情況下,元素的寬度和高度包含其內容、內邊距和邊框。但是,使用box-sizing 屬性,我們可以更改此行為,以包含或排除內邊距和邊框在寬度和高度計算中的影響。
語法
box-sizing: content-box | border-box | initial | inherit;
屬性值
值 | 描述 |
---|---|
content-box | 返回預設行為,其中內邊距和/或邊框會新增到元素的總寬度和高度中。預設值。 |
border-box | 它告訴瀏覽器調整傳遞給元素的內邊距或外邊距值。這會導致內容區域縮小,並吸收為元素指定的邊框和/或內邊距。 |
initial | 這將屬性設定為其預設值。 |
inherit | 這從父元素繼承屬性。 |
CSS 盒大小屬性示例
以下示例解釋了使用不同值的box-sizing 屬性。
使用 content-box 的盒大小屬性
為了使元素的寬度依賴於為元素指定的內邊距和邊框,使得總寬度成為元素的指定寬度、內邊距和外邊距之和,我們使用content-box 值。這在以下示例中顯示。
示例
<!DOCTYPE html> <html> <head> <style> .sample-box { width: 200px; height: 100px; background-color: rgb(241, 234, 85); } .content-box { width: 200px; height: 100px; padding: 20px; border: 3px solid rgb(64, 10, 215); background-color: lightpink; box-sizing: content-box; } </style> </head> <body> <h2> CSS box-sizing property </h2> <p> Content-box width = specified width + padding (left and right)+ border(left and right), so the size changes. </p> <div class="sample-box"> SAMPLE BOX <p> This box has a width: 200px padding: 0px, border: 0px, total width: 200px </p> </div><br/> <div class="content-box"> CONTENT BOX <p> This box has a width: 200px, padding: 20px, border:3px, total width: 246px, see the width of this box has increased </p> </div> </body> </html>
使用 border-box 的盒大小屬性
為了使元素的寬度獨立於為元素指定的內邊距和邊框,使得總寬度包含內邊距和外邊距,同時仍保持指定的寬度,我們使用border-box 值。這在以下示例中顯示。
示例
<!DOCTYPE html> <html> <head> <style> .sample-box { width: 200px; height: 100px; background-color: rgb(241, 234, 85); } .content-box { width: 200px; height: 100px; padding: 4px; border: 3px solid rgb(64, 10, 215); background-color: lightgreen; box-sizing: border-box; } </style> </head> <body> <h2> CSS box-sizing property </h2> <p> border-box width = specified width (includes padding and boder), see the boxes, the size does not change. </p> <div class="sample-box"> SAMPLE BOX <p> This box has a width: 200px padding: 0px, border:0px, total width: 200px </p> </div><br/> <div class="content-box"> BORDER BOX <p> This box has a width: 200px, padding: 4px, border:3px, total width:200px </p> </div> </body> </html>
border-box 值的特性
由於 border-box 值不會影響元素在有無內邊距和/或邊框的情況下寬度,因此它最受開發人員歡迎,因為它避免了元素寬度計算。以下示例顯示了結構。
示例
<!DOCTYPE html> <html> <head> <style> *{ box-sizing: border-box; } .sample1{ width: 300px; padding: 10px; border: 10px solid green; } .sample2{ width: 300px; background-color: lightblue; } </style> </head> <body> <h2> CSS box-sizing property </h2> <h3 class="sample1"> This is a h3 heading- the width is 300px only despite having padding and border. </h3> <p class="sample2"> This is a paragraph- the width is 300px without padding and border. </p> </body> </html>
支援的瀏覽器
屬性 | ![]() |
![]() |
![]() |
![]() |
![]() |
---|---|---|---|---|---|
box-sizing | 10.0 | 8.0 | 29.0 | 5.1 | 9.5 |
css_properties_reference.htm
廣告