简体   繁体   English

Chrome和Safari中表格内图片高度错误100%

[英]Image 100% height error inside table in Chrome and Safari

I tried pretty hard to find the answer to this on here, on google, and elsewhere, but it seems pretty obscure. 我在这里,谷歌和其他地方都非常努力地找到答案,但这似乎很模糊。 I had to do some fancy CSS in order to create a box with a specific aspect ratio inside which a thumbnail would be centered vertically and horizontally. 为了创建一个具有特定长宽比的框,其中的缩略图将在垂直和水平方向居中,我不得不做一些花哨的CSS。 Those are straight-forward ideas that are actually somewhat complicated to implement in CSS. 这些都是直截了当的想法,实际上在CSS中实现起来有些复杂。 My solution works great everywhere except inside a table in Chrome with an image that has dimensions larger than the container. 我的解决方案在所有地方都适用,除了Chrome的表格内部的图像尺寸大于容器的尺寸。

Here is code that demonstrates the issue: 这是演示问题的代码:

 /* 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> 

Hopefully as you can see, in Chrome (I'm using 43.0.2357.132 64-bit) and Safari (8.0.7) the tall/large image is exceeding the boundaries of its parent and its height is being set to the natural height of the image. 希望如您所见,在Chrome(我使用的是64位43.0.2357.132)和Safari(8.0.7)中,高大图像超出了其父级的边界,并且其高度已设置为图片。 The wide images all work as expected, so this appears to only be an issue of height. 宽图像均按预期工作,因此这似乎只是高度问题。

My question: What is a simple or straight-forward way to fix this issue in Chrome and Safari? 我的问题:在Chrome和Safari中解决此问题的简单或直接方法是什么? Or is it a bug and should I look for a less-than-ideal work-around that makes it look less terrible? 还是它是一个错误,我是否应该寻找一种不太理想的解决方法,使其看起来不那么糟糕? What is causing this issue? 是什么导致此问题?

Thanks! 谢谢!

Why do you need .fill-container to have absolute positioning? 为什么需要.fill-container进行绝对定位? If I remove the lines below from styles then everything looks fine in Chrome (I can't test in Safari): 如果我从样式中删除以下行,则在Chrome中一切正常(我无法在Safari中测试):

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

You also haven't closed img tags, you have 您还没有关闭img标签,

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

instead of (notice /> at the end of line) 而不是(行尾的通知/>)

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

FYI, on smaller screens (screenwidth < 650px), your first image inside the table breaks as well. 仅供参考,在较小的屏幕(屏幕宽度<650像素)上,表格内的第一张图像也会损坏。

To fix it, change your img to use the absolute positioning centering trick : 要解决此问题,请更改您的img以使用绝对定位居中技巧

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

This also means you don't need the image-background::before declaration. 这也意味着您image-background::before声明image-background::before不需要image-background::before :::。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM