没有行高和 table-cell 的垂直对齐中间文本

Vertical align middle text without lineheight and table-cell

我有一个带有 "toggle" 动画的预告片,可以看到 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>

我想将 link 对齐到蓝色框的垂直中心。 link 可以是单行或两行(最多),但应始终垂直居中。 CSS 属性 lineheight 不适用于两行 links,而 table(-cell) 方法也不起作用(请参阅以上)。

有什么方法可以使我的框中的一行和两行 link 居中吗?

显示类型 "Flexbox" 可能对此有用。将此 CSS 应用于您想要居中的 child 的 parent:

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

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

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

您必须将 table 单元格替换为列才能复制当前样式。感谢 Sebastian Ekstrom 的解决方案。

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

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;
}