【问题标题】:how to Add sub menu to dropdown menu如何将子菜单添加到下拉菜单
【发布时间】:2013-07-25 11:52:40
【问题描述】:

我想在这个下拉功能中添加子菜单,但我对 css 知之甚少,我不知道如何做到这一点。当光标放在子菜单上时,我希望子菜单在右侧打开这是与之相关的 css 和 html

CSS

ul.dark_menu {
    list-style: none;
    padding: 5px 1px;
    font-family:'Segoe UI Light', 'Open Sans', Verdana, Arial, Helvetica, sans-serif;
    font-weight: 200;
    font-size: 16px;
    letter-spacing: 0.01em;
    font-smooth: always;
    color: #000000;
    line-height: 15px;
    margin: auto;
    width: 1018px;
    position: relative;
    background: #2B5797;
}
/* Blog johanes djogzs.blogspot.com */
 ul.dark_menu:after {
    content:"";
    clear: both;
    display: block;
    overflow: hidden;
    visibility: hidden;
    width: 0;
    height: 0;
}
ul.dark_menu li {
    float: left;
    position: relative;
    margin: 1px;
}
/* Blog johanes djogzs.blogspot.com */
 ul.dark_menu li a, ul.dark_menu li a:link {
    color: #fafafa;
    text-decoration: none;
    display: block;
    padding: 8px 26px;
    -webkit-transition: all 0.2s ease;
    -moz-transition: all 0.2s ease;
    -o-transition: all 0.2s ease;
    -ms-transition: all 0.2s ease;
    transition: all 0.2s ease;
}
/* Blog johanes djogzs.blogspot.com */
 ul.dark_menu li a:hover {
    color: #fafafa;
    -webkit-transition: all 0.5s ease;
    -moz-transition: all 0.5s ease;
    -o-transition: all 0.5s ease;
    transition: all 0.5s ease;
}
/* Blog johanes djogzs.blogspot.com */
 ul.dark_menu li a.selected {
    border-right: 1px solid #ddd;
    text-transform: uppercase;
    margin-left: 10px;
}
ul.dark_menu li a.selected, ul.dark_menu li a:active {
    color: #fafafa;
    -webkit-transition: all 0.5s ease;
    -moz-transition: all 0.5s ease;
    -o-transition: all 0.5s ease;
    transition: all 0.5s ease;
}
ul.dark_menu li ul {
    display: none;
}
/* Blog johanes djogzs.blogspot.com */
 ul.dark_menu li ul:before {
    content:" ";
    position: absolute;
    display: block;
    z-index: 1500;
    left: 0;
    top: -10px;
    height: 10px;
    width: 100%;
}
ul.dark_menu li ul {
    -webkit-transition: all 0.5s ease;
    -moz-transition: all 0.5s ease;
    -o-transition: all 0.5s ease;
    transition: all 0.5s ease;
    top:55px;
}
ul.dark_menu li:hover ul {
    position: absolute;
    display: block;
    z-index: 1000;
    box-shadow: 0 5px 10px rgba(0, 0, 0, 0.2);
    left: 0;
    border-radius: 0px 0px 5px 5px;
    top: 37px;
    padding: 5px 0;
    list-style: none;
    background: #fff;
}
/* Blog johanes djogzs.blogspot.com */
 ul.dark_menu li ul li {
    float: none;
    margin:0px;
}
ul.dark_menu li ul li:first-child {
    margin:0px;
    border-top: 0 none;
}
ul.dark_menu li ul li:last-child {
    border-bottom: 0 none;
}
/* Blog johanes djogzs.blogspot.com */
 ul.dark_menu[data-role="dropdown"] > a::before {
    position: absolute;
    content:"^";
    display: block;
    font-size: 15px;
    left: 100%;
    margin-left: -20px;
    top: 10px;
    -webkit-transform: rotate(180deg);
    -moz-transform: rotate(180deg);
    -ms-transform: rotate(180deg);
    -o-transform: rotate(180deg);
    transform: rotate(180deg);
}
ul.dark_menu li ul li a, ul.dark_menu li ul li a:link {
    color: #222;
    display: block;
    background: transparent none;
    padding: 10px 25px 10px 25px;
    white-space: nowrap;
}
ul.dark_menu li ul li a:hover {
    background:#2D89EF;
    -moz-transition: all 0.1s ease-in-out;
    color:#fff;
    -webkit-transition: all 0.1s ease-in-out;
}
/* Blog johanes djogzs.blogspot.com */
 .menujohanes {
    position:relative;
}
#search input[type="text"]:hover {
    width:848px;
}
#search input[type="text"] {
    background: #fff url("http://3.bp.blogspot.com/-uYZni0pIn-E/T-xY2vVu_-I/AAAAAAAACUY/ZMfR3_BvRFE/s1600/SEARCH_32x32-32.png")no-repeat center left;
    font-size: 13px;
    color: #222;
    width: 0px;
    padding: 10px 0px 11px 35px;
    z-index: 9;
    border: 1px solid #fff;
    -webkit-transition: all 0.5s ease-in-out;
    -moz-transition: all 0.5s ease-in-out;
    margin-bottom: -1px;
    position: absolute;
    top: 0px;
    right:27px;
}
#main4, #main5, #main6 {
    width: 303px;
    list-style-type: none;
    float: left;
    margin: 10px;
}
.main3 {
    width:305px;
    list-style-type: none;
    padding-top: 10px;
    float: left;
}
::-webkit-scrollbar {
    height:12px;
    background: #474747;
}
::-webkit-scrollbar-thumb {
    background-color: #000;
}

HTML

<div class='menujohanes'>
        <ul class='dark_menu'>
          <li>
            <a class='selected' expr:href='data:blog.homepageUrl' title='Home'>
              Home
            </a>
          </li>

       <li data-role='dropdown'>
            <a href='#'>
              Categories
            </a>
            <ul>
                 <li>
                    <a href='#'>
                    Lorem ipsum
                    </a>
                </li>

               <li>
                <a href='#'>
                  Lorem ipsum
                </a>
              </li>
              <li>
                <a href='#'>
                  Lorem ipsum
                </a>
              </li>
              <li>
                <a href='#'>
                  Lorem ipsum
                </a>
              </li>
              <li>
                <a href='#'>
                  Download
                </a>
              </li>
              <li>
                <a href='#'>
                  Lorem ipsum
                </a>
              </li>
            </ul>
          </li>
        </ul>
        <form action='/search' id='search' method='get'>
          <input name='q' placeholder='Search Somethings..' size='40' type='text'/>
        </form>
      </div>

请帮忙,因为我确实在 Google 上搜索过,但不明白如何在其中包含子菜单。

谢谢

【问题讨论】:

  • 所以你想要一个下拉菜单,在选择任何你希望其他下拉菜单打开的选项时??
  • 您可以使用JSFiddle 显示您的代码吗?

标签: html css templates drop-down-menu submenu


【解决方案1】:

试试这个

 <li class='sub'>
     <a href='#'>Lorem ipsum </a>
        <ul>
           <li>
             <a href='#'>
                sub-item 1
               </a>
            </li>
        </ul>
 </li>

并在 css 中添加这个

 li.sub ul {
display:none;
position: absolute; left: 100%; top:0;}

li.sub:hover ul{
display: block;}

要创建新的子菜单,只需将 class='sub' 添加到

  • 并在其下方添加新的
  • 【讨论】:

      【解决方案2】:

      试试这个:

      ul.dark_menu li ul li ul li { display: none; }
      ul.dark_menu li ul li:hover ul li { display: block;}
      ul.dark_menu li ul li:hover ul { left: 100%; top: 0; }
      

      http://jsfiddle.net/tkbub/1/

      【讨论】:

        【解决方案3】:

        /*Strip the ul of padding and list styling*/
        ul {
            list-style-type:none;
            margin:0;
            padding:0;
            position: absolute;
        }
        /*Create a horizontal list with spacing*/
        li {
            display:inline-block;
            float: left;
            margin-right: 1px;
        }
        /*Strip the ul of padding and list styling*/
        ul {
            list-style-type:none;
            margin:0;
            padding:0;
            position: absolute;
        }
        /*Create a horizontal list with spacing*/
        li {
            display:inline-block;
            float: left;
            margin-right: 1px;
        }
        /*Style for menu links*/
        li a {
            display:block;
            min-width:140px;
            height: 50px;
            text-align: center;
            line-height: 50px;
            font-family: "Helvetica Neue", Helvetica, Arial, sans-serif;
            color: #fff;
            background: #2f3036;
            text-decoration: none;
        }
        /*Hover state for top level links*/
        li:hover a {
            background: #19c589;
        }
        /*Style for dropdown links*/
        li:hover ul a {
            background: #f3f3f3;
            color: #2f3036;
            height: 40px;
            line-height: 40px;
        }
        /*Hover state for dropdown links*/
        li:hover ul a:hover {
            background: #19c589;
            color: #fff;
        }
        /*Hide dropdown links until they are needed*/
        li ul {
            display: none;
        }
        /*Make dropdown links vertical*/
        li ul li {
            display: block;
            float: none;
        }
        /*Prevent text wrapping*/
        li ul li a {
            width: auto;
            min-width: 100px;
            padding: 0 20px;
        }
        /*Display the dropdown on hover*/
        ul li a:hover + .hidden, .hidden:hover {
            display: block;
        }
        /*Style 'show menu' label button and hide it by default*/
        .show-menu {
            font-family: "Helvetica Neue", Helvetica, Arial, sans-serif;
            text-decoration: none;
            color: #fff;
            background: #19c589;
            text-align: center;
            padding: 10px 0;
            display: none;
        }
        /*Hide checkbox*/
        input[type=checkbox]{
            display: none;
        }
        /*Show menu when invisible checkbox is checked*/
        input[type=checkbox]:checked ~ #menu{
            display: block;
        }
        /*Responsive Styles*/
        @media screen and (max-width : 760px){
            /*Make dropdown links appear inline*/
            ul {
                position: static;
                display: none;
            }
            /*Create vertical spacing*/
            li {
                margin-bottom: 1px;
            }
            /*Make all menu links full width*/
            ul li, li a {
                width: 100%;
            }
            /*Display 'show menu' link*/
            .show-menu {
                display:block;
            }
        }
        <!doctype html>
        <html lang="en">
        <head>
            <meta charset="UTF-8">
            <title>CSS Only Navigation Menu</title>
            <meta name="viewport" content="width=device-width, initial-scale=1">
            <link rel="stylesheet" href="style.css">
        </head>
        <body>
            <ul id="menu">
                <li><a href="#">Home</a></li>
                <li>
                    <a href="#">About ↓</a>
                    <ul class="hidden">
                        <li><a href="#">Who We Are</a></li>
                        <li><a href="#">What We Do</a></li>
                    </ul>
                </li>
                <li>
                    <a href="#">Portfolio ↓</a>
                    <ul class="hidden">
                        <li><a href="#">Photography</a></li>
                        <li><a href="#">Web & User Interface Design</a></li>
                        <li><a href="#">Illustration</a></li>
                    </ul>
                </li>
                <li><a href="#">News</a></li>
                <li><a href="#">Contact</a></li>
            </ul>
        </body>
        </html

        【讨论】:

        • 您能否解释一下代码的作用以及它与 OP 问题的关系。
        猜你喜欢
        • 2020-09-06
        • 1970-01-01
        • 2015-09-30
        • 1970-01-01
        • 1970-01-01
        • 2019-07-22
        • 1970-01-01
        • 2022-11-20
        • 2011-11-28
        相关资源
        最近更新 更多