水平垂直居中

1.margin 负值调整偏移实现

兼容性: 当前流行的使用方法。

    <div class="box">
<div class="content"></div>
</div>
.box{
width: 100%;
height: 100%;
}
.content{
position: absolute;
left: 50%;
top: 50%;
margin-left:-50px;
margin-top:-50px;
/* transform: translate(-50%,-50%);*/ height:100px;/* height的一半 */
width:100px; /*width的一半 */
background: pink;
}

首先设置绝对定位元素,相对于原点left,top设置50%。由于是基于content元素的开始位置,真正居中,需要设置content的中心点 水平垂直居中。

2.margin auto 实现

兼容性:IE7及之前版本不支持

    <div class="box">
<div class="content"></div>
</div>
/* margin:auto */
.box{
position: relative;/* 非static定位 */ width: 200px;
height: 200px;
border: 1px solid;
}
.content{
position:absolute;/* 相对于父元素第一个非static定位 */
margin:auto;
top:;
left:;
right:;
bottom:; height:100px;
width:100px;
background: pink;
}

要点:绝对定位,margin:auto, TRBL 。

2.flex 实现

兼容性:css3特性

    <div class="box">
<div class="content"></div>
</div>
.box{
display: flex;
justify-content: center;
align-items: center; width: 200px;
height: 200px;
border: 1px solid;
}
.content {
width: 100px;
height: 100px;
background: pink;
}

设置容器使用flex布局,justify-content 设置主轴居中(水平居中),align-items 设置垂直居中。

4)table-cell 实现

    <div class="box">
<div class="content"></div>
</div>
.box{
display: table-cell;
text-align: center;
vertical-align: middle; width: 200px;
height: 200px;
border: 1px solid;
}
.content{ display: inline-block;
height:100px;
width:100px;
background: pink;
}

table-cell 布局,把内容水平居中和垂直居中。

移动端css水平垂直居中的更多相关文章

  1. CSS水平垂直居中总结

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

  2. HTML+CSS水平垂直居中

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

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

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

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

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

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

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

  6. css 水平垂直居中总结

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

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

    直接进入主题! 一.脱离文档流元素的居中 方法一:margin:auto法 CSS代码: div{ width: 400px; height: 400px; position: relative; b ...

  8. CSS水平垂直居中的几种方法

    直接进入主题! 一.脱离文档流元素的居中 方法一:margin:auto法 CSS代码: div{ width: 400px; height: 400px; position: relative; b ...

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

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

随机推荐

  1. Linux:Gentoo系统的安装笔记(四)

    本来以为结束,谁知离正常的系统还是比较远,不过不放弃,这期的笔记我敢肯定是最后一期了,写了那么多我也觉得烦,被gentoo折磨烦了. 安装KDE桌面 选择正确的配置文件 先获取root权限来安装桌面环 ...

  2. 学习小片段——springboot 错误处理

    一:先看看springboot默认的错误处理机制 springboot默认会判断是否是浏览器(http请求头Accept是否含有 text/html)来选择返回html错误页面或json错误信息 原因 ...

  3. echarts 滚动条 缩放

    // 初始化是否需要展示滚动条,与初始显示的数据数(总数据数的百分比) var isShowScroll = false;// 是否显示滚动条,默认false不显示 var dataZoom_end; ...

  4. python 启动shell报错 Subprocess Startup Error

    如图所示启动python的IDLE以后,按F5启动module或者启动shell的时候就会启动失败提示上面错误,刚开始百度知道解决方案不靠谱,最后找到原因是我之前看小甲鱼的教学视频时,新建了一个pic ...

  5. c++中函数的内存注意项

    函数不能返回指向栈内存的指针!因为返回的都是值拷贝! char *str = "abcd"这样就是字符串常量,这种能返回,而char str[] = "abcd" ...

  6. Python:从入门到实践--第三章--列表简介--练习

    #1.将一些朋友的姓名存储在一个列表中,并将其命名为friends.依次访问该列表中的每个元素,从而将每个朋友的姓名都打印出来. #2.继续使用1中的列表,为每人打印一条消息,每条消息包含相同的问候语 ...

  7. 30天代码day2 Operators

    Operators These allow you to perform certain operations on your data. There are 3 basic types: Unary ...

  8. Java线程池不错的总结博客

    ImportNew线程池总结 Java多线程之Executor.ExecutorService.Executors.Callable.Future与FutureTask 线程池,这一篇或许就够了

  9. php中include和require的区别(整理)

      require 的使用方法如 require("MyRequireFile.php"); .这个函数通常放在 PHP 程序的最前面,PHP 程序在执行前,就会先读入 requi ...

  10. 关于jstl中碰到的Property 'username' not found on type java.lang.String异常

    在jstl的forEach循环的时候总是有异常,刚开始以为是把类的属性名打错了,因为显示的是Property not found,但就算从类文件里面复制属性名过来依然显示的是Property not ...