一、前言:


 垂直居中有很多方式,我们要做的不是写出完美代码,而是在合适的情况下根据需求选择合适方式。

主要方式:

  1. line-height
  2. 绝对定位
  3. 表格 display:table-cell

主要需求:

  1. 固定宽高
  2. 不固定宽高

主要兼容:

  1. ie8+  主流浏览器
  2. ie6,7

二、行高


1. 利用行高与高度相同,实现单行文本居中

缺点:只能是单行文本

 1 <!DOCTYPE html>
 2 <html>
 3 <head>
 4     <title></title>
 5     <style type="text/css">
 6     .d1{width: 200px;height: 200px;background-color: blue;line-height: 200px;}
 7     </style>
 8 </head>
 9 <body>
10 <div class="d1">
11     fdsfdsfdsfdfsfds
12 </div>
13 </body>
14 </html>
View Code

相关文章:

  • 2021-08-31
  • 2022-12-23
  • 2021-11-27
  • 2021-12-05
  • 2021-12-13
  • 2021-11-23
猜你喜欢
  • 2021-12-14
  • 2022-01-16
  • 2022-12-23
  • 2022-01-09
  • 2022-01-12
  • 2021-12-03
  • 2021-10-04
相关资源
相似解决方案