垂直对齐中间文本没有lineheight和table-cell

时间:2015-06-30 11:26:24

标签: css vertical-alignment

我有一个预告片"切换" -Animation,可以看到on JSFiddle或以下:



.ax {
    height:60px;
    width:150px;
    background:gold;
}
 .caption {
position: absolute;
    left:0;
  top: 35%;
  overflow: hidden;
  right: 0;
  background-color: rgba(24,88,140,0.7);
  width: 100%;
  height: 52px;
  z-index: 2;
  -o-transition: 500ms;
  -webkit-transition: 500ms;
  -moz-transition: 500ms;
  -ms-transition: 500ms;
  transition: 500ms;
  font-weight: lighter;
  padding: 10px;
  color: #fff;
}

a.link{
  color: #fff;
  overflow: hidden;
  width: 80%;
  margin-bottom: 30px;
  font-weight: lighter;
  font-size: 16px;
  line-height: 22px;
}

 .caption:hover {
  height: 100%;
  top: 0;
}
.box {
    position:relative;    
    width:250px;
    height:200px;
}

/*TABLE CELL METHOD*/

 .caption2 {
     position: absolute;
    left:0;
  top: 35%;
  overflow: hidden;
  right: 0;
  background-color: rgba(24,88,140,0.7);
  width: 100%;
  height: 52px;
  z-index: 2;
  -o-transition: 500ms;
  -webkit-transition: 500ms;
  -moz-transition: 500ms;
  -ms-transition: 500ms;
  transition: 500ms;
  font-weight: lighter;
  padding: 10px;
  color: #fff;
    display:table;
   
}
 .caption2:hover {
  height: 100%;
  top: 0;
}
a.link2{
    display:table-cell;
   vertical-align: middle;

  overflow: hidden;
  width: 80%;
  margin-bottom: 30px;
  font-weight: lighter;
  font-size: 16px;
  line-height: 22px;
}

<div class="box">
<div class="caption">
<a href="#" class="link">Lorem Ipsum blabla bla blahah ipsum lorem blablablahh</a>
<p class="captiontext">Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus.</p> 
</div>
</div><br><br

<br><br>
    table cell method (div.caption2 display:table and a.link display:table-cell + vertical-algin:middle)
    <br><br>
        
<div class="box">
<div class="caption2">
<a class="link2" href="#">Lorem Ipsum blabla bla blahah ipsum lorem blablablahh</a>
<p class="captiontext">Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus.</p> 
</div>
</div>
&#13;
&#13;
&#13;

我想将链接对齐蓝框的垂直中心。链接可以是单行或两行(最多),但应始终垂直居中。 CSS属性lineheight不适用于两个线路链接,而表格(-cell) - 方法也不起作用(见上文)。

有没有办法将我的盒子中的一个和两个线路链接对中?

3 个答案:

答案 0 :(得分:19)

显示类型“Flexbox”可能对此有用。将此CSS应用于您想要居中的孩子的父级:

display: flex;
flex-direction: column;
justify-content: center;

答案 1 :(得分:13)

要将任何元素垂直居中,您可以应用此样式:

.element
{
    position: relative;
    top: 50%;
    transform: translateY(-50%);
}

您必须使用列替换表格单元格才能复制当前样式。感谢Sebastian Ekstrom获得解决方案。

答案 2 :(得分:0)

另一种方法是在伪元素上进行垂直对齐:

https://codepen.io/darxide/pen/xRmYdQ

<div class="block" style="height: 300px;">
    <div class="centered">
        vertical center
    </div>
</div>


.block:before {
  content: ' ';
  display: inline-block;
  height: 100%; 
  vertical-align: middle;
}

.centered {
  display: inline-block;
  vertical-align: middle;
}