微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

css – 如何使浮动的div的内容居中:left?

我发现 this article,我决定我喜欢他们的样式链接和按钮的方式。
所以我从文章中拿出了CSS …
.buttons a,.buttons button{
    display:block;
    float:left;
    margin:0 7px 0 0;
    background-color:#f5f5f5;
    border:1px solid #dedede;
    border-top:1px solid #eee;
    border-left:1px solid #eee;

    font-family:"Lucida Grande",Tahoma,Arial,Verdana,sans-serif;
    font-size:100%;
    line-height:130%;
    text-decoration:none;
    font-weight:bold;
    color:#565656;
    cursor:pointer;
    padding:5px 10px 6px 7px; /* Links */
}
.buttons button{
    width:auto;
    overflow:visible;
    padding:4px 10px 3px 7px; /* IE6 */
}
.buttons button[type]{
    padding:5px 10px 5px 7px; /* Firefox */
    line-height:17px; /* Safari */
}
*:first-child+html button[type]{
    padding:4px 10px 3px 7px; /* IE7 */
}
.buttons button img,.buttons a img{
    margin:0 3px -3px 0 !important;
    padding:0;
    border:none;
    width:16px;
    height:16px;
}

然后我有几个按钮,我想要这样使用…

<div class="buttons">
    <a href="/password/reset/"><img src="pict1.png" class="positive" alt=""/>Button 1</a>
    <a href="/password/reset/"><img src="pict2.png" alt=""/>Button 2</a>
    <a href="/password/reset/"><img src="pict3.png" class="negative" alt=""/>Button 3</a>
</div>

见这个例子:http://reljac.com/csstest.php

但是这一排按钮可能需要对齐中心,而不是全部对齐或左右。如果我将CSS更改为…

.buttons a,.buttons button{
    /*display:block;
    float:left;*/
    margin:0 7px 0 0;

当有图像时,按钮不再正确显示,具体在IE 6,7和8。

见这个例子:http://reljac.com/csstest_wo.php

我可以将浮点数更改为右边的按钮,但是我无法弄清楚如何使它们居中(如< td>< / td>)。

所以我不想使用这种风格,但如果需要,我也需要能够对齐按钮。

解决方法

尝试将其添加到CSS中:
.buttons
{
    text-align:center;
    margin: 0px auto 0px auto;
}

自动使每边的边距相等。 text-align是旧版浏览器的风格。

编辑:

在称为buttonwrapper的按钮周围添加一个额外的div。然后应用这个CSS

.buttonwrapper
{
    position:relative;
    float:left;
    left:50%;
}

.buttons
{
    position:relative;
    float:left;
    left:-50%;
}

http://www.pmob.co.uk/temp/centred-float4.htm采取(但未测试)的方法

原文地址:https://www.jb51.cc/css/218221.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。