您好,登錄后才能下訂單哦!
這篇文章主要講解了“css實(shí)現(xiàn)任意圖片垂直居中的方法有哪些”,文中的講解內(nèi)容簡(jiǎn)單清晰,易于學(xué)習(xí)與理解,下面請(qǐng)大家跟著小編的思路慢慢深入,一起來(lái)研究和學(xué)習(xí)“css實(shí)現(xiàn)任意圖片垂直居中的方法有哪些”吧!
方法一:
將外部容器的顯示模式設(shè)置成display:table,這個(gè)設(shè)置的意思不用多說(shuō)了吧… img標(biāo)簽外部再嵌套一個(gè)span標(biāo)簽,并設(shè)置span的顯示模式為display:table-cell,這樣span內(nèi)部的內(nèi)容就相當(dāng)于表格,可以很方便的使用vertical-align屬性來(lái)對(duì)齊其中的內(nèi)容了。
代碼如下:
<html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=gb2312" /> <title>方法1 - 未知高度的圖片垂直居中 - www.cleanthem.com</title> <style type="text/css"> body { height:100%; } #box{ width:500px;height:400px; display:table; text-align:center; border:1px solid #d3d3d3;background:#fff; } #box span{ display:table-cell; vertical-align:middle; } #box img{ border:1px solid #ccc; } </style> <!--[if lte IE 7]> <style type="text/css">? #box{ position:relative; overflow:hidden; } #box span{ position:absolute; left:50%;top:50%; } #box img{ position:relative; left:-50%;top:-50%; } </style> <![endif]--> </head> <body> <div id="box"> <span><img src="images/demo_zl.png" alt="" /></span> </div> </body> </html>
演示地址
方法二:
標(biāo)準(zhǔn)瀏覽器的情況還是和上面一樣,不同的是針對(duì)IE6/IE7利用在img標(biāo)簽的前面插入一對(duì)空標(biāo)簽的辦法。
代碼如下:
<html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=gb2312" /> <title>方法2 - 未知高度的圖片垂直居中 - www.cleanthem.com</title> <style type="text/css"> body { height:100%; } #box{ width:500px;height:400px; display:table-cell; text-align:center; vertical-align:middle; border:1px solid #d3d3d3;background:#fff; } #box img{ border:1px solid #ccc; } </style> <!--[if IE]> <style type="text/css">? #box i { display:inline-block; height:100%; vertical-align:middle } #box img { vertical-align:middle } </style> <![endif]--> </head> <body> <div id="box"> <i></i><img src="images/demo_zl.png" alt="" /> </div> </body> </html>
演示地址
方法三:
在img標(biāo)簽外包裹一個(gè)p標(biāo)簽,標(biāo)準(zhǔn)瀏覽器利用p標(biāo)簽的偽類屬性:before來(lái)實(shí)現(xiàn)居中,另外,對(duì)于IE6/IE7使用了CSS表達(dá)式來(lái)實(shí)現(xiàn)兼容。
代碼如下:
<html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=gb2312" /> <title>方法3 - 未知高度的圖片垂直居中 - www.cleanthem.com</title> <style type="text/css"> body { height:100%; } #box{ width:500px;height:400px; text-align:center; border:1px solid #d3d3d3;background:#fff; } #box p{ width:500px;height:400px; line-height:400px; /* 行高等于高度 */ } /* 兼容標(biāo)準(zhǔn)瀏覽器 */ #box p:before{ content:"."; /* 具體的值與垂直居中無(wú)關(guān),盡可能的節(jié)省字符 */ margin-left:-5px; font-size:10px; /* 修復(fù)居中的小BUG */ visibility:hidden; /*設(shè)置成隱藏元素*/ } #box p img{ *margin-top:expression((400 - this.height )/2); /* CSS表達(dá)式用來(lái)兼容IE6/IE7 */ vertical-align:middle; border:1px solid #ccc; } </style> </head> <body> <div id="box"> <p><img src="images/demo_zl.png" alt="" /></p> </div> </body> </html>
感謝各位的閱讀,以上就是“css實(shí)現(xiàn)任意圖片垂直居中的方法有哪些”的內(nèi)容了,經(jīng)過(guò)本文的學(xué)習(xí)后,相信大家對(duì)css實(shí)現(xiàn)任意圖片垂直居中的方法有哪些這一問題有了更深刻的體會(huì),具體使用情況還需要大家實(shí)踐驗(yàn)證。這里是億速云,小編將為大家推送更多相關(guān)知識(shí)點(diǎn)的文章,歡迎關(guān)注!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。