han*_*nah 4 html css image absolute
我正在尝试将文本放在页面上的图像中间.我想在页面上具有不同高度和宽度的其他图像上使用它.我有相对定位的图像和文本绝对定位,但底部:0似乎没有工作:
<div class="image">
<img src="http://lorempixel.com/300/300/">
<header class="text">
<h1>header</h1>
<a href="#">button</a>
</header>
</div>
Run Code Online (Sandbox Code Playgroud)
这是CSS:
.image {
display: inline-block;
position: relative;
}
.text {
position: absolute;
top: 0;
left: 0;
color: white;
text-align: center;
bottom: 0;
right: 0;
}
.text h1 {
text-transform: uppercase;
font-family: Helvetica, Arial;
font-size: 16px;
letter-spacing: .5px;
}
.text a {
font-size: 14px;
border: 1px solid white;
padding: 5px 30px;
color: white;
text-decoration: none;
}
Run Code Online (Sandbox Code Playgroud)
和小提琴的链接:http://jsfiddle.net/yg4Ar/
你有 :
position: absolute;
top: 0; // This keeps text top.
left: 0;
color: white;
text-align: center;
bottom: 0;
right: 0;
Run Code Online (Sandbox Code Playgroud)
所以这使你的文本达到100%的高度..因为那top:0
......所以只需删除它top:0
.
如果你想垂直居中你的文字,你必须为你的文字指定/计算一些高度,并将其中一半作为减去边距添加到你的CSS并将顶部值放到50%,如:
.text {
position: absolute;
top: 50%;
left: 0;
margin-top: -30px; // This should be half of your text div:s height
color: white;
text-align: center;
bottom: 0;
right: 0;
}
Run Code Online (Sandbox Code Playgroud)
Example:
http://jsfiddle.net/9zyx2/
小智 5
尝试将顶部设置为百分比:
.text {
position: absolute;
top: 25%;
left: 0;
color: white;
text-align: center;
bottom: 0;
right: 0;
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
7750 次 |
最近记录: |