提交酷站 !!
登陆
注册
首页
酷站
设计
代码
素材
美图
论坛
您的位置:
首页
-
代码
-
CSS教程
- 正文
如何让图片在容器里垂直居中?
作者:未知 文章来源:互联网 发布时间:2007-01-04 20:28:38
我们会经常遇到这样的问题,有很多人为图片在容器里垂直居中这个问题而迷惑。参考了网上的资料,如有不全面,请大家指出!下面我们就来解析这个问题。
第一种图片垂直居中的方法:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>www.resday.com</title> <style type="text/css"> <!-- * {margin:0;padding:0} div { width:500px; height:500px; border:1px solid #c00; overflow:hidden; position:relative; display:table-cell; text-align:center; vertical-align:middle } div p { position:static; +position:absolute; top:50% } img { position:static; +position:relative; top:-50%;left:-50%; width:200px; height:90px } --> </style> </head> <body> <div><p><img src="http://www.resday.com/templates/resday/images/logo.jpg" /></p></div> </body> </html>
提示:您可以先修改部分代码再运行
标准浏览器下另类方法:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>www.resday.com</title> <style type="text/css"> <!-- body { margin:0;padding:0 } div { width:500px; height:500px; line-height:500px; border:1px solid #ccc; overflow:hidden; position:relative; text-align:center; } div p { position:static; +position:absolute; top:50% } img { position:static; +position:relative; top:-50%;left:-50%; width:200px; height:90px; vertical-align:middle } p:after { content:".";font-size:1px; visibility:hidden } --> </style> </head> <body> <div><p><img src="http://www.resday.com/templates/resday/images/logo.jpg" /></p></div> </body> </html>
提示:您可以先修改部分代码再运行
标准浏览器严格型申明下的方法:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>www.resday.com</title> <style type="text/css"> <!-- * {margin:0;padding:0} div { width:500px; height:500px; line-height:500px; border:1px solid #ccc; overflow:hidden; position:relative; text-align:center; } div p { position:static; +position:absolute; top:50%; vertical-align:middle } img { position:static; +position:relative; top:-50%;left:-50%; width:200px; height:90px; vertical-align:middle } --> </style> </head> <body> <div><p><img src="http://www.resday.com/templates/resday/images/logo.jpg" /></p></div> </body> </html>
提示:您可以先修改部分代码再运行
最简单当然是背景图片的方法:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>www.resday.com</title> <style type="text/css"> <!-- * {margin:0;padding:0;} div { width:500px;border:1px solid #c00; height:500px; background:url("http://www.resday.com/templates/resday/images/logo.jpg") center no-repeat } --> </style> </head> <body> <div></div> </body> </html>
提示:您可以先修改部分代码再运行
Web标准
CSS教程
XHtml教程
CSS布局实例
热门文章
为何要遗弃HTML,执...
为什么要Web标准化,...
有人说div+CSS网页布...
三列式网页布局如何...
如何将链接的下划线...
Div+CSS网页布局:用cl...
HTML4.0与XHTML1.0真正的...
CSS标识当前位置页效...
DIV CSS列形导航一例...
一定要学CSS吗?有什...
最新文章
DIV CSS网页布局:HTML...
DIV CSS网页布局:实...
DIV CSS网页布局:最...
DIV CSS网页布局常用...
DIV CSS网页布局:三...
DIV CSS网页布局:用CS...
左中右3栏布局中最...
CSS floats来创建三栏...
页脚最小高度100%保...
解决列高度自适应的...