Search code examples
htmlcssmenucenternav

Trouble centering an inline menu


I'm a bit frustrated. I made a menu inline and blocked and centered it, but it's still a little to the right. I could adjust it with margin, but I think there's a better way to do it. Thank you for your help, I really appreciate it. Here's the HTML:

<body>
<div id="home">
<div id="header">
    <a href="index.html"><h1>Josh Lamstein</h1></a>
</div>
<div id="book">
<div id="topbar">
    <ul id="menu">
    <li><a href="bio.html">About</a></li>
    |
    <li><a href="stories.html">Stories</a></li>
    |
    <li><a href="http://joshlamstein.wordpress.com">Blog</a></li>
    |
    <li><a href="Resume.pdf">Resume</a></li>
    </ul>
</div>

And here's the CSS:

#home{
width:900px;
height:480px;
margin:0 auto;
}

#book{
width:100%;
height:50%;
margin:0 auto;
background-color:;
text-align:center;
}

#topbar{
width:100%;
height:20%;
background-color:blue;
display:inline-block;
list-style:none;
list-style-type:none;
text-align:center;
margin:auto;

}

#menu{
list-style-type:none;
background-color:;
display:block;
display:inline;
font-family:"helvetica neue", helvetica, sans-sarif;
font-variant:small-caps;
font-size:1em;
letter-spacing:.5em;
margin:auto;
}

#menu li {
display:inline;
}

Solution

  • That's because browsers apply a default padding-left property to HTML list elements such as <ul> (Google Chrome set -webkit-padding-start: 40px;).

    You could reset the padding property by:

    #menu {
        /*  other CSS declaration here ... */
        padding: 0; /* <-- Reset the default padding of user agent stylesheet */
    }
    

    Here is the JSBin Demo.