203

I want to center a background image. There is no div used, this is the CSS style:

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

The above CSS tiles all over and does center it, but half the image is not seen, it just kind of moves up. What I want to do is center the image. Could I adopt the image to view even on a 21" screen?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
X10nD
  • 21,638
  • 45
  • 111
  • 152

13 Answers13

380
background-image: url(path-to-file/img.jpg);
background-repeat:no-repeat;
background-position: center center;

That should work.

If not, why not make a div with the image and use z-index to make it the background? This would be much easier to center than a background image on the body.

Other than that try:

background-position: 0 100px;/*use a pixel value that will center it*/ Or I think you can use 50% if you have set your body min-height to 100%.

body{

    background-repeat:no-repeat;
    background-position: center center;
    background-image:url(../images/images2.jpg);
    color:#FFF;
    font-family:Arial, Helvetica, sans-serif;
    min-height:100%;
}
Kyle
  • 65,599
  • 28
  • 144
  • 152
  • It does centralize, but displays only half the screen, I wish stackoverflow had an option to attach screenshots – X10nD Apr 15 '10 at 07:32
  • You could make a screenshot and upload it to your server or a free pic hosting site like photobucket.com, or make a temporary example at www.jsfiddle.net and post a link for us. From what you've said, the image is 1600x1200 and most screen resolutions don't go that high, try resizing your image to fit. I use a widescreen monitor at 1440x900 for example. – Kyle Apr 15 '10 at 07:34
  • screenshot - http://imagevat.com/picview.php?ig=6179 I am not sure how to upload images to jsfiddle, if you can help me out there http://www.jsfiddle.net/yWrQP/ – X10nD Apr 15 '10 at 07:52
  • To add an image to jsfiddle, just use the full http path EX: `background-image:url(http://www.myurl.com/images/image.jpg);` put that in the CSS. – Kyle Apr 15 '10 at 07:59
  • I dont have the server to do it, but used an imagehosting site, but still check it out now – X10nD Apr 15 '10 at 08:03
  • I checked it out, I saw what you're going for, and am actually kinda stumped as to why it doesn't work. I edited my answer, try setting your body min-height. – Kyle Apr 15 '10 at 08:05
  • I thought it was a browser used, and checked on IE 8, FF, Chrome. – X10nD Apr 15 '10 at 08:08
  • Check cross browser compatibility after you have it working in FF, did you set the `min-height`? – Kyle Apr 15 '10 at 08:09
  • min-height worked, I gave it the height of the image since I know it, probably have to use jquery to do it. Now why is there a 5px space on top. I did top:0px; does not seem to work though, any ideas? check jsfiddle.net/yWrQP – X10nD Apr 15 '10 at 08:10
  • Your jsfiddle doesn't do anything, when you click run it should show up your result in the bottom right frame, please amend your fiddle. Here's an example of one I've been messing with http://jsfiddle.net/waFC8/. – Kyle Apr 15 '10 at 08:16
25

I use this code, it works nice

html, body {
  height: 100%;
  width: 100%;
  padding: 0;
  margin: 0;
  background: black url(back2.png) center center no-repeat;;
-webkit-background-size: cover;
-moz-background-size: cover;
-o-background-size: cover;
background-size: cover;
}
victor Martinez
  • 251
  • 3
  • 2
20

I think this is what is wanted:

body
{
    background-image:url('smiley.gif');
    background-repeat:no-repeat;
    background-attachment:fixed;
    background-position:center;
} 
Ry-
  • 218,210
  • 55
  • 464
  • 476
Crab
  • 201
  • 2
  • 2
8

Try

background-position: center center;
Pekka
  • 442,112
  • 142
  • 972
  • 1,088
  • 1
    How come the double center center work? Anyway it does not solve the issue, still the same – X10nD Apr 15 '10 at 07:14
  • 2
    @Jean: look at the specs, background-position actually can actually take 2 values (horizontal and vertical). http://www.w3.org/TR/CSS2/colors.html#propdef-background-position – Rob van Groenewoud Apr 15 '10 at 07:18
  • 1
    @rob its not working, the image is tiling. The image is a 1600x1200 and my screen size is 1280x800 – X10nD Apr 15 '10 at 07:21
  • `background-image` only takes one argument. You need to specify `background-repeat: no-repeat` as a separate instruction – nikc.org Apr 15 '10 at 07:25
  • 3
    @Jean you can't resize a background image (yet). It's possible in CSS 3 but that isn't supported widely enough yet. `center center` in tandem with `background-repeat: no-repeat` should do what you ask for - center the image. – Pekka Apr 15 '10 at 07:26
  • Guys, please try my code with a 1600x1200 on localhost, you will understand what I am trying to say. – X10nD Apr 15 '10 at 07:27
  • @Jean is your problem the re-sizing of the image? – Pekka Apr 15 '10 at 07:41
  • it could be the page content isnt big enough to display the content, add a `min-height` and `height` to the body CSS too to handle this. Lastly, you could use javascript to get the screen height and pick a correct image to display in the background of the body, so you would always see the correct image. – Mauro Apr 15 '10 at 07:43
  • If I remove the syntax background-repeat:no-repeat; background-position: center center; THen the image is displayed from left and tiled – X10nD Apr 15 '10 at 08:00
  • @Jean but why would you remove the syntax? – Pekka Apr 15 '10 at 08:02
  • 3
    @Jean ahh, I see now from your screen shot. You need to give the `body` a `min-height` of `100%` so it extends across the full screen. That may sort it. – Pekka Apr 15 '10 at 08:04
  • @pekka, it did sort out, but there is another problem now check http://www.jsfiddle.net/yWrQP/4/ – X10nD Apr 15 '10 at 08:11
  • @Jean can you describe the problem? I don't see it. – Pekka Apr 15 '10 at 08:17
7

Like this:

background-image:url(https://upload.wikimedia.org/wikipedia/commons/thumb/8/8b/Dore_woodcut_Divine_Comedy_01.jpg/481px-Dore_woodcut_Divine_Comedy_01.jpg);
background-repeat:no-repeat;
background-position: center;
background-size: cover;

html{
height:100%
}

body{
background-image:url(https://upload.wikimedia.org/wikipedia/commons/thumb/8/8b/Dore_woodcut_Divine_Comedy_01.jpg/481px-Dore_woodcut_Divine_Comedy_01.jpg);
background-repeat:no-repeat;
background-position: center;
background-size: cover;
}
Ronnie Royston
  • 16,778
  • 6
  • 77
  • 91
4

There's an error in your code. You're using a mix of full syntax and shorthand notation on the background-image property.This is causing the no-repeat to be ignored, since it's not a valid value for the background-image property.

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

Should become one of the following:

body{
    background:url(../images/images2.jpg) center center no-repeat;
}

or

body
{
    background-image: url(path-to-file/img.jpg);
    background-repeat:no-repeat;
    background-position: center center;
}

EDIT: For your image 'scaling' issue, you might want to look at this question's answer.

Community
  • 1
  • 1
Rob van Groenewoud
  • 1,824
  • 1
  • 13
  • 17
  • 'background:url(../images/images2.jpg) center center no-repeat;' does not work, since the syntax is wrong And the others do not work either – X10nD Apr 15 '10 at 07:58
2

Try this background-position: center top;

This will do the trick for you.

n1ckolas
  • 4,380
  • 3
  • 37
  • 43
Magnus
  • 379
  • 1
  • 15
2
background-repeat:no-repeat;
background-position:center center;

Does not vertically center the background image when using a html 4.01 'STRICT' doctype.

Adding:

background-attachment: fixed;

Should fix the problem

(So Alexander is right)

Stephan
  • 21
  • 2
2

simply replace

background-image:url(../images/images2.jpg) no-repeat;

with

background:url(../images/images2.jpg)  center;
Praveen
  • 388
  • 3
  • 7
2
background-position: center center;

doesn't work for me without...

background-attachment: fixed;

using both centered my image on x and y axis

background-position: center center;
background-repeat: no-repeat;
background-attachment: fixed;
greenbeen
  • 21
  • 3
0

if your not satisfied with the answers above then use this

    * {margin: 0;padding: 0;}

    html
    {
        height: 100%;
    }

    body
    {
        background-image: url("background.png");
        background-repeat: no-repeat;
        background-position: center center;
        background-size: 80%;
    }
Toye_Brainz
  • 100
  • 7
0

Had the same problem. Used display and margin properties and it worked.

.background-image {
  background: url('yourimage.jpg') no-repeat;
  display: block;
  margin-left: auto;
  margin-right: auto;
  height: whateveryouwantpx;
  width: whateveryouwantpx;
}
-2

The only thing that worked for me is..

margin: 0 auto
MarmiK
  • 5,639
  • 6
  • 40
  • 49
plop
  • 1