垂直水平居中

Author Avatar
Ninefire 6月 07, 2018
  • 在其它设备中阅读本文章

已知元素高度和宽度的情况

方法一:

<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title>已知高宽居中</title>
        <style type="text/css">
            * {
                margin: 0;
                padding: 0;
            }
            .wrap {
                position: relative;
                width: 800px;
                height: 600px;
                margin: auto;
                background: #bfa;
            }
            .wrap .inner {
                position: absolute;
                top: 50%;
                left: 50%;
                margin-top: -100px;
                margin-left: -100px;
                width: 200px;
                height: 200px;
                background: orange;
            }
        </style>
    </head>
    <body>
        <div class="wrap">
            <div class="inner">inner</div>
        </div>
    </body>
</html>

戳我查看效果

方法二:

<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title>已知高宽居中</title>
        <style type="text/css">
            * {
                margin: 0;
                padding: 0;
            }
            .wrap {
                position: relative;
                width: 800px;
                height: 600px;
                margin: auto;
                background: #bfa;
            }
            .wrap .inner {
                position: absolute;
                top: 0;
                right: 0;
                bottom: 0;
                left: 0;
                margin: auto;
                width: 200px;
                height: 200px;
                background: orange;
                text-align: center;
                line-height: 200px;
            }
        </style>
    </head>
    <body>
        <div class="wrap">
            <div class="inner">inner</div>
        </div>
    </body>
</html>

戳我查看效果

未知元素高度和宽度的情况

<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title>未知高宽居中</title>
        <style type="text/css">
            * {
                margin: 0;
                padding: 0;
            }
            .wrap {
                position: relative;
                width: 800px;
                height: 600px;
                margin: auto;
                background: #bfa;
            }
            .wrap .inner {
                position: absolute;
                top: 50%;
                left: 50%;
                transform: translate(-50%,-50%);
                background: orange;
            }
        </style>
    </head>
    <body>
        <div class="wrap">
            <div class="inner">
                inner inner inner inner <br />
                inner inner inner inner <br />
                inner inner inner inner <br />
                inner inner inner inner <br />
                inner inner inner inner <br />
                inner inner inner inner <br />
            </div>
        </div>
    </body>
</html>

戳我查看效果

如发现错误请联系我,谢谢你。
本文链接:http://ninefire.tk/CSS2.1/07.html