安装flex.csss

通过npm安装:

1
npm install --save flex.css

1.设置主轴方向

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">
<title>设置主轴方向</title>
<link rel="stylesheet" href="./flex.css">
<style type="text/css">
.box {
width: 150px;
height: 150px;
border: 1px solid #ddd;
}
.item {
width: 30px;
height: 30px;
line-height: 30px;
color: #fff;
text-align: center;
}
</style>
</head>

<body>
<h2>从上到下</h2>
<div class="box" flex="dir:top">
<div class="item" style="background: red;">1</div>
<div class="item" style="background: blue;">2</div>
<div class="item" style="background: #000;">3</div>
</div>
<h2>从右到左</h2>
<div class="box" flex="dir:right">
<div class="item" style="background: red;">1</div>
<div class="item" style="background: blue;">2</div>
<div class="item" style="background: #000;">3</div>
</div>
<h2>从下到上</h2>
<div class="box" flex="dir:bottom">
<div class="item" style="background: red;">1</div>
<div class="item" style="background: blue;">2</div>
<div class="item" style="background: #000;">3</div>
</div>
<h2>从左到右(默认)</h2>
<div class="box" flex="dir:left">
<div class="item" style="background: red;">1</div>
<div class="item" style="background: blue;">2</div>
<div class="item" style="background: #000;">3</div>
</div>
</body>

</html>

Aaron Swartz

Aaron Swartz

主轴对齐方式

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">
<title>主轴对齐方式</title>
<link rel="stylesheet" href="./flex.css">
<style type="text/css">
.box {
width: 150px;
height: 150px;
border: 1px solid #ddd;
}
.item {
width: 30px;
height: 30px;
line-height: 30px;
color: #fff;
text-align: center;
}
</style>
</head>

<body>
<h2>从右到左</h2>
<div class="box" flex="main:right">
<div class="item" style="background: red;">1</div>
<div class="item" style="background: blue;">2</div>
<div class="item" style="background: #000;">3</div>
</div>
<h2>从左到右(默认)</h2>
<div class="box" flex="main:left">
<div class="item" style="background: red;">1</div>
<div class="item" style="background: blue;">2</div>
<div class="item" style="background: #000;">3</div>
</div>
<h2>两端对齐</h2>
<div class="box" flex="main:justify">
<div class="item" style="background: red;">1</div>
<div class="item" style="background: blue;">2</div>
<div class="item" style="background: #000;">3</div>
</div>
<h2>居中对齐</h2>
<div class="box" flex="main:center">
<div class="item" style="background: red;">1</div>
<div class="item" style="background: blue;">2</div>
<div class="item" style="background: #000;">3</div>
</div>
</body>

</html>

Aaron Swartz

Aaron Swartz

交叉轴对齐方式

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
<!DOCTYPE html>
<html lang="en">

<head>
<meta charset="UTF-8">
<title>交叉轴对齐方式</title>
<link rel="stylesheet" href="./flex.css">
<style type="text/css">
.box {
width: 150px;
height: 150px;
border: 1px solid #ddd;
}
.item {
width: 30px;
/*height: 30px;*/
line-height: 30px;
color: #fff;
text-align: center;
}
</style>
</head>

<body>
<h2>从上到下(默认)</h2>
<div class="box" flex="cross:top">
<div class="item" style="background: red;">1</div>
<div class="item" style="background: blue;">2</div>
<div class="item" style="background: #000;">3</div>
</div>
<h2>从下到上</h2>
<div class="box" flex="cross:bottom">
<div class="item" style="background: red;">1</div>
<div class="item" style="background: blue;">2</div>
<div class="item" style="background: #000;">3</div>
</div>
<h2>基线对齐</h2>
<div class="box" flex="cross:baseline">
<div class="item" style="font-size: 30px; background: red;">1</div>
<div class="item" style="font-size: 12px; background: blue;">2</div>
<div class="item" style="font-size: 40px; background: #000;">3</div>
</div>
<h2>居中对齐</h2>
<div class="box" flex="cross:center">
<div class="item" style="background: red;">1</div>
<div class="item" style="background: blue;">2</div>
<div class="item" style="background: #000;">3</div>
</div>
<h2>高度并排铺满</h2>
<div class="box" flex="cross:stretch">
<div class="item" style="background: red;">1</div>
<div class="item" style="background: blue;">2</div>
<div class="item" style="background: #000;">3</div>
</div>
</body>

</html>

Aaron Swartz

Aaron Swartz

水平居中

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 rel="stylesheet" href="./flex.css">
<style type="text/css">
.box {
width: 150px;
height: 150px;
border: 1px solid #ddd;
}
.item {
width: 30px;
height: 30px;
line-height: 30px;
color: #fff;
text-align: center;
}
</style>
</head>

<body>
<h2>水平居中</h2>
<div class="box" flex="main:center cross:center">
<div class="item" style="background: red;">1</div>
<div class="item" style="background: blue;">2</div>
<div class="item" style="background: #000;">3</div>
</div>
</body>

</html>

Aaron Swartz