使用 CSS 将导航栏中的链接居中

cssweb developmentfront end technology更新于 2025/5/12 18:20:17

要将导航栏中的链接居中,您需要将 ‘text-align: center’ 添加到 <li>

示例

<!DOCTYPE html>
<html>
   <head>
      <style>
         ul {
            list-style-type: none;
            margin: 0;
            padding: 0;
            width: 200px;
         }
         li a {
            display: block;
            background-color: #F0E7E7;
         }
         .active {
            background-color: #4CAF50;
            color: white;
         }
         li {
            text-align: center;
            border-bottom: 1px solid #555;
         }
      </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>

相关文章


有用资源