简体   繁体   English

使用CSS / HTML居中页脚

[英]Centering Footer With CSS/HTML

I have a problem centering the copyright footer on my website. 我的版权页脚在我的网站上存在问题。 I have two icons on the left (Facebook and Twitter), but if I add another for MySpace, it goes off. 我左边有两个图标(Facebook和Twitter),但如果我为MySpace添加另一个图标,它就会消失。 How can I fix this? 我怎样才能解决这个问题? Can it be indefinitely centered so I won't have to change it every time? 它可以无限期居中,所以我不必每次都改变它吗? Thanks. 谢谢。

TEMPLATE: 模板:

<div id="footer">
    <div class="social">
        <ul>
            <li><a href="http://www.facebook.com/DearRicky" target="_blank" title="Facebook"><img src="../images/icons/facebook.png" /></a></li>
            <li><a href="http://www.twitter.com/DearRicky" target="_blank" title="Twitter"><img src="../images/icons/twitter.png" /></a></li>
        </ul>
    </div>
    <div class="copyright">
        Copyright &copy; 2011 Ricky Wai Kit Tsang.  All rights reserved.
    </div>
    <div class="clear"></div>
</div>

CSS: CSS:

#footer {
    margin: 0px auto;
    padding-bottom: 60px;
    width: 850px;
}

#footer .social {
    float: left;
}

#footer .social ul {
    list-style: none;
    margin: 10px 0px 0px;
    padding: 0px;
}

#footer .social li {
    float: left;
    margin-right: 5px;
}

#footer .social img {
    border: 0px;
}

#footer .copyright {
    color: #fff;
    float: left;
    line-height: 32px;
    margin-left: 200px;
    margin-top: 10px;
    text-align: center;
}

#footer .resize {
    float: right;
    width: 400px;
}

By floating your copyright and not specifying a width, your "text-align:center;" 通过浮动您的版权而不指定宽度,您的“text-align:center;” rule has little effect. 规则影响不大。 A floated element without a defined width shrinks to fit its content. 没有定义宽度的浮动元素会缩小以适合其内容。 What is giving you your perceived sense of center is your "margin-left:200px;" 什么让你感觉到你的中心感是你的“左边缘:200px;” rule. 规则。 It is pushing your copyright to the right of the bookmarks by 200px. 它将您的版权推向书签的权利200px。

--edit-- - 编辑 -

Centered in footer 以页脚为中心

#footer { position:relative; width:850px; } /* position children relative to parent */
#footer .social { position:absolute; left:0; top:0 } /* take the bookmarks out of the flow and position in upper left corner of the footer */
#footer .copyright { text-align:center; } /* since bookmarks are out of normal flow, this div stretches entire length, so all you have to do is center it */

Centered in space to right of bookmarks 以空间为中心到书签右侧

#footer .social { float:left; width:200px; } /* give a specific width and float left */
#footer .copyright { float:left; width:650px; text-align:center; } /* center in remaining space */

HTML HTML

<div id="footer">
<div class="copyright">
    Copyright &copy; 2011 Ricky Wai Kit Tsang.  All rights reserved.
</div>
<div class="social">
    <ul>
        <li><a href="http://www.facebook.com/DearRicky" target="_blank" title="Facebook"><img src="../images/icons/facebook.png" /></a></li>
        <li><a href="http://www.twitter.com/DearRicky" target="_blank" title="Twitter"><img src="../images/icons/twitter.png" /></a></li>
        <li><a href="http://www.myspace.com/DearRicky" target="_blank" title="MySpace"><img src="../images/icons/myspace.png" /></a></li>
    </ul>
</div>
</div>

CSS CSS

#footer {
margin: 0px auto;
width: 850px;
}    
#footer .social {
padding: 30px 0;
width: 425px;
text-align: center;
}
#footer .copyright {
float: right;
padding: 30px 0;
width: 425px;
text-align: center;
}
#footer{position:relative}
#footer .copyright{position:absolute;top:10px;left:40%;}

position absolute the copyrights will do the trick as it will take it out of the flow. 绝对的位置版权将成功,因为它将把它从流程中取出。

but dont forget to set parent(footer) to relative. 但不要忘记将父(页脚)设置为亲戚。 so it would contain the copyright element inside it. 所以它将包含其中的版权元素。

You don't --have-- to position absolute to make this work. 你没有 - 为了使这项工作绝对定位。 Just left float both columns, set width to ensure they don't overrun the container, and clearfloat at the end if you have anything that will follow. 只需向左浮动两列,设置宽度以确保它们不会超出容器,如果您有任何后续内容,则在最后清除浮动。 Pretty easy, really. 很简单,真的。

To keep the text centered no matter what, take the list containing the social icons out of the flow, Absolutely position the social list relative to the footer, that way it has no bearing on the centering of the actual text 为了使文本保持居中,无论如何,从流程中取出包含社交图标的列表,绝对地将社交列表相对于页脚定位,这样就不会影响实际文本的居中

Working example of the following code : HERE 以下代码的工作示例: HERE

CSS: CSS:

#footer {
    margin: 0px auto;
    padding-bottom: 60px;
    width: 850px;
    background: #444;
    position: relative; /* so social list can be positioned relative to the footer*/
    text-align: center; /* center the copyright text */
}

#footer .social {
    position: absolute; /* position the list */
    top: 0; /* adjust to suit */
    left: 10px; /* adjust to suit */
}

#footer .social ul {
    list-style: none;
    margin: 10px 0px 0px;
    padding: 0px;
}

#footer .social li {
    float: left;
    margin-right: 5px;
}

#footer .social img {
    border: 0px;
}

#footer .copyright {
/* no need to float so no need for the clearing div at the bottom of your HTML */
    color: #fff;
    line-height: 32px;
    margin-top: 10px;
}

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

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