【问题标题】:Image contained in div not centering with margin: auto包含在 div 中的图像不以边距为中心:自动
【发布时间】:2012-06-19 00:10:04
【问题描述】:

我有以下 html 和 css 代码:

<!DOCTYPE HTML>
<html>
<head>
</head>

<body>

    <div class="wrapper">
        <header>
            <section class="top">
                <div id="quote"><a href="contact.html"><p>Request a quote</p></a></div>
                <div class="arrow"><img src="icons/top-icon.png" alt=""></div>
            </section>
</body>
</html>​​​​​​​​​​​​​​​​​​

a {
    font-size: 1.6em;
    color: #fff;
    text-decoration: none;
    }
.top {
    height: 3.2em;    
    width: 100%;
    background: rgb(255,214,94); /* Old browsers */
    background: -moz-linear-gradient(top,  rgba(255,214,94,1) 0%, rgba(254,191,4,1) 100%); /* FF3.6+ */
    background: -webkit-gradient(linear, left top, left bottom, color-stop(0%,rgba(255,214,94,1)), color-stop(100%,rgba(254,191,4,1))); /* Chrome,Safari4+ */
    background: -webkit-linear-gradient(top,  rgba(255,214,94,1) 0%,rgba(254,191,4,1) 100%); /* Chrome10+,Safari5.1+ */
    background: -o-linear-gradient(top,  rgba(255,214,94,1) 0%,rgba(254,191,4,1) 100%); /* Opera 11.10+ */
    background: -ms-linear-gradient(top,  rgba(255,214,94,1) 0%,rgba(254,191,4,1) 100%); /* IE10+ */
    background: linear-gradient(top,  rgba(255,214,94,1) 0%,rgba(254,191,4,1) 100%); /* W3C */
    filter: progid:DXImageTransform.Microsoft.gradient( startColorstr='#ffd65e', endColorstr='#febf04',GradientType=0 ); /* IE6-9 */
    position: fixed;
    top: 0;
    left :0;
    z-index: 10;
    text-align: center;
    border-bottom: 1px solid #f9e555;
    -webkit-box-shadow: 0px 0px 8px #555;
    -moz-box-shadow: 0px 0px 8px #555;
    box-shadow: 0px 0px 8px #555;
    }
.top div#quote {
    width: 20em;
    background: rgb(254,252,234); /* Old browsers */
    background: -moz-linear-gradient(top,  rgba(254,252,234,1) 0%, rgba(241,218,54,1) 100%); /* FF3.6+ */
    background: -webkit-gradient(linear, left top, left bottom, color-stop(0%,rgba(254,252,234,1)), color-stop(100%,rgba(241,218,54,1))); /* Chrome,Safari4+ */
    background: -webkit-linear-gradient(top,  rgba(254,252,234,1) 0%,rgba(241,218,54,1) 100%); /* Chrome10+,Safari5.1+ */
    background: -o-linear-gradient(top,  rgba(254,252,234,1) 0%,rgba(241,218,54,1) 100%); /* Opera 11.10+ */
    background: -ms-linear-gradient(top,  rgba(254,252,234,1) 0%,rgba(241,218,54,1) 100%); /* IE10+ */
    background: linear-gradient(top,  rgba(254,252,234,1) 0%,rgba(241,218,54,1) 100%); /* W3C */
    filter: progid:DXImageTransform.Microsoft.gradient( startColorstr='#fefcea', endColorstr='#f1da36',GradientType=0 ); /* IE6-9 */
    margin: 0 auto;
    }
.top div#quote p {
    color: #f2572a;
    height: 3.5em;
    font-size: 1.5em;
    padding: 0;
    margin: 0;        
    }
.top div#quote a {
    font-size: 1.1em;
    }
.top div#quote p:hover {
    color: #ed3419;
    }    
.arrow {
    display: inline-block;
    margin: 0 auto;
    border: 1px solid #000;
    position: relative;
    }

这里有两个问题:第一个是“Request a quote div”大于css中定义的3.2em,第二个是如果我删除了text-aling: center in .top style引用 div 下方的图像不会保持居中。我试图相对定位 .arrow div 并定位绝对 img 图标,但它不起作用,因为 div 完全消失了。还有其他想法吗? ​

【问题讨论】:

    标签: css


    【解决方案1】:

    由于.arrow 元素的默认宽度为100%,因此设置margin: 0 auto 对水平方向没有影响。而且由于img 是一个内联元素,它也不会起作用。您需要在.arrow 元素上设置显式宽度,或者在img 元素上设置display: blockmargin: 0 auto

    【讨论】:

    • 这行得通,谢谢。我试过margin: 0 auto;在带有 display: inline 或 inline-block 在箭头元素上的图像元素上,但它不起作用。我没想过将 display:block 放在 img 元素上。
    【解决方案2】:

    “请求报价” div 更大,因为它的高度与字体大小有关。 em 基于字母M 的当前宽度(至少在classic typography 中)。由于您更改了元素中的font-size .top 中的3.2em.top div#quote p 中的3.2em 不同。

    即使您不更改font-size,这些值仍然不同(.top 中的3.2em.top div#quote p 中的3.5em)。

    您应该删除所有padding-tops、padding-bottoms、margin-tops 和margin-bottoms,而只需使用height:100%

    HTML

    <div class="wrapper">
        <header>
            <section class="top">
                <p class="quote"><a href="contact.html">Request a quote</a></p>
                <img class="arrow" src="icons/top-icon.png" alt="Arrow"></div>
            </section>
        </header>
    </div>
    

    CSS

    a {
        color: #fff;
        text-decoration: none;
    }
    
    .top {
        position: fixed;
        top: 0;
        left: 0;
        right: 0;
    
        z-index: 10;
    
        height: 3.2em;
    
        background: rgb(255,214,94); /* Old browsers */
        background: linear-gradient(top,  rgba(255,214,94,1) 0%,rgba(254,191,4,1) 100%); /* W3C */    
    
        text-align: center;
    
        border-bottom: 1px solid #f9e555;
        box-shadow: 0px 0px 8px #555;
    }
    
    .top p.quote {
        width: 20em;
        height:100%;  
    
        padding: 0;
        margin: 0 auto;
    
        color: #f2572a;    
        font-size: 1.6em;
        line-height:2.1em;
    
        background: rgb(254,252,234); /* Old browsers */
        background: linear-gradient(top,  rgba(254,252,234,1) 0%,rgba(241,218,54,1) 100%); /* W3C */
    }
    
    .top p.quote a{
        color: #f2572a;
    }
    
    .top p.quote a:hover{
        color: #ed3419;
    }    
    .arrow {
        display: inline-block;
        margin: 0 auto;
        border: 1px solid #000;
        position: relative;
    }
    
    /* gradient and other vendor specific quirks */
    .top{
        /* background rules */
        background: -moz-linear-gradient(top,  rgba(255,214,94,1) 0%, rgba(254,191,4,1) 100%); /* FF3.6+ */
        background: -webkit-gradient(linear, left top, left bottom, color-stop(0%,rgba(255,214,94,1)), color-stop(100%,rgba(254,191,4,1))); /* Chrome,Safari4+ */
        background: -webkit-linear-gradient(top,  rgba(255,214,94,1) 0%,rgba(254,191,4,1) 100%); /* Chrome10+,Safari5.1+ */
        background: -o-linear-gradient(top,  rgba(255,214,94,1) 0%,rgba(254,191,4,1) 100%); /* Opera 11.10+ */
        background: -ms-linear-gradient(top,  rgba(255,214,94,1) 0%,rgba(254,191,4,1) 100%); /* IE10+ */
        filter: progid:DXImageTransform.Microsoft.gradient( startColorstr='#ffd65e', endColorstr='#febf04',GradientType=0 ); /* IE6-9 */
    
        /* vendor specific box shadows */    
        -webkit-box-shadow: 0px 0px 8px #555;
        -moz-box-shadow: 0px 0px 8px #555;
    }
    
    .top p.quote{    
        background: -moz-linear-gradient(top,  rgba(254,252,234,1) 0%, rgba(241,218,54,1) 100%); /* FF3.6+ */
        background: -webkit-gradient(linear, left top, left bottom, color-stop(0%,rgba(254,252,234,1)), color-stop(100%,rgba(241,218,54,1))); /* Chrome,Safari4+ */
        background: -webkit-linear-gradient(top,  rgba(254,252,234,1) 0%,rgba(241,218,54,1) 100%); /* Chrome10+,Safari5.1+ */
        background: -o-linear-gradient(top,  rgba(254,252,234,1) 0%,rgba(241,218,54,1) 100%); /* Opera 11.10+ */
        background: -ms-linear-gradient(top,  rgba(254,252,234,1) 0%,rgba(241,218,54,1) 100%); /* IE10+ */    
        filter: progid:DXImageTransform.Microsoft.gradient( startColorstr='#fefcea', endColorstr='#f1da36',GradientType=0 ); /* IE6-9 */
    }
    

    JSFiddle Demo

    【讨论】:

    • 我明白了,我怎么能去定义一个更大的字体大小,并且仍然通过在 em 中定义高度来保持两个元素相等?
    • 例如,您可以使用适当的标记。 &lt;p&gt;aragraph 是一个块。它应该包含&lt;a&gt;nchor,而不是相反。它不需要父母&lt;div&gt;。你可以设置它的class="quote"。尽量保持 HTML 尽可能简单,然后height:100% 会创造奇迹。
    • 你是对的,这样更合乎逻辑。但是问题仍然在于此解决方案中的行高,我无法将行高定义为与 .top 相同,这对于垂直居中元素很重要。
    • @viktor:要实现完美的垂直对齐,子&lt;p&gt;line-heightfont-size 的乘积必须与父&lt;div&gt;height 相同,例如3.5 ≈ 2.1 * 1.6(不包括填充/边距)。
    猜你喜欢
    • 2016-11-08
    • 1970-01-01
    • 2016-12-25
    • 1970-01-01
    • 1970-01-01
    • 2012-01-03
    • 2015-05-29
    • 2011-05-18
    • 2013-10-31
    相关资源
    最近更新 更多