Horizontal Menu with Vertical Submenu (HTML/CSS Only)

你说的曾经没有我的故事 提交于 2019-12-02 06:05:02

问题


I'm having a difficult time trying to make my submenu vertical instead of horizontal. Any help would be much appreciated.

HTML:

<ul id="menu">
  <li><a href="/" title="HOME">HOME</a></li>
  <li>
    <a href="/" title="ECO ENERGY">ECO ENERGY</a>
    <ul>
      <li><a href="/Evaluations" title="Evaluations">Evaluations</a></li>
      <li><a href="/Incentives" title="Incentives">Incentives</a></li>
      <li><a href="/HouseFiles" title="House Files">House Files</a></li>
      <li><a href="/UpdateSubmissions" title="Update Submissions">Update Submissions</a>/li>
    </ul>
    </li>
    <li>
      <a href="/" title="NEW HOMES">NEW HOMES</a>
      <ul>
        <li><a href="/NH" title="Evaluations">Evaluations</a></li>
      </ul>
    </li>
</ul>

CSS:

#menu {
  background-color: #206676;
  float: left;
  width: 100%;
  height: 60px;
}

ul#menu {
  font-size: 1.3em;
  font-weight: 600;
  margin: 0 0 5px;
  padding: 0;
  text-align: left;
}

ul#menu li {
  display: inline;
  list-style: none;
  padding-left: 15px;
  float: left;
}

ul#menu li a {
  background: none;
  color: #FFF;
  text-decoration: none;
}

ul#menu li a:hover {
  color: #FFF;
  text-decoration: none;
}

ul#menu ul {
  display: none;
}

ul#menu li {
  display: inline;
}

ul#menu li:hover ul {
  display: block;
}

ul#menu li:hover ul li a {
  display: block;
  color: red;
}

I have it to the point where I can toggle the colour when hovered over the main menu items, but just can't get them vertical.

fiddle HERE


回答1:


ul#menu ul
{
    display:none;
    position:absolute;
    left:0;
    top:20px;
}
ul#menu li
{
    display:block;   
}

Most important changes. JSFIDDLE> http://jsfiddle.net/LSbvJ/ (You will have to tweak paddings, margins, text-align... but this is good start, I hope.)




回答2:


    #menu {
      background-color: #206676;
      float: left;
      width: 100%;
      height: 60px;
    }
    ul#menu {
      font-size: 1.3em;
      font-weight: 600;
      margin: 0 0 5px;
      padding: 0;
      text-align: left;
    }
    ul#menu li {
      display: inline;
      list-style: none;
      padding-left: 15px;
      float: left;
    }
    ul#menu li a {
      background: none;
      color: #FFF;
      text-decoration: none;
    }
    ul#menu li a:hover {
      color: #FFF;
      text-decoration: none;
    }
    ul#menu ul {
      display: none;
    }
    ul#menu li {
      display: block;
    }
    ul#menu li:hover ul {
      display: block;
      position: absolute;
      width: 50px;
    }
    ul#menu li:hover ul li a {
      display: block;
      color: red;
    }
<ul id="menu">
  <li><a href="/" title="HOME">HOME</a>
  </li>
  <li><a href="/" title="ECO ENERGY">ECO ENERGY</a> 

    <ul>
      <li><a href="/Evaluations" title="Evaluations">Evaluations</a>
      </li>
      <li><a href="/Incentives" title="Incentives">Incentives</a>
      </li>
      <li><a href="/HouseFiles" title="House Files">House Files</a>
      </li>
      <li><a href="/UpdateSubmissions" title="Update Submissions">Update Submissions</a>/li>

    </ul>
    </li>
    <li><a href="/" title="NEW HOMES">NEW HOMES</a>

      <ul>
        <li><a href="/NH" title="Evaluations">Evaluations</a>
        </li>
      </ul>
    </li>
</ul>



回答3:


Just try this updated CSS.

ul#menu li:hover ul {
    display: block;
    position: absolute;
    top: 10x;
    width: 200px;
}

ul#menu {
    font-size: 1.3em;
    font-weight: 600;
    margin: 0 0 5px;
    padding: 0;
    text-align: left;
    position: relative
}

Below is a working Demo.
http://jsbin.com/buculimi/1/edit




回答4:


<html>
<head>
<style type="text/css" media="screen">
#menu{
    width:100%;
    display:block;
    position:relative;
    height:60px;
    color:#fff;
    text-decoration:none;
    border-style:inset;
}
#menu a{
    text-decoration:none;
    color:#fff;
}
#menu ul {
    padding:1; margin:1; list-style:none;font-size: 1.3em;
    font-weight: 600;
}
#menu li {
    float:left; position:relative; padding-right:100; display:block;
    border:none;
}
#menu li ul {
    display:none;
    position:absolute;
}
#menu li:hover ul{
    display:block;
    background:red;
    height:auto; width:8em;
}
#menu li ul li{
    clear:both;
    border-style:none;
}
</style>
</head>
<body>
  <div style="background-color:#206676;;width:100%;">
    <div id="menu">
        <ul>
          <li><a href="#" title="HOME">HOME</a></li>
          <li>
            <a href="#" title="ECO ENERGY">ECO ENERGY</a>
            <ul>
              <li><a href="/Evaluations" title="Evaluations">Evaluations</a></li>
              <li><a href="/Incentives" title="Incentives">Incentives</a></li>
              <li><a href="/HouseFiles" title="House Files">House Files</a></li>
              <li><a href="/UpdateSubmissions" title="Update Submissions">Update Submissions</a></li>
            </ul>
          </li>
          <li><a href="/" title="NEW HOMES">NEW HOMES</a>
            <ul>
              <li><a href="/NH" title="Evaluations">Evaluations</a></li>
            </ul>
          </li>
        </ul>

    </div>
</div>
</body>
</html>

This page might help you as well: http://mrbool.com/how-to-create-menu-with-submenu-using-csshtml/26146



来源:https://stackoverflow.com/questions/21997728/horizontal-menu-with-vertical-submenu-html-css-only

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!