0

enter image description here

AS shown in image I have a [wrapper] div which has background image inside this Image I want to place another div but as the Screen size changes the background image has different dimensions and thus the position of second div must change.

I have tried jquery to get width and height of the background image but it gives out 0,0.

What should I do.

jsfiddle code jsfiddle[dot]net/AFvak/

1
  • can you make a fiddle out of your question, jsfiddle.net Commented Feb 15, 2013 at 6:58

4 Answers 4

2

To my knowledge, there is no facility for querying for that kind of information about a background image. The only solutions I've seen seem to involve just loading in the image by some other means (e.g. with an img tag) and then querying that for the information.

See: How do I get background image size in jQuery?

Sign up to request clarification or add additional context in comments.

Comments

1

If the center div should always be centered with a fix height and width then you could try this:

<div class="wrapper">
   <div class="inside"></div>
</div>

Styles:

.wrapper {
   width: 600px;
   height: 500px;
   margin: 40px auto 0;
   position: relative;
   border: 1px solid black;
   background: url(image_here.jpg) no-repeat center center;
}

.inside {
   top: 50%;
   left: 50%;
   width: 200px;
   height: 100px;
   margin-top: -50px; /* height/2 */
   margin-left: -100px; /* width/2 */
   position: absolute;
   background: #000;
}

DEMO

1 Comment

@tehTerminator add in the div2 all the classes that I've wrote for the inside. Off course you'll have to change the width, height and the margins.
0

try ..

$backWidth=$(window).width();
$backHeight=$(window).height();

Comments

0

As per my understanding you try to div tag should be on image with fixed position even browser will resized.

Here code:

<div id="wrapper">
             <div id="test">
                   <img src="test.jpg" id="yourimg">
                   <div id="yourdiv"></div>
             <div>
        </div>





<style>
               #test{
                     position:relative;
               }
               #yourimg{
                     position:absolute;
                     top:100px;
                     left:100px;                  
               }
               #yourdiv{
                     position:absolute;
                     top:120px;
                     left:120px;                  
               }
         </style>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.