垂直和水平居中对齐文本

时间:2016-02-02 11:33:46

标签: html css

我的网站上有2个区块。

我希望在两个区域上集中垂直和水平对齐文本。现在它位于下面:(

这是我目前的代码:

https://jsfiddle.net/byp0pevr/



.text-block div {
    width: 100%;
    display: inline-block;
    vertical-align: middle;
    line-height: 1;
    position: relative;
    color: black;
    z-index: 5;
}

#adbox1 {
    width: 300px;
    height: 620px;
    position: absolute;
    top: 0;
    left: 10px;
}

#adbox2 {
    width: 300px;
    height: 300px;
    position: absolute;
    top: 0;
    left: 320px;
}

.image-holder {
    display:inline-block;
    position: relative;
}
.image-holder:after {
    content:'';
    top: 0;
    left: 0;
    z-index: 10;
    width: 100%;
    height: 100%;
    display: block;
    position: absolute;
    background: black;
    opacity: 0;
}
.image-holder:hover:after {
    opacity: 0.1;
}

<div id="adbox1" class="image-holder">
  <img src="http://placehold.it/300x620" title="1">
  <div class="text-block">
    <div>Some<br>Text</div>
  </div>
</div>

<div id="adbox2" class="image-holder">
  <img src="http://placehold.it/300x300" title="1">
  <div class="text-block">
    <div>Some<br>Text</div>
  </div>
</div>
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:2)

position: relative;提交给#adbox1并提供以下css。

.text-block {
    left: 50%;
    position: absolute;
    top: 50%;
    transform: translate(-50%, -50%);
}

<强> Working Fiddle