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

复制代码
<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
        <title>常见元素布局</title>
        <style type="text/css">
            /* 一、垂直居中布局 */
            /* 1.单个元素垂直居中 高度固定 缺点:用到定位,脱离文档流*/
            .content {
                position: relative;
                height: 100px;
                background: #008000;/* background和height测试更好的观看效果 */
            }

            .box {
                height: 50px;
                position: absolute;
                top: 0;
                bottom: 0;
                margin: auto 0;
                background: #ff9933;
                color: #fff; /* background和color测试更好的观看效果 */
                line-height: 50px; /* 文字垂直居中 */
            }
        </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 {
                height: 100px;
                line-height: 100px;
                background: #008000;/* background测试更好的观看效果 可忽略*/
            }

            .box {
                display: inline-block;
                height: 50px;
                vertical-align: middle;
                background: #ff9933;
                color: #fff;/* background和color测试更好的观看效果 */
                line-height: 50px;/* 文字垂直居中 */
            }
        </style>
    </head>
    <body>
        <div class="content">
            <div class="box">
                高度固定
            </div>
        </div>
    </body>
</html>
复制代码

效果:

 

posted @   鱼樱前端  阅读(358)  评论(0编辑  收藏  举报
努力加载评论中...
 
往后余生 - 王贰浪
00:00 / 00:00
An audio error has occurred, player will skip forward in 2 seconds.
  1. 1 往后余生 王贰浪
  2. 2 拂雪 不才
  3. 3 我的一个道姑朋友 洛尘鞅
  4. 4 大田后生仔 王雨萌
点击右上角即可分享
微信分享提示