以css下拉菜单为中心

时间:2011-07-01 16:51:34

标签: css menu drop-down-menu center alignment

我正试图将我的css下拉菜单放在中心位置,但我无法看到它让它工作!我试过添加text-align:center和margin-left:auto; margin-right:auto,但所有中心都是下拉项目。这是怎么做到的?

这是css:

.bg {background-color: #008503;}
.menu {padding:0 0 0 32px; margin:0; list-style:none; height:30px; background-color: #1e52b8; position:relative; font-family: Helvetica, Verdana, sans-serif; }
.menu li.top {display:block; float:left; position:relative;}
.menu li a.top_link {display:block; float:left; height:30px; line-height:33px; color:#FFF; text-decoration:none; font-size:15px; font-weight:bold; padding:0 0 0 12px; cursor:pointer;}
.menu li a.top_link span {float:left; font-weight:bold; display:block; padding:0 24px 0 12px; height:30px;}
.menu li a.top_link span.down {float:left; display:block; padding:0 24px 0 12px; height:40px; background:url(images/down.gif) no-repeat right top;}
.menu li a.top_link:hover {color:#000; background-color: #008503;}
.menu li a.top_link:hover span {background-color: #008503;}
.menu li a.top_link:hover span.down {background-color: #FF0000;}

.menu li:hover > a.top_link {color:#FFF; background-color: #008503;}
.menu li:hover > a.top_link span {background-color: #008503;}
.menu li:hover > a.top_link span.down {background-color: #FF0000;}

.menu table {border-collapse:collapse; width:0; height:0; position:absolute; top:0; left:0;}

.menu a:hover {visibility:visible;}
.menu li:hover {position:relative; z-index:200;}

.menu ul, 
.menu :hover ul ul, 
.menu :hover ul :hover ul ul,
.menu :hover ul :hover ul :hover ul ul,
.menu :hover ul :hover ul :hover ul :hover ul ul {position:absolute; left:-9999px; top:-9999px; width:0; height:0; margin:0; padding:0; list-style:none;}

.menu :hover ul.sub {left:0; top:30px; right:2px; background: #fff; padding:3px 0; border:1px solid #008503; white-space:nowrap; width:200px; height:auto;}
.menu :hover ul.sub li {display:block; height:20px; position:relative; float:left; width:250px;}
.menu :hover ul.sub li a {font-weight:normal;display:block; font-size:11px; height:20px; width:192px; line-height:20px; text-indent:5px; color:#000; text-decoration:none; border:3px solid #fff; border-width:0 0 0 3px;}
.menu :hover ul.sub li a.fly {background:#fff url(images/arrow.gif) 80px 7px no-repeat;}
.menu :hover ul.sub li a:hover {background:#1e52b8; color:#fff;}
.menu :hover ul.sub li a.fly:hover {background:#999999 url(images/arrow_over.gif) 80px 7px no-repeat; color:#fff;}
.menu :hover ul li:hover > a.fly {background:#999999 url(images/arrow_over.gif) 80px 7px no-repeat; color:#fff;} 

.menu :hover ul :hover ul,
.menu :hover ul :hover ul :hover ul,
.menu :hover ul :hover ul :hover ul :hover ul,
.menu :hover ul :hover ul :hover ul :hover ul :hover ul
{left:90px; top:-4px; background: #fff; padding:3px 0; border:1px solid 999999; white-space:nowrap; width:93px; z-index:200; height:auto;}

这是html:

<div style="width: 980px"> <!-- container -->
<ul class="menu">
    <li class="top"><a href="#" class="top_link"><span>Introduction</span></a></li>
    <li class="top"><a href="#" class="top_link"><span>About us</span></a>
        <ul class="sub">
            <li><a href="#">Sample Menu This is some longer text</a></li>           
            <li><a href="#">Sample Menu</a></li>
            <li><a href="#">Sample Menu</a></li>
            <li><a href="#">Sample Menu</a></li>
            <li><a href="#">Sample Menu</a></li>
        </ul>
    </li>
    <li class="top"><a href="#" class="top_link"><span>The island</span></a>
        <ul class="sub">
            <li><a href="#">Sample Menu This is some longer text</a></li>           
            <li><a href="#">Sample Menu</a></li>
            <li><a href="#">Sample Menu</a></li>
            <li><a href="#">Sample Menu</a></li>
        </ul>
    </li>
    <li class="top"><a href="#" class="top_link"><span>Hotels</span></a></li>
    <li class="top"><a href="#" class="top_link"><span>Tours</span></a></li>
    <li class="top"><a href="#" class="top_link"><span>Reservation</span></a></li>
    <li class="top"><a href="#" class="top_link"><span>Contact us</span></a></li>
</ul>
</div>

2 个答案:

答案 0 :(得分:4)

由于您使用的是float,因此唯一的方法是将宽度应用于包含元素并使用margin-left:auto; margin-right:auto。没有办法保持灵活的宽度。

float的替代方法是display:inline-block;这些可以使用text-align:center进行居中,但您必须重新编写css。

答案 1 :(得分:0)

嗯,这是一种方法,你可以在菜单上放一个div容器......

#menuContainer {margin:auto;}

&lt; div id ='menuContainer'style =“width:980px”&gt;