如何使这个菜单居中

时间:2013-06-14 14:01:06

标签: html css menu

您好我有一个下拉菜单,我需要以div为中心。 div是970px宽,我希望菜单位于中间。问题是无论我尝试什么,我都能让它发挥作用。

<div id="menuwrapper">
    <ul class="menu" id="menu">
        <li><a href="#" >Home</a></li>
        <li><a href="#">Services</a>
            <ul>
                <li><a href="#">Flights</a></li>
                <li><a href="#">Car Hire</a></li>
                <li><a href="#">Hotel Arrangements</a></li>
                <li><a href="#">Ferries</a></li>
                <li><a href="#">Other Services</a></li>
            </ul>
        </li>
        <li><a href="#">About us</a></li>
        <li><a href="#">Why us</a></li>
        <li><a href="#">Frequently Asked Questions</a></li>
        <li><a href="#">Testimonials</a></li>
        <li><a href="#">Contact us</a></li>
    </ul>
</div>

这是css

#menuwrapper {
    height: auto;
    width: 970px;
    position: relative;
    float: left;
    padding: 0px;
    margin: 0px;
    clear: both;
}

ul.menu {
    list-style:none;
    margin:0;
    padding:0;
    font: 16px/20px 'TitilliumText22LLight', Arial, sans-serif;
    text-shadow: #638517 1px 1px 1px;
    color:#FFF;
}

ul.menu * {
    margin:0;
    padding:0
}

ul.menu a {
    display:block;
    color:#FFF;
    text-decoration:none
}

ul.menu li {
    position:relative;
    float:left;
    margin-right:0px;
    border-right-style:none;
    border-left-style:none;
    height: 27px;
    border-top-style: none;
    border-bottom-style: none;
    background-image: url(menuslit.gif);
    background-repeat: repeat-y;
    background-position: left top;
    padding-top: 10px;
    padding-right: 6px;
    padding-bottom: 0px;
    padding-left: 6px;
}

ul.menu ul {
    position:absolute;
    top:37px;
    left:0;
    background:#3f3f3f;
    display:none;
    opacity:0;
    text-shadow: none;
    list-style:none;
}

ul.menu ul li {
    display:block;
    background-color:#3f3f3f;
    width: 200px;
    background-image: url(over2.gif);
    background-repeat: repeat;
    padding: 5px 0px 5px 5px;
    font: 14px/20px 'TitilliumText22LLight', Arial, sans-serif;
    height: auto;
}

ul.menu ul li:hover {
    background-image: url(over3small.gif);
    background-repeat: repeat;
}

ul.menu ul ul {
    left:205px;
    top:-0px
}

ul.menu .menulink {
    border:0px solid #aaa;
    padding:10px 7px 0px;
    font-weight:normal;
    width:auto;
    height:30px;
}

ul.menu li:hover {
    background-image: url(over3.gif);
    background-repeat: repeat;
    border-left-width: 0px;
    border-left-style: solid;
    border-left-color: #b7b7b7;
    text-shadow: none;
    background-position: left top;
}

ul.menu .sub {
    background:#d1d1d1 url(sdsimages/arrow.gif) 190px 8px no-repeat
}

ul.menu .topline {
    border-top:0px solid #aaa
}

3 个答案:

答案 0 :(得分:2)

我想这个:

#menuwrapper {text-align: center;}
ul.menu {display: inline-block;}

可能会这样做。

答案 1 :(得分:0)

给ul.menu一个宽度和可扩展的边距:

ul.menu{
    width: 600px;
    margin: 0 auto;
}

“margin:0 auto”表示顶部和底部没有边距,而右侧和左侧具有“自动”边距...意味着它将适合其容器。希望它有效!

答案 2 :(得分:0)

这是一个很棒的链接:

.container {
    width: 600px;
}

.list-centered {
    margin: 0 auto;
    width: -webkit-fit-content;
       width: -moz-fit-content;
            width: fit-content;
}

.list-centered li {
    float: left;
}

http://theamazingweb.net/2013/04/23/centered-fluid-width-navigation-with-floating-links-thanks-to-fit-content/