2025年【CSS】实现div块上下左右居中对齐的5种方式

【CSS】实现div块上下左右居中对齐的5种方式1 绝对定位 margin auto 的方式 style parent width 200px height 200px background color rgba 255 0 0 1 position relative style

大家好,我是讯享网,很高兴认识大家。

1. 绝对定位+margin:auto的方式

<!DOCTYPE html>
<html>
    <head>
        <style>
            .parent {
                width: 200px;
                height: 200px;
                background-color: rgba(255,0,0,1);
                position: relative;
            }
            .child {
                width: 50%;
                height: 50%;
                background-color: rgba(0,255,0,1);
                position: absolute;
                top: 0;
                right: 0;
                bottom: 0;
                left: 0;
                margin: auto;
            }
        </style>
    </head>
    <body>
        <div class="parent">
            <div class="child"></div>
        </div>
    </body>
</html>
讯享网

2. 绝对定位+transform:translate(-50%, -50%)的方式

讯享网<!DOCTYPE html>
<html>
    <head>
        <style>
            .parent {
                width: 200px;
                height: 200px;
                background-color: rgba(255,0,0,1);
                position: relative;
            }
            .child {
                width: 50%;
                height: 50%;
                background-color: rgba(0,255,0,1);
                position: absolute;
                top: 50%;
                left: 50%;
                transform: translate(-50%, -50%);
            }
        </style>
    </head>
    <body>
        <div class="parent">
            <div class="child"></div>
        </div>
    </body>
</html>

3. 绝对定位+设置左和上的外边距(需要知道子块宽高具体值)的方式

<!DOCTYPE html>
<html>
    <head>
        <style>
            .parent {
                width: 200px;
                height: 200px;
                background-color: rgba(255,0,0,1);
                position: relative;
            }
            .child {
                width: 100px;
                height: 100px;
                background-color: rgba(0,255,0,1);
                position: absolute;
                top: 50%;
                left: 50%;
                margin-top: -50px;
                margin-left: -50px;
            }
        </style>
    </head>
    <body>
        <div class="parent">
            <div class="child"></div>
        </div>
    </body>
</html>

4. 父块display: table-cell + vertical-align + 子块margin的方式

讯享网<!DOCTYPE html>
<html>
    <head>
        <style>
            .parent {
                width: 200px;
                height: 200px;
                background-color: rgba(255,0,0,1);
                display: table-cell;
                vertical-align: middle;
            }
            .child {
                width: 50%;
                height: 50%;
                background-color: rgba(0,255,0,1);
                margin: 0 auto;
            }
        </style>
    </head>
    <body>
        <div class="parent">
            <div class="child"></div>
        </div>
    </body>
</html>

5. flex布局方式

<!DOCTYPE html>
<html>
    <head>
        <style>
            .parent {
                width: 200px;
                height: 200px;
                background-color: rgba(255,0,0,1);
                display: flex;
                justify-content: center;
                align-items: center;
            }
            .child {
                width: 50%;
                height: 50%;
                background-color: rgba(0,255,0,1);
            }
        </style>
    </head>
    <body>
        <div class="parent">
            <div class="child"></div>
        </div>
    </body>
</html>

以上2,4,5方式,注意浏览器版本,较老版本浏览器可能不兼容。

注:以上,如有不合理之处,还请帮忙指出,大家一起交流学习~

小讯
上一篇 2025-02-21 10:33
下一篇 2025-02-24 10:13

相关推荐

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容,请联系我们,一经查实,本站将立刻删除。
如需转载请保留出处:https://51itzy.com/kjqy/131794.html