使用CSS将样式设置为导航栏中的当前链接

Set style to current link in a Navigation Bar with CSS

要将样式设置为导航栏中的当前链接,请将样式添加到.active。 您可以尝试运行以下代码来设置当前链接的样式:

现场演示

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
<!DOCTYPE html>
<html>
 <head>
   <style>
    ul {
      list-style-type: none;
      margin: 5;
      padding: 5;
    }
    li a {
      display: block;
      width: 70px;
      background-color: #F0E7E7;
    }
    .active {
      background-color: #4CAF50;
      color: white;
    }
   </style>
 </head>
 <body>
   
<ul>

   
<li>
Home
</li>

   
<li>
Company
</li>

   
<li>
Product
</li>

   
<li>
Services
</li>

   
<li>
Contact
</li>

   
</ul>

 </body>
</html>