1. 程式人生 > >給多行文字設定垂直居中

給多行文字設定垂直居中

效果圖:

頁面程式碼:

<div class="left">
    <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Mauris sem erat,
        egestas quis luctus ut, rhoncus vitae risus. Integer venenatis libero sit amet sapien dictum,
        quis bibendum mi volutpat. Cras pretium quam quis magna facilisis, eget feugiat nisi lacinia.</
p> </div>

首先給父級元素設定display:table;

.left{
    width: 630px;
    background: #ff0000;
    height: 300px;
    float: left;
    display: table;
}

然後給子元素設定display:table-cell; vertical-align:middle;

.left p{
    display: table-cell;
    vertical-align: middle;
    text-align: center;
}