使用 css 在滚动期间保持背景图像固定

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2662039/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 21:55:37  来源:igfitidea点击:

Keep background image fixed during scroll using css

css

提问by X10nD

How do I keep the background image fixed during a page scroll? I have this CSS code, and the image is a background of the body and not <div></div>

如何在页面滚动期间保持背景图像固定?我有这个 CSS 代码,图像是身体的背景而不是<div></div>

body {
  background-position:center;
  background-image:url(../images/images5.jpg);
}

回答by Chris Walter

background-image: url("/your-dir/your_image.jpg");
min-height: 100%;
background-repeat: no-repeat;
background-attachment: fixed;
background-position: center;
background-size: cover;}

回答by Omar Masry

Just add background-attachment to your code

只需将背景附件添加到您的代码中

body {
    background-position: center;
    background-image: url(../images/images5.jpg);
    background-attachment: fixed;
}