line-height对单行文本进行垂直居中.html 819 B

123456789101112131415161718192021222324252627
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6. <title>使用 line-height 对单行文本进行垂直居中</title>
  7. <style>
  8. #box {
  9. width: 300px;
  10. height: 300px;
  11. background: #ddd;
  12. line-height: 300px;
  13. }
  14. </style>
  15. </head>
  16. <body>
  17. <h1>使用 line-height 对单行文本进行垂直居中</h1>
  18. <p>
  19. 要注意的是,line-height (行高) 的值不能设为 100%,我们来看看官方文档中给出的关于 line-height 取值为百分比时候的描述:“基于当前字体尺寸的百分比行间距”。也就是说,这里的百分比并不是相对于容器元素尺寸而言的,而是相对于字体尺寸。
  20. </p>
  21. <div id="box">test vertical align</div>
  22. </body>
  23. </html>