r/HTML•
switchbackk

Help with keeping background image the same size

Solved

Hi everyone. You were really helpful before with my last question so fingers crossed I can find my answer here again.

I have a few pages on our website but a few of them seem to load the background image in different sizes and dimensions. I cannot see where this is happening in either the HTML or CSS.

The main page is here

But you will notice the background changes for this page

I know the background images aren't great quality, they are just placeholders until I get round to taking a decent picture.

Cheers all.

Posted Jul 22, 2014, 8:54 AM UTC

15
NaGeL182•

It's because you set the background-size to cover. At the index the image is stretched horizontally to fill in the div. At the Contact page it stretched vertically to fill in the page.

This is done so because it tries to find the most optimal way to fill in the divs. at first it tries to do it vertically, then horizontally.

at the first page, the vertical one was more than enough, but not the horizontal one, so it stretched it further. but at the contact page the vertical one was enough.

hope this helps.

2
switchbackkOP•

Yes it helped Thanks for your reply.

1
martijnf1•

I'd say that this solves it:

body {
    background: url('ref/to/bg.jpg') center center no-repeat;
    background-size: 100%;
}
2
switchbackkOP•

This certainly seems to have helped. Though now I have trouble with white at the bottom. I assume this is because of the picture not stretching down the foot of the page.

Any way I can make these backgrounds at the bottom black?

EDIT - I think I got it now. Thanks.

1
martijnf1•

Glad to have helped, you can shove the background color at the end of the background css like this:

body {
    background: url('ref/to/bg.jpg') center center no-repeat #000;
    background-size: 100%;
}

Just in case :D

1