1、绝对定位+负margin

兼容性很好,但需要指定子块的高度和宽度,以及负margin

.wp{
position: relative;
width: 200px;
height: 200px;
background-color: #ccc;
}
.test{
height: 100px;
position: absolute;
top:50%;
left: 50%;
margin-top: -50px;
margin-left: -50px;
background-color: #edd;
width: 100px;
}
<div class="wp">
<div class="test"></div>
</div>

2、绝对定位加margin:auto

缺点:IE6\7不支持,需要指定子块的宽度和高度

.wp{
position: relative;
width: 200px;
height: 200px;
background-color: #ccc;
}
.test {
position:absolute;
top:;
bottom:;
left:;
right:;
margin:auto;
height:100px;
width: 100px;
background-color: #edd;
}
<div class="wp">
<div class="test"></div>
</div>

3、绝对定位+translate

不需要指定子块的高宽,当子块中无内容且未设置高宽,子块不可见。

IE6/7/8不支持

  .wp {
width:200px;
height:200px;
background-color:yellow;
position:relative;
}
.test {
left:50%; top:50%;
transform:translate(-50%,-50%);
-webkit-transform:translate(-50%,-50%);
background-color:gray;
color:white;
position:absolute;
}
<div class="wp">
<div class="test">内容</div>
</div>

4、弹性盒

不需要指定子块的高宽,当子块中无内容且未设置高宽,子块不可见。

IE6/7/8/9不支持

.wp{
display: -webkit-box;
display: -moz-box;
display: -ms-flexbox;
display: -webkit-flex;
display: flex;
-webkit-box-align: center;
-moz-box-align: center;
-ms-flex-align: center;
-webkit-align-items: center;
align-items: center;
-webkit-box-pack: center;
-moz-box-pack: center;
-ms-flex-pack: center;
-webkit-justify-content: center;
justify-content: center;
height: 200px;
width: 200px;
background-color: #ccc; }
.test{
background-color: #edd;
}
<div class="wp">
<div class="test">内容</div>
</div>

5、table-cell

IE6/7不支持

.wp{
width: 200px;
height: 200px;
display: table;
background-color: #ccc;
}
.test {
display: table-cell;
text-align: center;
vertical-align: middle;
}
<div class="wp">
<div class="test">内容</div>
</div>

css水平垂直居中块整理的更多相关文章

  1. CSS水平垂直居中总结

    行内元素水平居中 把行内元素包裹在块级父元素中,且父元素中的css设置text-align:center; <!DOCTYPE html> <html> <head> ...

  2. CSS水平垂直居中的方法

    原文链接:http://caibaojian.com/370.html 水平垂直居中,特别是使用在列表的时候经常会用到的,以前有需求的时候我也做过类似的代码,是使用display:table-cell ...

  3. HTML+CSS水平垂直居中

    啦啦啦,好了,今天来分享自己的第一个知识点,难得自己还能想起来过来博客园,写写博客的. 好了,言归正传,今天分享关于html和css的一个简单的知识点,对于大部分从事前端开发的人员来说可能都是很简单的 ...

  4. css水平垂直居中对齐方式

    1.文字或者内联元素的垂直水平居中对齐 css属性 -- 水平居中:text-aligin:center; 垂直居中: line-height:height; 例子:. html: <div c ...

  5. 把简单做好也不简单-css水平垂直居中

    44年前我们把人送上月球,但在CSS中我们仍然不能很好实现水平垂直居中. 作者:Icarus 原文链接:http://xdlrt.github.io/2016/12/15/2016-12-15 水平垂 ...

  6. css 水平垂直居中总结

    空闲总结了下水平垂直居中方案,欢迎补充: 水平居中 水平居中有两种情况: 子元素是内联元素 这种那个情况下只需要在父元素定义: text-align:center; 例子: html: //省略了bo ...

  7. 常见的几种 CSS 水平垂直居中解决办法

    用CSS实现元素的水平居中,比较简单,可以设置text-align center,或者设置 margin-left:auto; margin-right:auto 之类的即可. 主要麻烦的地方还是在垂 ...

  8. css水平垂直居中

    margin法(水平居中) 需要满足三个条件: 元素定宽 元素为块级元素或行内元素设置display:block 元素的margin-left和margin-right都必须设置为auto 三个条件缺 ...

  9. css水平垂直居中的方法与 vertical-align 的用法

    前言:这是笔者学习之后自己的理解与整理.如果有错误或者疑问的地方,请大家指正,我会持续更新! 1. 已知元素宽度 方法一:已知宽高,可以用position定位 + margin负值的方法 : 绝对定位 ...

随机推荐

  1. Android记录程序崩溃Log写入文件

    将导致程序崩溃的堆栈调用Log写入文件,便于收集bug.在调试安卓程序,由于某些原因调试时手机不能连接PC端,无法通过IDE查看程序崩溃的Log,希望log能够写入文件中,对于已经发布的App可以通过 ...

  2. BZOJ 1632 [Usaco2007 Feb]Lilypad Pond:spfa【同时更新:经过边的数量最小】【路径数量】

    题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=1632 题意: 有一个n*m的池塘.0代表水,1代表荷花,2代表岩石,3代表起点,4代表终点 ...

  3. div遮罩弹框口

    <html> <head> <meta http-equiv="Content-Type" content="text/html; char ...

  4. innerText和innerHTML

    起因 由于公司的项目以前不考虑浏览器的兼容性问题,当时只考虑ie8浏览器,封装的控件也只针对ie8,我后面的做的时候,也就针对ie8,最近发现,封装的日期控件,在firefox竟然没法显示出来,去看J ...

  5. Ueditor文本编辑工具栏自定义

    1.找到 ueditor.config.js 文件 2.在 toolbars 数组内进行删减,添加 , toolbars: [[ 'fullscreen', 'source', '|', 'undo' ...

  6. World Finals 2017 (水题题解)

    看大佬做2017-WF,我这种菜鸡,只能刷刷水题,勉强维持生活. 赛后补补水题. 题目pdf链接,中文的,tls翻译的,链接在这里 个人喜欢在vjudge上面刷题. E Need for Speed ...

  7. robotium

    Test run failed: Permission Denial: starting instrumentation ComponentInfo{android.support.v7.appcom ...

  8. 使用svnsync实时备份SVN版本库

    前段时间把SVN版本库从win迁移到了Linux上,没隔几天那台win的磁盘就严重坏道了....这TMD什么运气! 花费了点时间研究了下svn自己的同步工具.写个日志记录下. 注意:svnsync要求 ...

  9. 【Boost】boost库asio详解3——io_service作为work pool

    无论如何使用,都能感觉到使用boost.asio实现服务器,不仅是一件非常轻松的事,而且代码很漂亮,逻辑也相当清晰,这点上很不同于ACE.使用io_service作为处理工作的work pool,可以 ...

  10. 2-1Java简介

    java程序执行流程 java平台: