文章阅读

沉浸阅读完整内容

写文章

CSS中DIV水平垂直居中方法

CSS中DIV水平垂直居中方法

正在统计阅读人数 正在统计字数
登录收藏0
字号 100%

效果图
CSS中DIV水平垂直居中方法

第一种

HTML

    <div class="box1">
            <div class="box2">
            </div>
        </div>

CSS

.box1{
        width: 100px;
        height: 100px;
        background-color: #ff5500;
        /* 相对定位 */
        position:relative;
    }
    .box2{
        width: 50px;
        height: 50px;
        background-color: #ffaaff;
        /* 绝对定位 */
        position: absolute;
        top: 50%;
        left: 50%;
        margin-top: -25px;
        margin-left: -25px;
    }

第二种

CSS

    .box1{
        width: 100px;
        height: 100px;
        background-color: #ff5500;
        /* 相对定位 */
        position:relative;
    }
    .box2{
        width: 50px;
        height: 50px;
        background-color: #ffaaff;
        /* 绝对定位 */
        position: absolute;
        top:0;
        left:0;
        right:0;
        bottom:0;
        margin: auto;
    }

第三种

CSS

    .box1{
                width: 100px;
                height: 100px;
                background-color: #ff5500;
                /* 相对定位 */
                position:relative;
                margin: 0 auto;
            }
            .box2{
                width: 50px;
                height: 50px;
                background-color: #ffaaff;
                /* 绝对定位 */
                position: absolute;
                top:50%;
                left:50%;
                transform: translate(-50%,-50%);
            }

第四种

css

.box1{
                width: 100px;
                height: 100px;
                background-color: #ff5500;
                display: flex;
                justify-content: center;
                align-items: center;
            }
            .box2{
                width: 50px;
                height: 50px;
                background-color: #ffaaff;
            }

推荐阅读

4 篇精选
文章 / 165Vue自定义指令进入页面光标自动聚焦到input框上 文章 / 162React遍历对象包数组key是标题 文章 / 163slot的理解及用法 文章 / 164Vue.observable让一个对象变成响应式数据

发表评论

评论 0

还没有评论,欢迎留下第一条讨论。

分享精彩,传递价值

生成专属海报,分享你的文章,让更多人发现好内容

浏览器不支持 Canvas,无法生成海报。

每日签到

每天来这里,记录一点持续积累。

当前积分--积分
本月签到--
本月获得--积分
正在读取