如何在一个div中使其子div居中

网上其他地方已讲述过对其的不同实现方式,今天主要做一个详细的汇总,希望对大家有帮助。
ps:我面试的时候就被问到过这个问题,当时都回答错了,蓝瘦。

假设父div的类名为father,子div的类名为son。在html中的形式如下:
<div class="father">
       <div class="son">
  </div>
接下来用css设置son居中的方法主要有4种。
  1. 方法一(使用绝对布局):
    .father{
    width:500px;
    height:500px;
    position:relative;
    background-color:red;
    }
    .son{
    width:200px;
    height:200px;
    position:absolute;
    top:50%;
    left:50%;
    margin-top:-100px;
    margin-left:-100px;
    background-color:black;
    }
    效果图如下:
    上面son类的margin-top是取自身高一半值的负数,margin-left同理

  2. 方法二(使用table-cell形式)
    .father{
    width:500px;
    height:500px;
    display:table-cell;
    text-align:center;
    vertical-align:middle;
    background-color:red;
    }
    .son{
    width:200px;
    height:200px;
    display:inline-block; ps:这句话一定要加,不然没效果哦
    background-color:black;
    }
    效果如上
    3.方法三(使用弹性布局flex)
    .father{
    width:500px;
    height:500px;
    display:flex;
    justify-content:center; 内容水平居中
    align-items:center; 内容垂直居中
    background-color:red;
    }
    .son{
    width:200px;
    height:200px;
    background-color:black;
    }
    效果如上
    4.方法四(使用绝对布局)
    .father{
    width:500px;
    height:500px;
    display:relative;
    background-color:red;
    }
    .son{
    width:200px;
    height:200px;
    position:absolute;
    top:0;
    right:0;
    bottom:0;
    left:0;
    margin:auto;
    background-color:black;
    }
    效果如上

这是目前我所了解的4种方法,ie和chrome都兼容,其他浏览器没测,目测是都兼容的。欢迎大家查漏补缺!

  • 15
    点赞
  • 80
    收藏
    觉得还不错? 一键收藏
  • 19
    评论
有几种方法可以将一个div居中放置在另一个div中。 方法一:使用display:flex属性。在父div中设置display:flex,并使用justify-content:center和align-items:center属性来实现水平和垂直居中。例如: .parent { width: 300px; height: 200px; border: 1px solid red; display: flex; justify-content: center; /*水平居中*/ align-items: center; /*垂直居中*/ } .child { width: 100px; height: 100px; border: 1px solid violet; } 方法二:使用定位的方法。在父div中设置position:relative,在div中设置position:absolute,并使用top:50%和left:50%来将div的左上角定位到父div的中心点,然后使用负margin来将div居中。例如: .parent { width: 400px; height: 300px; border: 2px solid blue; position: relative; } .child { width: 150px; height: 100px; border: 1px solid pink; position: absolute; top: 50%; left: 50%; margin-top: -50px; /*这里是div高的一半*/ margin-left: -75px; /*这里是div宽的一半*/ } 方法三:通过设置元素绝对定位和margin:auto。在父div中设置position:relative,在div中设置position:absolute,并将top、left、right和bottom都设置为0,然后将margin设置为auto。例如: .parent { position:relative; width:800px; height:500px; border:2px solid #000; } .child { position: absolute; width:200px; height:200px; margin: auto; top: 0; left: 0; right: 0; bottom: 0; background-color: red; } 以上是三种常用的方法,可以根据实际需求选择适合的方法来实现将div居中放置在另一个div中。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* [前端开发常用知识点一:如何设置div在父div中垂直居中显示?](https://blog.csdn.net/weixin_42396597/article/details/85242212)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *3* [一个div在另一个div里垂直居中显示](https://blog.csdn.net/zyw0101/article/details/119250203)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 19
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值