自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

實現(xiàn)任意圖片垂直居中的三種方法

開發(fā) 前端
在網(wǎng)站開發(fā)過程中,可能會有希望圖片垂直居中的情況,而且,需要垂直居中的圖片的高度也不確定,這就會給頁面的布局帶來一定的挑戰(zhàn)。

在網(wǎng)站開發(fā)過程中,可能會有希望圖片垂直居中的情況,而且,需要垂直居中的圖片的高度也不確定,這就會給頁面的布局帶來一定的挑戰(zhàn)。我總結(jié)了一下,曾經(jīng)使用過的幾種方法來使圖片垂直居中,除了***種方法只限于標準瀏覽器外,另外兩種方法的兼容性還不錯。

方法一:

將外部容器的顯示模式設(shè)置成display:table,這個設(shè)置的意思不用多說了吧… img標簽外部再嵌套一個span標簽,并設(shè)置span的顯示模式為display:table-cell,這樣span內(nèi)部的內(nèi)容就相當于表格,可以很方便的使用vertical-align屬性來對齊其中的內(nèi)容了。

 

 

代碼如下:

  1. <html xmlns="http://www.w3.org/1999/xhtml">  
  2. <head>  
  3.     <meta http-equiv="Content-Type" content="text/html; charset=gb2312" />  
  4.     <title>方法1 - 未知高度的圖片垂直居中 - www.cleanthem.com</title>  
  5. <style type="text/css">  
  6. body {  
  7.     height:100%;  
  8. }  
  9. #box{  
  10.     width:500px;height:400px;  
  11.     display:table;  
  12.     text-align:center;  
  13.     border:1px solid #d3d3d3;background:#fff;  
  14. }  
  15. #box span{  
  16.     display:table-cell;  
  17.     vertical-align:middle;  
  18. }  
  19. #box img{  
  20.     border:1px solid #ccc;  
  21. }  
  22. </style>  
  23. <!--[if lte IE 7]>  
  24. <style type="text/css">?  
  25. #box{  
  26.     position:relative;  
  27.     overflow:hidden;  
  28. }  
  29. #box span{  
  30.     position:absolute;  
  31.     left:50%;top:50%;  
  32. }  
  33. #box img{  
  34.     position:relative;  
  35.     left:-50%;top:-50%;  
  36. }  
  37. </style>  
  38. <![endif]-->  
  39. </head>  
  40. <body>  
  41. <div id="box">  
  42.     <span><img src="images/demo_zl.png" alt="" /></span>  
  43. </div>  
  44. </body>  
  45. </html> 

演示地址

#p#

方法二:

標準瀏覽器的情況還是和上面一樣,不同的是針對IE6/IE7利用在img標簽的前面插入一對空標簽的辦法。

 

代碼如下:

  1. <html xmlns="http://www.w3.org/1999/xhtml"> 
  2. <head> 
  3.     <meta http-equiv="Content-Type" content="text/html; charset=gb2312" /> 
  4.     <title>方法2 - 未知高度的圖片垂直居中 - www.cleanthem.com</title> 
  5.  
  6. <style type="text/css"> 
  7. body {  
  8.     height:100%;  
  9. }  
  10. #box{  
  11. width:500px;height:400px;  
  12. display:table-cell;  
  13. text-align:center;  
  14. vertical-align:middle;  
  15. border:1px solid #d3d3d3;background:#fff;  
  16. }  
  17. #box img{  
  18. border:1px solid #ccc;  
  19. }  
  20. </style> 
  21. <!--[if IE]> 
  22. <style type="text/css">?  
  23. #box i {  
  24.     display:inline-block;  
  25.     height:100%;  
  26.     vertical-align:middle  
  27.     }  
  28. #box img {  
  29.     vertical-align:middle  
  30.     }  
  31. </style> 
  32. <![endif]--> 
  33. </head> 
  34. <body> 
  35. <div id="box"> 
  36.     <i></i><img src="images/demo_zl.png" alt="" /> 
  37. </div> 
  38.  
  39. </body> 
  40. </html> 

演示地址

#p#

方法三:

在img標簽外包裹一個p標簽,標準瀏覽器利用p標簽的偽類屬性:before來實現(xiàn)居中,另外,對于IE6/IE7使用了CSS表達式來實現(xiàn)兼容。

代碼如下:

  1. <html xmlns="http://www.w3.org/1999/xhtml"> 
  2. <head> 
  3.     <meta http-equiv="Content-Type" content="text/html; charset=gb2312" /> 
  4.     <title>方法3 - 未知高度的圖片垂直居中 - www.cleanthem.com</title> 
  5.  
  6. <style type="text/css"> 
  7. body {  
  8.     height:100%;  
  9. }  
  10. #box{  
  11.     width:500px;height:400px;  
  12.     text-align:center;  
  13.     border:1px solid #d3d3d3;background:#fff;  
  14. }  
  15. #box p{  
  16.     width:500px;height:400px;  
  17.     line-height:400px;  /* 行高等于高度 */  
  18. }  
  19.  
  20. /* 兼容標準瀏覽器 */  
  21. #box p:before{  
  22.     content:".";  /* 具體的值與垂直居中無關(guān),盡可能的節(jié)省字符 */  
  23.     margin-left:-5px; font-size:10px;  /* 修復(fù)居中的小BUG */  
  24.     visibility:hidden;  /*設(shè)置成隱藏元素*/  
  25. }  
  26.  
  27. #box p img{  
  28.     *margin-top:expression((400 - this.height )/2);  /* CSS表達式用來兼容IE6/IE7 */  
  29.     vertical-align:middle;  
  30.     border:1px solid #ccc;  
  31. }  
  32. </style> 
  33. </head> 
  34. <body> 
  35. <div id="box"> 
  36.     <p><img src="images/demo_zl.png" alt="" /></p> 
  37. </div> 
  38. </body> 
  39. </html> 

演示地址

 

原文鏈接:http://www.cnblogs.com/cnliu/archive/2012/06/20/image-center.html

責任編輯:張偉 來源: CNL的博客
相關(guān)推薦

2010-08-24 14:47:48

CSS居中

2010-08-27 10:30:16

CSS垂直居中

2013-01-04 15:47:54

Android開發(fā)平鋪UI設(shè)計

2021-07-13 12:31:27

IT組織改進首席技術(shù)官

2009-07-08 12:56:32

編寫Servlet

2011-06-10 10:43:12

Ubuntu應(yīng)用安裝

2009-06-23 10:45:18

Hibernate支持

2010-09-10 08:54:52

DIV居中

2015-12-11 09:24:38

加密數(shù)據(jù)Linux

2010-09-06 10:04:31

CSS樣式表

2009-12-11 18:49:39

預(yù)算編制博科資訊

2022-07-13 16:06:16

Python參數(shù)代碼

2024-11-15 07:00:00

Python發(fā)送郵件

2011-04-18 15:32:45

游戲測試測試方法軟件測試

2023-08-14 17:58:13

RequestHTTP請求

2010-09-14 15:10:49

CSS注釋

2010-08-26 16:19:41

DIV圓角

2020-06-17 10:52:00

DDoS攻擊網(wǎng)絡(luò)攻擊網(wǎng)絡(luò)安全

2016-10-12 13:53:38

JavaByteBufferRandomAcces

2010-09-08 13:29:48

CSS
點贊
收藏

51CTO技術(shù)棧公眾號