css - 在 body 元素中将图像附加到页面顶部,img 不滚动

标签 css background background-attachment background-size

我遇到以下问题。我想,有一个快速的方法可以做到这一点,但现在我找不到解决的方法。

所以,我有一个图像,我希望它作为 <body> 页面顶部的背景图像。元素。目前,如果我向下滚动,它会移动到页面底部。

我有以下代码:

body {
    color: #d0d0d0;
    font-family: Verdana, Tahoma, sans-serif; 
    font-size: 12pt;
    font-weight: 400; 
    font-style: normal;
    line-height: 1.5; 
    text-rendering: optimizeLegibility;
    -webkit-font-smoothing: antialiased;
    margin: 0;
    z-index: 0;
    background:url(../img/main-bg-top.png) no-repeat top center;
    background-attachment: fixed;
    background-size: cover;
}

最佳答案

那么,您可以创建另一个类并将其插入到所有内容之前,如下所示: Jsfiddle

body {
    color: #d0d0d0;
    font-family: Verdana, Tahoma, sans-serif; 
    font-size: 12pt;
    font-weight: 400; 
    font-style: normal;
    line-height: 1.5; 
    text-rendering: optimizeLegibility;
    -webkit-font-smoothing: antialiased;
    margin: 0;
    z-index: 0;
    background:#000;
}
.top-image{height:0;}
.top-image img{max-width: 100%;}
.container{height:900px; }

html:

<div class="top-image">
    <img src="http://www.wormzweb.com/images/photoalbum/album_28/nature_2118.jpg">
</div>
<div class="container">
    <p>random text.</p>
</div>

这是一个快速草图,是适合您的最佳解决方法,以及编写此代码的正确方法(如果您愿意)。

关于css - 在 body 元素中将图像附加到页面顶部,img 不滚动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25945694/

相关文章:

css - 如何垂直和水平对齐图像上的文本?

android - 如何在可绘制 View 中设置背景 xml 文件?

CSS - 背景附件 :fixed not stopping scroll (tumblr theme)

CSS 背景大小 : cover + background-attachment: fixed clipping background images

javascript - 如何替换后台附件:fixed on Mobile?

jquery - 点击后弹出全屏div

html - 如果不 float ,如何在内容下方对齐侧边栏

jquery - 如何在 Bootstrap 不同的两列大小时使图像高度相等

android - 应用程序在后台时 Gimbal PlaceEventListener Api 的可用性?

html - block 元素下的 CSS 阴影(不是框阴影,阴影图像,旧方法)