CSS垂直水平居中方式大全(一)----水平居中-垂直居中

冷不防 2022-06-04 01:53 534阅读 0赞

CSS中的居中可分为水平居中垂直居中。水平居中分为行内元素居中块状元素居中两种情况,而块状元素又分为定宽块状元素居中不定宽块状元素居中

一、水平居中

1、行内元素居中

顾名思义,行内元素居中是只针对行内元素的,比如文本(text)、图片(img)、按钮等行内元素,可通过给父元素设置 text-align:center 来实现。另外,如果块状元素属性display 被设置为inline时,也是可以使用这种方法。但有个首要条件是子元素必须没有被float影响,否则一切都是无用功。

示例:

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>居中</title>
  6. <style>
  7. .div1{
  8. text-align:center;
  9. width: 500px;
  10. height: 500px;
  11. background: #999999;
  12. }
  13. </style>
  14. </head>
  15. <body>
  16. <div class="div1">Center</div>
  17. </body>
  18. </html>

效果:

20171201105226776

2、块状元素居中

(1)、定宽块状元素居中

满足定宽(块状元素的宽度width为固定值)和块状两个条件的元素可以通过设置“左右margin”值为“auto”来实现居中。

示例:

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>居中</title>
  6. <style>
  7. .div1{
  8. width: 500px;
  9. height: 500px;
  10. background: #FFA500;
  11. border:1px solid red;
  12. margin:0 auto;
  13. }
  14. </style>
  15. </head>
  16. <body>
  17. <div class="div1">Center</div>
  18. </body>
  19. </html>

效果:

20171201111032606

(2)、不定宽块状元素居中

在实际工作中我们会遇到需要为“不定宽度的块状元素”设置居中,比如网页上的分页导航,因为分页的数量是不确定的,所以我们不能通过设置宽度来限制它的弹性。(不定宽块状元素:块状元素的宽度width不固定。)

有三种方法可以对不定宽块状元素进行居中:

方法1:

将要显示的元素加入到 table 标签当中,然后为 table 标签设置“左右margin”值为“auto”来实现居中;

示例:

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>居中</title>
  6. <style>
  7. table{
  8. margin:0 auto;
  9. }
  10. </style>
  11. </head>
  12. <body>
  13. <div>
  14. <table>
  15. <tbody>
  16. <tr><td>
  17. <div style="width: 100px;height: 100px;background: #FFA500;">Center</div>
  18. </td></tr>
  19. </tbody>
  20. </table>
  21. </div>
  22. </body>
  23. </html>

效果:

20171201130624487

或使用 display : table;然后设该元素“左右margin”值为“auto”来实现居中。后面的display:table; 方法会更简洁。

示例:

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>居中</title>
  6. <style>
  7. .wrap{
  8. width: 100px;
  9. height: 100px;
  10. background:#ccc;
  11. display:table;
  12. margin:0 auto;
  13. }
  14. </style>
  15. </head>
  16. <body>
  17. <div class="wrap">
  18. Center
  19. </div>
  20. </body>
  21. </html>

效果:
20171201130846652

为什么加入table标签? 是利用table标签的长度自适应性—-即不定义其长度也不默认父元素body的长度(table其长度根据其内文本长度决定),因此可以看做一个定宽度块元素,然后再利用定宽度块状居中的margin的方法,使其水平居中。

方法2:

改变块级元素的 display 为 inline 类型(设置为 行内元素 显示),然后使用 text-align:center 来实现居中效果。

这种方法相比第一种方法的优势是不用增加无语义标签,但也存在着一些问题:它将块状元素的 display 类型改为 inline,变成了行内元素,所以少了一些功能,比如设定长度值(变成inline-block就可以设置宽高)。

示例:

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>居中</title>
  6. <style>
  7. .container{
  8. text-align:center;
  9. }
  10. .container div{
  11. list-style:none;
  12. margin:0;
  13. padding:0;
  14. display:inline;
  15. }
  16. </style>
  17. </head>
  18. <body>
  19. <div class="container">
  20. <div>
  21. 12122121
  22. </div>
  23. </div>
  24. </body>
  25. </html>

效果:

20171201131220418

方法3:

通过给父元素设置 float,然后给父元素设置 position:relative 和 left:50%,子元素设置 position:relative 和 left: -50% 来实现水平居中。

示例:

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>居中</title>
  6. <style>
  7. .wrap{
  8. float:left;
  9. position:relative;
  10. left:50%;
  11. clear:both;
  12. }
  13. .wrap-center{
  14. background:#ccc;
  15. position:relative;
  16. left:-50%;
  17. }
  18. </style>
  19. </head>
  20. <body>
  21. <div class="wrap">
  22. <div class="wrap-center">Center</div>
  23. </div>
  24. </body>
  25. </html>

效果:

20171201131517090

先设置父元素wrap清除浮动,然后左浮动。定位让wrap向右偏移50%。然后定义子元素相对于父元素向左偏移50%。脱离父元素。

二、垂直居中

垂直居中可分为父元素高度确定的单行文本,以及父元素高度确定的多行文本

1、父元素高度确定的单行文本的竖直居中的方法是通过设置父元素的 height 和 line-height 高度一致来实现的。(height: 该元素的高度,line-height: 顾名思义,行高,指在文本中,行与行之间的 基线间的距离 )。

示例:

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>居中</title>
  6. <style>
  7. .wrap h2{
  8. margin:0;
  9. height:100px;
  10. line-height:100px;
  11. background:#ccc;
  12. }
  13. </style>
  14. </head>
  15. <body>
  16. <div class="wrap">
  17. <h2>Hello world</h2>
  18. </div>
  19. </body>
  20. </html>

效果:

20171201132045268

说明:

line-height(行高) 的值不能设为100%,我们来看看官方文档中给出的关于line-height取值为百分比时候的描述:基于当前字体尺寸的百分比行间距。这里的百分比并不是相对于父元素尺寸而言,而是相对于字体尺寸来讲的。

2、父元素高度确定的多行文本

有两种方法:

方法1:

使用插入 table (包括tbody、tr、td)标签,同时设置 vertical-align:middle。

示例1:

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>居中</title>
  6. <style>
  7. .wrap{
  8. height:300px;
  9. background:#ccc;
  10. vertical-align:middle; /* td 标签默认情况下就默认设置了 vertical-align 为 middle,可以不需要显式地设置 */
  11. }
  12. </style>
  13. </head>
  14. <body>
  15. <table>
  16. <tbody>
  17. <tr>
  18. <td class="wrap">
  19. <div>
  20. <p>Hello world</p>
  21. <p>Hello world</p>
  22. <p>Hello world</p>
  23. </div>
  24. </td>
  25. </tr>
  26. </tbody>
  27. </table>
  28. </body>
  29. </html>

效果1:

20171201132326343

示例2:

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>居中</title>
  6. <style>
  7. .wrap{
  8. background:#ccc;
  9. display:table;
  10. vertical-align:middle;
  11. }
  12. </style>
  13. </head>
  14. <body>
  15. <div class="wrap">
  16. <p>Hello world</p>
  17. <p>Hello world</p>
  18. <p>Hello world</p>
  19. </div>
  20. </body>
  21. </html>

效果2:

20171201132520098

方法2:

设置块级元素的 display 为 table-cell(设置为表格单元显示),激活 vertical-align 属性。但这种方法兼容性比较差, IE6、7 并不支持这个样式。

示例:

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>居中</title>
  6. <style>
  7. .wrap{
  8. height:300px;
  9. background:#ccc;
  10. display:table-cell;/*IE8以上及Chrome、Firefox*/
  11. vertical-align:middle;/*IE8以上及Chrome、Firefox*/
  12. }
  13. </style>
  14. </head>
  15. <body>
  16. <div class="wrap">
  17. <p>Hello world</p>
  18. <p>Hello world</p>
  19. <p>Hello world</p>
  20. </div>
  21. </body>
  22. </html>

效果:

20171201132724403

还可以通过以下一些方式来实现垂直居中:

1.使用绝对定位和负外边距对块级元素进行垂直居中:

示例:

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>居中</title>
  6. <style>
  7. #box {
  8. width: 300px;
  9. height: 300px;
  10. background: #ddd;
  11. position: relative;
  12. }
  13. #child {
  14. width: 150px;
  15. height: 100px;
  16. background: orange;
  17. position: absolute;
  18. top: 50%;
  19. margin: -50px 0 0 0;
  20. line-height: 100px;
  21. }
  22. </style>
  23. </head>
  24. <body>
  25. <div id="box">
  26. <div id="child">Hello world</div>
  27. </div>
  28. </body>
  29. </html>

效果:

20171201133446013

说明:

必须提前知道被居中块级元素的尺寸,否则无法准确实现垂直居中。

2.使用绝对定位和transform

示例:

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>居中</title>
  6. <style>
  7. #box {
  8. width: 300px;
  9. height: 300px;
  10. background: #ddd;
  11. position: relative;
  12. }
  13. #child {
  14. background: #93BC49;
  15. position: absolute;
  16. top: 50%;
  17. transform: translate(0, -50%);
  18. }
  19. </style>
  20. </head>
  21. <body>
  22. <div id="box">
  23. <div id="child">
  24. Hello world,Hello world,Hello worldHello world
  25. </div>
  26. </div>
  27. </body>
  28. </html>

效果:

20171201135309044

说明:

这种方法有一个非常明显的好处就是不必提前知道被居中元素的尺寸了,因为transform中translate偏移的百分比就是相对于元素自身的尺寸而言的。

3.另外一种使用绝对定位和负外边距进行垂直居中的方式

示例:

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>居中</title>
  6. <style>
  7. #box {
  8. width: 300px;
  9. height: 300px;
  10. background: #ddd;
  11. position: relative;
  12. }
  13. #child {
  14.   width: 50%;
  15. height: 30%;
  16. background: pink;
  17. position: absolute;
  18. top: 50%;
  19. margin: -15% 0 0 0;
  20. }
  21. </style>
  22. </head>
  23. <body>
  24. <div id="box">
  25. <div id="child">
  26. Hello world,Hello world,Hello worldHello world
  27. </div>
  28. </div>
  29. </body>
  30. </html>

效果:

20171201135522068

说明:

这种方式的原理实质上和前两种相同。margin的取值也可以是百分比,这时这个值规定了该元素基于父元素尺寸的百分比,可以根据实际的使用场景来决定是用具体的数值还是用百分比。

4.绝对定位结合margin: auto

示例:

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>居中</title>
  6. <style>
  7. #box {
  8. width: 300px;
  9. height: 300px;
  10. background: #ddd;
  11. position: relative;
  12. }
  13. #child {
  14. width: 200px;
  15. height: 100px;
  16. background: #A1CCFE;
  17. position: absolute;
  18. top: 0;
  19. bottom: 0;
  20. margin: auto;
  21. line-height: 100px;
  22. }
  23. </style>
  24. </head>
  25. <body>
  26. <div id="box">
  27. <div id="child">
  28. Hello world,Hello world
  29. </div>
  30. </div>
  31. </body>
  32. </html>

效果:

20171201135725365

说明:

这种实现方式的两个核心是:把要垂直居中的元素相对于父元素绝对定位,top和bottom设为相等的值,我这里设成了0,当然你也可以设为99999px或者-99999px无论什么,只要两者相等就行,这一步做完之后再将要居中元素的margin设为auto,这样便可以实现垂直居中了。
被居中元素的宽高也可以不设置,但不设置的话就必须是图片这种自身就包含尺寸的元素,否则无法实现。

5.使用padding实现子元素的垂直居中

示例:

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>居中</title>
  6. <style>
  7. #box {
  8. width: 300px;
  9. background: #ddd;
  10. padding: 100px 0;
  11. }
  12. #child {
  13. width: 200px;
  14. height: 100px;
  15. background: #F7A750;
  16. line-height: 50px;
  17. }
  18. </style>
  19. </head>
  20. <body>
  21. <div id="box">
  22. <div id="child">
  23. Hello world,Hello world
  24. </div>
  25. </div>
  26. </body>
  27. </html>

效果:

20171201140037985

说明:

这种实现方式非常简单,就是给父元素设置相等的上下内边距,则子元素自然是垂直居中的,当然这时候父元素是不能设置高度的,要让它自动被填充起来,除非设置了一个正好等于上内边距+子元素高度+下内边距的值,否则无法精确的垂直居中。

6.设置第三方基准

示例:

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>居中</title>
  6. <style>
  7. #box {
  8. width: 300px;
  9. height: 300px;
  10. background: #ddd;
  11. }
  12. #base {
  13. height: 50%;
  14. background: #AF9BD3;
  15. }
  16. #child {
  17. height: 100px;
  18. background: rgba(131, 224, 245, 0.6);
  19. line-height: 50px;
  20. margin-top: -50px;
  21. }
  22. </style>
  23. </head>
  24. <body>
  25. <div id="box">
  26. <div id="base"></div>
  27. <div id="child">
  28. Hello world,Hello world
  29. </div>
  30. </div>
  31. </body>
  32. </html>

效果:

20171201140625037

说明:

这种方式也非常简单,首先设置一个高度等于父元素高度一半的第三方基准元素,那么此时该基准元素的底边线自然就是父元素纵向上的中分线,做完这些之后再给要垂直居中的元素设置一个margin-top,值的大小是它自身高度的一半取负,则实现垂直居中。

7.使用flex布局

示例:

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>居中</title>
  6. <style>
  7. #box {
  8. width: 300px;
  9. height: 300px;
  10. background: #ddd;
  11. display: flex;
  12. align-items: center;
  13. }
  14. </style>
  15. </head>
  16. <body>
  17. <div id="box">
  18. Hello world,Hello world
  19. </div>
  20. </body>
  21. </html>

效果:

20171201140925114

块级元素示例如下:

示例:

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>居中</title>
  6. <style>
  7. #box {
  8. width: 300px;
  9. height: 300px;
  10. background: #ddd;
  11. display: flex;
  12. align-items: center;
  13. }
  14. #child {
  15. width: 300px;
  16. height: 100px;
  17. background: #8194AA;
  18. line-height: 100px;
  19. }
  20. </style>
  21. </head>
  22. <body>
  23. <div id="box">
  24. <div id="child">
  25. Hello world,Hello world
  26. </div>
  27. </div>
  28. </body>
  29. </html>

效果:

20171201141116627

8.使用弹性布局的方式

示例:

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>居中</title>
  6. <style>
  7. #box {
  8. width: 300px;
  9. height: 300px;
  10. background: #ddd;
  11. display: flex;
  12. flex-direction: column;
  13. justify-content: center;
  14. }
  15. #child {
  16. width: 300px;
  17. height: 100px;
  18. background: #08BC67;
  19. line-height: 100px;
  20. }
  21. </style>
  22. </head>
  23. <body>
  24. <div id="box">
  25. <div id="child">
  26. Hello world,Hello world
  27. </div>
  28. </div>
  29. </body>
  30. </html>

效果:

20171201141315490

说明:

这种方式也是首先给父元素设置display:flex,设置好之后改变主轴的方向flex-direction: column

9.使用 line-height 和 vertical-align 对图片进行垂直居中

示例:

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>居中</title>
  6. <style>
  7. #box{
  8. width: 300px;
  9. height: 300px;
  10. background: #ddd;
  11. line-height: 300px;
  12. }
  13. #box img {
  14. vertical-align: middle;
  15. }
  16. </style>
  17. </head>
  18. <body>
  19. <div id="box">
  20. <img src="img/user.png">
  21. </div>
  22. </body>
  23. </html>

效果:

20171201141739477

10.使用 display 和 vertical-align 对容器里的文字进行垂直居中

示例:

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>居中</title>
  6. <style>
  7. #box {
  8. width: 300px;
  9. height: 300px;
  10. background: #ddd;
  11. display: table;
  12. }
  13. #child {
  14. display: table-cell;
  15. vertical-align: middle;
  16. }
  17. </style>
  18. </head>
  19. <body>
  20. <div id="box">
  21. <div id="child">Hello world,Hello world</div>
  22. </div>
  23. </body>
  24. </html>

效果:

20171201141956033

说明:

vertical-align属性只对拥有valign特性的html元素起作用,例如表格元素中的等等。

vertical-align属性规定单元格中内容的垂直排列方式,语法:

value的可能取值有四种:

top:对内容进行上对齐
middle:对内容进行居中对齐
bottom:对内容进行下对齐
baseline:基线对齐

发表评论

表情:
评论列表 (有 0 条评论,534人围观)

还没有评论,来说两句吧...

相关阅读

    相关 CSS水平垂直居中

            在实际前端开发中,我们会遇到让一些元素居中的情况,如水平居中、垂直居中或者水平垂直居中等等。CSS的表示方法有很多,现在小编在这里总结一下,以便开发的时候可以快