div中包含的图像不以margin为中心:auto

时间:2012-06-15 22:17:54

标签: css

我有以下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;
    }

这里有两个问题:第一个问题是&#34;请求报价div&#34;大于css中定义的3.2em,第二个是如果我删除文本结构:中心.top样式,引号div下面的图像将不会保持居中。我试图将相对的.arrow div和绝对位置定位为img图标,但是当div完全消失时它不起作用。还有其他想法吗?

2 个答案:

答案 0 :(得分:7)

由于.arrow元素的默认宽度为100%,因此设置margin: 0 auto水平无效。由于img是一个内联元素,因此它也不会起作用。您需要在.arrow元素上设置显式宽度,或在display: block元素上设置margin: 0 autoimg

答案 1 :(得分:2)

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

即使您不更改font-size值仍然不同(3.2em中的.top3.5em中的.top div#quote p

您应该删除所有padding-toppadding-bottommargin-topmargin-bottom,而只需使用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