5种方式固定底部菜单
devin 6/20/2021 htmlCSS
# 方法一:将 .content 的 margin-bottom 设为负数
<!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>Sticky Footer</title>
<style>
html,
body {
margin: 0;
padding: 0;
height: 100%;
text-align: center;
background-color: yellowgreen;
}
.content {
min-height: 100%;
margin-bottom: -50px; /* 等于 .footer 高度的相反数 */
}
.content-inside {
background-color: #c2ffa9;
}
.push {
height: 50px; /* .push 是额外的占位元素,高度与 .footer 一致 */
}
.footer {
height: 50px;
line-height: 50px; /* 实现文字垂直居中 */
background-color: #3ba4f9;
}
</style>
</head>
<body>
<div class="content">
<!-- div.content-inside 的高度可以随意设置,亦可根据内容高度自适应 -->
<div class="content-inside">
<!-- content -->
<div>Content</div>
<div>Content</div>
<div>Content</div>
</div>
<!-- div.push 是额外的占位元素,高度与 div.footer 一致 -->
<div class="push"></div>
</div>
<div class="footer">Sticky 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
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
# 方法二:将 .footer 的 margin-top 设为负数
<!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>Sticky Footer</title>
<style>
html,
body {
margin: 0;
padding: 0;
height: 100%;
text-align: center;
background-color: yellowgreen;
}
.content {
min-height: 100%;
}
.content-inside {
background-color: #c2ffa9;
}
.footer {
height: 50px;
line-height: 50px;
/* 实现文字垂直居中 */
background-color: #3ba4f9;
margin-top: -50px;
/* 等于 .footer 高度的相反数 */
}
</style>
</head>
<body>
<div class="content">
<!-- div.content-inside 的高度可以随意设置,亦可根据内容高度自适应 -->
<div class="content-inside">
<!-- content -->
<div>Content</div>
<div>Content</div>
<div>Content</div>
</div>
</div>
<div class="footer">Sticky 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
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
# 方法三:使用 calc() 设置 .content 的高度
<!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>Sticky Footer</title>
<style>
html,
body {
margin: 0;
padding: 0;
height: 100%;
text-align: center;
background-color: yellowgreen;
}
.content {
/* 如果 .content 与 .footer 之间有间距,记得减去 */
min-height: calc(100vh - 50px);
}
.content-inside {
background-color: #c2ffa9;
}
.footer {
height: 50px;
line-height: 50px; /* 实现文字垂直居中 */
background-color: #3ba4f9;
}
</style>
</head>
<body>
<div class="content">
<div class="content-inside">
<!-- content -->
<div>Content</div>
<div>Content</div>
<div>Content</div>
</div>
</div>
<div class="footer">Sticky 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
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
# 方法四:使用 Flexbox 弹性布局
<!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>Sticky Footer</title>
<style>
html,
body {
margin: 0;
padding: 0;
height: 100%;
text-align: center;
background-color: yellowgreen;
}
body {
min-height: 100%;
display: flex;
flex-direction: column;
}
.content {
flex: 1;
}
.content-inside {
background-color: #c2ffa9;
}
.footer {
/* 这种方案不需要固定 .footer 的高度 */
background-color: #3ba4f9;
}
</style>
</head>
<body>
<div class="content">
<div class="content-inside">
<!-- content -->
<div>Content</div>
<div>Content</div>
<div>Content</div>
</div>
</div>
<div class="footer">Sticky 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
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
# 方法五:使用 Grid 网格布局
<!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>Sticky Footer</title>
<style>
html,
body {
margin: 0;
padding: 0;
height: 100%;
text-align: center;
background-color: yellowgreen;
}
body {
min-height: 100%;
display: grid;
grid-template-rows: 1fr auto;
}
.content-inside {
background-color: #c2ffa9;
}
.footer {
grid-row-start: 2;
grid-row-end: 3;
background-color: #3ba4f9;
}
</style>
</head>
<body>
<div class="content">
<div class="content-inside">
<!-- content -->
<div>Content</div>
<div>Content</div>
<div>Content</div>
</div>
</div>
<div class="footer">Sticky 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
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