pc端常见布局---垂直居中布局 单元素不定高

<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
        <title>常见元素布局</title>
        <style type="text/css">
            /* 一、垂直居中布局 */
            /* 1.单个元素垂直居中 高度不固定*/
            .content {
                position: relative;
                height: 100px;
                background: #008000;/* height和background测试更好的观看效果 可忽略*/
            }

            .box {
                position: absolute;
                top: 50%;
                transform: translateY(-50%);
                background: #ff9933;
                color: #fff;/* background和color测试更好的观看效果 */
            }
        </style>
    </head>
    <body>
        <div class="content">
            <div class="box">
                高度不固定
            </div>
        </div>
    </body>
</html>
<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
        <title>常见元素布局</title>
        <style type="text/css">
            /* 一、垂直居中布局 */
            /* 2.单个元素垂直居中 高度不固定 缺点:由父类控制是否居中*/
            .content {
                display: table-cell;
                vertical-align: middle;
            }
        </style>
    </head>
    <body>
        <div class="content">
            <div class="box">
                高度不固定
            </div>
        </div>
    </body>
</html>

效果:

 

posted @ 2019-02-14 15:01  鱼樱前端  阅读(445)  评论(0编辑  收藏  举报