码迷,mamicode.com
首页 > 其他好文 > 详细

div在父元素内上下左右居中

时间:2019-06-25 09:52:47      阅读:75      评论:0      收藏:0      [点我收藏+]

标签:idt   form   height   for   nbsp   color   justify   flex   ali   

(1)宽度和高度已知的

  

     .box {
            width: 400px;
            height: 200px;
            position: relative;
            background: red;
        }
        .content {
            width: 200px;
            height: 100px;
            position: absolute;
            top: 50%;
            left: 50%;
            margin-left: -100px;
            margin-top: -50px;
            background: green;
        }
    </style>

    <div class="box">
        <div class="content"></div>
    </div>

(2)宽度和高度未知

  

     <style>
        .box {
            width: 400px;
            height: 200px;
            position: relative;
            background: red;
        }
        .content {
            position: absolute;
            top: 50%;
            left: 50%;
            transform: translate(-50%, -50%);
            background: green;
        }
    </style>

    <div class="box">
        <div class="content"></div>
    </div>

(3)flex布局

  

    <style>
        .box {
            width: 400px;
            height: 200px;
            background: red;
            display: flex;
            justify-content: center;
            align-items: center;
        }
        .content {
            width: 200px;
            height: 100px;
            background: green;
        }
    </style>
    <div class="box">
        <div class="content"></div>
    </div>

 

div在父元素内上下左右居中

标签:idt   form   height   for   nbsp   color   justify   flex   ali   

原文地址:https://www.cnblogs.com/hongchenzimo/p/11080770.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!