简体   繁体   中英

How to center li when it's floated left

Hi this seems to be a common problem, however the solutions I've found haven't worked for me yet :(

I found this however this solution didn't work for me for some reason.

My demo link: http://leongaban.com/_stack/centering/

I'm trying to get the top nav to center, as well as the Portfolio Nav to center as well.

My JSFiddle: http://jsfiddle.net/8DM65/

Please help! Driving me nuts X_x

HTML

<header>

    <div id="main-nav">
        <ul>
            <li><a href="#content">Portfolio</a></li>
            <li><a href="#footer">Contact Me</a></li>
            <li><a href="#">Resume</a></li>
        </ul>
    </div>

    <div id="logo-title">
        <img src="images/leon_gaban.png" width="256" height="256" class="avatar" />

        <h1>Hello</h1>
        <h2>Web Designer &amp; Developer</h2>
        <h3>And self-improvement blogger</h3>
    </div>

</header>

<section id="content">

    <div class="portfolio-nav">
        <ul>
            <li class="cta">Select Portfolio</li>
            <li class="selected"><a href="#">Design &amp; Development</a></li>
            <li class="not-selected"><a href="#">Flash &amp; Animation</a></li>
        </ul>
    </div>

    <div id="showcase-div">
        <ul id="showcase-boxes">
            <li>Test</li>
            <li></li>
            <li></li>
            <li></li>
            <li></li>
            <li></li>
            <li></li>
            <li></li>
            <li></li>
            <li></li>
            <li></li>
            <li></li>
        </ul>
    </div>

</section>

CSS

/* 02 Header */
header {
width: 100%;
height: 720px;
margin: 0 auto;
background: #ededed;
border-bottom: 1px solid #ccc;
}

header h1 {
font-size: 2em;
font-weight: 400;
font-style: italic;
}

header h2 {
font-size: 3.125em;
font-weight: 700;
}

header h3 {
font-size: 1.125em;
font-weight: 400;
font-style: italic;
}

#logo-title {
width: 100%;
margin: 60px auto;
text-align: center;
}

.avatar {
width: 256px;
height: 256px;
margin-bottom: 20px;

-webkit-border-radius: 128px;
-moz-border-radius: 128px;
-ms-border-radius: 128px;
-o-border-radius: 128px;
border-radius: 128px;

-webkit-box-shadow: 0 0 0px 6px white, 0 0 0 8px #cccccc, 0 10px 40px #333333;
-moz-box-shadow: 0 0 0px 6px white, 0 0 0 8px #cccccc, 0 10px 40px #333333;
box-shadow: 0 0 0px 6px white, 0 0 0 8px #cccccc, 0 10px 40px #333333;
}

#main-nav {
width: 80%;
height: 100px;
margin: 0 auto;
text-align: center;
font-size: 1.5em;
border-bottom: 2px solid white;
}

#main-nav ul {
clear: left;
float: left;
width: 100%;
list-style: none;
padding: 30px 0;
position:relative;
left:50%;
}

#main-nav ul li {
display:block;
position:relative;
right:50%;
float: left;
padding: 0 20px;
}

/* 03 Content */
#content {
width: 100%;
height: 100%;
margin: 0 auto;
}

#content ul {
list-style: none;
}

.portfolio-nav {
height: 60px;
padding: 30px 0 0 0;
background: #ccc;
}

.portfolio-nav ul {
margin: 0 auto;
text-align: center;
}

.portfolio-nav ul li {
display: inline;
float: left;
padding: 0 20px;
text-align: center;
}

#showcase-div {
width: 80%;
height: 100%;
margin: 0 auto;
background: blue;
padding-bottom: 60px;
}

You've actually added too much CSS to these elements and appear to be coding yourself in the wrong direction. I would scrap all the position: relative; stuff and instead focus on building your li 's around inline-block . That makes the li 's not expand width-wise push each other into a vertical stack.

#main-nav ul li {
    display: inline-block;
    padding: 0 20px;
}

#main-nav ul {
    list-style: none;
    padding: 30px 0;
}

Why have you gone with a big mixture of floats, clears, display and positioning? You need to remove a lot of code if you're going to make any sense of what you're doing.

For example, that first navigation. You don't need to display the list-items as block-level elements, and then float them, and then clear them, and then try and position them halfway across the page.

They are list-items, and you want to display them inline .

#main-nav ul {
    width: 100%;
    list-style: none;
    padding: 30px 0;
}

#main-nav ul li {
    display: inline;
    padding: 0 20px;
}

http://jsfiddle.net/8DM65/2/

It appears I can get this to work very easily with the following:

.portfolio-nav{
    text-align: center;
}


.portfolio-nav > ul{
    display: inline-block;
}

What i found was that changing the #main-nav width css to 35% fixed it but removes the extra white separator line, so you could add an hr tag with a color of white to fix that. To fix the #portfolio-nav, I changed the width to 44% and added margin: 0 auto.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM