2015-07-10 57 views
0

我很努力地在這裏,谷歌和其他地方找到這個答案,但它似乎很隱晦。我不得不做一些花哨的CSS來創建一個具有特定長寬比的框,其中縮略圖將垂直和水平居中。這些是直接的想法,在CSS中實現起來實際上有些複雜。我的解決方案在任何地方都能很好地工作,除了在Chrome中的表格中放置尺寸大於容器的圖片。圖片在Chrome和Safari中100%的表內高度錯誤

這裏是代碼,演示了這個問題:

/* 
 
sets aspect ratio of container by adding padding that is calculated 
 
according to width of its parent element 
 
*/ 
 
.aspect-ratio { 
 
    width: 100%; 
 
    display: inline-block; 
 
    position: relative; 
 
} 
 

 
.aspect-ratio:after { 
 
    padding-top: 76.19%; 
 
    display: block; 
 
    content: ''; 
 
} 
 

 
/* 
 
parent has no height, this fills the container's space 
 
*/ 
 
.fill-container { 
 
    position: absolute; 
 
    top: 0; 
 
    bottom: 0; 
 
    right: 0; 
 
    left: 0; 
 
    font-size: 0; 
 
} 
 

 
/* 
 
centers image horizontally and vertically 
 
background color 
 
*/ 
 
.image-background { 
 
    text-align: center; 
 
    background-color: #444; 
 
    height: 100%; 
 
    width: 100%; 
 
} 
 

 
.image-background::before { 
 
    content: ''; 
 
    display: inline-block; 
 
    height: 100%; 
 
    vertical-align: middle; 
 
    margin-right: -0.25em; 
 
} 
 

 
img { 
 
    display: inline-block; 
 
    padding: 5px; 
 
    box-sizing: border-box; 
 
    vertical-align: middle; 
 
} 
 

 
/* 
 
Firefox: image height fills the parent element 
 
Chrome: 
 
inside table - image is rendered at its natural height 
 
outside table - image height fills the parent element as expected 
 
*/ 
 
.fill-height { 
 
    height: 100%; 
 
} 
 

 
.fill-width { 
 
    width: 100%; 
 
} 
 

 
/* other styles */ 
 
h1, h2, h3 { 
 
    text-align: center; 
 
} 
 

 
table { 
 
    width: 100%; 
 
} 
 

 
.thumbnail-viewer { 
 
    width: 40%; 
 
    margin: 10px auto; 
 
}
<h1>tall image</h1> 
 
<h2>small</h2> 
 
<h3>table</h3> 
 
<table> 
 
    <tbody> 
 
    <tr> 
 
     <td> 
 
     <div class="thumbnail-viewer"> 
 
      <div class="aspect-ratio"> 
 
      <div class="fill-container"> 
 
       <div class="image-background"> 
 
       <img class="fill-height" src="http://www.irmaagro.com/images/d.jpg" style="height: 100%;"> 
 
       </div> 
 
      </div> 
 
      </div> 
 
     </div> 
 
     </td> 
 
    </tr> 
 
    </tbody> 
 
</table> 
 
<h3>no table</h3> 
 
<div class="thumbnail-viewer"> 
 
    <div class="aspect-ratio"> 
 
    <div class="fill-container"> 
 
     <div class="image-background"> 
 
     <img class="fill-height" src="http://www.irmaagro.com/images/d.jpg" style="height: 100%;"> 
 
     </div> 
 
    </div> 
 
    </div> 
 
</div> 
 

 
<h2>large</h2> 
 
<h3>table (works in firefox and IE, breaks in chrome and safari)</h3> 
 
<table> 
 
    <tbody> 
 
    <tr> 
 
     <td> 
 
     <div class="thumbnail-viewer"> 
 
      <div class="aspect-ratio"> 
 
      <div class="fill-container"> 
 
       <div class="image-background"> 
 
       <img class="fill-height" src="http://www.landscapeontario.com/thumbnailer.php?image=/assets/1320240573.Twine_wrap_2.JPG&imgWH=500" style="height: 100%;"> 
 
       </div> 
 
      </div> 
 
      </div> 
 
     </div> 
 
     </td> 
 
    </tr> 
 
    </tbody> 
 
</table> 
 
<h3>no table</h3> 
 
<div class="thumbnail-viewer"> 
 
    <div class="aspect-ratio"> 
 
    <div class="fill-container"> 
 
     <div class="image-background"> 
 
     <img class="fill-height" src="http://www.landscapeontario.com/thumbnailer.php?image=/assets/1320240573.Twine_wrap_2.JPG&imgWH=500" style="height: 100%;"> 
 
     </div> 
 
    </div> 
 
    </div> 
 
</div> 
 

 
<h1>wide image</h1> 
 
<h2>small</h2> 
 
<h3>table</h3> 
 
<table> 
 
    <tbody> 
 
    <tr> 
 
     <td> 
 
     <div class="thumbnail-viewer"> 
 
      <div class="aspect-ratio"> 
 
      <div class="fill-container"> 
 
       <div class="image-background"> 
 
       <img class="fill-width" src="http://www.beach.com/images/activity-photo-county-londonderry-ireland-3-day-lake-district-and-hadrian-s-wall-small-group-tour-from-edinburgh-1.jpg"> 
 
       </div> 
 
      </div> 
 
      </div> 
 
     </div> 
 
     </td> 
 
    </tr> 
 
    </tbody> 
 
</table> 
 
<h3>no table</h3> 
 
<div class="thumbnail-viewer"> 
 
    <div class="aspect-ratio"> 
 
    <div class="fill-container"> 
 
     <div class="image-background"> 
 
     <img class="fill-width" src="http://www.beach.com/images/activity-photo-county-londonderry-ireland-3-day-lake-district-and-hadrian-s-wall-small-group-tour-from-edinburgh-1.jpg"> 
 
     </div> 
 
    </div> 
 
    </div> 
 
</div> 
 

 
<h2>large</h2> 
 
<h3>table</h3> 
 
<table> 
 
    <tbody> 
 
    <tr> 
 
     <td> 
 
     <div class="thumbnail-viewer"> 
 
      <div class="aspect-ratio"> 
 
      <div class="fill-container"> 
 
       <div class="image-background"> 
 
       <img class="fill-width" src="http://www.craterlaketrust.org/pub/photo/thumb/Crater-Summer_cropto_500x200.JPG"> 
 
       </div> 
 
      </div> 
 
      </div> 
 
     </div> 
 
     </td> 
 
    </tr> 
 
    </tbody> 
 
</table> 
 
<h3>no table</h3> 
 
<div class="thumbnail-viewer"> 
 
    <div class="aspect-ratio"> 
 
    <div class="fill-container"> 
 
     <div class="image-background"> 
 
     <img class="fill-width" src="http://www.craterlaketrust.org/pub/photo/thumb/Crater-Summer_cropto_500x200.JPG"> 
 
     </div> 
 
    </div> 
 
    </div> 
 
</div>

希望你可以看到,在Chrome(我使用43.0.2357.132 64位)和Safari(8.0。 7)高/大圖像超出了其父圖像的邊界,並且其高度被設置爲圖像的自然高度。廣泛的圖像都按預期工作,所以這似乎只是一個高度問題。

我的問題:在Chrome和Safari中解決這個問題的簡單或直接的方法是什麼?或者它是一個錯誤,我應該尋找一種不太理想的解決方法,使它看起來不那麼糟糕?什麼導致這個問題?

謝謝!

回答

0

FYI,在小屏幕上(屏幕寬度< 650像素),表內斷裂的第一圖像,以及。

要修復它,改變你的img使用絕對定位centering trick

img { 
    padding: 5px; 
    box-sizing: border-box; 
    position: absolute; 
    left: 50%; 
    top: 50%; 
    transform: translate(-50%, -50%); 
} 

這也意味着你不需要image-background::before聲明。

+0

真棒,它看起來像你的中心技巧是比我使用的技巧更符合我的代碼。 –

0

爲什麼你需要.fill-container來擁有絕對定位?如果我從風格刪除下面的線,然後一切都在Chrome看起來不錯(我不能在Safari瀏覽器測試):

.fill-container { 
    position: absolute; 
    top: 0; 
    bottom: 0; 
    right: 0; 
    left: 0; 
    font-size: 0; 
} 

您還沒有關閉的img標籤,你有

<img class="fill-height" src="http://www.irmaagro.com/images/d.jpg" style="height: 100%;"> 

代替的(通知/>在一行的末尾)

<img class="fill-height" src="http://www.irmaagro.com/images/d.jpg" style="height: 100%;" /> 
+0

感謝您的回答。這些樣式的原因是,包含盒響應地保持其寬高比並限制任何尺寸的圖像的高度和寬度。我發現代碼來完成[這裏](http://stackoverflow.com/a/12121309/5104089)。 –