文章参考:http://css-tricks.com/centering-css-complete-guide/?utm_source=ourjs.com(翻译不当之处请谅解)

一、水平居中

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属性对齐,
演示地址:http://jsfiddle.net/Web_Code/5fvrwwk1/1/embedded/result/
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: }

原文首发:http://www.ido321.com/824.html

如何实现CSS居中?–CSS居中常用方法相关推荐

  1. css表格文本居中的指令,CSS似乎无法在表格单元中居中文本

    我使用所有此代码,因为它正在更改现有网站的行为和外观,所以我无法触及代码结构(请参阅HTML).我所能碰到的只有CSS(也就是为什么你看到这么多!重要,覆盖) 现在我遇到的问题是我似乎无法将tabe- ...

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

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

  3. CSS实现元素居中原理解析

    原文:CSS实现元素居中原理解析 在 CSS 中要设置元素水平垂直居中是一个非常常见的需求了.但就是这样一个从理论上来看似乎实现起来极其简单的,在实践中,它往往难住了很多人. 让元素水平居中相对比较简 ...

  4. html盒子嵌套居中,css在盒子中垂直居中和固定居中

    顶部固定居中 我是固定的 .w960{ width: 960px; margin:0 auto; } .fixed{ position: absolute; top:0; left: 0; right ...

  5. php页面底部信息居中,css底部如何局中?css三种居中方法

    本篇文章给大家带来的内容是关于css底部如何局中?css三种居中方法,有一定的参考价值,有需要的朋友可以参考一下,希望对你有所帮助. 某天组长让我改一个表格的样式,要求底部局中.当时想很简单的嘛,哼哧 ...

  6. html盒子自动居中,css盒子怎么居中?

    在前端切图的时候,可能经常会遇到一个div盒子怎么在固定区域垂直居中的需求,下面我们来看一下css实现盒子居中的方法. css设置盒子居中的方法: 第一种: 用css的position属性 .div1 ...

  7. CSS Center(居中专题)

    CSS Center(居中专题) 水平居中(左右居中) // 1.inline-block + text-align.parent{text-align: center;}.child{display ...

  8. php中文字怎么上下居中,CSS怎么设置垂直居中?

    在我们开发前端页面的时候,为了让页面效果美观,会出现需要垂直居中效果的地方.本章就让我们来了解一下用css如何设置垂直居中,详细介绍一下设置文字与div盒子的垂直居中的几种方法.有一定的参考价值,有需 ...

  9. div css将文字居中显示图片,css文字居中、图片居中、div居中解决方案

    一.文字居中 若文字只有一行 文本只有一行 .box { width: 200px; height: 200px; background-color: red; } .text { line-heig ...

  10. css绝对定位如何居中?css绝对定位居中的四种实现方法-web前端教程

    在网页进行css布局时居中是经常需要用到的,其中就有css绝对定位居中,那么,css绝对定位如何实现居中?今天的这篇文章将给大家来介绍关于css绝对定位居中的实现方法. css绝对定位居中的实现方法有 ...

最新文章

  1. 软raid1 重新同步_盾构同步注浆施工,看完你就会!
  2. 了解Java中的检查与未检查异常
  3. ASP.NET 的数据绑定,DataList,Repeater 的绑定示例
  4. linux下使用cat打开文件乱码
  5. 等午饭吃过后的dwzjzx
  6. python local global_Python 关键字global全局变量详解
  7. 如何使用SSH连接到远程MySQL服务器
  8. cassandra写数据CommitLog
  9. VMware vCloud Director视频教程
  10. 【算法/C语言】01背包问题(动态规划DP)
  11. 深度学习-23:矩阵理论(L0/L1/L2范数)
  12. Jspx.net Framework 6.38发布
  13. 288388D-EnterCAT调试
  14. 同时采集抖音里多个视频,并批量添加相同的背景图片
  15. 算法设计与分析——算法基础初步了解
  16. 5G(一)——DNN及APN
  17. (3)riak_core系统的工作方式
  18. IA-32寄存器(通用寄存器、EFLAGS寄存器、指令指针寄存器、段寄存器)
  19. 楼天城:中国大学生计算机编程第一人
  20. 优声云打印机打印模板介绍

热门文章

  1. pointer-events属性
  2. Android adb你真的会用吗?
  3. 物联网与应用数字战略
  4. vue.js环境安装
  5. springboot集成swagger2构建RESTful API文档
  6. AOP之PostSharp2-OnMethodBoundaryAspect
  7. 常用的Firefox浏览器插件、Chrome浏览器插件收藏
  8. select,poll,epoll区别。
  9. Unity3D为何能跨平台?聊聊CIL(MSIL)
  10. oracle 11g rac asm磁盘组增加硬盘