views:

170

answers:

4

Hi,

I am doing a website (WIP). I encountered some CSS problems, hope you guys can help me.

My current situation:

  • I have am image, lets called it "bg.png" with the width about 2500px, and at the center of the background image has my logo.
  • My website should target all size (from small 800 x 600 to 2400 x XXX) users.

My problems:

  • How do I centralize the background image(bg.png), so that the logo always positioned in the centered(horizontally) of different size of monitors screen?
+4  A: 
body { background: url('bg.png') 50% 50% no-repeat; }

This will place the image at full-size at the center of the page, and the user will see as much of this background image as their browser window permits.

Graphain
thanks, it works. But I need to wait another 9 minutes to accept an answer. Thanks Graphain
studdler
Welcome studdler, glad to help out. It is worth registering so you can keep track of your questions and answers over time and to encourage others to help you out.
Graphain
Noted with thanks :)
studdler
Hi Graphain, it works on FF, chrome, opera and safari. But it doesn't work on IE, the image does not show up.
studdler
Problem solved. instead of putting all the properties in 1 line like this: background: url('images/bg.png') 50% 50% no-repeat; I seperate them into this: background: url('images/bg.png') no-repeat; background-position: 50% 50%;
studdler
Nice fix. Maybe the order matters on IE or perhaps one property is missing (like bg colour at start).
Graphain
A: 

If it was just a logo file you could use

<style>
img
{
    position:absolute;
    left:-50%;
    top:-50%;
    z-index:-1;
}
</style>
rdkleine
This doesn't exactly centre the image. The left of the image is centred, but the centre of the image isn't centred.
strager
To do that, add margin-left: -50%, margin-top: -50%.
Sjoerd
A: 
#your_img {
  width: 2500px; //In example 2500px
  margin: 50%;
  padding: -1250px; // 2500 divided by 2
}

It probably don't work on Internet Explorer, but you can use a little hack with position: absolute, as above

A: 
#your_img {

   position: absolute;
   left: 50%;
   margin: -1250px;
}

This solution is necessary, when you put img element onto another box. But be careful - it might change size of parent's box.