背景图像:如果图像很小,如何填充整个div,反之亦然
我遇到三个问题:
当我尝试在较小尺寸的 div 中使用背景图像时,div 仅显示图像的一部分。如何显示图像的完整或特定部分?
我有一个较小的图像,我想在更大的 div 中使用。但不想使用重复功能。
CSS 有没有办法控制图像的不透明度?
I have three problems:
When I tried to use a background image in a smaller size div, the div shows only part of image. How can I show the full or a specific part of image?
I have a smaller image and I want to use in a bigger div. But don't want to use repeat function.
Is there any way in CSS to manipulate the opacity of an image?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
调整图像大小以适合 div 大小。
使用 CSS3,您可以执行以下操作:
如何拉伸网页中的背景图像< /a>:
关于不透明度
或者查看 CSS 图像不透明度/透明度
Resize the image to fit the div size.
With CSS3 you can do this:
How Do you Stretch a Background Image in a Web Page:
About opacity
Or look at CSS Image Opacity / Transparency
要自动放大图像并覆盖整个 div 部分而不留下任何未填充的部分,请使用:
To automatically enlarge the image and cover the entire div section without leaving any part of it unfilled, use:
这对我来说非常有效
This worked perfectly for me
尝试下面的代码段,我之前自己尝试过:
Try below code segment, I've tried it myself before :
而不是给
background-size:100%;
我们可以给
background-size:contain;
查看可用的不同选项:http://www.css3.info/preview/background-size/
Rather than giving
background-size:100%;
We can give
background-size:contain;
Check out this for different options avaliable: http://www.css3.info/preview/background-size/
这将像魅力一样发挥作用。
This will work like a charm.
我同意yossi的例子,拉伸图像以适应div,但方式略有不同(没有背景图像,因为这在css 2.1中有点不灵活)。显示完整图像:
使用背景位置显示图像的一部分:
<前><代码>#yourdiv
{
背景图像: url(image.jpg);
背景重复:不重复;
背景位置:10px 25px;
}
与 (1) 的第一部分相同,图像将缩放到 div,因此更大或更小都可以工作
与 yossi 的相同。
I agree with yossi's example, stretch the image to fit the div but in a slightly different way (without background-image as this is a little inflexible in css 2.1). Show full image:
Show part of the image using background-position:
Same as the first part of (1) the image will scale to the div so bigger or smaller will both work
Same as yossi's.