9、CSS盒子模型
9.1 CSS常用长度单位
<style>
html {
font-size: 50px;
}
/* px(像素单位) */
#div1 {
height: 200px;
width: 200px;
font-size: 20px;
background-color: aqua;
}
/* em(相对于当前元素font-size的倍数),若没有font-size,则在祖先元素中寻找,祖先元素中都不存在则采用浏览器默认值 */
#div2 {
height: 10em;
width: 10em;
font-size: 20px;
background-color: yellow;
}
/* rem(相对于根元素html的font-size)的倍数 */
#div3 {
height: 4rem;
width: 4rem;
font-size: 20px;
background-color: blue;
}
#div4 {
height: 200px;
width: 200px;
font-size: 20px;
background-color: gray;
}
/* 相对其父元素的百分比 */
.div5 {
width: 50%;
height: 20%;
font-size: 150%;
background-color: red;
}
</style>
9.2 元素的显示模式
-
块元素
9.3 修改元素的显示模式
<style>
div {
height: 400px;
width: 400px;
font-size: 40px;
display: inline-block;
}
</style>
10、盒子模型的组成部分
- margin不影响盒子大小,只影响盒子位置。
<style>
div {
/* 内容区的宽 */
width: 400px;
/* 内容区的高 */
height: 400px;
/* 内边距,背景颜色填充到内边距区域 */
padding: 10px;
/* 边框,背景颜色填充到边框区域 */
border: 20px dashed burlywood;
/* 外边距 */
margin: 50px;
font-size: 20px;
background-color: gray;
}
</style>
10.1 内容区
CSS属性 | 功能 | 属性值 |
---|---|---|
width | 设置内容区域的宽度 | 长度 |
min-width | 设置内容区域的最小宽度 | 长度 |
max-width | 设置内容区域的最大宽度 | 长度 |
height | 设置内容区域的高度 | 长度 |
min-height | 设置内容区域的最小高度 | 长度 |
max-height | 设置内容区域的最大高度 | 长度 |
- width一般不与min-width、max-width使用
- height一般不与min-height、max-height使用