68

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>

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

3 Answers 3

144
background-attachment: fixed;

http://www.w3.org/TR/CSS21/colors.html#background-properties

5
  • 2
    That would make the pic go off the center position
    – X10nD
    Apr 18, 2010 at 11:24
  • You might want to specify the y position as well as the x position then.
    – Quentin
    Apr 18, 2010 at 11:27
  • background: no-repeat-x no-repeat-y; does not seem to work, or if I am getting it wrong here
    – X10nD
    Apr 18, 2010 at 11:30
  • thanks, I need to play around with it, the image file is huge.
    – X10nD
    Apr 18, 2010 at 11:51
  • 1
    How can you avoid the imaging "zooming" when you apply position: fixed ? Jan 13, 2017 at 20:18
20
background-image: url("/your-dir/your_image.jpg");
min-height: 100%;
background-repeat: no-repeat;
background-attachment: fixed;
background-position: center;
background-size: cover;}
1
  • the min-height and min-width are the key here Sep 7, 2022 at 17:28
1

Just add background-attachment to your code

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.