1.文字或者内联元素的垂直水平居中对齐

css属性 -- 水平居中:text-aligin:center; 垂直居中: line-height:height; 例子:、

html:

<div class="mb10 line-align">
我是垂直水平居中对齐的文字哦!
</div>
<div class="mb10 line-align">
<span>我是垂直水平居中对齐的内联元素span!</span>
</div>

css:

.mb10 {
  margin-bottom: 10px;
} /*文字或者内联元素的垂直水平居中对齐*/
.line-align {
  height: 40px;
  line-height: 40px;
  text-align: center;
  background: tan;
}

2.块元素的垂直水平居中对齐

1) 定位法

优点:方便理解。一目了然。

缺点:子元素必须固定宽高才可以使用。

html:

<div class="relativeDiv">
<div class="absoluteDiv"></div>
</div>

css:

/*定位法*/
.relativeDiv {
  height: 100px;
  background: tomato;
  position: relative;
}
.absoluteDiv {
  height: 20px;
  width: 100px;
  background: #000;
  position: absolute;
  left: 50%;
  top: 50%;
  margin-left: -50px;
  margin-top: -10px;
}

2) css3 transform结合position方法

优点:相比完全的定位法,这个方法中,子元素不比固定宽高。

缺点:兼容性不是很好,ie9+的浏览器才支持。

html:

<div class="transform">
<div class="transformChild"></div>
</div>

css:

/*css3 transform*/
.transform{
height: 100px;
background: green;
position: relative;
}
.transformChild{
width: 100px;
height: 20px;
background: #000;
position: absolute;
left: 50%;
top: 50%;
-webkit-transform: translate(-50%,-50%);
-ms-transform: translate(-50%,-50%);
-o-transform: translate(-50%,-50%);
transform: translate(-50%,-50%);
}

3. 变身table属性法

1) td中放span或者文字

html:

<div class="mb10 displayTable">
  <div class="displayTableCell">
  <span>我相当于是td中的span</span>
  </div>
</div>

2) td中放div

html:

<div class="mb10 displayTable">
<div class="displayTableCell">
<div class="tableDiv">我相当于是td中的div</div>
</div>
</div>

css:

.displayTable{
display: table;
width: 100%;
}
.displayTableCell{
width: 100%;
height: 100px;
display:table-cell;
background: palegoldenrod;
vertical-align: middle;
text-align: center;
}
.tableDiv{
width: 200px;
height: 30px;
background: #000;
color: #fff;
margin: 0 auto;
}

示例代码下载

4.图片水平垂直居中!!!很好用的方式

图片的水平处置居中都可以使用上面的方式,不过还有个超级好用的方式,padding+伪类方法。

缺点:只支持ie9+

例子:

html:

<div class="imgGroup">
<div class="imgAlone">
<div class="imgWidth">
<img src="data:image/1.png">
</div>
</div>
<div class="imgAlone">
<div class="imgWidth">
<img src="data:image/2.png">
</div>
</div>
<div class="imgAlone">
<div class="imgWidth">
<img src="data:image/1.png">
</div>
</div>
<div class="imgAlone">
<div class="imgWidth">
<img src="data:image/2.png">
</div>
</div>
<div class="imgAlone">
<div class="imgWidth">
<img src="data:image/1.png">
</div>
</div>
<div class="imgAlone">
<div class="imgWidth">
<img src="data:image/2.png">
</div>
</div>
</div>

css:

<style type="text/css">
.imgGroup{
width: 100%;
}
.imgAlone{
float: left;
width: 20%;
position: relative;
margin:2.5%;
border: 1px solid #ccc;
box-sizing: border-box;
}
.imgAlone:after{
content: "";
display: block;
padding-top:100%;
}
.imgWidth{
position: absolute;
left:;
top:;
width: 100%;
height: 100%;
}
.imgWidth img{
max-height: 100%;
max-width: 100%;
position: absolute;
top:;
left:;
right:;
bottom:;
margin: auto;
}
</style>

图片示例代码下载

示例效果查看

css水平垂直居中对齐方式的更多相关文章

  1. CSS实现水平垂直居中的方式有哪些?

    CSS实现水平垂直居中的方式有哪些? 基本结构样式: .box { width: 400px; height: 400px; background-color: red; } .inner { wid ...

  2. CSS水平垂直居中总结

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

  3. CSS制作水平垂直居中对齐 多种方式各有千秋

    作为前端攻城师,在制作Web页面时都有碰到CSS制作水平垂直居中,我想大家都有研究过或者写过,特别的其中的垂直居中,更是让人烦恼.这段时间,我收 集了几种不同的方式制作垂直居中方法,但每种方法各有千秋 ...

  4. CSS制作水平垂直居中对齐

    作为前端攻城师,在制作Web页面时都有碰到CSS制作水平垂直居中,我想大家都有研究过或者写过,特别的其中的垂直居中,更是让人烦恼.这段时间,我收集了几种不同的方式制作垂直居中方法,但每种方法各有千秋呀 ...

  5. 用CSS/CSS3 实现水平居中和垂直居中,水平垂直居中的方式

    一.水平居中 (1)行内元素解决方案:父为块元素+text-align: center 只需要把行内元素包裹在一个属性display为block的父层元素中,并且把父层元素添加如下属性即可: 使用te ...

  6. css水平垂直居中 三种最常用的方式

    代码在下面,可以直接用. bb两句,个人见解: text-align 是让里面的内容水平居中 line-height 是行高,行高等于元素的高度 就能让内容垂直居中 left和top 50% 是根据h ...

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

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

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

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

  9. css 水平垂直居中总结

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

随机推荐

  1. iOS系列 基础篇 04 探究视图生命周期

    iOS系列 基础篇 04 探究视图生命周期 视图是应用的一个重要的组成部份,功能的实现与其息息相关,而视图控制器控制着视图,其重要性在整个应用中不言而喻. 以视图的四种状态为基础,我们来系统了解一下视 ...

  2. DBMS_NETWORK_ACL_ADMIN

    DBMS_NETWORK_ACL_ADMIN学习 转载 http://blog.sina.com.cn/s/blog_4f925fc30102e2se.html 标签: oracle it 分类: 数 ...

  3. x01.TestViewContent: 插件测试

    开发神器 SharpDevelop 的插件系统,很有学习的必要. 1.首先在 github 上下载源代码,确保编译运行无误. 2.在 AddIns/Misc 下添加 SharpDevelop 插件项目 ...

  4. Leetcode, construct binary tree from inorder and post order traversal

    Sept. 13, 2015 Spent more than a few hours to work on the leetcode problem, and my favorite blogs ab ...

  5. Error invoking SqlProvider method (com.github.abel533.mapper.MapperProvider.dynamicSQL). Cause: java.lang.InstantiationException: com.github.abel533.mapper.MapperProvider

    org.apache.ibatis.exceptions.PersistenceException: ### Error querying database.  Cause: org.apache.i ...

  6. CWMP开源代码研究5——CWMP程序设计思想

    声明:本文涉及的开源程序代码学习和研究,严禁用于商业目的. 如有任何问题,欢迎和我交流.(企鹅号:408797506) 本文介绍自己用过的ACS,其中包括开源版(提供下载包)和商业版(仅提供安装包下载 ...

  7. [LeetCode] H-Index II 求H指数之二

    Follow up for H-Index: What if the citations array is sorted in ascending order? Could you optimize ...

  8. [LeetCode] First Missing Positive 首个缺失的正数

    Given an unsorted integer array, find the first missing positive integer. For example,Given [1,2,0]  ...

  9. ActiveMQ在Linux中的安装

    1.下载相关activeMQ安装包 下载路径:http://activemq.apache.org/download.html 下载最新安装包,选择Linux版进行下载 2.解压重命名 (1)解压: ...

  10. beanstalkd 消息队列

    概况:Beanstalkd,一个高性能.轻量级的分布式内存队列系统,最初设计的目的是想通过后台异步执行耗时的任务来降低高容量Web应用系统的页面访问延迟,支持过有9.5 million用户的Faceb ...