将背景图像放置在div中

发布于 2024-11-29 23:18:15 字数 990 浏览 0 评论 0原文

我想在顶部标题之后显示一个背景图像。该图像应位于页面的其余部分。在后期,我什至可以有一个页脚,并且图像应该占据页眉和页脚之间的整个空白空间。我试图将这个背景图像放在空白的空间中,以便在图像顶部我可以让我的 div 显示内容等。 当我简单地将图像放在 body 背景中时,我成功地实现了这一点,但是,现在我想要在顶部标题结束之后的背景图像。

示例: http://jsbin.com/opokev/2

CSS:

.backgroundimage {
 background: url(http://s1.postimage.org/ur0h52mvy/Sketch2.jpg) no-repeat center center fixed;
        -webkit-background-size: cover;
        -moz-background-size: cover;
        -o-background-size: cover;
        background-size: cover;


}
div#masthead {
    background-color: #262626;
    height: 85px;
    padding: 0;
    position: relative;
    width: 100%;
    z-index: 500;
}

HTML

<link rel="stylesheet" href="/stylesheets/style.css" type="text/css" media="screen" charset="utf-8">
<body>
  <div id="masthead"></div>
  <div class="backgroundimage"></div>
</body>

I have a background image that I want to show after the top header. This image should be on the remaining of the page. At later stage I can even have a footer and the image should take up the whole empty space between the header and the footer. I am trying to have this background image in the empty space so that on top of the image I can have my div's that show content etc.
I was successful in achieving this when I simply put the image in the body background, however, now I want the background image AFTER the top header ended.

Example: http://jsbin.com/opokev/2

CSS:

.backgroundimage {
 background: url(http://s1.postimage.org/ur0h52mvy/Sketch2.jpg) no-repeat center center fixed;
        -webkit-background-size: cover;
        -moz-background-size: cover;
        -o-background-size: cover;
        background-size: cover;


}
div#masthead {
    background-color: #262626;
    height: 85px;
    padding: 0;
    position: relative;
    width: 100%;
    z-index: 500;
}

HTML

<link rel="stylesheet" href="/stylesheets/style.css" type="text/css" media="screen" charset="utf-8">
<body>
  <div id="masthead"></div>
  <div class="backgroundimage"></div>
</body>

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

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

发布评论

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

评论(2

听你说爱我 2024-12-06 23:18:15

您可以将图像设置为正文的背景图像,并带有标题的偏移量,如下所示:

body {
    background-image: url('http://s1.postimage.org/ur0h52mvy/Sketch2.jpg');
    background-position: 0px 85px;
    background-repeat: no-repeat;
}
div#masthead {
    background-color: #262626;
    height: 85px;
    padding: 0;
    width: 100%;
    margin: 0;
}

You can set your image as the body's background-image, with an offset for the header, like this:

body {
    background-image: url('http://s1.postimage.org/ur0h52mvy/Sketch2.jpg');
    background-position: 0px 85px;
    background-repeat: no-repeat;
}
div#masthead {
    background-color: #262626;
    height: 85px;
    padding: 0;
    width: 100%;
    margin: 0;
}
虫児飞 2024-12-06 23:18:15
 body { background: url('http://s1.postimage.org/ur0h52mvy/Sketch2.jpg') no-repeat; background-position: 0px 85px; }
 body { background: url('http://s1.postimage.org/ur0h52mvy/Sketch2.jpg') no-repeat; background-position: 0px 85px; }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文