元素水平垂直居中

水平居中

  • 对于行内元素: text-align: center;
  • 对于确定宽度的块级元素:
    • width和margin实现。margin: 0 auto;
    • 绝对定位和margin-left: -width/2, 前提是父元素position: relative
  • 对于宽度未知的块级元素
    • table标签配合margin左右auto实现水平居中。使用table标签(或直接将块级元素设值为display:table),再通过给该标签添加左右margin为auto。
    • inline-block实现水平居中方法。display:inline-block和text-align:center实现水平居中。
    • 绝对定位+transform,translateX可以移动本身元素的50%。
    • flex布局使用justify-content:center

垂直居中

  • 利用line-height实现居中,这种方法适合纯文字类
  • 通过设置父容器相对定位,子级设置绝对定位,标签通过margin实现自适应居中
  • 弹性布局flex:父级设置display: flex; 子级设置margin为auto实现自适应居中
  • 父级设置相对定位,子级设置绝对定位,并且通过位移transform实现
  • table布局,父级通过转换成表格形式,然后子级设置vertical-align实现。(需要注意的是:vertical-align: middle使用的前提条件是内联元素以及display值为table-cell的元素)。

浮动布局的优缺点,以及有哪些方式清除浮动.

定义

浮动布局简介:当元素浮动以后可以向左或向右移动,直到它的外边缘碰到包含它的框或者另外一个浮动元素的边框为止。元素浮动以后会脱离正常的文档流,所以文档的普通流中的框就变现的好像浮动元素不存在一样。

优点

这样做的优点就是在图文混排的时候可以很好的使文字环绕在图片周围。另外当元素浮动了起来之后,它有着块级元素的一些性质例如可以设置宽高等,但它与inline-block还是有一些区别的,第一个就是关于横向排序的时候,float可以设置方向而inline-block方向是固定的;还有一个就是inline-block在使用时有时会有空白间隙的问题

缺点

最明显的缺点就是浮动元素一旦脱离了文档流,就无法撑起父元素,会造成父级元素的高度塌陷。

清除浮动的方式

  • 添加额外标签

    1
    2
    3
    4
    5
    <div class="parent_box">
    //添加额外标签并且添加clear属性
    <div style="clear:both"></div>
    //也可以加一个br标签
    </div>
  • 父级添加overflow属性,或者设置高度

    1
    2
    3
    <div class="parent_box" style="overflow:hidden">
    //将父元素的overflow设置为hidden或auto.
    </div>
  • 推荐建立伪类选择器 + clear:both 属性清除浮动.

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    <style>
    .parent:after{
    /* 设置添加子元素的内容是空 */
    content: '';
    /* 设置添加子元素为块级元素 */
    display: block;
    /* 设置添加的子元素的高度0 */
    height: 0;
    /* 设置添加子元素看不见 */
    visibility: hidden;
    /* 设置clear:both */
    clear: both;
    }
    </style>

    <div class="parent">
    // 浮动元素
    </div>

display:inline-block 小特性(知识扩展)

两个display:inline-block元素放到一起会产生一段空白。

复现代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
<!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>Document</title>
<style>
.container {
width: 800px;
height: 200px;
}

.left {
font-size: 14px;
background: red;
display: inline-block;
width: 100px;
height: 100px;
}

.right {
font-size: 14px;
background: blue;
display: inline-block;
width: 100px;
height: 100px;
}
</style>
</head>
<body>
<div class="container">
<div class="left">

</div>
<div class="right">

</div>
</div>
</body>
</html>

效果如图:

产生空白的原因

元素被当成行内元素排版的时候,元素之间的空白符(空格、回车换行等)都会被浏览器处理,根据CSS中white-space属性的处理方式(默认是normal,合并多余空白),原来HTML代码中的回车换行被转成一个空白符,在字体不为0的情况下,空白符占据一定宽度,所以inline-block的元素之间就出现了空隙。

解决办法

  • 将子元素标签的结束符和下一个标签的开始符写在同一行或把所有子标签写在同一行

    1
    2
    3
    4
    5
    6
    7
    <div class="container">
    <div class="left">

    </div><div class="right">

    </div>
    </div>
  • 父元素中设置font-size: 0,在子元素上重置正确的font-size

    1
    2
    3
    .container{
    font-size: 0;
    }
  • 为子元素设置float:left

    1
    2
    3
    4
    5
    6
    7
    8
    .left{
    float: left;
    font-size: 14px;
    background: red;
    display: inline-block;
    width: 100px;
    height: 100px;
    }

CSS之品字布局

第一种

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
<!doctype html>
<html>

<head>
<meta charset="utf-8">
<title>品字布局</title>
<style>
* {
margin: 0;
padding: 0;
}
body {
overflow: hidden;
}
div {
margin: auto 0;
width: 100px;
height: 100px;
background: red;
font-size: 40px;
line-height: 100px;
color: #fff;
text-align: center;
}

.div1 {
margin: 100px auto 0;
}

.div2 {
margin-left: 50%;
background: green;
float: left;
transform: translateX(-100%);
}

.div3 {
background: blue;
float: left;
transform: translateX(-100%);
}
</style>
</head>

<body>
<div class="div1">1</div>
<div class="div2">2</div>
<div class="div3">3</div>
</body>

</html>

效果如图:

第二种(全屏版)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>品字布局</title>
<style>
* {
margin: 0;
padding: 0;
}

div {
width: 100%;
height: 100px;
background: red;
font-size: 40px;
line-height: 100px;
color: #fff;
text-align: center;
}

.div1 {
margin: 0 auto 0;
}

.div2 {
background: green;
float: left;
width: 50%;
}

.div3 {
background: blue;
float: left;
width: 50%;
}
</style>
</head>

<body>
<div class="div1">1</div>
<div class="div2">2</div>
<div class="div3">3</div>
</body>
</html>

效果如图:

CSS之圣杯布局

如图:

左右宽度固定,中间宽度自适应.

利用flex布局

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<style>
*{
margin: 0;
padding: 0;
}
.header,.footer{
height:40px;
width:100%;
background:red;
}
.container{
display: flex;
}
.middle{
flex: 1;
background:yellow;
}
.left{
width:200px;
background:pink;
}
.right{
background: aqua;
width:300px;
}
</style>
</head>
<body>
<div class="header">这里是头部</div>
<div class="container">
<div class="left">左边</div>
<div class="middle">中间部分</div>
<div class="right">右边</div>
</div>
<div class="footer">这里是底部</div>
</body>
</html>

float布局(全部float:left)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
<!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>Document</title>
<style>
*{
margin: 0;
padding: 0;
}
.header,
.footer {
height: 40px;
width: 100%;
background: red;
}

.footer {
clear: both;
}

.container {
padding-left: 200px;
padding-right: 250px;
}

.container div {
position: relative;
float: left;
}

.middle {
width: 100%;
background: yellow;
}

.left {
width: 200px;
background: pink;
margin-left: -100%;
left: -200px;
}

.right {
width: 250px;
background: aqua;
margin-left: -250px;
left: 250px;
}
</style>
</head>

<body>
<div class="header">这里是头部</div>
<div class="container">
<div class="middle">中间部分</div>
<div class="left">左边</div>
<div class="right">右边</div>
</div>
<div class="footer">这里是底部</div>
</body>

</html>

这里的float布局 是相对较难理解的, 主要是运用了浮动 + 负 margin 配合定位.

float布局(左边float: left, 右边float: right)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
<!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>Document</title>
<style>
*{
margin: 0;
padding: 0;
}
.header,
.footer {
height: 40px;
width: 100%;
background: red;
}
.container{
overflow: hidden;
}

.middle {
background: yellow;
}

.left {
float: left;
width: 200px;
background: pink;
}

.right {
float: right;
width: 250px;
background: aqua;
}
</style>
</head>

<body>
<div class="header">这里是头部</div>
<div class="container">
<div class="left">左边</div>
<div class="right">右边</div>
<div class="middle">中间部分</div>
</div>
<div class="footer">这里是底部</div>
</body>

</html>

绝对定位

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
<!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>Document</title>
<style>
*{
margin: 0;
padding: 0;
}
.header,
.footer {
height: 40px;
width: 100%;
background: red;
}
.container{
min-height: 1.2em;
position: relative;
}

.container>div {
position: absolute;
}

.middle {
left: 200px;
right: 250px;
background: yellow;
}

.left {
left: 0;
width: 200px;
background: pink;
}

.right {
right: 0;
width: 250px;
background: aqua;
}
</style>
</head>

<body>
<div class="header">这里是头部</div>
<div class="container">
<div class="left">左边</div>
<div class="right">右边</div>
<div class="middle">中间部分</div>
</div>
<div class="footer">这里是底部</div>
</body>

</html>

grid布局

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
<!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>Document</title>
<style>
body{
display: grid;
}
#header{
background: red;
grid-row:1;
grid-column:1/5;
}

#left{
grid-row:2;
grid-column:1/2;
background: orange;
}
#right{
grid-row:2;
grid-column:4/5;
background: cadetblue;
}
#middle{
grid-row:2;
grid-column:2/4;
background: rebeccapurple
}
#footer{
background: gold;
grid-row:3;
grid-column:1/5;
}
</style>
</head>

<body>
<div id="header">header</div>
<div id="left">left</div>
<div id="middle">middle</div>
<div id="right">right</div>
<div id="footer">footer</footer></div>

</body>

</html>

CSS之双飞翼布局

如图

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
<!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>Document</title>
<style>
*{
margin: 0;
padding: 0;
}
.container {
min-width: 600px;
}
.left {
float: left;
width: 200px;
height: 400px;
background: red;
margin-left: -100%;
}
.center {
float: left;
width: 100%;
height: 500px;
background: yellow;
}
.center .inner {
margin: 0 200px;
}
.right {
float: left;
width: 200px;
height: 400px;
background: blue;
margin-left: -200px;
}
</style>
</head>

<body>
<article class="container">
<div class="center">
<div class="inner">双飞翼布局</div>
</div>
<div class="left"></div>
<div class="right"></div>
</article>
</body>

</html>

理解了 圣杯布局的 浮动 + 负margin 这个就理解起来就很容易了.

css之BFC

定义

W3C对BFC的定义如下: 浮动元素和绝对定位元素,非块级盒子的块级容器(例如 inline-blocks, table-cells, 和 table-captions),以及overflow值不为”visiable”的块级盒子,都会为他们的内容创建新的BFC(Block Fromatting Context, 即块级格式上下文)。

如何创建 BFC

  • float属性不为none.

  • position属性不为static和relative.

  • display属性为下列之一:table-cell,table-caption,inline-block,flex,or inline-flex.

  • overflow属性不为visible.

BFC渲染特性

  • 计算BFC高度的时候浮动元素也会参与计算
  • BFC是一个独立的容器,外面的元素不会影响里面的元素
  • BFC的区域不会与浮动元素的box重叠
  • BFC垂直方向边距重叠

常用场景

  • 解决 浮动导致的父元素高度坍塌
  • 避免外边距折叠