简体   繁体   English

HTML / CSS –如何使用Firefox和Internet Explorer在包含的调整大小后的图像和父容器之间实现完美的尺寸匹配?

[英]HTML/CSS – How to get a perfect size match between a contained resized-image and a parent container with Firefox and Internet Explorer?

I have an issue with Firefox and IE in the code below : Everything is working extremely well with Chrome and Safari, but in Firefox and IE each image container does not respect the dimensions of its contained image. 我在下面的代码中遇到了Firefox和IE的问题:Chrome和Safari的运行情况都非常好,但是在Firefox和IE中,每个图像容器都不尊重其所包含图像的尺寸。 The image is resized proportionally with "height" command ; 使用“高度”命令按比例调整图像大小; that does work. 确实有效。 But IE and Firefox seem to take as a "width" value for the container of the image the original width of the jpeg file. 但是IE和Firefox似乎将jpeg文件的原始宽度作为图像容器的“宽度”值。 How could we get a perfect result as in Chrome and Safari (with the contained image and its parent-container getting the same width and height each other) ? 我们如何在Chrome和Safari中获得完美的结果(所包含的图像及其父容器的宽度和高度彼此相同)?

Here is the HTML code : 这是HTML代码:

<!-- Gallery -->
<div class="thegallery">
<div class="eachimage"><img src="images/image-1.jpg" /><div class="rollovertxt"><p>Caption</p></div><div class="rolloverbg"></div></div>
<div class="eachimage"><img src="images/image-2.jpg" /><div class="rollovertxt"><p>Caption</p></div><div class="rolloverbg"></div></div>
<div class="eachimage"><img src="images/image-3.jpg" /><div class="rollovertxt"><p>Caption</p></div><div class="rolloverbg"></div></div>
</div>

Here is the Css code : 这是Css代码:

.thegallery {
position:absolute;
top:6em; bottom:4em;
left:50%;
white-space:nowrap;
background-color:#36F;
z-index:1;
}

.eachimage {
position:relative;
height:100%; /* Full height of div.gallery */
display: inline-block;
margin-right:1em;
background-color:#63F;
}

.eachimage img {
height:100%; /* Full height of div.eachimage */
width:auto;
}

.eachimage:hover .rollovertxt { visibility:visible; }
.eachimage:hover .rolloverbg { visibility:visible; }

.rollovertxt {
position: absolute;
left:0.5em;
top:0.5em;
width:160px;
white-space:normal;
color:#FFF;
visibility:hidden;
z-index:3;
background-color:#F0F;
}

.rolloverbg {
position: absolute;
left:0;
top:0;
width:100%;
height:100%;
visibility:hidden;
z-index:2;
filter: alpha(opacity=50);
opacity: 0.5;
background-color:#000;  
}

Problem fixed. 问题已解决。 It seems the problem was coming from the fact Firefox and IE do not process correctly (as Chrome and Safari would do) a relative "height" of an element when you just give these two browsers an "em" top and bottom positioning of that element. 似乎问题出在以下事实:Firefox和IE无法正确处理(如Chrome和Safari会正确处理)某个元素的相对“高度”,而您只是向这两个浏览器赋予该元素的“ em”顶部和底部位置。 I fixed the problem by adding a calc() function (height = 100% - ((top-position expressed in "em")+(bottom-position expressed in "em"))). 我通过添加calc()函数解决了该问题(高度= 100%-((以“ em”表示的顶部位置)+(以“ em”表示的底部位置))))。

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

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