css实现不定高度的元素垂直居中问题

<!DOCTYPE html>
<html>
<head lang="en">
    <meta charset="UTF-8">
    <title></title>
    <style>
        *{
            margin:0;
            padding:0;
        }
        .cc{
            display: table;
            width:300px;
            height:300px;
            margin:30px auto;
            text-align:center;
            background:#00c1de;
        }
        p{
            display:table-cell;
            background:green;
            vertical-align:middle;
        }
    </style>
</head>
<body>
<div class="cc">
    <p>1111</p>
</div>
</body>
</html>

给父元素设置display:table     子元素设置display:table-cell;vertical-align:middle

如果需要需要让不定高的图片垂直居中,则需要给父元素设置

  1. display: flex;
  2. justify-content: center;
  3. align-items: center;
posted @ 2017-04-19 18:22  dongxiaolei  阅读(1074)  评论(0编辑  收藏  举报