CSS水平下拉列表

时间:2015-12-01 08:34:59

标签: html css drop-down-menu

我目前正在尝试实现一个水平css下拉菜单,其中包含一个水平的子菜单。我已经尽力而为,直到初学者为止。我所能得到的只是默认的垂直下拉子菜单。

这是我的HTML代码:

<div class="dropdown">
    <button id="btn-icon">
        <i id="img-primary" class="fa fa-sitemap fa-2x"></i>
    </button>
    <ul class="dropdown-menu">
        <li><a href={% url 'acttemplate:listAll' %}>template</a></li>
        <li><a href={% url 'mission:create' userName %}>new</a></li>
    </ul>
</div> 

CSS:

.dropdown {
        position: relative;
        display: inline-block;
    }

    .dropdown .dropdown-menu {
        position: absolute;
        top: 100%;
        display: none;
        margin: 0px;
        list-style: none;
        padding: 0px;
    }

    .dropdown:hover .dropdown-menu {
        display: block;
    }

    .dropdown .dropdown-menu ul li {
        display: inline;
    }

    .dropdown button {
        color: #FF6223;
        background-color: #FFFFFF;
        border: 1px solid #B3B3B3;
        margin: 0px;
        font-size: 16px;
    }

    .dropdown a {
        display: block;
        padding: 0.2em 0.8em;
        text-decoration: none;
        background: #FFFFFF;
        border: 1px solid #B3B3B3;
        color: #333333;
    }

    .dropdown a:hover {
        background: #BBBBBB;
    }

    #img-primary {
        color: #347EFF;
        max-height: 20px;
        max-width: 28px;
        margin: 5px;
        margin-left: 50px;
    }

    #img-primary:hover {
        color: #000000;
        max-height: 20px;
        max-width: 28px;
        margin: 5px;
        margin-left: 50px;
    }

    #btn-icon {
        color: #000000;
        background-color: #FFFFFF;
        border: 1px solid #FFFFFF;
        border-radius: 5px;
        cursor: pointer;
    }

JSFidle

1 个答案:

答案 0 :(得分:0)

please see this link

我会帮助你。

其他例子:

&#13;
&#13;
/* Main menu settings */
#centeredmenu {
   clear:both;
   float:left;
   margin:0;
   padding:0;
   border-bottom:1px solid #000; /* black line below menu */
   width:100%;
   font-family:Verdana, Geneva, sans-serif; /* Menu font */
   font-size:90%; /* Menu text size */
   z-index:1000; /* This makes the dropdown menus appear above the page content below */
   position:relative;
}

/* Top menu items */
#centeredmenu ul {
   margin:0;
   padding:0;
   list-style:none;
   float:right;
   position:relative;
   right:50%;
}
#centeredmenu ul li {
   margin:0 0 0 1px;
   padding:0;
   float:left;
   position:relative;
   left:50%;
   top:1px;
}
#centeredmenu ul li a {
   display:block;
   margin:0;
   padding:.6em .5em .4em;
   font-size:1em;
   line-height:1em;
   background:#ddd;
   text-decoration:none;
   color:#444;
   font-weight:bold;
   border-bottom:1px solid #000;
}
#centeredmenu ul li.active a {
   color:#fff;
   background:#000;
}
#centeredmenu ul li a:hover {
   background:#36f; /* Top menu items background colour */
   color:#fff;
   border-bottom:1px solid #03f;
}
#centeredmenu ul li:hover a,
#centeredmenu ul li.hover a { /* This line is required for IE 6 and below */
   background:#36f; /* Top menu items background colour */
   color:#fff;
   border-bottom:1px solid #03f;
}

/* Submenu items */
#centeredmenu ul ul {
   display:none; /* Sub menus are hiden by default */
   position:absolute;
   top:2em;
   left:0;
   right:auto; /*resets the right:50% on the parent ul */
   width:10em; /* width of the drop-down menus */
}
#centeredmenu ul ul li {
   left:auto;  /*resets the left:50% on the parent li */
   margin:0; /* Reset the 1px margin from the top menu */
   clear:left;
   width:100%;
}
#centeredmenu ul ul li a,
#centeredmenu ul li.active li a,
#centeredmenu ul li:hover ul li a,
#centeredmenu ul li.hover ul li a { /* This line is required for IE 6 and below */
   font-size:.8em;
   font-weight:normal; /* resets the bold set for the top level menu items */
   background:#eee;
   color:#444;
   line-height:1.4em; /* overwrite line-height value from top menu */
   border-bottom:1px solid #ddd; /* sub menu item horizontal lines */
}
#centeredmenu ul ul li a:hover,
#centeredmenu ul li.active ul li a:hover,
#centeredmenu ul li:hover ul li a:hover,
#centeredmenu ul li.hover ul li a:hover { /* This line is required for IE 6 and below */
   background:#36f; /* Sub menu items background colour */
   color:#fff;
}

/* Flip the last submenu so it stays within the page */
#centeredmenu ul ul.last {
   left:auto; /* reset left:0; value */
   right:0; /* Set right value instead */
}

/* Make the sub menus appear on hover */
#centeredmenu ul li:hover ul,
#centeredmenu ul li.hover ul { /* This line is required for IE 6 and below */
   display:block; /* Show the sub menus */
}
&#13;
<div id="centeredmenu">
   <ul>
      <li><a href="#">Tab one</a>
         <ul>
            <li><a href="#">Link one</a></li>
            <li><a href="#">Link two</a></li>
            <li><a href="#">Link three</a></li>
            <li><a href="#">Link four</a></li>
            <li><a href="#">Link five</a></li>
         </ul>
      </li>
      <li class="active"><a href="#" class="active">Tab two</a>
         <ul>
            <li><a href="#">Link one</a></li>
            <li><a href="#">Link two</a></li>
            <li><a href="#">Link three</a></li>
            <li><a href="#">Link four</a></li>
            <li><a href="#">Link five is a long link that wraps</a></li>
         </ul>
      </li>
      <li><a href="#">Long tab three</a>
         <ul>
            <li><a href="#">Link one</a></li>
            <li><a href="#">Link two</a></li>
            <li><a href="#">Link three</a></li>
            <li><a href="#">Link four</a></li>
            <li><a href="#">Link five</a></li>
         </ul>
      </li>
      <li><a href="#">Tab four</a>
         <ul class="last">
            <li><a href="#">Link one</a></li>
            <li><a href="#">Link two</a></li>
            <li><a href="#">Link three</a></li>
            <li><a href="#">Link four</a></li>
            <li><a href="#">Link five</a></li>
         </ul>
      </li>
   </ul>
</div>
&#13;
&#13;
&#13;