以下方法来自百度知道:https://zhidao.baidu.com/question/558984366971173044.html

方法1:

.parent {
width: 800px;
height: 500px;
border: 2px solid #000;
position: relative;
} .child {
width: 200px;
height: 200px;
margin: auto;
position: absolute;
top:;
left:;
bottom:;
right:;
background-color: red;
}

方法2:

.parent {
width: 800px;
height: 500px;
border: 2px solid #000;
display: table-cell;
vertical-align: middle;
text-align: center;
} .child {
width: 200px;
height: 200px;
display: inline-block;
background-color: red;
}

方法3:

.parent {
width: 800px;
height: 500px;
border: 2px solid #000;
display: flex;
justify-content: center;
align-items: center;
} .child {
width: 200px;
height: 200px;
background-color: red;
}

方法4:

.parent {
width: 800px;
height: 500px;
border: 2px solid #000;
position: relative;
} .child {
width: 300px;
height: 200px;
margin: auto;
position: absolute; /*设定水平和垂直偏移父元素的50%,再根据实际长度将子元素上左挪回一半大小*/
left: 50%;
top: 50%;
margin-left: -150px;
margin-top: -100px;
background-color: red;
}

如何让一个div水平和垂直居中对齐的更多相关文章

  1. 关于页面布局中,如何让一个div水平和垂直居中的五个方案

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

  2. 让DIV水平和垂直居中的几种方法

    我们在设计页面的时候,经常要把DIV居中显示,而且是相对页面窗口水平和垂直方向居中显示,如让登录窗口居中显示.我们传统解决的办法是用纯CSS来让DIV居中.在本文中,我将给大家讲述如何用CSS和jQu ...

  3. 【转】如何让DIV水平和垂直居中

    来源:http://blog.163.com/www.wxs_123/blog/static/82784664201321831746921/ 我们在设计页面的时候,经常要把DIV居中显示,而且是相对 ...

  4. DIV水平和垂直居中的实现

    在div的宽度和高度固定的情况下,实现div水平和垂直居中普遍采用如下的方式: <!DOCTYPE html> <html> <head> <style ty ...

  5. CSS:使用CSS3将一个div水平和垂直居中显示

    使用css3将一个div水平和垂直居中显示 方案一: div绝对定位水平垂直居中[margin:auto实现绝对定位元素的居中], 代码两个关键点:1.上下左右均0位置定位: 2.margin: au ...

  6. div中字垂直居中对齐

    div中的文本水平居中,一般都是用text-align:center;就可以解决,那么垂直居中呢,知道vertiacl-align:middle;但有时候却不起作用:整理下div中文本垂直居中对齐的问 ...

  7. 使当前对象相对于上层DIV 水平、垂直居中定位

    <!doctype html> <html> <head> <meta http-equiv="content-type" content ...

  8. CSS3 div水平、垂直居中,IE9以上、Firefox、Chrome均正常

    <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content ...

  9. 如何让一个DIV水平,垂直方向都居中于浏览器?

    <style type="text/css"><!-- div {position:absolute;top:50%;left:50%;margin:-150px ...

随机推荐

  1. python2.7使用requests时报错SSLError: HTTPSConnectionPool(host='b-ssl.duitang.com', port=443)

    import requests url='https://www.duitang.com/napi/blog/list/by_search/?kw=%E6%A0%A1%E8%8A%B1&sta ...

  2. linux du

    命令参数: -a或-all  显示目录中个别文件的大小. -b或-bytes  显示目录或文件大小时,以byte为单位. -c或--total  除了显示个别目录或文件的大小外,同时也显示所有目录或文 ...

  3. centos7安装tomcat8 新手入门 图文教程

    系统环境 操作系统:64位CentOS Linux release 7.2.1511 (Core) JDK版本:1.8.0_121 下载tomcat8压缩包 访问官网:http://tomcat.ap ...

  4. BMP操作_测试

    1.参考网址: http://blog.sina.com.cn/s/blog_678b377a0100mlyb.html http://blog.csdn.net/weiyongtao87/artic ...

  5. Activity回传值报错:Failure delivering result ResultInfo{who=null,request=7,result = 0,data=null}

    Activity  A   -----值------->  Activity  B   -----值----->  Activity  A     场景:当A跳转到B,再从B直接点击返回按 ...

  6. macOS下Hive 2.x的安装与配置

    1 简介 Hive是基于Hadoop的一个数据仓库工具,可以将结构化的数据文件映射为一张数据库表,并提供简单的[SQL]查询功能,可以将SQL语句转换为MapReduce任务进行运行.其优点是学习成本 ...

  7. Linux下boost库的编译、安装详解

    下载boost源码 boost下载地址 解压到一个目录 tar -zxvf boost_1_66_0.tar.gz 编译boost库 进入boost_1_66_0目录中 cd boost_1_66_0 ...

  8. U8工具栏特别小是怎么回事

    用友的工具栏特别窄了,填制凭证里的保存.增加凭证等按钮因为工具栏特别窄都看不清了 解决方法:正常机器下的system32下面的mscomctl.ocx文件替换到有问题的机器下,您的系统应该是XP的,这 ...

  9. Qt访问注册表并调用子进程

    在实际应用中需要在一个进程启动另外一个进程,可以将子进程的路径写入注册表中,然后主进程读取注册表中子进程路径,启动子进程,并以命令行参数的形式传入参数,启动子进程.具体实现方式如下 (1)      ...

  10. ace后台管理系统扁平化框架

    Bootstrap ACE后台管理界面模板(扁平化) 所属分类:后台模板 文件大小:1.22 MB 阅读:236697次 下载:55929次 来源:www.daimajiayuan.com 分享到:更 ...