网上其他地方已讲述过对其的不同实现方式,今天主要做一个详细的汇总,希望对大家有帮助。
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;

    }

    效果图如下:

  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都兼容,其他浏览器没测,目测是都兼容的。欢迎大家查漏补缺!

如何在一个div中使其子div居中的更多相关文章

  1. js在一个div里面移动其子div

    var ChildDiv = $("#cid"); var width = 0; //鼠标点击子div的地方和子div的左边边距距离 var height = 0; //鼠标点击子 ...

  2. 为什么当多个inline-block的div中,如果有的div没有内容而有的div有内容,有内容的会下沉?

    为什么当多个inline-block的div中,如果有的div没有内容而有的div有内容,有内容的会下沉? 就像这样 两个div高度相同,第二个我写了一个1当作 有内容吧,它就下沉了... 奇怪... ...

  3. div中宽高度自适应文字换行居中问题解决

    <html> <head> <meta charset="UTF-8"/> <title>div中宽高度自适应文字换行居中demo& ...

  4. js控制div滚动条,滚动滚动条使div中的元素可见并居中

    1.html代码如下 <div id="panel"> <div id="div1"></div> <div id=& ...

  5. ThinkPHP框架下,jq实现在div中添加标签并且div的大小会随之变化

    php初学者,有什么不对的还请指正. 首先是在html页面中用jq实现添加标签:divAchivePersonnal是select所在的div的外层div,divselectAchivePersonn ...

  6. div中img依据不同分辨率居中显示,超出部分隐藏

    在做banner居中时 碰到的问题,知道可以用背景图实现居中显示,但是内心是想深究下的,故找到几种办法收集一下,后面两种真的是奇技淫巧 来着下面两处 https://www.zhihu.com/que ...

  7. 父div高度不能根据子div高度自动变化的解决方案

    <div id="parent"> <div id="content"> </div> </div> 当cont ...

  8. 小div在大div中垂直居中,以及div在页面垂直居中

    <html> <head> <title>淘宝 2faner</title> <style type="text/css"&g ...

  9. css 实现 左右div 等高, 同时父级div就是最高的子div的高度

    原文地址:https://www.cnblogs.com/cbza/p/7145384.html 方法一: 通过父级overflow:hidden,  自己设置padding-bottom 和 mar ...

随机推荐

  1. HDU 3864 D_num Miller Rabin 质数推断+Pollard Rho大整数分解

    链接:http://acm.hdu.edu.cn/showproblem.php? pid=3864 题意:给出一个数N(1<=N<10^18).假设N仅仅有四个约数.就输出除1外的三个约 ...

  2. [appium]-9宫格解锁方法

    from appium.webdriver.common.touch_action import TouchAction TouchAction(self.driver).press(x=228,y= ...

  3. maven的pom.xml配置文件讲解

    <project xmlns="http://maven.apache.org/POM/4.0.0 "     xmlns:xsi="http://www.w3.o ...

  4. HDU——T 3501 Calculation 2

    http://acm.hdu.edu.cn/showproblem.php?pid=3501 Time Limit: 2000/1000 MS (Java/Others)    Memory Limi ...

  5. [Angular] Protect The Session Id with https and http only

    For the whole signup process. we need to Hash the password to create a password digest Store the use ...

  6. 配置PL/SQL Developer连接server数据库

    配置PL/SQL Developer连接server数据库 远程应用server上安装client客户端软件,可在oracle官网上下载. 举例: 环境 应用server操作系统 WIN 7 本地地址 ...

  7. JSP语法基础(一)

    一.JSP页面中的凝视 (1)HTML凝视 <!-- comment [ <%=expression %> ] --> 能在client显示的一种凝视,标记内的全部JSP脚本元 ...

  8. JS实现弹性势能效果(弹力球效果[实现插件封装])

    <!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8&quo ...

  9. android蓝牙主动发起配对实例

    package cn.madfinger.core; import java.io.IOException; import java.lang.reflect.Method; import java. ...

  10. swift开发网络篇 - 用户登录POST JSON and header

    版权声明:本文为博主原创文章,未经博主允许不得转载. import UIKit import Alamofire class ViewController: UIViewController { va ...