您现在的位置: 365建站网 > 365文章 > 在html+css中实现文字垂直居中的方法

在html+css中实现文字垂直居中的方法

文章来源:365jz.com     点击数:1031    更新时间:2021-07-17 12:50   参与评论

使用css进行文字的水平和垂直居中,并不是一件容易的事情,有些新手在使用css方法去实现css居中,在浏览器并没有什么效果,可能是由于兼容的问题或者是一些非主流的浏览器。

在html+css中实现文字垂直居中的方法总结如下:

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

css垂直居中效果:


在线测试工具:html在线编辑器

css垂直居中实现代码:

</>code

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4.     <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  5.     <title>365建站演示</title>
  6.     <style type="text/css">
  7.         #box {
  8.             width: 300px;
  9.             height: 300px;
  10.             background: #DDD;
  11.             position: relative;
  12.         }
  13.         
  14.         #child {
  15.             width: 150px;
  16.             height: 100px;
  17.             position: absolute;
  18.             top: 50%;
  19.             margin-top: -50px;
  20.             margin-left: 75px;
  21.             background: orange;
  22.             line-height: 100px;
  23.             text-align: center;
  24.         }
  25.     </style>
  26. </head>
  27. <body>
  28.     <div id="box">
  29.         <div id="child">
  30.             我是测试DIV
  31.         </div>
  32.     </div>
  33. </body>
  34. </html>

这个方法兼容性不错,但是有一个小缺点:必须提前知道被居中块级元素的尺寸,否则无法准确实现垂直居中。

2.使用绝对定位和transform

代码如下:

</>code

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4.     <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  5.     <title>365建站演示</title>
  6.     <style type="text/css">
  7.         #box {
  8.             width: 300px;
  9.             height: 300px;
  10.             background: #DDD;
  11.             position: relative;
  12.         }
  13.         
  14.         #child {
  15.             width: 150px;
  16.             height: 100px;
  17.             position: absolute;
  18.             top: 50%;
  19.             transform: translate(50%, -50%);
  20.             background: orange;
  21.             line-height: 100px;
  22.             text-align: center;
  23.         }
  24.     </style>
  25. </head>
  26. <body>
  27.     <div id="box">
  28.         <div id="child">
  29.             我是测试DIV
  30.         </div>
  31.     </div>
  32. </body>
  33. </html>

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

3.绝对定位结合margin:auto

</>code

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4.     <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  5.     <title>365建站演示</title>
  6.     <style type="text/css">
  7.         #box {
  8.             width: 300px;
  9.             height: 300px;
  10.             background: #DDD;
  11.             position: relative;
  12.         }
  13.         
  14.         #child {
  15.             width: 150px;
  16.             height: 100px;
  17.             position: absolute;
  18.             top: 0;
  19.             right: 0;
  20.             left: 0;
  21.             bottom: 0;
  22.             margin: auto;
  23.             background: orange;
  24.             line-height: 100px;
  25.             text-align: center;
  26.         }
  27.     </style>
  28. </head>
  29. <body>
  30.     <div id="box">
  31.         <div id="child">
  32.             我是测试DIV
  33.         </div>
  34.     </div>
  35. </body>
  36. </html>

这种方式的两个核心是:把要垂直居中的元素相对于父元素绝对定位,top和bottom设置为相等的值,我这里设置成0了,当然也可以设置为99999px或者-99999px,无论什么,只要两者相等就行。这一一步做完之后再将要居中的元素的margin设为auto,这样就可以实现垂直居中了。

被居中元素的宽度也可以不设置,但是不设置的话,就必须是图片这种自身就包含尺寸的元素,否则无法实现。

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

</>code

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4.     <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  5.     <title>365建站演示</title>
  6.     <style type="text/css">
  7.         #box {
  8.             width: 300px;
  9.             background: #DDD;
  10.             padding: 100px 0;
  11.         }       
  12.         #child {
  13.             width: 150px;
  14.             height: 100px;
  15.             background: orange;
  16.             line-height: 100px;
  17.             margin: 0 auto;
  18.             text-align: center;
  19.         }
  20.     </style>
  21. </head>
  22. <body>
  23.     <div id="box">
  24.         <div id="child">
  25.             我是测试DIV
  26.         </div>
  27.     </div>
  28. </body>
  29. </html>

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

这种方式看似没有什么技术含量,但其实在某种场景下也是非常好用的。

5.使用flex布局

</>code

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4.     <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  5.     <title>365建站演示</title>
  6.     <style type="text/css">
  7.         #box {
  8.             width: 300px;
  9.             height: 100px;
  10.             background: #DDD;
  11.             /*flex 布局*/
  12.             display: flex;
  13.             /*实现垂直居中*/
  14.             align-items: center;
  15.             /*实现水平居中*/
  16.             justify-content: center;
  17.         }
  18.     </style>
  19. </head>
  20. <body>
  21.     <div id="box">
  22.         <div id="child">
  23.             我是测试DIV
  24.         </div>
  25.     </div>
  26. </body>
  27. </html>

flex布局(弹性布局/伸缩布局)里门道颇多,这里先针对用到的东西简单说一下。

flex也就是flexible,意思为灵活的,柔韧的,易弯曲的。

元素可以通过设置display:flex;将其指定为flex布局的容器,指定好了容器之后再为其添加align-items属性,该属性定义项目在交叉轴(这里是纵向周)上的对齐方式,可能的取值有五种,分别如下:

flex-start:交叉轴的起点对齐;flex-end:交叉轴的重点对齐;

center:交叉轴的重点对齐;baseline项目第一行文字的基线对齐;

strech(该值是默认值):如果项目没有设置高度或者设置为auto,那么将占满整个容器的高度。

6.弹性布局

</>code

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4.     <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  5.     <title>365建站演示</title>
  6.     <style type="text/css">
  7.         #box {
  8.             width: 300px;
  9.             height: 300px;
  10.             background: #DDD;
  11.             /*flex 布局*/
  12.             display: flex;
  13.             /*实现垂直居中*/
  14.             align-items: center;
  15.             /*实现水平居中*/
  16.             justify-content: center;
  17.         }    
  18.         #child {
  19.             width: 300px;
  20.             height: 100px;
  21.             background: orange;
  22.             line-height: 100px;
  23.             text-align: center;
  24.         }
  25.     </style>
  26. </head>
  27. <body>
  28.     <div id="box">
  29.         <div id="child">
  30.             我是测试DIV
  31.         </div>
  32.     </div>
  33. </body>
  34. </html>

这种方式也是给父元素设置display:flex,设置好之后改变主轴的flex-direction:column,该属性可能的取值有四个,分别如下:

row(该值为默认值):主轴为水平方向,起点在左端;

row-reverse,主轴是水平方向,起点在有端;

column主轴为垂直方向,起点在上沿;

column-reverse:主轴为垂直方向,起点在下沿。

justify-content属性定义了项目在主轴上的对齐方式,可能取的值有五个,分别如下(不过具体的对齐方式与主轴的方向有关,以下的值都是假设主轴为从左到右的):

flex-staart(该值是默认值):左对齐;

flex-end:右对齐;

center:居中对齐;

space-between:两端对齐,各个项目之间的间隔均对齐;

space-around:各个项目两侧的间隔相等。

7.还有一种在前面已经见到过很多次的方式就是使用line-height对单行文本进行垂直居中

</>code

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4.     <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  5.     <title>365建站演示</title>
  6.     <style type="text/css">
  7.         #box {
  8.             width: 300px;
  9.             height: 300px;
  10.             background: #DDD;
  11.             /*flex 布局*/
  12.             display: flex;
  13.             flex-direction: column;
  14.             justify-content: center;
  15.         }
  16.         
  17.         #child {
  18.             width: 300px;
  19.             height: 100px;
  20.             background: orange;
  21.             line-height: 100px;
  22.             text-align: center;
  23.         }
  24.     </style>
  25. </head>
  26. <body>
  27.     <div id="box">
  28.         <div id="child">
  29.             我是测试DIV
  30.         </div>
  31.     </div>
  32. </body>
  33. </html>

这里有一个小坑需要大家注意:line-height(行高)的值不能设为100%;我们来看看官网文档中给出的关于line-height取值为百分比时候的描述:基于当前字体尺寸的百分比行间距,所以大家就明白了,如果是百分比是相对于字体尺寸来讲的。

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

</>code

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4.     <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  5.     <title>365建站演示</title>
  6.     <style type="text/css">
  7.         #box {
  8.             width: 300px;
  9.             height: 300px;
  10.             background: #DDD;
  11.             display: table;
  12.             text-align: center;
  13.         }
  14.         #child {
  15.             display: table-cell;
  16.             vertical-align: middle;
  17.         }
  18.     </style>
  19. </head>
  20. <body>
  21.     <div id="box">
  22.         <div id="child">
  23.             我是测试DIV
  24.         </div>
  25.     </div>
  26. </body>
  27. </html>

这里关于vertical-align啰嗦两句:vertical-align属性只对拥有valign特性的html元素起作用,例如表格元素中的<td><th>等等,而像<div><span>这样的元素是不行的。

valign属性规定单元格中内容的垂直排列方式,语法:<tdvalign="value">,value的可能取值有四种:

top:对内容进行上对齐

middle:对内容进行居中对齐

bottom:对内容进行下对齐

baseline:基线对齐

关于baseline值:基线是一条虚构的线。在一行文本中,大多数字母以基线为基准。baseline值设置行中的所有表格数据都分享相同的基线。该值的效果常常与bottom值相同。不过,如果文本的字号各不相同,那么baseline的效果会更好。

9.css3的box方法实现水平垂直居中

</>code

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4.     <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  5.     <title>365建站演示</title>
  6.     <style type="text/css">
  7.         #box {
  8.             width: 300px;
  9.             height: 200px;
  10.             padding: 10px;
  11.             border: 1px solid #ccc;
  12.             background: #DDD;
  13.             color: #fff;
  14.             margin: 20px auto;
  15.             display: -webkit-box;
  16.             -webkit-box-orient: horizontal;
  17.             -webkit-box-pack: center;
  18.             -webkit-box-align: center;
  19.             display: -moz-box;
  20.             -moz-box-orient: horizontal;
  21.             -moz-box-pack: center;
  22.             -moz-box-align: center;
  23.             display: -o-box;
  24.             -o-box-orient: horizontal;
  25.             -o-box-pack: center;
  26.             -o-box-align: center;
  27.             display: -ms-box;
  28.             -ms-box-orient: horizontal;
  29.             -ms-box-pack: center;
  30.             -ms-box-align: center;
  31.             display: box;
  32.             box-orient: horizontal;
  33.             box-pack: center;
  34.             box-align: center;
  35.         }
  36.     </style>
  37. </head>
  38. <body>
  39.     <div id="box">
  40.         <div id="child">
  41.             我是测试DIV
  42.         </div>
  43.     </div>
  44. </body>
  45. </html>

上面就是分享在html+css中实现文字垂直居中的方法,希望对你有帮助!


如对本文有疑问,请提交到交流论坛,广大热心网友会为你解答!! 点击进入论坛

发表评论 (1031人查看0条评论)
请自觉遵守互联网相关的政策法规,严禁发布色情、暴力、反动的言论。
昵称:
最新评论
------分隔线----------------------------

快速入口

· 365软件
· 杰创官网
· 建站工具
· 网站大全

其它栏目

· 建站教程
· 365学习

业务咨询

· 技术支持
· 服务时间:9:00-18:00
365建站网二维码

Powered by 365建站网 RSS地图 HTML地图

copyright © 2013-2024 版权所有 鄂ICP备17013400号