html+css實現響應式導航欄

效果展示:

在這裏插入圖片描述
在這裏插入圖片描述

下面是html結構代碼:

  • 需要引入fontAwesome的字體圖標庫
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <link rel="stylesheet" href="css/responsiveNavbar.css" />
    <link
      rel="stylesheet"
      href="../font-awesome-4.7.0/font-awesome/css/font-awesome.min.css"
    />
    <title>responsiveNavbar</title>
  </head>
  <body>
    <header>
      <nav class="container">
        <div class="logo">Stunning98</div>
        <div class="menu">
          <ul>
            <li>
              <a href="#"
                ><i class="fa fa-home" aria-hidden="true"></i>&nbsp;首頁</a
              >
            </li>
            <li>
              <a href="#"
                ><i class="fa fa-book" aria-hidden="true"></i>&nbsp;博客</a
              >
            </li>
            <li>
              <a href="#"
                ><i class="fa fa-film" aria-hidden="true"></i>&nbsp;電影</a
              >
            </li>
            <li>
              <a href="#"
                ><i class="fa fa-github" aria-hidden="true"></i>&nbsp;GitHub</a
              >
            </li>
          </ul>
        </div>
        <div class="bar"><i class="fa fa-bars" aria-hidden="true"></i></div>
      </nav>
    </header>
    <main class="content container"></main>
  </body>
</html>

下面是css代碼:

* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}
a {
  text-decoration: none;
  color: #1289a7;
}
.clearfix::before,
.clearfix::after {
  content: "";
  display: table;
  clear: both;
}
a:hover {
  color: #3498db;
}
ul {
  list-style: none;
}
.container {
  width: 1200px;
  margin: 0 auto;
}
header {
  position: fixed;
  top: 0;
  width: 100%;
  background-color: #bdc3c7;
}
header nav {
  display: flex;
  justify-content: space-between;
  height: 50px;
  text-align: center;
  line-height: 50px;
}
header nav .logo {
  font-size: 32px;
  color: #2980b9;
  padding: 0 20px;
}
header nav .menu ul {
  display: flex;
  justify-content: space-evenly;
}
header nav .menu ul li a {
  font-size: 20px;
  padding: 0 15px;
}
header nav .bar {
  font-size: 24px;
  height: 50px;
  line-height: 50px;
  padding-right: 15px;
  cursor: pointer;
  display: none;
}
@media screen and (min-width: 1200px) {
  .container {
    width: 1200px;
  }
}
@media screen and (min-width: 768px) and (max-width: 1200px) {
  .container {
    width: 100%;
  }
}
@media screen and (max-width: 768px) {
  .container {
    width: 100%;
  }
  .menu {
    display: none;
  }
  .bar {
    display: block !important;
  }
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章