以下方法来自百度知道: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. ajax操作json的三种方式

    一. 什么是json? 1. JSON是一种轻量级的数据交换格式 2. JSON 可以将 JavaScript 对象中表示的一组数据转换为字符串,然后就可以在网络或者程序之间轻松地传递这个字符串,并在 ...

  2. FileNotFoundException报错, src\main\....\....(拒绝访问) , 原因:1. 方法没有判断文件夹和文件, 2.没有指明文件的具体路径和名字

  3. 分布式系统的唯一id生成算法你了解吗?

    在分库分表之后你必然要面对的一个问题,就是id咋生成? 因为要是一个表分成多个表之后,每个表的id都是从1开始累加自增长,那肯定不对啊. 举个例子,你的订单表拆分为了1024张订单表,每个表的id都从 ...

  4. Python - requests https请求的坑

    #-*-coding:utf-8-*- # Time:2017/9/25 20:41 # Author:YangYangJun import requests import ssl from requ ...

  5. Android DatePickerDialog 使用方法

    (一)在Android 4.0以上系统的某些手机(如本人的测试机红米Note(系统4.4.4),以及模拟器(系统4.0)),使用如下代码创建时间选择器时,页面效果如图: Calendar cal = ...

  6. 为 昂达 v891 安装上了 remix OS 了

    起因: 默认的ROM自带一堆垃圾app,最主要的是没有root , 所以卸载不了. 然后试了 Root大师 , 刷机精灵 之类的软件. 我 CTMD , 简直比出厂ROM 还流氓, 不断的强制安装各种 ...

  7. GenomicConsensus (quiver, arrow)使用方法 | 序列 consensus

    https://github.com/PacificBiosciences/GenomicConsensus GenomicConsensus 是pacbio开发的,我个人非常不喜欢pacbio开发的 ...

  8. idea ----> 学习笔记

    使用的是社区版的idea 1. 2.关键点之二:配置artifacts.参照 <使用IDEA2017创建java web+Maven项目>http://blog.csdn.net/love ...

  9. WPF打印涉及到的关键类

    DocumentViewer--->FixedDocument----> PageContent  ---->FixedPage

  10. WebStrom配置

    1.下载安装Node.jshttps://nodejs.org/en/download/2.配置node路径3.修改文件默认字符集