Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Resizable background-image in css body but avoid repeating

I am trying to add this image https://upload.wikimedia.org/wikipedia/commons/thumb/4/40/Coffee_and_Friends_(Unsplash).jpg/2560px-Coffee_and_Friends_(Unsplash).jpg as background-image for body selector.

Here’s my code

body {
    background-image: url("https://upload.wikimedia.org/wikipedia/commons/thumb/4/40/Coffee_and_Friends_%28Unsplash%29.jpg/2560px-Coffee_and_Friends_%28Unsplash%29.jpg");
    background-size: cover;
    background-repeat: no-repeat;
}

it’s working fine but at certain size image started to repeat so I added
background-repeat: no-repeat; in code.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Now in place of repeated image there is white space in window, How to fill this white space with single image ?

>Solution :

Just add the background-position and background-attachment properties:

  • background-position will set the starting position of your background image
  • background-attachment (set to fixed) will prevent your background image from scrolling with the content of your page
body {
    background-image: url("https://upload.wikimedia.org/wikipedia/commons/thumb/4/40/Coffee_and_Friends_%28Unsplash%29.jpg/2560px-Coffee_and_Friends_%28Unsplash%29.jpg");
    background-size: cover;
    background-repeat: no-repeat;
    background-attachment: fixed;
    background-position: center;
}
<body>
Some text
</body>
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading