来源:http://www.ido321.com/824.html

一、水平居中

1、内联元素居中:相对父级块级元素居中对齐

   1: .center-children {
   2:   text-align: center;
   3: }
 
      2、块级元素居中:设置margin-left和margin-right为auto让它居中(同时还要设置width,否则它就会承满整个容器,无法看出居中效果)
   1: .center-me {
   2:   margin: 0 auto;
   3: }

如果有很多块级元素需要水平居中成一行,最好使用一个不同的display类型。这是一个使用inline-block和flex的例子。

演示地址:http://jsfiddle.net/Web_Code/5fvrwwk1/embedded/result/

   1: <main class="inline-block-center">
   2:   <div>
   3:     I'm an element that is block-like with my siblings and we're centered in a row.
   4:   </div>
   5:   <div>
   6:     I'm an element that is block-like with my siblings and we're centered in a row. I have more content in me than my siblings do.
   7:   </div>
   8:   <div>
   9:     I'm an element that is block-like with my siblings and we're centered in a row.
  10:   </div>
  11: </main>
  12: <main class="flex-center">
  13:   <div>
  14:     I'm an element that is block-like with my siblings and we're centered in a row.
  15:   </div>
  16:   <div>
  17:     I'm an element that is block-like with my siblings and we're centered in a row. I have more content in me than my siblings do.
  18:   </div>
  19:   <div>
  20:     I'm an element that is block-like with my siblings and we're centered in a row.
  21:   </div>
  22: </main>

css:

   1: body {
   2:   background: #f06d06;
   3:   font-size: 80%;
   4: }
   5: main {
   6:   background: white;
   7:   margin: 20px 0;
   8:   padding: 10px;
   9: }
  10: main div {
  11:   background: black;
  12:   color: white;
  13:   padding: 15px;
  14:   max-width: 125px;
  15:   margin: 5px;
  16: }
  17: .inline-block-center {
  18:   text-align: center;
  19: }
  20: .inline-block-center div {
  21:   display: inline-block;
  22:   text-align: left;
  23: }
  24: .flex-center {
  25:   display: flex;
  26:   justify-content: center;
  27: }

二、垂直居中

1、内联元素:设置相等的上下padding,或者利用line-height

   1: .link {
   2:   padding-top: 30px;
   3:   padding-bottom: 30px;
   4: }

文本不会换行的情况下,可以使用line-height,让其与height相等去对齐文本。

   1: .center-text-trick {
   2:   height: 100px;
   3:   line-height: 100px;
   4:   white-space: nowrap;
   5: }
 
多行的文本也可以利用上下等padding的方式也可以让多行居中,但是如果这方法没用,你可以让这些文字的容器按table cell模式显示,然后设置文字的vertical-align属性对齐,
html:
   1: <table>
   2:   <tr>
   3:     <td>
   4:       I'm vertically centered multiple lines of text in a real table cell.
   5:     </td>
   6:   </tr>
   7: </table>
   8: <div class="center-table">
   9:   <p>I'm vertically centered multiple lines of text in a CSS-created table layout.</p>
  10: </div>

css

   1: body {
   2:   background: #f06d06;
   3:   font-size: 80%;
   4: }
   5: table {
   6:   background: white;
   7:   width: 240px;
   8:   border-collapse: separate;
   9:   margin: 20px;
  10:   height: 250px;
  11: }
  12: table td {
  13:   background: black;
  14:   color: white;
  15:   padding: 20px;
  16:   border: 10px solid white;
  17:   /* default is vertical-align: middle; */
  18: }
  19: .center-table {
  20:   display: table;
  21:   height: 250px;
  22:   background: white;
  23:   width: 240px;
  24:   margin: 20px;
  25: }
  26: .center-table p {
  27:   display: table-cell;
  28:   margin: 0;
  29:   background: black;
  30:   color: white;
  31:   padding: 20px;
  32:   border: 10px solid white;
  33:   vertical-align: middle;
  34: }

2、块级元素

若元素有固定高度,可以这样垂直居中

   1: .parent {
   2:   position: relative;
   3: }
   4: .child {
   5:   position: absolute;
   6:   top: 50%;
   7:   height: 100px;
   8:   margin-top: -50px; /* 如果没有使用: border-box; 的盒子模型则需要设置这个 */
   9: }

如果不知道元素高度,则这样

   1: .parent {
   2:   position: relative;
   3: }
   4: .child {
   5:   position: absolute;
   6:   top: 50%;
   7:   transform: translateY(-50%);
   8: }

也可以使用flexbox

   1: <main>  
   2:   <div>
   3:      I'm a block-level element with an unknown height, centered vertically within my parent.
   4:   </div>  
   5: </main>
   1: body {
   2:   background: #f06d06;
   3:   font-size: 80%;
   4: }
   5: main {
   6:   background: white;
   7:   height: 300px;
   8:   width: 200px;
   9:   padding: 20px;
  10:   margin: 20px;
  11:   display: flex;
  12:   flex-direction: column;
  13:   justify-content: center;
  14:   resize: vertical;
  15:   overflow: auto;
  16: }
  17: main div {
  18:   background: black;
  19:   color: white;
  20:   padding: 20px;
  21:   resize: vertical;
  22:   overflow: auto;
  23: }

三、同时水平和垂直居中

1、元素有固定高度和宽度:先绝对居中,然后上移和左移50%的宽度即可

   1: //这种方案有极好的跨浏览器支持。
   2: .parent {
   3:   position: relative;
   4: }
   5: .child {
   6:   width: 300px;
   7:   height: 100px;
   8:   padding: 20px;
   9:   position: absolute;
  10:   top: 50%;
  11:   left: 50%;
  12:   margin: -70px 0 0 -170px;
  13: }

2、元素的高度和宽度未知或可变的:使用transofrm属性在两个方向都平移负50%

   1: .parent {
   2:   position: relative;
   3: }
   4: .child {
   5:   position: absolute;
   6:   top: 50%;
   7:   left: 50%;
   8:   transform: translate(-50%, -50%);
   9: }

如何实现CSS居中?–CSS居中常用方法的更多相关文章

  1. css布局---各种居中

    居中是我们使用css来布局时常遇到的情况.使用css来进行居中时,有时一个属性就能搞定,有时则需要一定的技巧才能兼容到所有浏览器,本文就居中的一些常用方法做个简单的介绍. 注:本文所讲方法除了特别说明 ...

  2. css中关于居中的那点事儿

    css中关于居中的那点事儿 关于居中,无论是水平居中,还是垂直居中都有很多方式,下面我来介绍一些常用的. 第一部分:水平居中 1.实现行内元素的居中.方法:在行内元素外面的块元素的样式中添加:text ...

  3. css中各种居中的奇技淫巧总结

    css中各种居中的奇技淫巧总结   第一种,在固定布局中比较常用的技巧设置container的margin:0 auto:   第二种(从布局中入手)   css .outer{ height:200 ...

  4. CSS实现div居中

    <!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title&g ...

  5. CSS布局和居中常用技巧

    1.常用居中方法 居中在布局中很常见,我们假设DOM文档结构如下,子元素要在父元素中居中: <div class="parent"> <div class=&qu ...

  6. css 浮动元素居中

    方法一 <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8 ...

  7. css设置图片居中、居左、居右

      CreateTime--2017年12月8日14:25:09 Author:Marydon css设置图片居中.居左.居右 图片一般默认是居左显示的,如何更改它的水平位置呢? <div st ...

  8. css布局之居中

    CSS布局之居中 本文主要是介绍水平居中,垂直居中,还有水平垂直居中的方法 水平居中 1.行内元素水平居中 使用text-align:center;就可以实现行内元素的水平居中,但是记得要在父元素中设 ...

  9. css中的居中问题

    前两天写了一篇关于display:table的用法,里面涉及到居中的问题,这两天愈发觉得css中的居中是一个值得关注的问题,现总结如下. 一.垂直居中 (1)inline或者inline-*元素 1. ...

  10. CSS中各种居中方法

    CSS中各种居中方法,本文回顾一下,便于后续的使用. 水平居中方法 1.行内元素居中 行内元素居中是只针对行内元素的,比如文本(text).图片(img).按钮等行内元素,可通过给父元素设置 text ...

随机推荐

  1. 1049: [HAOI2006]数字序列 - BZOJ

    Description 现在我们有一个长度为n的整数序列A.但是它太不好看了,于是我们希望把它变成一个单调严格上升的序列.但是不希望改变过多的数,也不希望改变的幅度太大.Input 第一行包含一个数n ...

  2. redis 在windows 上的安装与使用

    1.redis-windows 最近在做一个抢拍模块,由于过于平凡的insert与update I/O受不了,故只好把东西放内存里,等拍卖结束了,在写入磁盘. 至于为什么要用window呢? 因为服务 ...

  3. ExtJS4.2学习(20)动态数据表格之前几章总结篇1(转)

    鸣谢:http://www.shuyangyang.com.cn/jishuliangongfang/qianduanjishu/2014-02-18/196.html --------------- ...

  4. [转载]获取当前日期和农历的js代码

    原文地址: http://www.cnblogs.com/Gnepner/archive/2011/09/07/2169822.html 获取当前日期时间: function GetCurrentDa ...

  5. hdu 1024

    参考了一下 http://moxi466839201.blog.163.com/blog/static/18003841620110220374942/ 滚动数组   状态转移方程不太好理解 .... ...

  6. Redis hash数据类型操作

    Redis hash是一个string类型的field和value的映射表.一个key可对应多个field,一个field对应一个value.将一个对象存储 为hash类型,较于每个字段都存储成str ...

  7. ajax的GET和POST请求

    GET和POST请求 GET请求时最常见的请求类型,用于向服务器查询信息,必要时可以将查询字符串参数放在URL尾部发送给服务器,如果参数有特殊字符必须正确编码.我们上面使用的例子都是使用GET请求,非 ...

  8. 最短路径算法之二——Dijkstra算法

    Dijkstra算法 Dijkstra算法主要特点是以起始点为中心向外层层扩展,直到扩展到终点为止. 注意该算法要求图中不存在负权边. 首先我们来定义一个二维数组Edge[MAXN][MAXN]来存储 ...

  9. VS下遇到未能加载文件或程序集 错误

    这个的错误原因可能是在64的系统上编译32位的应用程序,遇到这个错误,可以通过下面的手段解决! 1.关闭Visual Studio. 2. 在Visual Studio Tools子目录,以管理员身份 ...

  10. 新LNMP环境,但是SESSION跨页面或者刷新 session丢失

    唠叨:本地WAMP做了一个微信项目,一切OK,昨天需要在LINUX架设一套LNMP环境.但是,在架设代码的时候,登录总是不成功~~~ 最终解决:权限 其实,作为一个程序猿,只要涉及到服务器内容,都应该 ...