Logan Case
Logan Case

Reputation: 3

I need help centering a ul item within a div containing a 100% width background

I am new to html and css. I am currently taking a class on html5 and css3. We are required to create a website for the class. I have started creating the layout and I am having trouble getting the navigation at the top to be centered on the page. There is a Ul with a 100% background but I cannont get the buttons (text) to be centered without the Ul no longer being 100% width. ` Here is my CSS code:

#horizontalNav {
    display: block;
}


#horizontalNav li {
    float: left;
    margin-left: 13px;
    margin-right: 30px;
}   

#horizontalNav ul {
    overflow: hidden;
    background-color: #003300;
    width: 100%;
    margin: 0;
    padding: 0;
    list-style-type: none;
}

/* Button Styles*/

#horizontalNav a.navBtn {
    color: #009933;
    font-size: 1.8em;
    text-decoration: none;
    font-family: Impact, Charcoal, sans-serif;
}   

#horizontalNav a.navBtn:hover {
    color: #FFFF99;
}   

/*Drop down menu */

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

#horizontalNav ul ul {
    display: none;
    position: absolute;
    min-width: 150px;
    max-width: 150px;
    padding-top: 2px;
    padding-bottom: 8px;
    margin-left: -10px;
}

#horizontalNav ul ul li {
    display: block;
    clear: both;
}   

#horizontalNav ul ul li a {
    color: #009933;
    font-size: 1.4em;
    font-family: Impact, Charcoal, sans-serif;
    text-decoration: none;
}   

#horizontalNav ul ul li a:hover {
    color: #FFFF99;
}`   

Upvotes: 0

Views: 24

Answers (1)

EternalHour
EternalHour

Reputation: 8621

As my comment stated, you cannot center floated elements. Use display: inline-block;

Change

#horizontalNav li {
    float: left;
    margin-left: 13px;
    margin-right: 30px;
}  

to

#horizontalNav li {
    display: inline-block;
    margin-left: 13px;
    margin-right: 30px;
}  

Upvotes: 1

Related Questions