【问题标题】:Pure css submenu纯 CSS 子菜单
【发布时间】:2018-07-20 07:50:57
【问题描述】:

我一直在使用以下代码无济于事,我无法让子菜单在悬停时显示为一列。

我不知道哪种方法是让它按预期工作的最佳方法。

我不明白为什么以下规则不会使子菜单项出现在列中(作为块元素):

.dropdown:hover ul, .dropdown ul li {display: block;}

你能告诉我我做错了什么吗?

谢谢!!

/*---------------------------- HEADER  */

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

.header ul {
    margin: 0 0;
    padding: 0;
    list-style: none;
    overflow: hidden;
    background-color: rgba(255,255,255,0.9);;
}

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

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


/* menu */
nav{
    position: relative;
    z-index: 1;
}
.header .menu {
  clear: both;
  max-height: 0;
  transition: max-height .2s ease-out;   
}

.menu {
    color: blue;
    font-size: 1.3rem;
}

.menu a{
    color: darkseagreen;
    font-size: 1.3rem;   
}

.menu a:hover{
    color: seagreen;
    font-size: 1.3rem;   
}




/* menu icon */


.header .menu-icon {
    cursor: pointer;
    display: inline-block;
    float: right;
    padding: 25px 17px;
    position: relative;
    user-select: none;
    border: 2px solid #999;
    border-radius: 5px;
    margin: 40px 25px 0 0;
}

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

.header .menu-icon .navicon:before,
.header .menu-icon .navicon:after {
  background: #999;
  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: 260px;
}

.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;
}

.logo{
    position: absolute;
    top: 10px;
    left: 3%;
    width: 70%;
    max-width: 200px;
}

/* SUBMENU TEST */
.menu li > ul{
  display: none;
}

.dropdown:hover ul, .dropdown ul li {
  display: block;
}

/* 48em = 768px */

@media (min-width: 48em) {
    

    .header li {
    float: left;
  }
    .header li a {
        padding: 20px 30px;
        border-bottom: none;
        border-right: 1px solid #f4f4f4;
}

  .header .menu {
    clear: none;
    float: right;
    max-height: none;
  }

  .header .menu-icon {
    display: none;
  }
}
<header class="header">
        <div class="logo">
            <img src="images/logo.png" />
        </div>
        <nav>
          <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="index.html">Inicio</a></li>
            <li><a href="nosotros.html">Nosotros</a></li>
            <li><a href="diferencial.html">Diferencial</a></li>
            <li class="dropdown">Divisiones
                <ul>
                  <li><a href="1.html">1</a></li>
                  <li><a href="2.html">2</a></li>
                  <li><a href="3.html">3</a></li>
                </ul></li>
            <li><a href="centros.html">Centros</a></li>
            <li><a href="contacto.html">Contacto</a></li>
          </ul>
        </nav>
    </header>    

【问题讨论】:

    标签: css drop-down-menu responsive-design submenu


    【解决方案1】:

    您在.head 中浮动所有 li。你只需要在.menu中浮动li

    改变

    .header  li {
      float: left;
    }
    

    .header .menu > li {
        float: left;
    }
    

    完整版:

    /*---------------------------- HEADER  */
    
    .header {
      background-color: #fff;
      box-shadow: 1px 1px 4px 0 rgba(0, 0, 0, .1);
      display: block;
      width: 100%;
      z-index: 3;
    }
    
    .header ul {
      margin: 0 0;
      padding: 0;
      list-style: none;
      overflow: hidden;
      background-color: rgba(255, 255, 255, 0.9);
      ;
    }
    
    .header li a {
      display: block;
      padding: 20px 20px;
      border-bottom: 1px solid #f4f4f4;
      text-decoration: none;
    }
    
    .header li a:hover,
    .header .menu-btn:hover {
      background-color: #f4f4f4;
    }
    
    
    /* menu */
    
    nav {
      position: relative;
      z-index: 1;
    }
    
    .header .menu {
      clear: both;
      max-height: 0;
      transition: max-height .2s ease-out;
    }
    
    .menu {
      color: blue;
      font-size: 1.3rem;
    }
    
    .menu a {
      color: darkseagreen;
      font-size: 1.3rem;
    }
    
    .menu a:hover {
      color: seagreen;
      font-size: 1.3rem;
    }
    
    
    /* menu icon */
    
    .header .menu-icon {
      cursor: pointer;
      display: inline-block;
      float: right;
      padding: 25px 17px;
      position: relative;
      user-select: none;
      border: 2px solid #999;
      border-radius: 5px;
      margin: 40px 25px 0 0;
    }
    
    .header .menu-icon .navicon {
      background: #999;
      display: block;
      height: 2px;
      position: relative;
      transition: background .2s ease-out;
      width: 18px;
    }
    
    .header .menu-icon .navicon:before,
    .header .menu-icon .navicon:after {
      background: #999;
      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: 260px;
    }
    
    .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;
    }
    
    .logo {
      position: absolute;
      top: 10px;
      left: 3%;
      width: 70%;
      max-width: 200px;
    }
    
    
    /* SUBMENU TEST */
    
    .menu li>ul {
      display: none;
    }
    
    .dropdown:hover ul,
    .dropdown ul li {
      display: block;
    }
    
    
    /* 48em = 768px */
    
    @media (min-width: 48em) {
      .header .menu > li {
        float: left;
      }
      .header li a {
        padding: 20px 30px;
        border-bottom: none;
        border-right: 1px solid #f4f4f4;
      }
      .header .menu {
        clear: none;
        float: right;
        max-height: none;
      }
      .header .menu-icon {
        display: none;
      }
    }
    <header class="header">
      <div class="logo">
        <img src="images/logo.png" />
      </div>
      <nav>
        <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="index.html">Inicio</a></li>
          <li><a href="nosotros.html">Nosotros</a></li>
          <li><a href="diferencial.html">Diferencial</a></li>
          <li class="dropdown">Divisiones
            <ul>
              <li><a href="1.html">1</a></li>
              <li><a href="2.html">2</a></li>
              <li><a href="3.html">3</a></li>
            </ul>
          </li>
          <li><a href="centros.html">Centros</a></li>
          <li><a href="contacto.html">Contacto</a></li>
        </ul>
      </nav>
    </header>

    【讨论】:

    • 感谢您的解决!!!检查员一直将这些标签显示为块元素,我没有意识到浮动规则。
    猜你喜欢
    • 2012-05-15
    • 1970-01-01
    • 2012-10-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多