简体   繁体   English

当li浮动时如何居中

[英]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/ 我的演示链接: 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/ 我的JSFiddle: http : //jsfiddle.net/8DM65/

Please help! 请帮忙! Driving me nuts X_x 让我发疯X_x

HTML 的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 的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. 实际上,您已经向这些元素添加了太多CSS,并且似乎在向错误的方向编码自己。 I would scrap all the position: relative; 我会放弃所有position: relative; stuff and instead focus on building your li 's around inline-block . 东西,而是专注于围绕inline-block构建li That makes the li 's not expand width-wise push each other into a vertical stack. 这使得li 不会在宽度方向扩展,将彼此推入垂直堆栈中。

#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 . 它们是列表项,您想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/ 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. 我发现的是,将#main-nav宽度css更改为35%可以​​解决此问题,但是会删除多余的白色分隔线,因此您可以添加带有白色的hr标签以解决此问题。 To fix the #portfolio-nav, I changed the width to 44% and added margin: 0 auto. 要修复#portfolio-nav,我将宽度更改为44%,并增加了边距:0自动。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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