在 CSS 流布局中自动调整图像大小以模拟 html 表格布局
我有一个图像,根据屏幕分辨率,它会在 CSS 流布局中下降到看不见的位置,因为我已将其宽度和高度设置为静态值。
CSS 流布局中是否有一种方法可以在有人缩小浏览器窗口时自动调整图像大小。 我已经在 html-table 布局中看到了这一点,并且我认为表格使得这成为可能 - 有没有办法在 CSS 流布局中也做到这一点?
I have an image that, depending on the screen resolution, drops down out of sight in my CSS flow layout because I have set its width and height to static values.
Is there a way in a CSS flow layout to have the image automatically resize while someone is making the browser window smaller. I have seen this done in a html-table layout and I assume the tables make it possible there - is there a way to also do this in a CSS flow layout?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
快速测试表明:
结合:
按您可能想要的方式调整大小。 图像尽职尽责地调整为包含它的框宽度的 50%,并垂直调整大小,保持纵横比。
至于根据垂直变化调整大小,它不会按照您想要的方式工作,至少不一致。 我尝试过:
在当前的 Google Chrome (2.0.172) 中,它的大小调整有些不一致; 尺寸是正确的,但每次拖动窗口后不会更新。 在当前的 Firefox (3.5) 中,高度似乎完全被忽略了。 我没有任何最新的 IE、Safari 等可供测试。 请随意编辑这些结果。 即使这些效果很好,它仍然可能是你想要避免的,并坚持宽度。
编辑:
为此,所有包含 img.test 的元素都需要使用百分比调整大小,而不是静态调整。
可以这样想:
现在假设我添加一个 div。 像这样...
然后
如果 div 具有“width: 100%”,则逻辑与以前相同。 只要它是某个百分比,并且不是固定的,您就可以使用 img 上的百分比并使其计算出您想要的大小。
A quick test shows that this:
combined with:
Resizes the way you probably want. The image dutifully resized to 50% the width of the box containing it, as well as resizing vertically, maintaining the aspect ratio.
As for resizing based on vertical changes, it doesn't work the way you would like, at least not consistently. I tried:
In current Google Chrome (2.0.172), it resizes somewhat inconsitently; the sizing is correct but does not update after every window drag. In current Firefox (3.5), the height seems to be ignored completely. I don't have any remotely recent IE, Safari, etc to test. Feel free to edit in those results. Even if those do well its still probably something you want to avoid, and stick with width.
EDIT:
For this to work, all the elements containing img.test need to be sized with percentages, not statically.
Think of it this way:
Now suppose I add a div. like this...
Then
If the div has "width: 100%" though, then the logic works out the same as before. As long as its some percentage, and not fixed, you can play with the percentage on the img and make it work out the size you want.
有点猜测,因为我的 css 很垃圾,但由于没有人回答,那么在图像中设置 % 宽度或高度或两者,使其成为其父级的百分比怎么样。 不知道?
bit of a guess since my css is rubbish, but since nobody is answering, what about setting a % width or height or both in the image so that it is a percent of its parent. dunno?
尝试将 max-width 设置为 95% 之类的值。 谢谢,当容器宽度小于图像宽度时,图像会缩小。 所有父容器都需要调整
Try setting max-width to something like 95%. Thank way the image will shrink when the container width is less then the width of the image. All of the parent containers would need to adju