【发布时间】:2013-09-10 10:13:13
【问题描述】:
我正在为网站创建横幅。我固执地尝试用 HTML5/CSS3 来做,而不是制作图像。横幅有一个不透明的背景图像,它跨越了页面包装的整个宽度(设置为 80% 的屏幕宽度)。除此之外,我有三个元素:一个徽标和两行文本。我已将三个元素放在我希望它们绝对化的位置,并使用顶部、左侧等。我还将这三个元素放在它们自己的 div 中。我想将它们放在背景图像的中心。我试过在两边设置自动边距(css3中的首选方式,但它不起作用。
实际上,不仅能够将三个组(徽标,第 1 行,第 2 行)居中,而且能够将它们作为绝对元素一起移动,这也很棒。这可能吗?
http://jsfiddle.net/abalter/965Mj/1/
* HTML *
<div id="wrapper">
<header>
<div id="background-image"></div>
<div id="header-group">
<img id="house-logo" src="http://arielbalter.com/cio/img/CheckItOutHouseNoBackground.png " />
<h1 id="line1">check it out!</h1>
<h1 id="line2">Home Inspection</h1>
</div>
</header>
</div>
* CSS *
#wrapper {
background-color: Wheat;
width: 1280px;
/* doing that because jsfiddle viewport is not the entire page width.*/
/* in real life, the wrapper extends below the banner -- that is where the main part of the website is */
}
header {
position: relative;
width: 100%;
height: 180px;
background-color: White;
}
header #background-image {
background-image: url("http://arielbalter.com/cio/img/3tab_slanted.jpg");
position: absolute;
left: 0;
top: 0;
height: 100%;
width: 100%;
background-repeat: no-repeat;
background-size: 100% 100%;
opacity: 0.3;
}
#header-group {
margin: 0 auto 0 auto;
}
#house-logo {
position: absolute;
float: left;
height: 160px;
top:10px;
left: 10px;
}
#line1 {
display: block;
position: absolute;
left: 220px;
top: -15px;
padding: 0;
margin: 0;
font-family:"KGLoveSomebody", "Comic Sans MS", sans-serif;
font-size: 7em;
color: FireBrick;
}
#line2 {
display: block;
position: absolute;
left: 185px;
bottom: 1px;
margin: 0;
padding: 0;
font-family:"Trebuchet", sans-serif;
font-size: 4em;
}
【问题讨论】:
标签: html css centering absolute