使图像宽度为父 div 的 100%,但不大于其自身宽度
我试图让图像(动态放置,对尺寸没有限制)与其父 div 一样宽,但前提是该宽度不比 100% 时其自身宽度宽。我已经尝试过这个,但没有成功:
img {
width: 100%;
height: auto;
max-width: 100%;
}
这些图像中的许多都比它们的父 div 宽得多,这就是为什么我希望它们相应地调整大小,但是当一个小图像弹出其中并放大到超出其正常尺寸时,看起来真的很可怕。有什么办法可以做到这一点吗?
I’m trying to get an image (dynamically placed, with no restrictions on dimensions) to be as wide as its parent div, but only as long as that width isn’t wider than its own width at 100%. I’ve tried this, to no avail:
img {
width: 100%;
height: auto;
max-width: 100%;
}
Many of these images are way wider than their parent div, which is why I’d like them to resize accordingly, but when a small image pops in there and gets scaled up beyond its normal dimensions, it really looks terrible. Is there any way of doing this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(12)
只需单独指定
max-width: 100%
即可。Just specify
max-width: 100%
alone, that should do it.在 Google 搜索中找到了这篇文章,感谢 @jwal 的回复,它解决了我的问题,但我对他的解决方案做了一个补充。
通过上面的内容,我将最大宽度更改为图像所在内容容器的尺寸。在本例中,它是:容器宽度 - 填充 - 边框 = 最大宽度
这样我的图像就不会脱离包含的 div ,我仍然可以在内容 div 中浮动图像。
我已经在 IE 9、FireFox 18.0.2 和 Chrome 25.0.1364.97、Safari iOS 中进行了测试,似乎可以工作。
另外:我在以 678 像素(最大宽度)显示的 1024 像素宽的图像和以 500 像素(图像的宽度)显示的 500 像素宽的图像上进行了测试。
Found this post on a Google search, and it solved my issue thanks to @jwal reply, but I made one addition to his solution.
With the above I changed the max-width to the dimensions of the content container that my image is in. In this case it is: container width - padding - boarder = max width
This way my image won't break out of the containing div, and I can still float the image within the content div.
I've tested in IE 9, FireFox 18.0.2 and Chrome 25.0.1364.97, Safari iOS and seems to work.
Additional: I tested this on an image 1024px wide displayed at 678px (the max width), and an image 500px wide displayed at 500px (width of the image).
将宽度设置为 100% 是其所在 div 的完整宽度,而不是原始的全尺寸图像。如果没有 JavaScript 或其他可以测量图像的脚本语言,就无法做到这一点。如果您的 div 可以有固定宽度或固定高度(例如 200 像素宽),那么为图像提供一个填充范围应该不会太难。但如果将 20x20 像素的图像放入 200x300 像素的框中,它仍然会失真。
Setting a width of 100% is the full width of the div it's in, not the original full-sized image. There is no way to do that without JavaScript or some other scripting language that can measure the image. If you can have a fixed width or fixed height of the div (like 200px wide) then it shouldn't be too hard to give the image a range to fill. But if you put a 20x20 pixel image in a 200x300 pixel box it will still be distorted.
我写了这段代码:
I wrote this code:
max-width: fit-content;
对我有用。max-width: fit-content;
worked for me.您应该设置最大宽度,如果需要,您还可以在一侧设置一些填充。就我而言,最大宽度:100% 很好,但图像就在屏幕末端旁边。
You should set the max width and if you want you can also set some padding on one of the sides. In my case the max-width: 100% was good but the image was right next to the end of the screen.
我也遇到了同样的问题,但我将 CSS 中的高度值设置为 auto ,这解决了我的问题。另外,不要忘记执行显示属性。
I was also having the same problem, but I set the height value in my CSS to auto and that fixed my problem. Also, don't forget to do the display property.
我找到了一个对我有用的答案,可以在以下链接中找到:
宽度有限的父容器中的全宽度容器
I found an answer which worked for me and can be found in the following link:
Full Width Containers in Limited Width Parents
我发现
max-width:inherit;
对我有用I found
max-width:inherit;
worked for me如果图像小于父图像...
If the image is smaller than parent...
我将使用属性
display: table-cell
这是 链接
I would use the property
display: table-cell
Here is the link