【前端从0开始】CSS——8、盒模型


1 盒模型

1.1 CSS宽度(width)

1.1.1 定义

width 属性设置元素的宽度。

说明:这个属性定义元素内容区的宽度。它不包括内边距、边框和外边距。

取值:

  • auto:元素实际宽度
  • length:设置像素级px宽度
  • %:基于父级元素的百分比宽度
  • inherit:从父级元素继承宽度

1.1.2 用法

.auto {
    width:auto;
}
.length {
    width:200px;
}
.pct {
    width:100%;
}
.inherit {
    width:inherit;
}

1.2 CSS高度(height)

1.2.1 定义

height 属性设置元素的高度。

注意:height 属性不包括填充,边框,或边距。

取值:

  • auto:元素实际高度
  • length:设置像素级px高度
  • %:基于父级元素的百分比高度
  • inherit:从父级元素继承高度

1.2.2 用法

.auto {
    height:auto;
}
.length {
    height:200px;
}
.pct {
    height:100%;
}
.inherit {
    height:inherit;
}

1.3 CSS内边距(padding)

1.3.1 定义

padding 简写属性设置所有内边距属性。

补充:

  • 非替换元素:元素的内容包含在文档中
  • 替换元素:作为其他内容的占位符

注意:不允许指定负边距值。

取值:

  • auto:自动计算内边距
  • length:设置像素级px内边距
  • %:基于父级元素宽度的百分比内边距
  • inherit:从父级元素继承内边距

1.3.2 用法

css

p {
    padding:10px 5px 15px 20px;
    padding:10px 5px 15px;
    padding:10px 5px;
    padding:10px;
}

1.4 CSS外边距(margin)

1.4.1 定义

margin 简写属性设置所有外边距属性。

注意:允许使用负值。

取值:

  • auto:自动计算外边距
  • length:设置像素级px外边距
  • %:基于父级元素宽度的百分比外边距
  • inherit:从父级元素继承外边距

1.4.2 用法

css

p {
    margin:10px 5px 15px 20px;
    margin:10px 5px 15px;
    margin:10px 5px;
    margin:10px;
}

1.5 CSS边框 (border)

1.5.1 定义

border 简写属性设置所有的边框属性。

取值:

  • border-width
  • border-style:例如 solid实线, dotted点状线, double双实线, dashed虚线
  • border-color

1.5.2 用法

p {
    border:5px solid #ff0000;
}
p {
    border-top:10px solid red;
    border-left:20px dotted green;
    border-right:30px double orange;
    border-bottom:40px dashed red;
}
div {
    width:0;
    height:0;
    border:10px solid transparent;
    border-top-color:red;
}

1.5.3 边框圆角(border-radius)

div{
	width: 300px;
	height: 200px;
	background-color: cadetblue;
	border-radius: 50%;
}

2 盒模型

2.1 定义

盒模型描述元素在页面中占用的空间。

  • W3C 标准模型

    • 总宽度 = 设置宽度 + 左填充 + 右填充 + 左边框 + 右边框 + 左边距 + 右边距
    • 总高度 = 设置高度 + 顶部填充 + 底部填充 + 上边框 + 下边框 + 上边距 + 下边距

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

程皮

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值