经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » HTML/CSS » CSS » 查看文章
CSS未知高度垂直居中的实现_CSS教程_CSS
来源:jb51  时间:2018/11/9 17:18:44  对本文有异议

本文主要介绍了CSS未知高度垂直居中的实现,分享给大家,具体如下:

  1. <!doctype html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="utf-8" />
  5. <meta content="IE=8" http-equiv="X-UA-Compatible"/>
  6. <title> CSS垂直居中</title>
  7. <style type="text/css">
  8. .container{
  9. width:500px;/*装饰*/
  10. height:500px;
  11. background:#B9D6FF;
  12. border: 1px solid #CCC;
  13. }
  14. </style>
  15. </head>
  16. <body>
  17. <h1>垂直居中(table)</h1>
  18. <div class='container'>
  19. <table width="100%" height="100%">
  20. <tr>
  21. <td align="center" valign="middle">
  22. <img src="http://images.cnblogs.com/cnblogs_com/rubylouvre/205314/r_iebug.jpg" />
  23. </td>
  24. </tr>
  25. </table>
  26. </div>
  27. </body>
  28. </html>

好了,我们看其CSS实现。凡是table能做到的,CSS都能做的,但各浏览器在CSS的差异比较大,因此要兼容它们难度很大。这涉及许多细节,各种流啊,display的表现效果与CSS hack,IE早些年搞了大堆的私有属性,这也有待我们深一步挖掘。我们先看最简单的实现,背景图片法

背景图片法

  1. <!doctype html>
  2. <html>
  3. <head>
  4. <title> CSS垂直居中</title>
  5. <style type="text/css">
  6. .container {
  7. width:500px;
  8. height:500px;
  9. line-height:500px;
  10. background:#B9D6FF url(http://images.cnblogs.com/cnblogs_com/rubylouvre/205314/r_iebug.jpg) no-repeat center center;
  11. border:1px solid #f00;
  12. text-align: center;
  13. }
  14. </style>
  15. </head>
  16. <body>
  17. <h1>垂直居中</h1>
  18. <div class="container">
  19. </div>
  20. </body>
  21. </html>

CSS表达式法

  1. <html lang="en">
  2. <head>
  3. <meta charset="utf-8" />
  4. <meta content="IE=8" http-equiv="X-UA-Compatible"/>
  5. <title>司徒正美 CSS垂直居中</title>
  6. <style type="text/css">
  7. .container{
  8. /*IE8与标准游览器垂直对齐*/
  9. display: table-cell;
  10. vertical-align:middle;
  11. width:500px;/*装饰*/
  12. height:500px;
  13. background:#B9D6FF;
  14. border: 1px solid #CCC;
  15. }
  16. .container img{
  17. display:block;/*让其具备盒子模型*/
  18. margin:0 auto;
  19. text-align:center;
  20. margin-top:expression((500 - this.height )/2);/*让IE567垂直对齐 */
  21. }
  22. </style>
  23. </head>
  24. <body>
  25. <h1>垂直居中(CSS表达式)</h1>
  26. <div class="container">
  27. <img src="http://images.cnblogs.com/cnblogs_com/rubylouvre/205314/r_iebug.jpg" />
  28. </div>
  29. </body>
  30. </html>

绝对定位法

  1. <!doctype html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="utf-8" />
  5. <meta content="IE=8" http-equiv="X-UA-Compatible"/>
  6. <title>司徒正美 CSS垂直居中</title>
  7. <style type="text/css">
  8. div {
  9. /*IE8与标准游览器垂直对齐*/
  10. display:table-cell;
  11. vertical-align:middle;
  12. overflow:hidden;
  13. position:relative;
  14. text-align:center;
  15. width:500px;/*装饰*/
  16. height:500px;
  17. border:1px solid #ccc;
  18. background:#B9D6FF;
  19. }
  20. div p {
  21. +position:absolute;
  22. top:50%
  23. }
  24. img {
  25. +position:relative;
  26. top:-50%;
  27. left:-50%;
  28. }
  29. </style>
  30. </head>
  31. <body>
  32. <h1>垂直居中(绝对定位)</h1>
  33. <div class="container">
  34. <p>
  35. <img src="http://images.cnblogs.com/cnblogs_com/rubylouvre/205314/r_iebug.jpg" />
  36. </p>
  37. </div>
  38. </body>
  39. </html>

display:inline-block法

  1. <!doctype html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="utf-8" />
  5. <meta content="IE=8" http-equiv="X-UA-Compatible"/>
  6. <title>司徒正美 CSS垂直居中</title>
  7. <style type="text/css">
  8. div {
  9. display:table-cell;
  10. vertical-align:middle;
  11. text-align:center;
  12. width:500px;
  13. height:500px;
  14. background:#B9D6FF;
  15. border: 1px solid #CCC;
  16. }
  17. </style>
  18. <!--[if IE]>
  19. <style type="text/css">
  20. i {
  21. display:inline-block;
  22. height:100%;
  23. vertical-align:middle
  24. }
  25. img {
  26. vertical-align:middle
  27. }
  28. </style>
  29. <![endif]-->
  30. </head>
  31. <body>
  32. <h1>垂直居中(inline-block法)</h1>
  33. <div class="container">
  34. <i></i>
  35. <img src="http://images.cnblogs.com/cnblogs_com/rubylouvre/205314/r_iebug.jpg" />
  36. </div>
  37. </body>
  38. </html>

writing-mode法

  1. <!doctype html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="utf-8" />
  5. <meta content="IE=8" http-equiv="X-UA-Compatible"/>
  6. <title> CSS垂直居中</title>
  7. <style type="text/css">
  8. div{
  9. width:500px;
  10. height:500px;
  11. line-height:500px;
  12. text-align:center;
  13. background:#B9D6FF;
  14. border:1px solid #f00;
  15. }
  16. div span{
  17. height:100%\9;
  18. writing-mode:tb-rl\9;
  19. }
  20. div img{
  21. vertical-align:middle
  22. }
  23. </style>
  24. </head>
  25. <body>
  26. <h1>垂直居中(writing-mode法)</h1>
  27. <div class="container">
  28. <span>
  29. <img src="http://images.cnblogs.com/cnblogs_com/rubylouvre/205314/r_iebug.jpg" />
  30. </span>
  31. </div>
  32. </body>
  33. </html>

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持w3xue。

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号