0

I have used a background image on the webpage and used this code in the css which makes it nicely resize when browser is resized.

body{   
    background: url("images/back.jpg") no-repeat  ;
    background-size: cover;
}

I need to place some other image on top of the background image at a specific place ( vase on table) .but when i do that then the background gets resized but the vase image remains in the same place and same size when browser is resized as shown in second picture below.

see the vase in these two images

browser in full size

resized browser

how can i make the vase image also get resized just like the background

JanetOhara
  • 137
  • 1
  • 2
  • 9

3 Answers3

1

I recently ran into exactly the same issue creating a hidden object game which needed images placed on top of a background image to maintain their position regardless of browser dimensions.

Here's what I did:

You can include a template version of the background image as an actual <img> with visibility:hidden (so it's not visible but still takes up it's space in the DOM and base the size (and background image size) based on that.

HTML:

<div class="image-container">
    <img src="http://www.w3.org/html/logo/downloads/HTML5_Logo_512.png" class="img-template">
    <div class="item"></div>
</div>

CSS:

/* This is your container with the background image */
.image-container {
    background:url('http://www.w3.org/html/logo/downloads/HTML5_Logo_512.png') no-repeat;
    background-size:100%;
    overflow-x: hidden;
  position:relative;
}

/* This is the template that resizes the DIV based on background image size */
img.img-template {
    visibility: hidden;
    width:100%;
    height:auto;
}

/* This is the item you want to place (plant pot) */
.item {
    position: absolute;
    left: 14.6%;
    bottom: 80.3%;
    width: 15%;
    height: 15%;
    background: yellow;
    border: 2px solid black;
}

Here is a working example: http://jsfiddle.net/cfjbF/3/

dav0r
  • 113
  • 2
  • 4
0

Try making the image relative position and setting the alignment manually.

http://jsfiddle.net/cfjbF/1/

<head>
    <style>
        body {
            background: #000000;
        }

        #image1 {
            background: #008000;
            position: relative;
            left: 50px;
            height: 50px;
            width: 50px;
        }
    </style>
</head>
<body>
<div id="image1"></div>
</body>
zmanc
  • 5,201
  • 12
  • 45
  • 90
0

Solution for your Problem:

https://stackoverflow.com/a/7660978/1256403

OR

http://buildinternet.com/2009/07/quick-tip-resizing-images-based-on-browser-window-size/

Community
  • 1
  • 1
Avin Varghese
  • 4,340
  • 1
  • 22
  • 31