0

i need to put an image in background of a div at the center position of it, like: Example

What i've tried is:

<div id="container">
    ...
</div
<div id="background">
    <img src="images/bg.png"/>
</div>

with this css:

div#container {
    margin: 0 auto;
    width: 52em;
}
div#bg img {
    z-index: -999;
    width: 100%;
    height: 100%;
    position: absolute;
    top: 22%;
}

In my browser, with my screen size, i put 22% and all is ok, but if i try on another screen with different size background img isn't in the center of div. What can i do? can someonehelp me?

Jayyrus
  • 12,961
  • 41
  • 132
  • 214

3 Answers3

3

Give

position:relative;

in the container div..It should work.. And the background div(with position:absolute;) should be a child of container div..then only u'll get the div overlapping property

Bhuvan Rikka
  • 2,683
  • 1
  • 17
  • 27
0

can use css as below:

#sample
{
    background:url(bg_apple_little.gif) no-repeat center center;
    height:200px;
    width:200px;
}

& you div would be with id="sample". height, width can be varied as per your requirement.

Musa
  • 96,336
  • 17
  • 118
  • 137
Suresh Sharma
  • 1,826
  • 22
  • 41
0

in css3 you can use #container { background-size: auto; }

Dima Daron
  • 548
  • 1
  • 7
  • 21