如何創建:帶圖標的導航欄

學習如何使用 CSS 創建帶有圖標的響應式導航欄。

帶圖標的導航欄

親自試一試

創建帶有圖標的響應式導航欄

第一步 - 添加 HTML:

<!-- Load an icon library -->
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
<div class="navbar">
  <a class="active" href="#"><i class="fa fa-fw fa-home"></i> Home</a>
  <a href="#"><i class="fa fa-fw fa-search"></i> Search</a>
  <a href="#"><i class="fa fa-fw fa-envelope"></i> Contact</a>
  <a href="#"><i class="fa fa-fw fa-user"></i> Login</a>
</div>

第二步 - 添加 CSS:

/* 設置導航欄的樣式 */
.navbar {
  width: 100%;
  background-color: #555;
  overflow: auto;
}
/* 導航欄鏈接樣式 */
.navbar a {
  float: left;
  text-align: center;
  padding: 12px;
  color: white;
  text-decoration: none;
  font-size: 17px;
}
/* 鼠標懸停時的導航欄鏈接樣式 */
.navbar a:hover {
  background-color: #000;
}
/* 當前/活動的導航欄鏈接 */
.active {
  background-color: #04AA6D;
}
/* 添加響應能力 - 在小于 500 像素的屏幕上自動垂直顯示導航欄,而不是水平顯示 Add responsiveness - will automatically display the navbar vertically instead of horizontally on screens less than 500 pixels */
@media screen and (max-width: 500px) {
  .navbar a {
    float: none;
    display: block;
  }
}

親自試一試

相關頁面

教程:CSS 導航欄

教程:如何創建圖標導航欄