全宽CSS下拉菜单 [英] Full width css dropdown menu

查看:101
本文介绍了全宽CSS下拉菜单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图创建纯粹简单的CSS下拉菜单,将在每个项目下方的全宽度模式下打开,但也将保持它的父项下面的每个下拉菜单。
此图片更好地解释了我要实现的目标:

I am trying to create pure and simple CSS drop down menu which will open in a full width mode underneath each item but will also keep each drop down menu underneath it's parent item. this image better explains what I am trying to achieve:

我只是不知道如何保持每个li内嵌套的UL,但也有一个全宽的背景包装all。

I just can't figure out how to keep the nested UL inside each li but also have a full width background wrapping it all.

这是我在 Codepan 上准备的演示:
http://cdpn.io/wLjFm

Here is a demo I prepared on Codepan: http://cdpn.io/wLjFm

推荐答案

/* not very relevant styling */
h1         { font-size: 20px; padding-top: 20px; }
.container { position: relative; margin: 20px auto 0 auto; width: 75%; }
.header    { background: #eee; }
.nav       { background: #ccc; }

/* relevant styling */
body { overflow-x: hidden; } /* trick from css-tricks comments */

/* FIRST LEVEL */

.nav > ul > li { 
  display: inline-block; 
  position: relative; 
  padding: 3px 10px 3px 0;
  z-index: 100;
}

/* SECOND LEVEL */
.nav > ul > li > ul {
  position: absolute;
  left: 0;
  top: 100%;
  padding: 0 1000em; /* trick from css-tricks comments */
  margin: 0 -1000em; /* trick from css-tricks comments */
  z-index: 101;
  visibility: hidden;
  opacity: 0;
  background: rgba(255, 240, 240, 0.8);
}

.nav > ul > li:hover > ul {
  visibility: visible;
  opacity: 1;
}

.nav > ul > li > ul > li {
  padding: 3px 0;
}

.nav > ul > li:hover .drop {
  font-weight: bold;
}

<div class="header">

  <div class="container">
    <h1>Hank's Big Leauge Widgets</h1>
    <span>You want a widget? we got 'em!</span>
  </div>


  <!-- NAVIGATION -->    
  <div class="nav">
    <ul class="container">
      <li>
        <a class="drop" href="#">Products</a>
        <ul>
          <li><a href="#">Widget A</a></li>
          <li><a href="#">Widget B</a></li>
          <li><a href="#">Widget C</a></li>
        </ul>
      </li>
      <li>
        <a class="drop" href="#">Locations</a>
        <ul>
          <li><a href="#">Location A</a></li>
          <li><a href="#">Location B</a></li>
          <li><a href="#">Location C</a></li>
        </ul>
      </li>
      <li>
        <a class="drop" href="#">Staff</a>
        <ul>
          <li><a href="#">President</a></li>
          <li><a href="#">VP</a></li>
          <li><a href="#">Manager</a></li>
        </ul>
      </li>
    </ul>
  </div>
</div>

<div class="content container">
  <p>All sorts of content</p>
  <p>All sorts of content</p>
  <p>All sorts of content</p>
  <p>All sorts of content</p>
  <p>All sorts of content</p>
  <p>All sorts of content</p>
</div>


**CSS and HTML**

JSFIDDLE

这篇关于全宽CSS下拉菜单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆