html - 如何防止在调整大小时更改父级的宽度

标签 html css image resize

我有一个问题:我在 div 中有一张图片。图像比 div 大,并且它具有 height:100% 以使其看起来不错。因此,当我调整大小时,图像会变大并且看起来不错。但是当我调整浏览器的大小时使它变小图像变小,但它的父级保存了原始图像的宽度。事实上,它只需要图像的宽度。我为您准备了一个 fiddle ,只需尝试调整浏览器或输出部分的大小即可看到红色背景出现。我很好奇是否有机会使 div 尺寸与图像的动态尺寸相同。我需要容器尺寸,因为除了图像之外我还有一些其他元素,它们使用 div 的坐标。谢谢。

重要!它的工作方式与我只在 FireFox 中看到的一样。 Chrome 的行为有所不同。

.img-wrapper {
  display: inline-block;
  height: 100%;
  position: relative;

    background-color: red;
}

.gallery-image {
  bottom: 90px;
  left: 0;
  position: absolute;
  right: 0;
  text-align: center;
  top: 25px;
    background-color: grey;
}

img {
    height: 100%;
}
<div class="gallery-image">
    <div class="img-wrapper">
        <img src="http://www.ibm.com/big-data/us/en/images/bigdata_homepage_maininfographic_345x194.jpg" alt=""/>
</div>
</div>
    
	    

最佳答案

这通常是通过使用 background-image:url("http://www.ibm.com/big-data/us/en/images/bigdata_homepage_maininfographic_345x194.jpg ") 的 CSS 完成的。这样您的图像和 div 就成为一个对象。然后你只需相应地控制 div 和背景图像的大小。 旁注...它也有助于提高性能。

关于html - 如何防止在调整大小时更改父级的宽度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27638760/

相关文章:

html - 在与另一个 div 内联的 div 中设置内容对齐方式

javascript - Uncaught ReferenceError : TweenLite is not defined

ruby-on-rails - 如何将图像字段添加到 Rails 应用程序中?

javascript - 使用 AngularJS 将正确编码的(base64?)图像上传到 SharePoint

c++ - 如何计算 OpenCV 中图像中的提示数?

jquery - 来自 jquery-ui 的菜单

html - 如果 Fixed div 溢出,则设置页面滚动以显示内容

html - 无法将行高设置为等于表格高度

html - 如何使此菜单仅以高分辨率行显示?

css - 如何去除按钮的圆 Angular (jQuery Mobile)?