Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
380 views
in Technique[技术] by (71.8m points)

html - How I can center a menu bar with ul

I have a simple menu bar inside a div and I can center the div but I want to center the menu bar inside the div.

This is the code:

CSS:

* {
    margin: 0px;
    padding: 0px;
}


.menu {
    height: 35px;
    width: auto;
    background: url("../img/slash-layer.png") repeat scroll 0% 0% rgb(130, 28, 107);
    border-radius: 15px;
    padding-top: 10px;
}

ul#navmenu, ul.sub1, ul.sub2 {
    list-style-type: none;
    font-size: 9pt;
    max-width: 660px;
    margin: 0 auto;
}
ul#navmenu li {
    width: 125px;
    text-align: center;
    position: relative;
    float: left;
    margin-right: 4px;
}
ul#navmenu a {
    text-decoration: none;
    display: block;
    width: 125px;
    height: 25px;
    line-height: 25px;
    background-color: #FFF;
    border: 1px solid #CCC;
    border-radius: 10px;
}
ul#navmenu .sub1 li {
}
ul#navmenu .sub1 a {
    margin-top: 5px;
}
ul#navmenu .sub2 a {
    margin-left: 3px;
}
ul#navmenu li:hover > a {
    background-color: #CFC;
}
ul#navmenu li:hover a:hover {
    background-color: #FF0;
}
ul#navmenu ul.sub1 {
    display: none;
    position: absolute;
    top: 26px;
    left: 0px;
}
ul#navmenu ul.sub2 {
    display: none;
    position: absolute;
    top: 0px;
    left: 126px;
}
ul#navmenu li:hover .sub1 {
    display: block;
}
ul#navmenu .sub1 li:hover .sub2 {
    display: block;
}
.darrow {
    font-size: 11pt;
    position: absolute;
    top: 5px;
    right: 4px;
}
.rarrow {
    font-size: 13pt;
    position: absolute;
    top: 6px;
    right: 4px;
}

HTML:

<html>
<head>
    <title><?php echo $title; ?></title>
    <link rel="stylesheet" type="text/css" href="recursos/css/main.css">
    <meta charset="utf-8">
    <div class="logo"></div>
    <div class="menu">
    <ul id="navmenu">
        <li><a href="#">Home</a></li>
        <li><a href="#">Novedades</a></li>
        <li><a href="#">Categorías <span class="darrow">▼</span></a>
        <ul class="sub1">
            <li><a href="#">Acción</a></li>
            <li><a href="#">Aventuras</a></li>
        </ul></li>
        <li><a href="#">Profile <span class="darrow">▼</span></a>
        <ul class="sub1">
            <li><a href="#">Menu 4.1</a></li>
            <li><a href="#">Menu 4.2 <span class="rarrow">?</span></a>
            <ul class="sub2">
                <li><a href="#">Menu 4.2.1</a></li>
                <li><a href="#">Menu 4.2.2</a></li>
            </ul></li>
        </ul></li>
    </ul>

    </div>
</head>
<body>

I tried to put

ul#navmenu {
    display: block
}

but it doesn't work and I don't understand what... Because I can center the div...

How I can do it?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

First of all, please note that you should put the elements inside the <body> tag.

Actually, The ul element is aligned at the center (See demo here). But the point is that the list items are floated to the left.

There are tow options:

  1. Using a proper fixed width (or max-width as you did) for the <ul> element. Working Demo

  2. Or use display: inline-block instead of float: left for the list items, and use text-align: center for the ul to align the inline elements horizontally. Working Demo.

But note that there is a white space between inline(-block) elements, which absolutely the normal behavior of inline elements in inline flow. You can refer my answer here to fix that.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...