CSS布局

前言

不会写前端的程序员不是好程序员

height撑满屏幕

根据定位实现

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>fixed, absolute让撑满整个屏幕</title>
<style>
* {
margin: 0;
padding: 0;
}
div {
width: 100%;
height: 100%;
background-color: yellow;
position: fixed;
}
</style>
</head>
<body>
<div></div>
</body>
</html>

parent设置高度

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>parent设置高度</title>
<style>
html,body {
height:100%;
}
div {
height: 100%;
background-color: yellow;
}
</style>
</head>
<body>
<div></div>
</body>
</html>

min-height设置高度

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>vh设置高度</title>
<style>
div {
min-height: 100vh;
background-color: yellow;
}
</style>
</head>
<body>
<div></div>
</body>
</html>

flex设置高度

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>flex设置高度</title>
<style>
html, body {
display: flex;
flex-direction: column;
height: 100%;
}
div {
flex: 1;
background-color: yellow;
}
</style>
</head>
<body>
<div></div>
</body>
</html>

两栏布局

实现1(浮动+margin-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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<style>
.left {
background-color: deepskyblue;
width: 200px;
height: 100px;
float: left
}
.right {
background-color: greenyellow;
height: 200px;
margin-left: 200px;
}
</style>
</head>
<body>
<div class="wrapper">
<div class="left"></div>
<div class="right"></div>
</div>
</body>
</html>

实现2(定位fixed+margin-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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<style>
.left {
background-color: deepskyblue;
width: 200px;
height: 100px;
position: fixed;
}
.right {
background-color: greenyellow;
height: 200px;
margin-left: 200px;
}
</style>
</head>
<body>
<div class="wrapper">
<div class="left"></div>
<div class="right"></div>
</div>
</body>
</html>

实现3(利用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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<style>
.wrapper {
display: flex;
}
.left {
background-color: deepskyblue;
width: 200px;
height: 100px;
}
.right {
background-color: greenyellow;
height: 200px;
flex: 1;
}
</style>
</head>
<body>
<div class="wrapper">
<div class="left"></div>
<div class="right"></div>
</div>
</body>
</html>

实现4(float + BFC)

BFC的区域不会与浮动元素发生重叠

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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<style>
.left {
background-color: deepskyblue;
width: 200px;
height: 100px;
float: left;
}
.right {
background-color: greenyellow;
height: 200px;
overflow: hidden;
}
</style>
</head>
<body>
<div class="wrapper">
<div class="left"></div>
<div class="right"></div>
</div>
</body>
</html>

渐进式布局

1
2
3
4
5
6
7
8
9
10
11
12
13
14
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>基本布局</title>
<link href="css/index.css" rel="stylesheet" />
</head>
<body>
<div class="wrapper">
<div class="sidebar"></div>
<div class="content"></div>
</div>
</body>
</html>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
*{
margin: 0;
padding: 0;
}
.wrapper .sidebar {
height: 100%; /* Full-height: remove this if you want "auto" height */
width: 160px; /* Set the width of the sidebar */
position: fixed; /* Fixed Sidebar (stay in place on scroll) */
z-index: 1; /* Stay on top, 2 is top of 1 */
top: 0; /* Stay at the top */
left: 0;
background-color: #21EDFF; /* Black */
overflow-x: hidden; /* Disable horizontal scroll */
padding: 45px 0; /* up down 45px, left right 0px */
}
div .content {
margin-left: 160px;
background-color: #F7294A;
width: calc(100% - 160px);
overflow: auto; /* scroll */
}

效果图

SideBar增加菜单

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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>基本布局</title>
<link href="css/index.css" rel="stylesheet"/>
</head>
<body>
<div class="wrapper">
<div class="sidebar">
<div class="profile">
<img src="https://1.bp.blogspot.com/-vhmWFWO2r8U/YLjr2A57toI/AAAAAAAACO4/0GBonlEZPmAiQW4uvkCTm5LvlJVd_-l_wCNcBGAsYHQ/s16000/team-1-2.jpg"
alt="profile_picture">
<h3>樱木花道</h3>
<p>天才</p>
</div>
<ul>
<li>
<a href="#" class="active">
<span class="item">主页</span>
</a>
</li>
<li>
<a href="#">
<span class="item">报告</span>
</a>
</li>
</ul>
</div>
<div class="content"></div>
</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
*{
margin: 0;
padding: 0;
}
.wrapper .sidebar {
height: 100%; /* Full-height: remove this if you want "auto" height */
width: 160px; /* Set the width of the sidebar */
position: fixed; /* Fixed Sidebar (stay in place on scroll) */
z-index: 1; /* Stay on top, 2 is top of 1 */
top: 0; /* Stay at the top */
left: 0;
background-color: #5A69C1; /* Black */
overflow-x: hidden; /* Disable horizontal scroll */
padding: 45px 0; /* up down 45px, left right 0px */
}
div .content {
margin-left: 160px;
background-color: #F7294A;
width: calc(100% - 160px);
overflow: auto; /* scroll */
}
.wrapper .sidebar .profile{
margin-bottom: 30px;
text-align: center;
}
.wrapper .sidebar .profile img{
display: block;
width: 100px;
height: 100px;
border-radius: 50%;
margin: 0 auto;
}
.wrapper .sidebar .profile h3{
color: #ffffff;
margin: 10px 0 5px;
}
.wrapper .sidebar .profile p{
color: rgb(206, 240, 253);
font-size: 14px;
}
.wrapper .sidebar ul li a{
display: block;
padding: 13px 30px;
border-bottom: 1px solid #10558d;
color: rgb(241, 237, 237);
font-size: 16px;
position: relative;
text-decoration: none;
}
.wrapper .sidebar ul li a.active {
background-color: #04AA6D;
color: white;
}
.wrapper .sidebar ul li a:hover:not(.active) {
background-color: #555;
color: white;
}

效果图

标准的盒子模型

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">
<title>Title</title>
<style>
html, body {

width: 100%;
height: 100%;
}

* {
margin: 0;
padding: 0;
box-sizing: border-box;
}

.container {
padding: 10px;
border: 1px solid #ccc;
}

.box {
border: 1px solid #ccc;
padding: 10px;
}

.name {
width: 100%;
}
</style>
</head>
<body>
<div class="container">
<div class="box">
<input type="text" class="name">
</div>
</div>
</body>
</html>

参考