1、已知块级元素的宽和高,使用绝对定位+外边距设定水平垂直居中。

父元素position:relative,子元素position:absolute;top:50%;left:50%;margin-top:-height/2;margin-left:-width/2;

效果图如下

代码:

<div class="box">
<div class="center-box1">
<p>【第一种方法】知道长和宽,使用绝对定位+外边距设定水平垂直居中</p>
</div>
</div> .box {
background: #6c94be;
width: 100%;
height: 450px;
position: relative;
}
.center-box1 {
position: absolute;
top: 50%;
left: 50%;
margin-top: -100px;
margin-left: -100px;
width: 200px;
height: 200px;
background: #5B83AD;
}

2、使用css3样式属性display:flex设定水平垂直居中,父元素样式属性display:flex;子元素使用margin:auto;未知子元素高度的时候依然可以使用。

一般chrome和火狐都能很好地支持。ie不支持

效果图如下:

代码:

<div class="box">
<div class="center-box2">
<p>【第二种方法】使用css3样式属性display:flex设定水平垂直居中</p>
</div>
</div> .box {
background: #6c94be;
width: 100%;
height: 450px;
display: flex;
}
.center-box2 {
margin: auto;
width: 200px;
background: #5B83AD;
}

3、依然使用绝对定位+css3样式属性transform

transform中translate偏移的百分比值是相对于自身大小的,无论绝对定位元素的尺寸是多少,其都是水平垂直居中显示的,但问题是兼容性不好。IE9+以及其他现代浏览器才支持。IE9之前版本不支持,在IE8模式下,不居中。

效果图如下:

代码:

<div class="box">
<div class="center-box3">
<p>【第三种方法】使用css3样式属性transform,transform中translate偏移的百分比值是相对于自身大小的</p>
</div>
</div> .box {
background: #6c94be;
width: 100%;
height: 450px;
position: relative;
} .center-box3 {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%,-50%);
background: #5B83AD;
width: 200px;
}

4、已知块级元素的宽和高,设置其样式属性position:absolute;top:0;left:0;bottom:0;right:0;+margin:auto;

效果图如下:

代码:

<div class="box">
<div class="center-box4">
<p>【第四种方法】已知宽和高,绝对定位+margin:auto;</p>
</div>
</div> .box {
background: #6c94be;
width: 100%;
height: 450px;
position: relative;
}
.center-box4 {
position: absolute;
top: 0;
left: 0;
bottom: 0;
right: 0;
width: 200px;
height: 200px;
background: #5B83AD;
margin: auto;
}

div块元素垂直水平居中方法总结的更多相关文章

  1. div+css实现未知宽高元素垂直水平居中

    div+css实现未知宽高元素垂直水平居中.很多同学在面试的时候都会遇到这样的问题:怎么用div+css的方法实现一个未知宽高的弹出框(或者图片)垂直水平居中??如果用JS的话就好办了,但是JS的使用 ...

  2. CSS之文本溢出隐藏,不定宽高元素垂直水平居中、禁止页面文本复制

    1.如何让不固定元素宽高的元素垂直水平居中 .center { position: absolute; top: 50%; left: 50%; background-color: #000; wid ...

  3. CSS垂直水平居中方法总结

    在布局的时候经常能用到居中,在此总结一下 html结构: <div class="wrap"> <div class="content"> ...

  4. CSS垂直水平居中方法整理

    CSS定位中常常用到垂直居中,比如覆盖层上的弹框. 兼容性比较好的方法: <!DOCTYPE html PUBliC "-//W3C//DTD XHTML 1.0 Transition ...

  5. 纯CSS实现元素垂直水平居中-非固定宽度

    这里不讨论行内元素的居中!! 盒子垂直居中+水平居中的需求时经常遇到的,看到的较多实现逻辑是固定content-box的宽度,通过让margin-left和margin-top等于宽或高的负一半来实现 ...

  6. 一个div在另一个div中水平垂直的方法

    html <div id="main"> <div id="box"> 一个div在另一个div中垂直居中实现方法 </div&g ...

  7. HTML中由于DIV(块元素)浮动,导致的父元素高度塌陷问题的解决方案

    费话不多说,直接上问题: 1.开始时,页面只有两个DIV的嵌套(见图) 运行结果是: 现在看运行的是正常的,但是当我设置让 class="box2" 的DIV浮动时 运行结果是这样 ...

  8. 设置DIV块元素在浏览器页面中垂直居中

    任务目标 实践HTML/CSS布局方式 深入了解position等CSS属性 任务描述 实现如 示例图(点击打开) 的效果 灰色元素水平垂直居中,有两个四分之一圆位于其左上角和右下角. 任务注意事项 ...

  9. 【CSS】3种CSS方法设置元素垂直水平居中

    1. 宽高固定 设置要水平垂直居中的 div 的 position 为 absolute,left:50%; margin-left为负的这个元素宽度的一半,同理,top:50%;margin-top ...

随机推荐

  1. JAVA 企业培训

  2. C++ Boost库 uBLAS 笔记

    构造 Vector #include <boost/numeric/ublas/vector.hpp> #include <boost/numeric/ublas/io.hpp> ...

  3. BAT-快速切换JDK1.6/1.7

    Code: @echo OFF rem 修改背景/字体颜色为黑/绿 color 0A rem 自动设置JDK环境变量,本程序不会对 [系统环境变量] 造成破环!不需要重启!! rem 没有%%JAVA ...

  4. 牛B的日本精神

    在汤森路透评选出的<2015全球创新企业百强>榜单里,日本以40家高居榜首,力压美国的35家.而中国内地无一入围.   在中国媒体上,我们见到的日本是“失去的20年”,经济衰退.创新能力丧 ...

  5. 64_m3

    molequeue-doc-0.8.0-2.20161222giteb397e.fc26.no..> 05-Apr-2017 10:04 451570 molequeue-libs-0.8.0- ...

  6. C# 加密解密以及sha256不可逆加密案例

    class Program { static void Main(string[] args) { string aa = "身份证"; string bb = "key ...

  7. u-boot引导内核过程

    目标板:2440 u-boot引导内核启动时,传入内核的参数为bootcmd=nand read.jffs2 0x30007FC0 kernel; bootm 0x30007FC0 一.nand re ...

  8. gbdt和xgboost中feature importance的获取

    来源于stack overflow,其实就是计算每个特征对于降低特征不纯度的贡献了多少,降低越多的,说明feature越重要 I'll use the sklearn code, as it is g ...

  9. CRM 业务

    1. 创建CRM项目 引入插件 创建数据库 from django.db import models from django.db import models class Department(mod ...

  10. 解决wordpress无法发送邮件的问题|配置好WP-Mail-SMTP的前提

    我的WordPress主机是万网的,配置WP-Mail-SMTP时一直无法发送邮件,导致设置失败.经过多次询问度娘才找到了解决wordpress无法发送邮件的方法,在这里把这个wordpress技巧分 ...