0

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

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

4

2 回答 2

8

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

于 2012-06-15T22:21:53.647 回答
2

“请求报价” div 更大,因为它的高度是相对于它的字体大小的。em基于字母的当前宽度M(至少在经典排版中)。由于您更改font-size元素3.2emin.top3.2emin不同.top div#quote p

即使您不更改font-size值仍然不同(3.2emin .top3.5emin .top div#quote p)。

您应该摆脱所有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 演示

于 2012-06-15T22:24:58.733 回答