黑松山资源网 Design By www.paidiu.com
本文主要介绍了CSS未知高度垂直居中的实现,分享给大家,具体如下:
<!doctype html> <html lang="en"> <head> <meta charset="utf-8" /> <meta content="IE=8" http-equiv="X-UA-Compatible"/> <title> CSS垂直居中</title> <style type="text/css"> .container{ width:500px;/*装饰*/ height:500px; background:#B9D6FF; border: 1px solid #CCC; } </style> </head> <body> <h1>垂直居中(table)</h1> <div class='container'> <table width="100%" height="100%"> <tr> <td align="center" valign="middle"> <img src="http://images.cnblogs.com/cnblogs_com/rubylouvre/205314/r_iebug.jpg" /> </td> </tr> </table> </div> </body> </html>
好了,我们看其CSS实现。凡是table能做到的,CSS都能做的,但各浏览器在CSS的差异比较大,因此要兼容它们难度很大。这涉及许多细节,各种流啊,display的表现效果与CSS hack,IE早些年搞了大堆的私有属性,这也有待我们深一步挖掘。我们先看最简单的实现,背景图片法
背景图片法
<!doctype html> <html> <head> <title> CSS垂直居中</title> <style type="text/css"> .container { width:500px; height:500px; line-height:500px; background:#B9D6FF url(http://images.cnblogs.com/cnblogs_com/rubylouvre/205314/r_iebug.jpg) no-repeat center center; border:1px solid #f00; text-align: center; } </style> </head> <body> <h1>垂直居中</h1> <div class="container"> </div> </body> </html>
CSS表达式法
<html lang="en"> <head> <meta charset="utf-8" /> <meta content="IE=8" http-equiv="X-UA-Compatible"/> <title>司徒正美 CSS垂直居中</title> <style type="text/css"> .container{ /*IE8与标准游览器垂直对齐*/ display: table-cell; vertical-align:middle; width:500px;/*装饰*/ height:500px; background:#B9D6FF; border: 1px solid #CCC; } .container img{ display:block;/*让其具备盒子模型*/ margin:0 auto; text-align:center; margin-top:expression((500 - this.height )/2);/*让IE567垂直对齐 */ } </style> </head> <body> <h1>垂直居中(CSS表达式)</h1> <div class="container"> <img src="http://images.cnblogs.com/cnblogs_com/rubylouvre/205314/r_iebug.jpg" /> </div> </body> </html>
绝对定位法
<!doctype html> <html lang="en"> <head> <meta charset="utf-8" /> <meta content="IE=8" http-equiv="X-UA-Compatible"/> <title>司徒正美 CSS垂直居中</title> <style type="text/css"> div { /*IE8与标准游览器垂直对齐*/ display:table-cell; vertical-align:middle; overflow:hidden; position:relative; text-align:center; width:500px;/*装饰*/ height:500px; border:1px solid #ccc; background:#B9D6FF; } div p { +position:absolute; top:50% } img { +position:relative; top:-50%; left:-50%; } </style> </head> <body> <h1>垂直居中(绝对定位)</h1> <div class="container"> <p> <img src="http://images.cnblogs.com/cnblogs_com/rubylouvre/205314/r_iebug.jpg" /> </p> </div> </body> </html>
display:inline-block法
<!doctype html> <html lang="en"> <head> <meta charset="utf-8" /> <meta content="IE=8" http-equiv="X-UA-Compatible"/> <title>司徒正美 CSS垂直居中</title> <style type="text/css"> div { display:table-cell; vertical-align:middle; text-align:center; width:500px; height:500px; background:#B9D6FF; border: 1px solid #CCC; } </style> <!--[if IE]> <style type="text/css"> i { display:inline-block; height:100%; vertical-align:middle } img { vertical-align:middle } </style> <![endif]--> </head> <body> <h1>垂直居中(inline-block法)</h1> <div class="container"> <i></i> <img src="http://images.cnblogs.com/cnblogs_com/rubylouvre/205314/r_iebug.jpg" /> </div> </body> </html>
writing-mode法
<!doctype html> <html lang="en"> <head> <meta charset="utf-8" /> <meta content="IE=8" http-equiv="X-UA-Compatible"/> <title> CSS垂直居中</title> <style type="text/css"> div{ width:500px; height:500px; line-height:500px; text-align:center; background:#B9D6FF; border:1px solid #f00; } div span{ height:100%\9; writing-mode:tb-rl\9; } div img{ vertical-align:middle } </style> </head> <body> <h1>垂直居中(writing-mode法)</h1> <div class="container"> <span> <img src="http://images.cnblogs.com/cnblogs_com/rubylouvre/205314/r_iebug.jpg" /> </span> </div> </body> </html>
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
黑松山资源网 Design By www.paidiu.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
黑松山资源网 Design By www.paidiu.com
暂无评论...
更新日志
2024年09月20日
2024年09月20日
- 战锤40K星际战士2狙击职业介绍|狙击职业技能效果一览
- RobertSaxtonTheResurrectionoftheSoldiers(2024)[24Bit-192kHz]FLAC
- 唐成杨东哥弹唱《情若真HQ》头版限量编号[WAV+CUE]
- 柏菲·HIFI风情1《一杯香茶》限量开盘母带ORMCD[WAV+CUE]
- 发糖了!《黑神话》动画导演绘制八戒紫蛛儿中秋贺图
- “时光系列”新作《时光3:永耀之境》首曝:画风更真实的恋爱模拟RPG
- 《战地》游戏主管:始终认为《战地》未来销量能超越《使命召唤》
- 《伤感民谣 值得收藏的音乐极品 2CD》[WAV/分轨][1.2GB]
- 《邓紫棋 巨肺唱将小天后有心人 2CD》[WAV/分轨][1.2GB]
- 《迪克牛仔 坛奇精选 2CD台首版》 [WAV+CUE][600MB]
- 陈明.1998-为了爱影视歌曲+新歌【中唱】【WAV+CUE】
- 黄小琥.2009-简单/不简单【华纳】【WAV+CUE】
- 洪卓立.2010-TASTE.OF.LOVE新曲+精丫英皇娱乐】【WAV+CUE】
- 战锤40K星际战士2职业强度排行
- 交错战线PVE国家队成员以及常用角色推荐