css - 在较小的 div 中如何让背景图像覆盖整个屏幕

标签 css

我有一个 <div class="container">覆盖屏幕的 60%。我如何在类背景上使用背景并使其覆盖整个屏幕?

<div class="container">
    Lorem ipsum dolor sit amet, consectetuer adipiscing elit.

    <div class="background">
        I want this to have background across entire page
    </div>

    Lorem ipsum dolor sit amet, consectetuer adipiscing elit.

    <div class="background">
        I also want the background here
    </div>
</div>
<style>
    .container {
        width=60%;
   }
   .background {
        background-image: url("img.jpg");
        /*???*/
    }
</style>

我不想把所有东西都放在单独的 div 中。

最佳答案

.container {
width:60%;
}

.background {
background-image: url("http://lorempixel.com/400/200/");
width:100vw; /* replaced with 100% */
height:50px;
}
<div class="container">
    Lorem ipsum dolor sit amet, consectetuer adipiscing elit.

    <div class="background">
        I want this to have background across entire page
    </div>

    Lorem ipsum dolor sit amet, consectetuer adipiscing elit.

    <div class="background">
        I also want the background here
    </div>
</div>

关于css - 在较小的 div 中如何让背景图像覆盖整个屏幕,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53462417/

相关文章:

html - html 电子邮件中的边框未显示在 outlook 2010 中,但在 Thunderbird 中有效

jquery - 当我将代码移动到与 $(document).ready(function()){}) 分开的文件时,addClass 不起作用

html - 如何在背景透明时为页面设置默认背景颜色

styles - 当悬停 <li> 与其同级元素重叠时,如何获得阴影?

javascript - Bootstrap Modal 使用数据图像作为源

html - 将 "?v=1"附加到链接和脚本标记中的 CSS 和 JavaScript URL 有什么作用?

javascript - rc calendar 当我在日历中选择日期时它在文本框中没有改变

html - 网格元素与位置粘性重叠

html - 从单词复制/粘贴到表达式

html - div 中的背景颜色宽度