CSS的盒子模型
一、看透网页布局的本质
网页布局过程:
- 先准备相关的网页元素,网页元素基本都是盒子box。
- 利用CSS设置好盒子样式,然后摆放到相应的位置。
- 往盒子里面装内容。
网页布局的本质:就是利用CSS拜访盒子的位置。
二、盒子模型组成
盒子模型:就是把HTML页面中的布局元素看作是一个矩形的盒子,也就是一个装内容的容器。
CSS盒子模型本质上是一个盒子,封装周围的HTML元素,它包括:边框、外边距、内边距和实际内容。
三、边框(border)
border可以设置元素的边框,边框有三部分组成:边框宽度、边框样式、边框颜色
border: border-width || border-style || border-color
属性 | 作用 |
---|---|
border-width | 定义边框粗细,单位是px |
border-style | 边框的样式 |
border-color | 边框颜色 |
div {
width: 300px;
height: 200px;
/*border-width 边框的粗细 一般情况下都用px*/
border-width: 5px;
/*border-style 边框的样式*/
border-style: solid; /*实线边框*/
border-style: dashed; /*虚线边框*/
border-style: dotted; /*点线边框*/
border-color: purple;
}
边框简写:
div {
/*没有顺序*/
border: 5px solid purple;
}
边框分开写法:
border-top: 5px solid red; /*只设定上边框,其余同理*/
1.表格细线边框
border-collapse属性控制浏览器绘制表格边框的方式,它控制相邻单元格的边框。
border-collapse: collapse;
- collapse单词是合并的意思
- border-collapse: collapse 表示相邻边框合并在一起
2.边框会影响盒子的大小
边框会额外增加合资的实际大小,因此我们有两种解决方案:
- 测量盒子大小的时候不去测量边框
- 如果测量的时候包含了边框,则需要width/height减去边框宽度
3.内边距(padding)
padding属性用于设置内边距,即边框与内容之间的距离。
属性 | |
---|---|
padding-top | 左内边距 |
padding-bottom | 右内边距 |
padding-left | 上内边距 |
padding-right | 下内边距 |
内边距复合写法:
padding属性(简写属性)可以有一到四个值
值得个数 | 描述 |
---|---|
padding: 5px; | 1个值,代表上下左右都有5像素内边距 |
padding: 5px 10px; | 2个值,代表上下内边距是5像素,左右内边距是10像素 |
padding: 5px 10px 20px; | 3个值,代表上内边距5px,左右内边距10像素,下内边距20像素 |
padding: 5px 10px 20px 30px; | 4个值,上5像素,右10像素,下20像素,左30像素 顺时针 |
当给盒子指定padding值之后,发生这两件事情:
- 内容和边框有了距离,添加了内边距
- padding影响了合资的实际大小
即如果盒子已经有了宽度和高度,此时再指定内边距,会撑大盒子。
解决方案:
如果保证盒子盒子跟效果图大小保持一致,则让width/height减去多出来的内边距大小即可。
如果做网页导航栏时,
例如:
<style>
.nav {
height: 41px;
border-top: 3px solid #ff8500;
border-bottom: 1px solid #edeef0;
background-color: #fcfcfc;
line-height: 41px;
}
.nav a {
display: inline-block;
height: 41px;
padding: 0 20px;
font-size: 12px;
color: #4c4c4c;
text-decoration: none;
}
.nav a:hover {
background-color: #eee;
color: #ff8500;
}
</style>
</head>
<body>
<div class="nav">
<a href="#">谢坤比</a>
<a href="#">你</a>
<a href="#">他妈</a>
<a href="#">真是个傻逼</a>
</div>
</body>
</html>
如果盒子本身没有指定width/height属性,则此时padding不会撑开盒子没有指定方向的的大小。
<style>
h1 {
height: 250px;
background-color: red;
/*这里没有指定 width,盒子宽度与浏览器一样宽。因此加入padding属性时不会增加宽度,只会增加高度。而一旦指定width,即使是100%,也会增加宽度,出现滚动条*/
padding: 30px;
}
div {
width: 300px;
height: 100px;
background-color: blue;
}
div p {
/*与上面同理,div就是上面的浏览器大小*/
padding: 30px;
background-color: purple;
}
</style>
<body>
<h1></h1>
<div>
<p></p>
</div>
</body>
4.外边距
margin属性用于设置外边距,即控制盒子和盒子之间的距离。
属性 | 作用 |
---|---|
margin-left | 左外边距 |
margin-right | 右外边距 |
margin-top | 上外 边距 |
margin-bottom | 下外边距 |
margin简写方式与padding相同。
外边距典型应用
4.1水平居中
外边距可以让块级盒子水平居中,但必须满足两个条件:
(1)盒子必须指定宽度(width)
(2)盒子左右的外边距都设置位auto
.header {
width: 90px;
margin: 0 auto;
}
以下三种方式都可以:
-
margin-left: auto;
margin-right: auto; -
margin: auto;
3. margin: 0 auto;
***注意:***以上方法是让块级元素水平居中,行内元素或者行内块元素居中给其父元素添加text-align:center即可。
<style>
.header {
width: 200px;
height: 200px;
margin: 100px auto
/*让盒子中的行内元素或行内块元素水平居中*/
text-align: center;
}
</style>
<body>
<div class="header">
<span>科比</span>
</div>
</body>
4.2外边距合并
使用margin定义元素的垂直外边距时,可以能会出现外边距的合并。
(1).相邻块元素垂直外边距的合并
当上下相邻的两个块元素(兄弟关系)相遇时,如果上面的元素有下外边距margin-bottom,下面的元素有上外边距magin-top,则他们之间的垂直间距不是margin-bottom与margin-top之和。取两个值中的较大者,这种现象被称为相邻块元素垂直外边距的合并。
解决方案:
尽量只给一个盒子添加margin值。
#####(2)嵌套块元素垂直外边距的塌陷
对于两个嵌套关系(父子关系)的块元素,父元素有上外边距同时子元素也有上外边距,此时父元素会塌陷较大的外边距值。
解决办法:
- 可以位父元素定义上边框。
- 可以为父元素定义上内边距。
- 可以为父元素添加overflow:hidden
<style>
.father {
width: 400px;
height: 400px;
background-color: purple;
margin-top: 50px;
/*防止发生塌陷问题*/
1.border: 1px solid transparent;
2.border: 1px;
3.overflow:hidden;
}
.son {
width: 200px;
height: 200px;
background-top: golden;
margin-top: 100px;
}
</style>
<body>
<div class="father">
<div class="son"></div>
</div>
</body>
4.清除内外边距
网页元素很多都带有默认的内外边距,而且不同浏览器默认的不一致。因此我们在布局,首先要清楚下网页元素的内外边距。
* {
padding: 0; /*清楚内边距*/
margin: 0; /*清楚外边距*/
}
***注意:***行内元素为了照顾兼容性,尽量只设置左右内外边距,不要设置上下内外边距,但是转换为块级和行内块元素就可以了。
<style>
* {
padding: 0;
margin: 0;
}
</style>
<body>
123
<ul>
<li>abcd</li>
<ul>
<span>行内元素尽量只设置左右内外边距</span>
</body>
四、圆角边框
在CSS3中,新增了圆角边框。
border-radius属性用于设置元素的外边框圆角。
border-radius: length;
radius半径原理:(椭)圆与边框的交集形成圆角效果。
- 参数值可以为数值或百分比的形式。
- 如果是正方形,想要设置为一个圆,把效数值改为高度或者宽度的一般即可,或者直接写为50%。
- 如果是个矩形,设置为高度的一般即可。
- 该属性是一个简写属性,可以取四个值,分别代表左上角、右上角、右下角、左下角。
- 分开写:border-top-left-radius、border-top-right-radius、border-bottom-right-radius、border-bottom-left-radius
<style>
.yuanxing {
width: 200px;
height: 200px;
background-color: red;
//半径设为高度或宽度的一半即可
//border-radius: 100px;
border-radius: 50%;
}
.juxing {
width: 300px;
height: 100px;
background-color: red;
//半径设为高度的一半
border-radius: 50px;
}
.radius {
width: 200px;
height: 200px;
border-radius: 10px 20px 30px 40px;
}
</style>
<body>
<div class="yuanxing"></div>
<div class="juxing"></div>
<div class="radius"></div>
</body>
五、盒子阴影
CSS3中新增了盒子阴影,可以用box-shadow属性为盒子添加阴影。
box-shadow: h-shadow v-shadow blur spread color inset;
值 | 描述 |
---|---|
h-shadow | 必需,水平阴影的位置,允许负值 |
v-shadow | 必需,垂直阴影的位置,允许负值 |
blur | 可选,虚实程度 |
spread | 可选,阴影的尺寸 |
color | 可选,阴影的颜色 |
inset | 可选,将外部阴影改为内部阴影 |
<style>
div {
width: 200px;
height: 200px;
background-color: red;
margin: 100px auto;
box-shadow: 10px 10px 10px 10px rgba(0,0,0, .3);
}
</style>
<body>
<div></div>
</body>
/*原来盒子没有阴影,鼠标一经过盒子就会链家阴影*/
<style>
div {
width: 200px;
height: 200px;
background-color: red;
margin: 100px auto;
}
div:hover {
box-shadow: 10px 10px 10px 10px rgba(0,0,0, .3);
}
</style>
<body>
<div></div>
</body>
注意:
- 默认的外阴影(outset),但是不可以写这个单词,佛则导致阴影无效。
- 盒子阴影不占用空间,不会影响其他盒子排列。
六、文字阴影
在CSS3中,test-shadow属性将阴影应用于文本。
text-shadow: h-shadow v-shadow blur color;
值 | 描述 |
---|---|
h-shadow | 必需,水平阴影的位置,允许负值 |
v-shadow | 必需,垂直阴影的位置,允许负值 |
blur | 可选,虚实程度 |
color | 可选,阴影的颜色 |