深入了解CSS中盒子模型
CSS中盒子模型介绍
- 什么是盒子?
- 盒子是用来存储物品,我们可以将盒子理解为酒盒,酒盒有什么组成的呢? 有酒可以喝、有填充物保护酒防止酒被摔坏、纸盒子。
- 我们怎么理解
CSS
中的盒子呢,CSS
中盒子有什么组成的呢?有内容、内边距、边框、外边距。 CSS
中盒子的主要属性有5
种如:width
宽度、height
高度、padding
内边距、border
边框、margin
外边距。
CSS中盒子模型实践
CSS
中盒子模型实践,给大家看看我们CSS
中的盒子长什么样。代码块
<!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>盒子模型</title>
<style>
div {
/*这里的宽度指的就是盒子内容的宽度*/
width: 100px;
/*这里的高度值的就是盒子内容的高度*/
height: 100px;
/*内边距就是盒子里面的内容到边框的距离*/
padding: 30px;
/*这个就是指盒子的外边框*/
border: 1px solid red;
/*这个就是指盒子的外边距,盒子与盒子之间的距离*/
margin: 20px;
}
</style>
</head>
<body>
<div>
微笑是最初的信仰,微笑是最初的信仰,微笑是最初的信仰。
</div>
</body>
</html>
结果图
- 如何计算一个盒子的总宽度和总高度,笔者那宽度举例:
一个盒子的总宽度
=盒子内容宽度
+左右2边内边距
+左右2边边框线
。
注意:一个盒子的高度一般情况下不设置高度,因为一个盒子的高度它应该是由其内容来决定的。
padding内边距介绍
padding
内边距的意思就是指的盒子中间的内容与边框的这段距离。padding
内边距分为4
个方向,所以我们能够设置或描述这4
个方向的内边距。padding
内边距属性值说明表:
属性值 | 描述 |
---|---|
padding-top | 设置向上的内边距的距离。 |
padding-bottom | 设置向下的内边的距距离。 |
padding-left | 设置向左的内边距的距离。 |
padding-right | 设置向右的内边距的距离。 |
padding | 设置上下左右内边距的距离,是上面的属性值缩写。 |
padding内边距实践
- 我们将
div
标签设置内边距,实践内容如:将div
标签上
边内边距设置为20px
、下
边内边距设置为30px
、左
边边距设置为40px
、右
边内边距设置为50px
。 代码块
<!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>盒子模型</title>
<style>
div {
/*这里的宽度指的就是盒子内容的宽度*/
width: 100px;
/*这里的高度值的就是盒子内容的高度*/
height: 100px;
border: 1px solid red;
padding-top: 20px;
padding-bottom: 30px;
padding-left: 40px;
padding-right: 50px;
}
</style>
</head>
<body>
<div>
微笑是最初的信仰,微笑是最初的信仰,微笑是最初的信仰。
</div>
</body>
</html>
结果图
padding内边距缩写实践
- 缩写是有方向的可以同时表示四个方向,但是这个
padding
属性的方向是有顺序的,顺序规则如:上
、右
、下
、左
。 padding
属性值有4
个,接下来我们就一一试试看看会有什么效果呢。- 我们给
padding
属性设置1
个值实践。 代码块
<!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>盒子模型</title>
<style>
div {
/*这里的宽度指的就是盒子内容的宽度*/
width: 100px;
/*这里的高度值的就是盒子内容的高度*/
height: 100px;
border: 1px solid red;
padding: 20px;
}
</style>
</head>
<body>
<div>
微笑是最初的信仰,微笑是最初的信仰,微笑是最初的信仰。
</div>
</body>
</html>
结果图
注意:假设我们给
padding
属性值设置了1
个值为:padding: 20px;
表示上
、右
、下
、左
、方向的内边距都为20px
像素。- 我们给
padding
属性设置2
个值实践。 代码块
<!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>盒子模型</title>
<style>
div {
/*这里的宽度指的就是盒子内容的宽度*/
width: 100px;
/*这里的高度值的就是盒子内容的高度*/
height: 100px;
border: 1px solid red;
padding: 20px 30px;
}
</style>
</head>
<body>
<div>
微笑是最初的信仰,微笑是最初的信仰,微笑是最初的信仰。
</div>
</body>
</html>
结果图
注意:假设我们给
padding
属性值设置了2
个值如:padding: 20px 30px;
表示内边距的(上、下)
为20px
像素、(左、右)
为30px
像素。我们给
padding
属性设置3
个值实践。代码块
<!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>盒子模型</title>
<style>
div {
/*这里的宽度指的就是盒子内容的宽度*/
width: 100px;
/*这里的高度值的就是盒子内容的高度*/
height: 100px;
border: 1px solid red;
padding: 20px 30px 40px;
}
</style>
</head>
<body>
<div>
微笑是最初的信仰,微笑是最初的信仰,微笑是最初的信仰。
</div>
</body>
</html>
结果图
注意:假设我们给
padding
属性值设置了3
个值如:padding: 20px 30px 40px;
表示内边距的上
为20px
像素、(左、右
)为30px
像素、下
为40px
像素。我们给
padding
属性设置4
个值实践。代码块
<!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>盒子模型</title>
<style>
div {
/*这里的宽度指的就是盒子内容的宽度*/
width: 100px;
/*这里的高度值的就是盒子内容的高度*/
height: 100px;
border: 1px solid red;
padding: 20px 30px 40px 50px;
}
</style>
</head>
<body>
<div>
微笑是最初的信仰,微笑是最初的信仰,微笑是最初的信仰。
</div>
</body>
</html>
结果图
注意:假设我们给
padding
属性值设置了3
个值如padding: 20px 30px 40px 50px;
表示内边距的上
为20px
像素、右
为30px
像素、下
为40px
像素、左
为50px
像素。
margin外边距介绍
margin
外边距的意思就是指的盒子与盒子之间的距离。margin
外边距分为4
个方向,所以我们能够设置或描述这4
个方向的外边距。margin
外边距属性值说明表:
属性值 | 描述 |
---|---|
margin-top | 设置向上的外边距的距离。 |
margin-bottom | 设置向下的外边的距距离。 |
margin-left | 设置向左的外边距的距离。 |
margin-right | 设置向右的外边距的距离。 |
margin | 设置上下左右外边距的距离,是上面的属性值缩写。 |
auto | 自动。 |
margin上下外边距实践
- 我们将
class
属性值为.top
元素设置上外边距为20px
像素并且将class
属性值为.bottom
设置下外边距为20px
像素。 代码块
<!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>盒子模型</title>
<style>
.bottom{
width: 100px;
height: 100px;
background-color: red;
margin-bottom: 20px;
}
.top{
width: 100px;
height: 100px;
background-color: slateblue;
margin-top: 20px;
}
</style>
</head>
<body>
<div class="bottom"></div>
<div class="top"></div>
</body>
</html>
calss
属性值为.bottom
结果图
calss
属性值为.top
结果图
注意:两张图有什么区别呢,事实证明外边距竖直方向的
margin
的属性值不会叠加,它会取最大的属性值,大家要明白哦。
margin左右外边距实践
- 我们将
class
属性值为.right
元素设置右外边距为20px
像素并且将class
属性值为.left
设置左外边距为20px
像素。 代码块
<!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>盒子模型</title>
<style>
.left{
background-color: slateblue;
margin-left: 20px;
}
.right{
background-color: red;
margin-right: 20px;
}
</style>
</head>
<body>
<span class="right">right</span>
<span class="left">left</span>
</body>
</html>
calss
属性值为.right
结果图
calss
属性值为.left
结果图
注意:两张图有什么区别呢,事实证明外边距水平线方向
margin
的属性值会叠加。大家要明白哦。若想让竖直方向的
margin
属性值叠加外边距的距离咱也是有办法如:将要设置margin
属性的元素进行浮动即可,元素浮动之后它的margin
属性值就会叠加,若有读者朋友不熟悉浮动的可以看看笔者之间发布的CSS中如果实现元素浮动和清除浮动,看这篇文章就足够了文章。代码块
<!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>盒子模型</title>
<style>
.box{
width: 110px;
border: 2px solid red;
overflow: hidden;
}
.bottom{
width: 100px;
height: 100px;
background-color: slateblue;
float: left;
margin-bottom: 20px;
}
.top{
width: 100px;
height: 100px;
background-color: darkblue;
float: left;
margin-top: 20px;
}
</style>
</head>
<body>
<div class="box">
<div class="bottom"></div>
<div class="top"></div>
</div>
</body>
</html>
calss
属性值为.bottom
结果图
calss
属性值为.top
结果图
margin外边距缩写实践
- 缩写是有方向的可以同时表示四个方向,但是这个
margin
属性的方向是有顺序的,顺序规则如:上
、右
、下
、左
。 margin
属性值有4
个,接下来我们就一一试试看看会有什么效果呢。- 我们给
margin
属性设置1
个值实践。 代码块
<!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>盒子模型</title>
<style>
.box {
/*这里的宽度指的就是盒子内容的宽度*/
width: 100px;
/*这里的高度值的就是盒子内容的高度*/
height: 100px;
background-color: red;
margin: 20px;
}
</style>
</head>
<body>
<div class="box"></div>
</body>
</html>
结果图
注意:假设我们给
margin
属性值设置了1
个值为:margin: 20px;
表示上
、右
、下
、左
、方向的外边距都为20px
像素。- 我们给
margin
属性设置2
个值实践。 代码块
<!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>盒子模型</title>
<style>
.box {
/*这里的宽度指的就是盒子内容的宽度*/
width: 100px;
/*这里的高度值的就是盒子内容的高度*/
height: 100px;
background-color: red;
margin: 20px 30px;
}
</style>
</head>
<body>
<div class="box"></div>
</body>
</html>
结果图
注意:假设我们
margin
属性值设置了2
个值如:margin: 20px 30px;
表示外边距的(上、下)
为20px
像素、(左、右)
为30px
像素。我们给
margin
属性设置3
个值实践。代码块
<!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>盒子模型</title>
<style>
.box {
/*这里的宽度指的就是盒子内容的宽度*/
width: 100px;
/*这里的高度值的就是盒子内容的高度*/
height: 100px;
background-color: red;
margin: 20px 30px 40px;
}
</style>
</head>
<body>
<div class="box"></div>
</body>
</html>
结果图
注意:假设我们给
margin
属性值设置了3
个值如:margin: 20px 30px 40px;
表示外边距的上
为20px
像素、(左、右
)为30px
像素、下
为40px
像素。我们给
margin
属性设置4
个值实践。代码块
<!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>盒子模型</title>
<style>
.box {
/*这里的宽度指的就是盒子内容的宽度*/
width: 100px;
/*这里的高度值的就是盒子内容的高度*/
height: 100px;
background-color: red;
margin: 20px 30px 40px 50px;
}
</style>
</head>
<body>
<div class="box"></div>
</body>
</html>
结果图
注意:假设我们给
margin
属性值设置了4
个值如margin: 20px 30px 40px 50px;
表示外边距的上
为20px
像素、右
为30px
像素、下
为40px
像素、左
为50px
像素。
margin属性居中介绍
margin
属性值设置为auto
,auto
表示自动的意思,当左外边距与右外边距的值都是auto
时那么这个盒子就会水平居中。- 用
margin
属性设置水平居中注意事项如: - 1、一定要给盒子设置固定的宽高度。
- 2、只有块级元素才可以实现水平居中,行内元素不能够实现水平居中。
- 3、只有标准文档流中的盒子才可以使用
margin
属性来实现水平居中。 - 4、
margin
属性是用来实现盒子的水平居中,而不是文本的水平居中。
margin属性值为auto实践
- 我们将使用
margin
属性值为auto
实现盒子水平线左居中的实践。 代码块
<!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>盒子模型</title>
<style>
.box{
width: 100px;
height: 100px;
background-color: red;
margin-left:auto;
}
</style>
</head>
<body>
<div class="box">
</div>
</body>
</html>
结果图
- 我们将使用
margin
属性值为auto
实现盒子水平线居中的实践。 代码块
<!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>盒子模型</title>
<style>
.box{
width: 100px;
height: 100px;
background-color: red;
margin-left:auto;
margin-right: auto;
}
</style>
</head>
<body>
<div class="box">
</div>
</body>
</html>
结果图
注意:
margin
属性值为auto
设置上下
外边距不起任何作用。代码块
<!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>盒子模型</title>
<style>
.box{
width: 100px;
height: 100px;
background-color: red;
margin-bottom:auto;
margin-top: auto;
}
</style>
</head>
<body>
<div class="box">
</div>
</body>
</html>
结果图
注意事项一
- 用实践来证明为什么:一定要给盒子设置固定的宽高度。
代码块
<!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>盒子模型</title>
<style>
.box{
height: 100px;
background-color: red;
margin-left: auto;
margin-right: auto;
}
</style>
</head>
<body>
<div class="box">
</div>
</body>
</html>
结果图
注意:如果该元素没有设置固定的宽度,那么该元素会占据其父元素的
100%
宽度,所以不能够实现水平线居中。
注意事项二
用实践来证明为什么:只有块级元素才可以实现水平居中,行内元素不能够实现水平居中。
代码块
<!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>盒子模型</title>
<style>
.box{
width: 100px;
height: 100px;
background-color: red;
margin-left: auto;
margin-right: auto;
}
</style>
</head>
<body>
<span class="box">微笑是最初的信仰
</span>
</body>
</html>
结果图
注意:因为行内元素不能设置宽度,所以无法实现水平线居中。
注意事项三
用实践来证明为什么:只有标准文档流中的盒子才可以使用
margin
属性来实现水平居中。代码块
<!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>盒子模型</title>
<style>
.box{
width: 100px;
height: 100px;
background-color: red;
margin-left: auto;
margin-right: auto;
float: left;
}
</style>
</head>
<body>
<div class="box">
</div>
</body>
</html>
结果图
注意:笔者给
class
属性值为.box
设置了一个float: left;
左浮动,浮动的元素已经脱离了标准文档流,所以无法实现水平线居中。
注意事项四
用实践来证明为什么:
margin
属性是用来实现盒子的水平线居中,而不是盒子的内容文本水平线居中。代码块
<!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>盒子模型</title>
<style>
.box{
width: 200px;
height: 100px;
background-color: red;
margin-left: auto;
margin-right: auto;
}
</style>
</head>
<body>
<div class="box">
微笑是最初的信仰
</div>
</body>
</html>
结果图
注意事项五
- 如果想让文本居中怎么办呢,使用
text-align
属性并且属性值为center
才可以实现文本水平线居中。 代码块
<!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>盒子模型</title>
<style>
.box{
width: 200px;
height: 100px;
background-color: red;
margin-left: auto;
margin-right: auto;
text-align: center;
}
</style>
</head>
<body>
<div class="box">
微笑是最初的信仰
</div>
</body>
</html>
结果图
深入了解CSS中盒子模型的更多相关文章
- CSS系列:CSS中盒子模型
盒子模型是CSS控制页面时一个很重要的概念.所有页面中的元素都可以看成是一个盒子,占据着一定的页面空间.可以通过调整盒子的边框和距离等参数,来调节盒子的位置和大小. 1. 盒子的内部结构 在CSS中, ...
- 认识CSS中盒子模型
前端之HTML,CSS(六) CSS 盒子模型 CSS中的重点,理解盒子模型对于CSS才能有更清晰的认识.网页说简单一点其实就是一块一块的拼接出来的,可以想象成拼图,所有图块拼接在一起就成了一幅图像. ...
- css中盒子模型与box-sizing属性
盒子模型 w3c标准:定义的width为 内容,有padding,border 都会使得 最终呈现的宽度为 定义的width+padding+border的总和,有margin另加 ie标准:定义的w ...
- CSS中盒子模型
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- CSS中盒模型的理解
今天突然看到一篇关于CSS中盒模型的文章,忽然觉得自己竟然遗忘了很多小的地方,所以写一篇文章来记忆一下 (摘抄于千与千寻写的CSS盒子模型理解,并在自己基础上添加了一些东西,希望更完善,对大家有帮助) ...
- 深入理解CSS系列(一):理解CSS的盒子模型
接触前端也有好几个年头了,但是,讲实话,对于CSS的理解真的是不敢恭维,相信很多同行也有类似的感受吧!这是为什么呢?因为我们都认为CSS太简单了,没有必要深入学习,果真如此?其实,只不过是自己图样图森 ...
- 这些HTML、CSS知识点,面试和平时开发都需要 No1-No4(知识点:HTML、CSS、盒子模型、内容布局)
这些HTML.CSS知识点,面试和平时开发都需要 No1-No4 系列知识点汇总 这些HTML.CSS知识点,面试和平时开发都需要 No1-No4(知识点:HTML.CSS.盒子模型.内容布局) ...
- 使用css弹性盒子模型
提示: 当期内容不充实, 修改后再来看吧 以下称:弹性子元素: 子元素, 弹性容器: 容器 弹性盒子的属性 1. css弹性盒子模型规定了弹性元素如何在弹性容器内展示 2. 弹性元素默认显示在弹性容器 ...
- #CSS的盒子模型、元素类型
CSS的盒子模型.元素类型 本文首先介绍了CSS元素的统一内部结构模型,盒子模型:然后介绍了CSS元素基于不同分类标准定义的元素类型,包括基于不同内容设置方式定义的replaced元素和non-r ...
随机推荐
- python str的一些操作及处理
一.str的定义:Python中凡是用引号引起来的数据可以称为字符串类型,组成字符串的每个元素称之为字符,将这些字符一个一个连接起来,然后在用引号起来就是字符串. 二.str的简单操作方法: conu ...
- Excel的IYQ钓鱼
0x00 环境准备 1.操作系统:windows7 2.microsoft office版本:office 2010 0x01 了解IYQ的基本概念 可以将IYQ简单的理解成内置在excel中的一种特 ...
- 【CF696D】Legen...(AC自动机)(矩阵快速幂)
题目描述 Barney was hanging out with Nora for a while and now he thinks he may have feelings for her. Ba ...
- Netty学习篇④-心跳机制及断线重连
心跳检测 前言 客户端和服务端的连接属于socket连接,也属于长连接,往往会存在客户端在连接了服务端之后就没有任何操作了,但还是占用了一个连接:当越来越多类似的客户端出现就会浪费很多连接,netty ...
- SpringBoot正确打日志的姿势
前篇 Spring Boot 日志处理你还在用Logback? 本文简介 前篇侧重 Log4j2 的配置,本篇侧重统一日志处理的应用,以下包含 HTTP 请求的日志处理.Exception 异常日志处 ...
- 网络安全-主动信息收集篇第二章-二层网络扫描之Netdiscover
专用于二层发现 可用于无限和交换网络环境 主动和被动探测 主动模式:netdiscover –i 网卡名 –r IP/网络位 / netdiscover –l IPList.txt 被动 net ...
- PHP 精典面试题(附答案)
1.输出Mozilla/4.0(compatible;MISIE5.01;Window NT 5.0)是,可能输出的语句是? A:$_SERVER['HTTP_USER_AGENT_TYPE']; B ...
- python学习之【第二篇】:Python中的数字及其所具有的方法
1.前言 Python 数字(number)数据类型用于存储数值.数据类型是不允许改变的,这就意味着如果改变数字数据类型的值,将重新分配内存空间. 2.创建数字对象 以下实例在变量赋值时 Number ...
- MapReduce任务提交源码分析
为了测试MapReduce提交的详细流程.需要在提交这一步打上断点: F7进入方法: 进入submit方法: 注意这个connect方法,它在连接谁呢?我们知道,Driver是作为客户端存在的,那么客 ...
- Uber Go 语言编码规范
Uber Go 语言编码规范 Uber 是一家美国硅谷的科技公司,也是 Go 语言的早期 adopter.其开源了很多 golang 项目,诸如被 Gopher 圈熟知的 zap.jaeger 等.2 ...