使用 CSS 在導航欄中設定當前連結的樣式


要為導航欄中的當前連結設定樣式,請將樣式新增到 .active。你可以嘗試執行以下程式碼來設定當前連結的樣式

示例

即時演示

<!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><a href="#home">Home</a></li>
         <li><a href="#company" class="active">Company</a></li>
         <li><a href="#product">Product</a></li>
         <li><a href="#services">Services</a></li>
         <li><a href="#contact">Contact</a></li>
      </ul>
   </body>
</html>

更新於:2020 年 7 月 1 日

1K+ 瀏覽

開啟你的 職業生涯

透過完成課程獲得認證

開始
廣告