Css 包含在div中的图像未居中,边距:自动
我有以下html和css代码:Css 包含在div中的图像未居中,边距:自动,css,Css,我有以下html和css代码: <!DOCTYPE HTML> <html> <head> </head> <body> <div class="wrapper"> <header> <section class="top"> <div id="quote"><a href="contact.html
<!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;
}
a{
字号:1.6em;
颜色:#fff;
文字装饰:无;
}
.顶{
身高:3.2米;
宽度:100%;
背景:rgb(255214,94);/*旧浏览器*/
背景:-moz线性梯度(顶部,rgba(255214,94,1)0%,rgba(254191,4,1)100%);/*FF3.6+*/
背景:-webkit渐变(线性、左上、左下、色挡(0%,rgba(255214,94,1))、色挡(100%,rgba(254191,4,1));/*铬、Safari4+*/
背景:-webkit线性梯度(顶部,rgba(255214,94,1)0%,rgba(254191,4,1)100%);/*Chrome10+,Safari5.1+*/
背景:-o-线性梯度(顶部,rgba(255214,94,1)0%,rgba(254191,4,1)100%);/*Opera 11.10+*/
背景:-ms线性梯度(顶部,rgba(255214,94,1)0%,rgba(254191,4,1)100%);/*IE10+*/
背景:线性梯度(顶部,rgba(255214,94,1)0%,rgba(254191,4,1)100%);/*W3C*/
过滤器:progid:DXImageTransform.Microsoft.gradient(startColorstr='#ffd65e',endColorstr='#febf04',GradientType=0);/*IE6-9*/
位置:固定;
排名:0;
左:0;
z指数:10;
文本对齐:居中;
边框底部:1px实心#f9e555;
-网络工具包盒阴影:0px 0px 8px#555;
-moz盒阴影:0px 0px 8px#555;
盒影:0px 0px 8px#555;
}
.top div#quote{
宽度:20em;
背景:rgb(25425234);/*旧浏览器*/
背景:-moz线性梯度(顶部,rgba(25425234,1)0%,rgba(241218,54,1)100%);/*FF3.6+*/
背景:-webkit渐变(线性、左上、左下、色挡(0%,rgba(25425234,1))、色挡(100%,rgba(241218,54,1));/*铬、Safari4+*/
背景:-webkit线性梯度(顶部,rgba(25425234,1)0%,rgba(241218,54,1)100%);/*Chrome10+,Safari5.1+*/
背景:-o-线性梯度(顶部,rgba(25425234,1)0%,rgba(241218,54,1)100%);/*Opera 11.10+*/
背景:-ms线性梯度(顶部,rgba(25425234,1)0%,rgba(241218,54,1)100%);/*IE10+*/
背景:线性梯度(顶部,rgba(25425234,1)0%,rgba(241218,54,1)100%);/*W3C*/
过滤器:progid:DXImageTransform.Microsoft.gradient(startColorstr='#fefcea',endColorstr='#f1da36',GradientType=0);/*IE6-9*/
保证金:0自动;
}
.top div#报价p{
颜色:#f2572a;
高度:3.5公分;
字号:1.5em;
填充:0;
保证金:0;
}
.top div#引用a{
字体大小:1.1米;
}
.top div#引号p:悬停{
颜色:#ed3419;
}
.阿罗{
显示:内联块;
保证金:0自动;
边框:1px实心#000;
位置:相对位置;
}
这里有两个问题:第一个问题是“Request a quote div”大于css中定义的3.2em,第二个问题是如果我删除文本aling:center in。顶部样式化quote div下面的图像将不会保持居中。我曾尝试定位相对箭头div和绝对位置img图标,但由于div完全消失,因此无法工作。还有其他想法吗?
由于
.arrow
元素的默认宽度为100%,因此设置边距:0 auto
在水平方向上没有效果。而且由于img
是一个内联元素,因此它也不适用于该元素。您需要在.arrow
元素上设置显式宽度,或者在img
元素上设置display:block
和margin:0 auto
。请求报价”div较大,因为其高度与其字体大小有关。基于字母的当前宽度M
(至少英寸)。由于您更改了元素中的字体大小,因此中的3.2em
与中的3.2em
不同
即使不更改字体大小
,值仍然不同(.top
中的3.2em
,.top div中的3.5em
)
你应该去掉所有的padding top
s、padding bottom
s、margin top
s和margin bottom
s,只需使用height:100%
HTML
这很有效,谢谢。我尝试了边距:0自动;在具有display:inline或inline block-on-arrow元素的图像元素上,但它不起作用。但是我没有想到将display:block放在img元素上。我明白了,如何定义更大的字体大小,并通过在em中定义高度来保持这两个元素相等?例如,可以使用适当的标记。一个图形是一个块。它应该包含一个nchor
,而不是相反。它不需要父级
。您可以设置它的class=“quote”
。尽量使HTML保持简单,然后height:100%
将产生奇迹。你是对的,这样做更符合逻辑。但是,问题仍然在于此解决方案中的线高度,我不能将线条高度定义为与.top相同,这对于使元素垂直居中很重要。@viktor:为了实现完美的垂直对齐,子的线条高度和字体大小的乘积必须与父的高度相同,例如3.5≈ 2.1*1.6
(不包括填充物/边距)。
<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>
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 */
}