背景图像:如果图像很小,如何填充整个div,反之亦然

发布于 2024-10-13 06:23:45 字数 188 浏览 8 评论 0原文

我遇到三个问题:

  1. 当我尝试在较小尺寸的 div 中使用背景图像时,div 仅显示图像的一部分。如何显示图像的完整或特定部分?

  2. 我有一个较小的图像,我想在更大的 div 中使用。但不想使用重复功能。

  3. CSS 有没有办法控制图像的不透明度?

I have three problems:

  1. 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?

  2. I have a smaller image and I want to use in a bigger div. But don't want to use repeat function.

  3. Is there any way in CSS to manipulate the opacity of an image?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

流绪微梦 2024-10-20 06:23:45

调整图像大小以适合 div 大小。
使用 CSS3,您可以执行以下操作:

/* with CSS 3 */
#yourdiv {  
    background: url('bgimage.jpg') no-repeat;
    background-size: 100%;
}

如何拉伸网页中的背景图像< /a>:

关于不透明度

#yourdiv {
    opacity: 0.4;
    filter: alpha(opacity=40); /* For IE8 and earlier */
}

或者查看 CSS 图像不透明度/透明度

Resize the image to fit the div size.
With CSS3 you can do this:

/* with CSS 3 */
#yourdiv {  
    background: url('bgimage.jpg') no-repeat;
    background-size: 100%;
}

How Do you Stretch a Background Image in a Web Page:

About opacity

#yourdiv {
    opacity: 0.4;
    filter: alpha(opacity=40); /* For IE8 and earlier */
}

Or look at CSS Image Opacity / Transparency

咽泪装欢 2024-10-20 06:23:45

要自动放大图像并覆盖整个 div 部分而不留下任何未填充的部分,请使用:

background-size: cover;

To automatically enlarge the image and cover the entire div section without leaving any part of it unfilled, use:

background-size: cover;
简单爱 2024-10-20 06:23:45

这对我来说非常有效

background-repeat: no-repeat;
background-size: 100% 100%;

This worked perfectly for me

background-repeat: no-repeat;
background-size: 100% 100%;
涫野音 2024-10-20 06:23:45

尝试下面的代码段,我之前自己尝试过:

#your-div {
    background: url("your-image-link") no-repeat;
    background-size: cover;
    background-clip: border-box;
}

Try below code segment, I've tried it myself before :

#your-div {
    background: url("your-image-link") no-repeat;
    background-size: cover;
    background-clip: border-box;
}
郁金香雨 2024-10-20 06:23:45

而不是给 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/

烟酒忠诚 2024-10-20 06:23:45

这将像魅力一样发挥作用。

background-image:url("http://assets.toptal.io/uploads/blog/category/logo/4/php.png");
background-repeat: no-repeat;
background-size: contain;

This will work like a charm.

background-image:url("http://assets.toptal.io/uploads/blog/category/logo/4/php.png");
background-repeat: no-repeat;
background-size: contain;
新人笑 2024-10-20 06:23:45
  1. 我同意yossi的例子,拉伸图像以适应div,但方式略有不同(没有背景图像,因为这在css 2.1中有点不灵活)。显示完整图像:

    #yourdiv img { 宽度:100%; /*高度将自动保持宽高比*/ }
  2. 使用背景位置显示图像的一部分:

    <前><代码>#yourdiv
    {
    背景图像: url(image.jpg);
    背景重复:不重复;
    背景位置:10px 25px;
    }

  3. 与 (1) 的第一部分相同,图像将缩放到 div,因此更大或更小都可以工作

  4. 与 yossi 的相同。

  1. 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:

    <div id="yourdiv">
        <img id="theimage" src="image.jpg" alt="" />
    </div>
    
    #yourdiv img {
        width:100%;
      /*height will be automatic to remain aspect ratio*/
    }
    
  2. Show part of the image using background-position:

    #yourdiv 
    {
        background-image: url(image.jpg);
        background-repeat: no-repeat;
        background-position: 10px 25px;
    }
    
  3. Same as the first part of (1) the image will scale to the div so bigger or smaller will both work

  4. Same as yossi's.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文