以下方法来自百度知道: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. ubuntu 14.04 安装pip

    sudo apt-get install python-pip

  2. eclipse中.project文件和.classpath文件详解

    一.概述.project是项目文件,项目的结构都在其中定义,比如lib的位置,src的位置,classes的位置..classpath的位置定义了你这个项目在编译时所使用的$CLASSPATH. 二. ...

  3. JavaSE习题 第四章 类与对象

    问答题: 1.在声明类时,类名应该遵守哪些习惯? 1.与文件名相同2.首字母大写 2.类体内容中有那两类比较重要的成员? 1.成员变量2.方法 3.实例方法可以操作类变量吗?类方法可以操作实例变量吗? ...

  4. HTML 标记 3 —— CSS

    <style type="text/css">body { background-color: #F00;} p{ color:#0F0; } .自己定义 { colo ...

  5. 2018 AICCSA Programming Contest

    2018 AICCSA Programming Contest A Tree Game B Rectangles 思路:如果存在大于0的交面积的话, 那么肯定能找到一条水平的直线 和 一条垂直的直线, ...

  6. legend2---开发日志6(后端和前端如何相互配合(比如php,js,元素状态和数据改变))

    legend2---开发日志6(后端和前端如何相互配合(比如php,js,元素状态和数据改变)) 一.总结 一句话总结:php给元素初始状态,js根据这个状态做初始化和后续变化,使用vue真的很方便( ...

  7. lncRNA表达定量方法评估

    见文章:Benchmark of lncRNA Quantification for RNA-Seq of Cancer Samples Overall, 10-16% of lncRNAs can ...

  8. Python的特殊成员

    Python 用下划线作为变量前缀和后缀指定特殊变量 _xxx 不能用’from module import *’导入 __xxx__ 系统定义名字 __xxx 类中的私有变量名 核心风格:避免用下划 ...

  9. 20 Interesting WPF Projects on CodePlex

    20 Interesting WPF Projects on CodePlex (Some for Silverlight too) Pete Brown - 22 November 2010   I ...

  10. MapReduce处理气象数据

    老师:MissDu 提交作业 1. 用Python编写WordCount程序并提交任务 程序 WordCount 输入 一个包含大量单词的文本文件 输出 文件中每个单词及其出现次数(频数),并按照单 ...