如何转换为CSS下拉菜单

时间:2012-03-24 22:00:20

标签: css drop-down-menu

CSS新手。我有一个水平菜单,我想转换为下拉菜单。

这是CSS:

.menu_container {
   position:relative;  
}

ul.semiopaquemenu{ /* main menu UL */
    font-family: 'Metrophobic', Arial, serif; font-weight: 800; 
    font-size:18px;
    width: 100%;
    background: #ccc;
    padding: 11px 0 8px 0; /* padding of the 4 sides of the menu */
    margin: 0;
    text-align: left; /* set value to "left", "center", or "right" to align menu accordingly */
}

ul.semiopaquemenu li{
    display: inline;
}

ul.semiopaquemenu li a{
    color:white;
    padding: 6px 8px 6px 8px; /* padding of the 4 sides of each menu link */
    margin-right: 15px; /* spacing between each menu link */
    text-decoration: none;
}

ul.semiopaquemenu li a:hover, ul.semiopaquemenu li a.selected{
    color: #0080FF;
    background: url(data:image/svg+xml;base64,PD94bWwgdmVyc2lvbj0iMS4wIiA/Pgo8c3ZnIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgd2lkdGg9IjEwMCUiIGhlaWdodD0iMTAwJSIgdmlld0JveD0iMCAwIDEgMSIgcHJlc2VydmVBc3BlY3RSYXRpbz0ibm9uZSI+CiAgPGxpbmVhckdyYWRpZW50IGlkPSJncmFkLXVjZ2ctZ2VuZXJhdGVkIiBncmFkaWVudFVuaXRzPSJ1c2VyU3BhY2VPblVzZSIgeDE9IjAlIiB5MT0iMCUiIHgyPSIwJSIgeTI9IjEwMCUiPgogICAgPHN0b3Agb2Zmc2V0PSIwJSIgc3RvcC1jb2xvcj0iI2ZmZmZmZiIgc3RvcC1vcGFjaXR5PSIwLjgyIi8+CiAgICA8c3RvcCBvZmZzZXQ9IjEwMCUiIHN0b3AtY29sb3I9IiNmZmZmZmYiIHN0b3Atb3BhY2l0eT0iMC4xNiIvPgogIDwvbGluZWFyR3JhZGllbnQ+CiAgPHJlY3QgeD0iMCIgeT0iMCIgd2lkdGg9IjEiIGhlaWdodD0iMSIgZmlsbD0idXJsKCNncmFkLXVjZ2ctZ2VuZXJhdGVkKSIgLz4KPC9zdmc+); /* IE9+ SVG equivalent  of linear gradients */
    background: -moz-linear-gradient(top,  rgba(255,255,255,0.82) 0%, rgba(255,255,255,0.16) 100%); /* fade from white (0.82 opacty) to 0.16 opacity */
    background: -webkit-gradient(linear, left top, left bottom, color-stop(0%,rgba(255,255,255,0.82)), color-stop(100%,rgba(255,255,255,0.16)));
    background: -webkit-linear-gradient(top,  rgba(255,255,255,0.82) 0%,rgba(255,255,255,0.16) 100%);
    background: -o-linear-gradient(top,  rgba(255,255,255,0.82) 0%,rgba(255,255,255,0.16) 100%);
    background: -ms-linear-gradient(top,  rgba(255,255,255,0.82) 0%,rgba(255,255,255,0.16) 100%);
    background: linear-gradient(top,  rgba(255,255,255,0.82) 0%,rgba(255,255,255,0.16) 100%);
    filter: progid:DXImageTransform.Microsoft.gradient( startColorstr='#d1ffffff', endColorstr='#29ffffff',GradientType=0 );
    -moz-box-shadow: 0 0 10px #595959; /* CSS3 box shadows */
    -webkit-box-shadow: 0 0 10px #595959;
    box-shadow: 0 0 10px #595959;
    padding-top: 12px; /* large padding to get menu item to protrude upwards */
    padding-bottom: 9px; /* large padding to get menu item to protrude downwards */
}

.semiopaquemenu ul  {
    position:absolute; 
    left:-9999px; 
    top:-9999px; 
    list-style-type:none;
}

.semiopaquemenu li:hover    {
    position:relative;
}

.semiopaquemenu li:hover ul     {
    left:0px; 
    top:30px;  
    padding:3px; 
    width:160px;
}

.semiopaquemenu li:hover ul li  {
    height:18px; 
    border:none;
}

.semiopaquemenu li:hover ul li a    {
    height:18px; 
    padding:0px; 
    display:block; 
    width:158px; 
    line-height:18px; 
    text-indent:5px;
}

.semiopaquemenu li:hover ul li a:hover {
    height:18px;
}

这是我的HTML:

<ul class="semiopaquemenu">
<li><a href="#">Home</a></li>
<li><a href="#" class="selected">Messaging</a></li>
<li><a href="#">Directory</a></li>
<li><a href="#">Options</a></li>
</ul>

如何将菜单项“选项”转换为下拉菜单

谢谢!

2 个答案:

答案 0 :(得分:3)

demo jsBin

添加此行并玩游戏:

CSS:

  ul.semiopaquemenu > li > ul{
      background:#ccc;
      padding:15px 20px;
  }

  ul.semiopaquemenu > li > ul li {
      color:white;
      cursor:pointer;
      display:block;
      padding:4px 10px;
  }

HTML:

<ul class="semiopaquemenu">
  <li><a href="#">Home</a></li>
  <li><a href="#" class="selected">Messaging</a></li>
  <li><a href="#">Directory</a></li>


  <li>
    <a href="#">Options</a>
    <ul>
      <li>Option 1</li>
      <li>Option 2</li>
      <li>Option 3</li>
      <li>Option 4</li>
    </ul>
  </li>
</ul>

答案 1 :(得分:2)

嗯,对于初学者来说,你需要一些额外的列表项来实际拥有“下拉”的东西,所以另一个<ul>。放置它的地方是<li><a href="#">Options</a> RIGHT HERE </li>。为了实现目标,只需将您已有的HTML复制/粘贴到该点:

<ul class="semiopaquemenu">
<li><a href="#">Home</a></li>
<li><a href="#" class="selected">Messaging</a></li>
<li><a href="#">Directory</a></li>
<li><a href="#">Options</a>
    <ul class="semiopaquemenu">
    <li><a href="#">Home</a></li>
    <li><a href="#" class="selected">Messaging</a></li>
    <li><a href="#">Directory</a></li>
    <li><a href="#">Options</a></li>
    </ul>
</li>
</ul>

当前的CSS产生了这个,一个工作下拉菜单:http://jsfiddle.net/yAmWb/