tags:

views:

228

answers:

3

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".

+4  A: 

Try

background-position: center center;
Pekka
How come the double center center work?Anyway it does not solve the issue, still the same
Jean
@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
@rob its not working, the image is tiling. The image is a 1600x1200 and my screen size is 1280x800
Jean
`background-image` only takes one argument. You need to specify `background-repeat: no-repeat` as a separate instruction
nikc
@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
Guys, please try my code with a 1600x1200 on localhost, you will understand what I am trying to say.
Jean
@Jean is your problem the re-sizing of the image?
Pekka
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
If I remove the syntax background-repeat:no-repeat; background-position: center center;THen the image is displayed from left and tiled
Jean
@Jean but why would you remove the syntax?
Pekka
@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
@pekka, it did sort out, but there is another problem now check http://www.jsfiddle.net/yWrQP/4/
Jean
@Jean can you describe the problem? I don't see it.
Pekka
@Jean, this is what you asked us to do..
Kyle Sevenoaks
+1  A: 
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 Sevenoaks
It does centralize, but displays only half the screen, I wish stackoverflow had an option to attach screenshots
Jean
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 Sevenoaks
screenshot - http://imagevat.com/picview.php?ig=6179I am not sure how to upload images to jsfiddle, if you can help me out therehttp://www.jsfiddle.net/yWrQP/
Jean
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 Sevenoaks
I dont have the server to do it, but used an imagehosting site, but still check it out now
Jean
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 Sevenoaks
I thought it was a browser used, and checked on IE 8, FF, Chrome.
Jean
Check cross browser compatibility after you have it working in FF, did you set the `min-height`?
Kyle Sevenoaks
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
Jean
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 Sevenoaks
I did add top:0px; does not work for me
Jean
I edited my answer to include what we think might help.
Kyle Sevenoaks
+1  A: 

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.

Rob van Groenewoud
'background:url(../images/images2.jpg) center center no-repeat;' does not work, since the syntax is wrongAnd the others do not work either
Jean