css - flex header navigation

css - flex header navigation

目标-用flex来写一个nav

  • Logo左边,中间title,右边菜单
  • 右边菜单宽度和左边logo所占据宽度一致
  • 中间title始终在最中央

效果图

step1: 确定html结构

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<div class="xheader">
<div class="logo"></div>
<div class="title">{{title}}</div>
<div class="userinfo">
<div>
<span>User:</span>
<span>
<span>URARA</span>
</span>
</div>
<div>
<span>Email:</span>
<span>[email protected]</span>
</div>
</div>
</div>

step2: css

  • 两边padding15px
  • 高度50px

以下是基础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
54
55
56
57
58
.xheader {
height: 50px;
display:flex;
background: linear-gradient(to bottom right, #1b0652, #ef618b);
}

.xheader .logo {
padding-left:15px;
height: 100%;
width: auto;
cursor: pointer;
background-size: contain;
background-image: url(/assets/images/logo.png);
background-repeat: no-repeat;
}

.xheader .title {
width: 275px;
}

.xheader .logo, .xheader .userinfo {
flex: 1;
}

.xheader .title {
display: flex;
justify-content: center;
align-items: center;
color: white;
font-size: 2.5rem;
font-weight: bold;
}

.xheader .userinfo {
display: flex;
flex-direction: row;
font-size: 1.6rem;
font-weight: bold;
color: white;
justify-content: flex-end;
align-items: center;
}

.xheader .userinfo > div {
display: flex;
}

.xheader .userinfo > div:first-child > span:last-child {
display: flex;
align-items: center;
justify-content: center;
cursor: pointer;
}

.xheader .userinfo > div:last-child {
padding-left: 15px;
padding-right: 15px;
}
#

Comments

Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×