pc端常见布局---水平居中布局 单元素定宽

<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
        <title>常见元素布局</title>
        <style type="text/css">
            /* 一、水平居中布局 */
            /* 1.单个元素水平居中 宽度固定 最常用*/
            .box {
                width: 400px;
                margin: 0 auto;
                background: #008000;
                color: #fff; /* background和color测试更好的观看效果 */
                text-align: center; /* 字体居中 */
            }
        </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">
            /* 一、水平居中布局 */
            /* 1.单个元素水平居中 宽度固定 定位居中布局*/
            .content {
                position: relative;
            }

            .box {
                width: 400px;
                position: absolute;
                left: 0;
                right: 0;
                margin: 0 auto;
                background: #ff9933;
                color: #fff; /* background和color测试更好的观看效果 */
                text-align: center; /* 字体居中 */
            }
        </style>
    </head>
    <body>
        <div class="content">
            <div class="box">
                宽度固定
            </div>
        </div>
    </body>
</html>

效果:

 

原文地址:https://www.cnblogs.com/lhl66/p/10374472.html