css3 - css垂直水平居中?
PHP中文网
PHP中文网 2017-04-17 11:06:33
[CSS3讨论组]

如何实现B在A中,垂直和水平方向都居中?
代码如下:

<!DOCTYPE html>
<html lang="zh">
<head>
    <meta http-equiv="X-UA-Compatible" content="IE=Edge">
    <meta charset="UTF-8"/>
    <title></title>
    <style>
        #A {
            position: relative;
            width: 500px;
            height: 500px;
            background-color: green;
        }

        #B {
            position: absolute;
            max-width: 300px;
            max-height: 300px;
            background-color: blue;
        }
    </style>
</head>
<body>


<p id="A">
    <p id="B">哥哥哥哥哥哥哥哥哥哥哥哥哥哥哥哥哥哥哥哥哥哥
        哥哥哥哥给哥哥哥哥哥哥哥哥哥哥哥哥哥哥哥哥哥哥哥哥哥哥哥哥哥哥哥哥哥哥哥哥哥哥哥哥给哥哥哥
        哥哥哥哥给哥哥哥哥哥哥哥哥哥哥哥哥哥哥哥哥哥哥哥哥哥哥哥哥哥哥哥哥哥哥哥哥哥哥哥哥给哥哥哥
        哥哥哥哥给哥哥哥哥哥哥哥哥哥哥哥哥哥哥哥哥哥哥哥哥哥哥哥哥哥哥哥哥哥哥哥哥哥哥哥哥给哥哥哥
        哥哥哥哥给哥哥哥哥哥哥哥哥哥哥哥哥哥哥哥哥哥哥哥哥哥哥哥哥哥哥哥哥哥哥哥哥哥哥哥哥给哥哥哥
    </p>
</p>


</body>
</html>
PHP中文网
PHP中文网

认证高级PHP讲师

全部回复(7)
阿神

两种方法:

第一种:

#A {
            position: relative;
            width: 500px;
            height: 500px;
            background-color: green;
        }

        #B {
            position: absolute;
            max-width: 300px;
            max-height: 300px;
            background-color: blue;
            /* add */
            margin:auto;
            left:0;
            right:0;
            top:0;
            bottom:0;
        }

第二种:

#A {
            position: relative;
            width: 500px;
            height: 500px;
            background-color: green;
        }

        #B {
            position: absolute;
            max-width: 300px;
            max-height: 300px;
            background-color: blue;
            /* add */
            top:50%;
            left:50%;
            transform:translate(-50%,-50%);
        }
PHP中文网
    #A {
        display:table-cell;
        vertical-align:middle;
    }

    #B {
        /* position:absolute; */
        margin:0 auto;
    }

http://jsfiddle.net/7uakz7p1/

阿神

今天刚学到的一种比较简单的方法:父容器设置display: flex; 要居中的子元素设置margin: auto;即可。
本例中

#A {
        position: relative;
        width: 500px;
        height: 500px;
        background-color: green;
        /*add*/
        display: flex;
        display: -webkit-flex;
    }
#B {
        /*position: absolute;*/
        max-width: 300px;
        max-height: 300px;
        background-color: blue;
        /*add*/
        margin: auto;
    }

ps. IE下好像有问题...

PHPz
        #A {
            position: relative;
            width: 500px;
            height: 500px;
            background-color: green;
        }

        #B {
            width: 50%;
            height: 50%;
            margin: auto;
            position: absolute;
            top: 0; left: 0; bottom: 0; right: 0;
            max-width: 300px;
            max-height: 300px;
            background-color: blue;
        }              
天蓬老师

正好发现一个网站,给了好多解决方案 Centering in CSS: A Complete Guide

高洛峰

http://jsfiddle.net/nbayw7ea/

阿神

http://css-tricks.com/centering-css-complete-guide/

热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号