简体   繁体   English

图像不会缩小以适应带有响应页面的 DIV

[英]Image won't shrink to fit DIV with responsive page

I am struggling to get an image ( featuredpic ) to respect the maximum-height bounds on its parent DIVs.我正在努力获取图像( featuredpic图片)以尊重其父 DIV 的最大高度范围。 The maximum dimensions of mainCategory is 1000px wide and 300px high (set by outside constraints) and when the page width shrinks, the 10:3 proportion is maintained as it shrinks with no issue. mainCategory 的最大尺寸为mainCategory宽和 300px 高(由外部约束设置),当页面宽度缩小时,保持 10:3 的比例,因为它没有问题地缩小。 But, the featuredpic (native size of 250x250px) will not change at all.但是,特色图片(原始大小为 250x250 像素)根本不会改变。 I've tried every combination of max-height, height, etc. set as percentages with no success.我已经尝试了将最大高度、高度等设置为百分比的所有组合,但没有成功。 Only setting a height in pixels will have an effect, but that won't work with the responsive design.仅以像素为单位设置高度会产生效果,但这不适用于响应式设计。 I even inserted the featuredPicContainer DIV to try to help control it, but it didn't make any visible change in my implementation.我什至插入了featuredPicContainer DIV 来帮助控制它,但它并没有在我的实现中做出任何明显的改变。 I'm assuming it's something to do with my use of absolute positioning, but I can't figure out an alternate solution.我假设这与我使用绝对定位有关,但我无法找到替代解决方案。 Any suggestions?有什么建议么?

 /* And the CSS: */.mainCategory { box-sizing: border-box; position: relative; margin-bottom: 10px; }.mainPic { display: block; }.mainGradient { box-sizing: border-box; position: absolute; top: 0; }.mainTextWrapper { box-sizing: border-box; position: absolute; top: 0; color: #eef7f6; text-align: center; height: 100%; display: table; width: 15%; padding: 5px; }.mainText { box-sizing: border-box; display: table-cell; vertical-align: middle; }.mainSectionWrapper { box-sizing: border-box; position: absolute; top: 0; display: table; }.featuredPicWrapper { box-sizing: border-box; position: absolute; top: 0; color: #eef7f6; text-align: center; height: 100%; display: table; padding: 5px; width: 85%; right: 0; }.featuredPicContainer { box-sizing: border-box; display: table-cell; vertical-align: middle; height: 100%; }.featuredPic { box-sizing: border-box; height: 100%; }
 <div class="mainCategory"> <a href="link"> <img class="mainPic" src="background.jpg"> <img class="mainGradient" src="gradient.jpg"> <div class="mainTextWrapper"> <div class="mainText">FEATURED PICTURE</div> </div> <div class="featuredPicWrapper"> <div class="featuredPicContainer"> <img class="featuredPic" src="featuredpic.jpg"> </div> </div> </a> </div>

I found the solution.我找到了解决方案。 I'm not 100% sure why it worked, but it did.我不是 100% 确定它为什么有效,但它确实有效。 I removed the display: table;我删除了display: table; from .featuredPicWrapper and added float:left and width:100% to .featuredPicContainer and that resolved the issue..featuredPicWrapper并将float:leftwidth:100%添加到.featuredPicContainer并解决了问题。 Removing the Display and adding the Float were key to allowing it to resize, and adding the width kept it centered over the background as I intended.移除 Display 并添加 Float 是允许它调整大小的关键,并且添加宽度使其保持在我想要的背景中心。

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

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