CSS 盒子模型

CSS 盒子模型

盒子模型

作用:布局网页,摆放盒子和内容

盒子尺寸和背景色

属性名作用
width宽度
height高度
background-color背景色

盒子模型 — 组成

内容区域 — width & height

内边距 — padding

边框线 — border

外边距 — margin

div {
    margin: 50px;
    border: 5px solid skyblue;
    padding: 20px;
    width: 200px;
    height: 200px;
    background-color: aqua;
}

在这里插入图片描述

边框线

四个方向

属性名:border

属性值:边框线粗细 线条样式 颜色(不区分顺序)

属性值线条样式
solid实线
dashed虚线
dotted点线

单方向边框线

属性名:border + 方位名词

属性值:边框线粗细 线条样式 颜色(不区分顺序)

属性名说明
border-top上边框线
border-bottom下边框线
border-left左边框线
border-right右边框线

内边距

作用:设置内容与盒子边缘之间的距离

  • 属性名

四个方向:padding

单个方向:padding + 方位名词

属性名说明
padding-top上边框线
padding-bottom下边框线
padding-left左边框线
padding-right右边框线

提示:添加 padding 会撑大盒子

  • padding 多值写法
取值个数示例含义
一个值padding: 10px四个方向一致
四个值padding: 10px 20px 40px 80px上 、右 、下 、左
三个值padding: 10px 20px 80px上 、左右 、下
两个值padding: 10px 80px上下 、左右

记忆: 从上开始顺时针赋值,当前方向没有数值则与对面取值相同

尺寸计算

默认情况:盒子尺寸 = 内容尺寸 + border尺寸 + padding尺寸

在这里插入图片描述

结论:给盒子加 border / padding 会撑大盒子

解决方法

手动做减法,减掉 border / padding 的尺寸

内减模式:box-sizing: border-box

div {
    width: 200px;
    height: 200px;
    background-color: blue;
    padding: 20px;
    box-sizing: border-box;
}

外边距

作用:拉开两个盒子之间的距离

属性名:margin

属性值:与 padding 属性值的写法一致

注意:外边距不会撑大盒子

版心居中

左右 margin 值伪 auto

div {
    width: 800px;
    height: 200px;
    background-color: blue;
    margin: 0 auto;	/* 版心居中 */
}

清除默认样式

作用: 清除标签的默认样式

* {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
}

/* 去掉列表的项目符号 */
li {
    list-style: none;
}

在这里插入图片描述

元素溢出

作用:控制溢出元素的内容的显示方式

属性名:overflow

属性值效果
hidden溢出隐藏
scroll溢出滚动(无论是否溢出,都显示滚动条位置)
auto溢出滚动(溢出才显示滚动条位置)

外边距问题

合并现象

场景:垂直排列的兄弟元素,上下 margin 会合并

现象:取两个 margin 中的较大值生效
在这里插入图片描述

.one {
    width: 100px;
    height: 100px;
    background-color: skyblue;
    margin-bottom: 50px;
}

.two {
    width: 100px;
    height: 100px;
    background-color: violet;
    margin-top: 20px;
}

外边距塌陷

场景:父子级标签,子级添加上边距会产生塌陷问题

现象:导致父级一起向下移动

在这里插入图片描述

.father {
    width: 200px;
    height: 200px;
    background-color: aqua;
}

.son {
    width: 100px;
    height: 100px;
    background-color: violet;
    margin-top: 50px;
}

解决方法

  • 方法一
.father {
    width: 200px;
    height: 200px;
    background-color: aqua;
    padding-top: 50px;
    box-sizing: border-box;
}

.son {
    width: 100px;
    height: 100px;
    background-color: violet;
    /* margin-top: 50px; */
}
  • 方法二
.father {
    width: 200px;
    height: 200px;
    background-color: aqua;
    overflow: hidden;
}

.son {
    width: 100px;
    height: 100px;
    background-color: violet;
    margin-top: 50px;
}
  • 方法三
.father {
    width: 200px;
    height: 200px;
    background-color: aqua;
    border-top: 1px solid black;
}

.son {
    width: 100px;
    height: 100px;
    background-color: violet;
    margin-top: 50px;
}

行内元素 — 内外边距问题

场景:行内元素添加 margin 和 padding ,无法改变元素垂直位置

解决方法:给行内元素添加 line-height 可以改变垂直位置

span {
    /* 行高可以改变垂直位置 */
    line-height: 50px;
}

圆角

作用:设置元素的外边框为圆角

属性名:border-radius

属性值:属性值+px / 百分比

提示:属性值是圆角半径

多值写法

取值个数示例含义
一个值border-radius: 10px四个角一致
四个值border-radius: 10px 20px 40px 80px左上 、右上 、右下 、左下
三个值border-radius: 10px 20px 80px左上 、右上+左下 、右下
两个值border-radius: 10px 80px左上+右下 、右上+左下

记忆:从左上角开始顺时针赋值,当前角没有数值则与对角取值相同

正圆形状

给正方形盒子设置圆角属性值为 宽高的一半 / 50%

img {
    width: 200px;
    height: 200px;
    border-radius: 100px;
    /* border-radius: 50% */
}

胶囊形状

给长方形盒子设置圆角属性值为 盒子高度的一半

div {
    width: 200px;
    height: 80px;
    background-color: skyblue;
    border-radius: 40px;
}

盒子阴影

作用:给元素设置阴影效果

属性名:box-showdow

属性值:X轴偏移量 Y轴偏移量 模糊半径 扩散半径 颜色 内外阴影

div {
    width: 200px;
    height: 80px;
    background-color: skyblue;
    box-shadow: 2px 5px inset;
}

注意:

  • X轴偏移量 和 Y轴偏移量是必须属性值
  • 默认是外阴影,内阴影需要添加 inset
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值