纯css实现下拉菜单

纯CSS实现下拉菜单

ul,li版本

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
67
68
69
70
71
72
73
74
75
<nav role="navigation">
<ul>
<li><a href="#">One</a></li>
<li><a href="#">Two</a>
<ul class="dropdown">
<li><a href="#">Sub-1</a></li>
<li><a href="#">Sub-2</a></li>
<li><a href="#">Sub-3</a></li>
</ul>
</li>
<li><a href="#">Three</a></li>
</ul>
</nav>

<style>
a {
text-decoration: none;
}

nav {
font-family: monospace;
}

ul {
background: darkorange;
list-style: none;
margin: 0;
padding-left: 0;
}

li {
color: #fff;
background: darkorange;
display: block;
float: left;
padding: 1rem;
position: relative;
text-decoration: none;
transition-duration: 0.5s;
}

li a {
color: #fff;
}

li:hover {
background: red;
cursor: pointer;
}

ul li ul {
background: orange;
visibility: hidden;
opacity: 0;
min-width: 5rem;
position: absolute;
transition: all 0.5s ease;
margin-top: 1rem;
left: 0;
display: none;
}

ul li:hover > ul,
ul li:focus-within > ul, /* this is the line we add */
ul li ul:hover {
visibility: visible;
opacity: 1;
display: block;
}

ul li ul li {
clear: both;
width: 100%;
}
</style>

div版本

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
67
68
69
70
71
72
73
74
75
76
77
<div class="nav-wrap">
<div class="nav-node-ul">
<div class="nav-node-li"><a href="#">One</a></div>
<div class="nav-node-li"><a href="#">Two</a>
<div class="nav-node-ul dropdown">
<div class="nav-node-li"><a href="#">Sub-1</a></div>
<div class="nav-node-li"><a href="#">Sub-2</a></div>
<div class="nav-node-li"><a href="#">Sub-3</a></div>
</div>
</div>
<div class="nav-node-li"><a href="#">Three</a></div>
</div>
</div>
<style>
/* 下拉菜单 */
/* 这个放到父级也可以 */
.nav-wrap{
position: absolute;
left: 0;
}

.nav-wrap a {
text-decoration: none;
}

.nav-wrap .nav-node-.nav-node-ul {
background: darkorange;
list-style: none;
margin: 0;
padding-left: 0;
}

.nav-wrap .nav-node-li {
color: #fff;
background: darkorange;
display: block;
float: left;
padding: 1rem;
position: relative;
text-decoration: none;
transition-duration: 0.5s;
}

.nav-wrap .nav-node-li a {
color: #fff;
}

.nav-wrap .nav-node-li:hover {
background: red;
cursor: pointer;
}

.nav-wrap .nav-node-ul .nav-node-li .nav-node-ul {
background: orange;
visibility: hidden;
opacity: 0;
min-width: 5rem;
position: absolute;
transition: all 0.5s ease;
margin-top: 1rem;
left: 0;
display: none;
}

.nav-wrap .nav-node-ul .nav-node-li:hover > .nav-node-ul,
.nav-wrap .nav-node-ul .nav-node-li:focus-within > .nav-node-ul, /* this is the line we add */
.nav-wrap .nav-node-ul .nav-node-li .nav-node-ul:hover {
visibility: visible;
opacity: 1;
display: block;
}

.nav-wrap .nav-node-ul .nav-node-li .nav-node-ul .nav-node-li {
clear: both;
width: 100%;
}
</style>
作者

Terwer

发布于

2022-07-09

更新于

2022-07-09

许可协议

评论