如何在html页面中间添加菜单?

时间:2015-12-23 15:49:23

标签: html5 css3 drop-down-menu

这是我想在中间添加的菜单代码

<div id="menu">

    <ul>
    <li><a href="Home.html">Home</a></li>
    <li><a href="Babyboy.html">Baby Boy</a>
       <ul>
        <li><a href="Boybodysuits.html">Bodysuits</a></li>
        <li><a href="Boyonepiece.html">One Piece</a></li>
        <li><a href="Boyshoes.html">Shoes</a></li>
       </ul>
     </li>
     <li><a href="Babygirl.html">Baby Girl</a>
       <ul>
        <li><a href="Girlbodysuits.html">Bodysuits</a></li>
        <li><a href="Girlonepiece.html">One Piece</a></li>
        <li><a href="Girlshoes.html">Shoes</a></li>
       </ul>
    </li>
    <li><a href="Accessories.html">Accessories</a></li>
    <li><a href="Contact_us.html">Contact Us</a></li>
    </ul>

    </div>

这是CSS代码

#menu ul{
    list-style:none;
    margin:0px;
    padding:0px;}

#menu ul li{
    background-color:#404a7f;
    border:1px solid white;
    border-radius:10px;
    margin-right:15px;
    font-family:Century Gothic;
    font-size:20px;
    font-weight:bold;
    width:120px;
    height:35px;
    line-height:35px;
    text-align:center;
    float:left;
    position:relative;}

#menu ul li a{
    text-decoration:none;
    color:white;
    display:block;}

#menu ul li a:hover{
    background-color:#191d33;
    border-radius:10px;}

#menu ul ul{
    position:absolute;
    display:none;}

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

所以我想让这个菜单水平显示在页面中间,但我不知道如何以简单的方式做到这一点。请有人帮帮我吗?

1 个答案:

答案 0 :(得分:0)

将此添加到您的CSS文件中:

#menu {
  position: absolute;
  top: 0;
  bottom: 0;
  left: 0;
  right: 0;
  margin: auto;
  width: 100px;
  height: 100px;
}

#menu ul ul positionabsolute更改为relative

#menu ul ul{
    position:relative;
    display:none;}

见小提琴:

https://jsfiddle.net/2j5k37mw/

或者如果您想将它们全部放在一行中,请将其添加到您的CSS文件中:

#menu {
    bottom: 0;
    display: table;
    height: 100%;
    left: 0;
    margin: 0 auto;
    position: absolute;
    right: 0;
    top: 0;
    width: 50%;
}