-
默认值
- left top right bottom width height: 默认值为 auto
- margin padding: 默认值为 0
-
百分比参照于谁
- width margin padding: 包含块的width
- height: 包含块的height
- left: 包含块的width
- top: 包含块的height
-
width: auto与width: 100%区别
- width: auto
子元素(包括content+padding+border+margin)撑满整个父元素的content区域。
子元素有margin、border、padding时,会减去子元素content区域相对应的width值
父元素的content = 子元素(content + padding + border + margin )
- width: 100%
强制将子元素的content区域 撑满 父元素的content区域
子元素有margin、border、padding时,不改变子元素content区域的width,而是溢出父盒子,保持原有值
父元素的content = 子元素的content
简单的例子:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>test</title>
<style type="text/css">
* {
margin: 0;
padding: 0;
}
.box {
margin:50px auto;
width: 400px;
border: 3px solid red;
padding: 0 50px;
}
.box1 {
width: auto;
height: 100px;
background: pink;
padding: 0 50px;
margin: 0 50px;
border-width: 0 50px;
border-style: solid;
border-color: green;
}
.box2 {
width: 100%;
height: 100px;
background: gold;
padding: 0 50px;
margin: 0 50px;
border-width: 0 50px;
border-style: solid;
border-color: green;
}
.box3 {
width: 100px;height: 100px;background: orange;
}
.box4 {
float: left;
width: 50px;height: 50px;background: pink;
}
</style>
</head>
<body>
<div class="box">
<div class="box1"></div>
<div class="box2"></div>
</div>
</body>
</html>