适合 div 的 img
我想要一个自动适合 div 大小的背景图像。
我有几个同一类的 div,每个都有不同的尺寸,是否可以将图像的宽度和高度精确设置为 div 的尺寸?
您认为使用 img
还是 background-image
更好?
PS我不知道是否可以设置背景图像的尺寸,当然对于标签img来说是可能的,但是如果我使用标签img,图像将与内容分离(它不会作为一个重叠)背景)
I want a background image that automatically fits the div size.
I have several div of the same class, each with different dimensions, is it possible to set the width and the height of the image, exactly to the dimension of the div ?
Do you think is better to use img
or background-image
?
P.S I don't know if is it possible to set the dimension of the background image, of course is possible for the tag img, but if I use the tag img the image is separated from the content (it dosen't overlap as a background)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
就个人而言,如果您要以这种方式修改内容,我认为使用 img 更好。
如果你想要的话,你可以直接设置 div 的高度和宽度。
然后 CSS 会是这样的:
无论如何,这是一种方法。您还可以将其设置为高度为 div 大小的 100%,但宽度设置为 auto,以便它随之缩放。这样图像就不会变形。像这样:
Personally if you're going to modify things in this way, I think using img is better.
You can literally just set the height and width of the div if that's what you want.
and then the CSS would be something like this:
That's one way of doing it anyway. You could also set it so that the height is 100% the size of the div, but the width is set to auto so that it scales with it. That way the image won't distort. Like this:
您可以尝试这样的操作:
对于背景图像,您将无法将图像大小调整为 div 标签的确切尺寸。
You could try something like this
With the background image, you'll not be able to resize the image to the exact dimensions of the div tag.