将切换菜单添加到导航栏

问题描述 投票:0回答:1

如何修改代码,以便在屏幕缩小到768px时,导航栏变为切换菜单?对于较小的屏幕,我希望导航栏显示汉堡包切换符号,并使每个菜单和子菜单项变为可点击而不是悬停。谢谢。

* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

#main-nav a {
  color: #ffffff;
  background: #d3333b;
  display: block;
  height: 48px;
  font-family: Arial, Helvetica, sans-serif;
  font-weight: bold;
  display: flex;
  align-items: center;
  justify-content: center;
  text-decoration: none;
  transition: background 0.4s;
}

#main-nav a:hover {
  background: #777;
}

#main-nav ul {
  list-style: none;
  display: flex;
}

#main-nav li {
  width: 100%;
  text-align: center;
  position: relative;
}

#main-nav li:hover .submenu>li {
  display: block;
  top: 0px;
}

.submenu li {
  display: none;
  position: absolute;
  top: 0px;
}

.submenu {
  display: flex;
  flex-direction: column;
  position: absolute;
  width: 100%;
}

.wrapper {
  margin-right: auto;
  /* 1 */
  margin-left: auto;
  /* 1 */
  max-width: 960px;
  /* 2 */
  padding-right: 10px;
  /* 3 */
  padding-left: 10px;
  /* 3 */
}
<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>Flexbox Dropdown Menu</title>
</head>

<body>
  <nav id="main-nav">
    <ul>
      <li>
        <a href="#">Home</a>
      </li>
      <li>
        <a href="#">Menu 1</a>
        <ul class="submenu">
          <li>
            <a href="#">Sub Menu 1.1</a>
          </li>
          <li>
            <a href="#">Sub Menu 1.2</a>
          </li>
          <li>
            <a href="#">Sub Menu 1.3</a>
          </li>
          <li>
            <a href="#">Sub Menu 1.4</a>
          </li>
          <li>
            <a href="#">Sub Menu 1.5</a>
          </li>
          <li>
            <a href="#">Sub Menu 1.6</a>
          </li>
          <li>
            <a href="#">Sub Menu 1.7</a>
          </li>

        </ul>
      </li>
      <li>
        <a href="">Menu 2</a>
        <ul class="submenu">
          <li>
            <a href="#">Sub Menu 2.1</a>
          </li>
          <li>
            <a href="#">Sub Menu 2.2</a>
          </li>
          <li>
            <a href="#">Sub Menu 2.3</a>
          </li>
        </ul>
      </li>
      <li>
        <a href="#">Menu 3</a>
      </li>
      <li>
        <a href="#">Menu 4</a>
      </li>
    </ul>
  </nav>


</body>

</html>
css html5
1个回答
0
投票

这可能对你有帮助

body {
  margin: 0;
  font-family: Helvetica, sans-serif;
  background-color: #f4f4f4;
}

a {
  color: #000;
}

/* header */

.header {
  background-color: #fff;
  box-shadow: 1px 1px 4px 0 rgba(0,0,0,.1);
  position: fixed;
  width: 100%;
  z-index: 3;
}

.header ul {
  margin: 0;
  padding: 0;
  list-style: none;
  overflow: hidden;
  background-color: #fff;
}

.header li a {
  display: block;
  padding: 20px 20px;
  border-right: 1px solid #f4f4f4;
  text-decoration: none;
}

.header li a:hover,
.header .menu-btn:hover {
  background-color: #f4f4f4;
}

.header .logo {
  display: block;
  float: left;
  font-size: 2em;
  padding: 10px 20px;
  text-decoration: none;
}

/* menu */

.header .menu {
  clear: both;
  max-height: 0;
  transition: max-height .2s ease-out;
}

/* menu icon */

.header .menu-icon {
  cursor: pointer;
  display: inline-block;
  float: right;
  padding: 28px 20px;
  position: relative;
  user-select: none;
}

.header .menu-icon .navicon {
  background: #333;
  display: block;
  height: 2px;
  position: relative;
  transition: background .2s ease-out;
  width: 18px;
}

.header .menu-icon .navicon:before,
.header .menu-icon .navicon:after {
  background: #333;
  content: '';
  display: block;
  height: 100%;
  position: absolute;
  transition: all .2s ease-out;
  width: 100%;
}

.header .menu-icon .navicon:before {
  top: 5px;
}

.header .menu-icon .navicon:after {
  top: -5px;
}

/* menu btn */

.header .menu-btn {
  display: none;
}

.header .menu-btn:checked ~ .menu {
  max-height: 240px;
}

.header .menu-btn:checked ~ .menu-icon .navicon {
  background: transparent;
}

.header .menu-btn:checked ~ .menu-icon .navicon:before {
  transform: rotate(-45deg);
}

.header .menu-btn:checked ~ .menu-icon .navicon:after {
  transform: rotate(45deg);
}

.header .menu-btn:checked ~ .menu-icon:not(.steps) .navicon:before,
.header .menu-btn:checked ~ .menu-icon:not(.steps) .navicon:after {
  top: 0;
}

/* 48em = 768px */

@media (min-width: 48em) {
  .header li {
    float: left;
  }
  .header li a {
    padding: 20px 30px;
  }
  .header .menu {
    clear: none;
    float: right;
    max-height: none;
  }
  .header .menu-icon {
    display: none;
  }
}
<header class="header">
  <a href="" class="logo">CSS Nav</a>
  <input class="menu-btn" type="checkbox" id="menu-btn" />
  <label class="menu-icon" for="menu-btn"><span class="navicon"></span></label>
  <ul class="menu">
    <li><a href="#work">Our Work</a></li>
    <li><a href="#about">About</a></li>
    <li><a href="#careers">Careers</a></li>
    <li><a href="#contact">Contact</a></li>
  </ul>
</header>
© www.soinside.com 2019 - 2024. All rights reserved.