1. 程式人生 > >CSS-兩種盒模型 box-sizing

CSS-兩種盒模型 box-sizing

CSS-兩種盒模型

box-sizing: content-box (W3C盒子)| border-box (IE盒子);

這裡寫圖片描述

區別: 設定為 border-box 後,width包含padding,border,整個盒子大小為 margin + width | height. 設定為content-box,width不包含padding,border,整個盒子大小為 margin + border + padding +width | height .

<!DOCTYPE html>
<html lang="en">
<head
>
<meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>Document</title> <style> body { margin: 0; text-align: center
} .content-box{ width: 100px; height: 100px; box-sizing: content-box; padding: 10px; border: 5px solid lightblue; background-color: blueviolet; margin:auto; } .border-box{ width: 100px; height: 100px; box-sizing: border-box
; padding: 10px; border: 5px solid lightblue; background-color: blueviolet; margin:auto; }
</style> </head> <body> <div style="height: 200px;"></div> <div class="content-box">content-box</div> <div style="height: 200px;"></div> <div class="border-box">border-box</div> </body> </html>