如何拉伸背景图像以覆盖整个 HTML 元素?

发布于 2024-07-07 12:14:44 字数 344 浏览 5 评论 0 原文

我正在尝试获取 HTML 元素(body、div 等)的背景图像以拉伸其整个宽度和高度。

运气不太好。 是否有可能,或者除了背景图像之外我还必须以其他方式做到这一点?

我当前的 CSS 是:

body {
    background-position: left top;
    background-image: url(_images/home.jpg);
    background-repeat: no-repeat;
}

编辑:我不热衷于维护 Gabriel 建议中的 CSS,所以我正在更改页面的布局。 但这似乎是最好的答案,所以我将其标记为这样。

I'm trying to get a background image of a HTML element (body, div, etc.) to stretch its entire width and height.

Not having much luck. Is it even possible or do I have to do it some other way besides it being a background image?

My current css is:

body {
    background-position: left top;
    background-image: url(_images/home.jpg);
    background-repeat: no-repeat;
}

Edit: I'm not keen on maintaining the CSS in Gabriel's suggestion so I'm changing the layout of the page instead. But that seems like the best answer so I'm marking it as such.

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

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

发布评论

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

评论(14

杀お生予夺 2024-07-14 12:14:44
<style>
    { margin: 0; padding: 0; }

    html { 
        background: url('images/yourimage.jpg') no-repeat center center fixed; 
        -webkit-background-size: cover;
        -moz-background-size: cover;
        -o-background-size: cover;
        background-size: cover;
    }
</style>
<style>
    { margin: 0; padding: 0; }

    html { 
        background: url('images/yourimage.jpg') no-repeat center center fixed; 
        -webkit-background-size: cover;
        -moz-background-size: cover;
        -o-background-size: cover;
        background-size: cover;
    }
</style>
嗫嚅 2024-07-14 12:14:44

简而言之,你可以尝试这个......

<div data-role="page" style="background:url('backgrnd.png'); background-repeat: no-repeat; background-size: 100% 100%;" >

我使用了很少的 css 和 js...

<link rel="stylesheet" href="css/jquery.mobile-1.0.1.min.css" />
<script src="js/jquery-1.7.1.min.js"></script>
<script src="js/jquery.mobile-1.0.1.min.js"></script>

它对我来说工作得很好。

In short you can try this....

<div data-role="page" style="background:url('backgrnd.png'); background-repeat: no-repeat; background-size: 100% 100%;" >

Where I have used few css and js...

<link rel="stylesheet" href="css/jquery.mobile-1.0.1.min.css" />
<script src="js/jquery-1.7.1.min.js"></script>
<script src="js/jquery.mobile-1.0.1.min.js"></script>

And it is working fine for me.

墨落成白 2024-07-14 12:14:44

不确定是否可以拉伸背景图像。 如果您发现在所有目标浏览器中这是不可能的或不可靠,您可以尝试使用拉伸的 img 标签,并将 z-index 设置为较低,并将位置设置为绝对,以便其他内容显示在其顶部。

让我们知道您最终会做什么。

编辑:我的建议基本上是加布里埃尔链接中的内容。 所以尝试一下:)

Not sure that stretching a background image is possible. If you find that it's not possible, or not reliable in all of your target browsers, you could try using a stretched img tag with z-index set lower, and position set to absolute so that other content appears on top of it.

Let us know what you end up doing.

Edit: What I suggested is basically what's in gabriel's link. So try that :)

听不够的曲调 2024-07-14 12:14:44

要扩展@PhiLho答案,您可以将一个非常大的图像(或任何尺寸的图像)放在页面上的中心:

{ 
background-image: url(_images/home.jpg);
background-repeat:no-repeat;
background-position:center; 
}

或者您可以使用一个较小的图像,其背景颜色与图像的背景相匹配(如果它是纯色) )。 这可能适合也可能不适合您的目的。

{ 
background-color: green;
background-image: url(_images/home.jpg);
background-repeat:no-repeat;
background-position:center; 
}

To expand on @PhiLho answer, you can center a very large image (or any size image) on a page with:

{ 
background-image: url(_images/home.jpg);
background-repeat:no-repeat;
background-position:center; 
}

Or you could use a smaller image with a background color that matches the background of the image (if it is a solid color). This may or may not suit your purposes.

{ 
background-color: green;
background-image: url(_images/home.jpg);
background-repeat:no-repeat;
background-position:center; 
}
云巢 2024-07-14 12:14:44

如果您需要在调整屏幕大小时拉伸背景图像,并且不需要与旧版浏览器版本兼容,则可以使用以下方法:

body {
    background-image: url('../images/image.jpg');
    background-repeat: no-repeat;
    background-size: cover;
}

If you need to stretch your background image while resizing the screen and you don't need compatibility with older browser versions this will do the work:

body {
    background-image: url('../images/image.jpg');
    background-repeat: no-repeat;
    background-size: cover;
}
美羊羊 2024-07-14 12:14:44

如果您有大的横向图像,此示例会在纵向模式下调整背景大小,使其显示在顶部,而底部留空:

html, body {
    margin: 0;
    padding: 0;
    min-height: 100%;
}

body {
    background-image: url('myimage.jpg');
    background-position-x: center;
    background-position-y: bottom;
    background-repeat: no-repeat;
    background-attachment: scroll;
    -webkit-background-size: cover;
    -moz-background-size: cover;
    -o-background-size: cover;
    background-size: cover;
}

@media screen and (orientation:portrait) {
    body {
        background-position-y: top;
        -webkit-background-size: contain;
        -moz-background-size: contain;
        -o-background-size: contain;
        background-size: contain;
    }
}

If you have a large landscape image, this example here resizes the background in portrait mode, so that it displays on top, leaving blank on the bottom:

html, body {
    margin: 0;
    padding: 0;
    min-height: 100%;
}

body {
    background-image: url('myimage.jpg');
    background-position-x: center;
    background-position-y: bottom;
    background-repeat: no-repeat;
    background-attachment: scroll;
    -webkit-background-size: cover;
    -moz-background-size: cover;
    -o-background-size: cover;
    background-size: cover;
}

@media screen and (orientation:portrait) {
    body {
        background-position-y: top;
        -webkit-background-size: contain;
        -moz-background-size: contain;
        -o-background-size: contain;
        background-size: contain;
    }
}
蓝天 2024-07-14 12:14:44

我主要使用以下代码来实现所要求的效果:

body {
    background-image: url('../images/bg.jpg');
    background-repeat: no-repeat;
    background-size: 100%;
}

The following code I use mostly for achieving the asked effect:

body {
    background-image: url('../images/bg.jpg');
    background-repeat: no-repeat;
    background-size: 100%;
}
不奢求什么 2024-07-14 12:14:44
background: url(images/bg.jpg) no-repeat center center fixed; 
-webkit-background-size: cover;
-moz-background-size: cover;
-o-background-size: cover;
background-size: cover;
background: url(images/bg.jpg) no-repeat center center fixed; 
-webkit-background-size: cover;
-moz-background-size: cover;
-o-background-size: cover;
background-size: cover;
说谎友 2024-07-14 12:14:44

这个对我有用

.page-bg {
  background: url("res://background");
  background-position: center center;
  background-repeat: no-repeat;
  background-size: 100% 100%;
}

It works for me

.page-bg {
  background: url("res://background");
  background-position: center center;
  background-repeat: no-repeat;
  background-size: 100% 100%;
}
∞觅青森が 2024-07-14 12:14:44

这是我的代码中的一个示例:

.content{
  background-image: url("SOMEURL");
  background-position:center;
  background-size: cover;

  text-align: center; /* Center-aligns text horizontally */
  justify-content: center; /* Center content horizontally */

}

Here is an example from my code:

.content{
  background-image: url("SOMEURL");
  background-position:center;
  background-size: cover;

  text-align: center; /* Center-aligns text horizontally */
  justify-content: center; /* Center content horizontally */

}

请持续率性 2024-07-14 12:14:44

你不能在纯 CSS 中。 在所有其他组件后面覆盖整个页面的图像可能是您最好的选择(看起来这就是上面给出的解决方案)。 无论如何,很可能它看起来会很糟糕。 我会尝试使用足够大的图像来覆盖大多数屏幕分辨率(例如高达 1600x1200,高于它的图像较少),以限制页面的宽度,或者仅使用平铺的图像。

You cannot in pure CSS. Having an image covering the whole page behind all other components is probably your best bet (looks like that's the solution given above). Anyway, chances are it will look awful anyway. I would try either an image big enough to cover most screen resolutions (say up to 1600x1200, above it is scarcer), to limit the width of the page, or just to use an image that tile.

酒解孤独 2024-07-14 12:14:44

图像{

background-size: cover;
  background-repeat: no-repeat;
  background-position: center;
  padding: 0 3em 0 3em; 
margin: -1.5em -0.5em -0.5em -1em; 
  width: absolute;
  max-width: 100%; 

image{

background-size: cover;
  background-repeat: no-repeat;
  background-position: center;
  padding: 0 3em 0 3em; 
margin: -1.5em -0.5em -0.5em -1em; 
  width: absolute;
  max-width: 100%; 
情归归情 2024-07-14 12:14:44

只需将 div 设为 body 的直接子元素(例如类名 bg),包含 body 中的所有其他元素,并将其添加到 CSS 文件中:

.bg {
    background-image: url('_images/home.jpg');//Put your appropriate image URL here
    background-size: 100% 100%; //You need to put 100% twice here to stretch width and height
}

请参阅此链接:https://www.w3schools.com/css/css_rwd_images.asp
向下滚动到显示以下内容的部分:

  • 如果background-size属性设置为“100% 100%”,背景图像将拉伸以覆盖整个内容区域
  • 无论您如何调整大小,它都会显示“img_flowers.jpg”拉伸到屏幕或浏览器的大小它。

    Simply make a div to be the direct child of body (with the class name bg for example), encompassing all other elements in the body, and add this to the CSS file:

    .bg {
        background-image: url('_images/home.jpg');//Put your appropriate image URL here
        background-size: 100% 100%; //You need to put 100% twice here to stretch width and height
    }
    

    Refer to this link: https://www.w3schools.com/css/css_rwd_images.asp
    Scroll down to the part that says:

    1. If the background-size property is set to "100% 100%", the background image will stretch to cover the entire content area

    There it shows the 'img_flowers.jpg' stretching to the size of the screen or browser regardless of how you resize it.

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