Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Background-image position, center center not working

I'm trying to create a page with a centered background image and using the position center center. But instead of appearing centered both vertically and horizontally the image appears to extend vertically beyond the top of the page. Where/why am I going wrong?

<!DOCTYPE html>
<html>
<style>
body
{
background-image:url('tumbleweed.jpg');
background-repeat:no-repeat;
background-position:center center;
background-color:#EAEAEA;
}
</style>
</html>
like image 479
Sally Avatar asked Oct 18 '25 04:10

Sally


2 Answers

Would you see the screen like below picture?

enter image description here

body{
  background-image:url(../IMG/BG/pizzaBackground.jpg);
  background-size: cover;
  background-position: 50% 50%;
  background-repeat: no-repeat;
}

I think It's because you didn't declare 'height'.

Add 'height:100vh'style.

body{
  height: 100vh;    /* ADD STYLE ! */
  background-image:url(../IMG/BG/pizzaBackground.jpg);
  background-size: cover;
  background-position: 50% 50%;
  background-repeat: no-repeat;
}

The reason for setting to 100vh is because the target is body tag.

If it's a different tag, use a different value.

Then this will be

enter image description here

And add 'margin:0px' style to remove the scroll.

body{
  margin: 0px;     /* ADD STYLE ! */
  height: 100vh;
  background-image:url(../IMG/BG/pizzaBackground.jpg);
  background-size: cover;
  background-position: 50% 50%;
  background-repeat: no-repeat;
}

Then you can see

enter image description here


Thanks!

like image 143
KimYC1223 Avatar answered Oct 21 '25 08:10

KimYC1223


You can use cover

background-image:url('tumbleweed.jpg');
-webkit-background-size: cover;
-moz-background-size: cover;
 -o-background-size: cover;
 background-size: cover;

http://jsfiddle.net/ou10gmfd/

like image 44
Stacker-flow Avatar answered Oct 21 '25 10:10

Stacker-flow