tags:

views:

48

answers:

2

I have a image centered on the screen. I need to display some text above the image and I want to set the text coordinates relative to image.

If a user have a different resolution:

  • The image always located on the top and center of the browser.
  • The text will be on the same position in the image.

I have tried:

<style type="text/css">
    #l1 {
       position: relative;
       left: 20px;
       top: 30px;
       color: #03C;
    }
</style>


<div align="center">
    <div id="l1" align="left">
       some
   </div>
 <img src="some.jpg" width="1024" height="788" />
</div>

But it doesn't work. How can I achieve my goal?

+2  A: 

Set the text to be position:absolute and the containing div to be position:relative

And also center the div using margins and not the deprecated align attribute..

<style type="text/css">
    .container{
           position:relative;
           margin-left:auto;
           margin-right:auto;
           width:1024px;}
    #l1 {
       position: absolute;
       left: 20px;
       top: 30px;
       color: #03C;
       text-align:left;
    }
</style>


<div class="container">
    <div id="l1">
       some
   </div>
 <img src="some.jpg" width="1024" height="788" />
</div>
Gaby
I tried but it doesn't work
demas
@demas, i had an error .. i needed to set dimensions on the container div to match the image.. now it should be fine ..
Gaby
Thanks. That is what I want :)
demas
+1  A: 

I would do it like this:

<!doctype html>
<html>
<head>
    <title></title>
    <style type="text/css">
        /*reset default margins, paddings, set body font*/
        html,body,h1,h2,h3,h4,h5,p,ul,li,form,button { margin:0; padding:0 }
        body { font:normal 62.5% tahoma }

        #my-image { 
            width:1024px; height:788px; 
            background:url(some.jpg); /* use image as background */
            margin:0 auto; /* this centers the div in the browser horizontally */
            position:relative; /* set positioning context for children */
        }

        #my-text { 
            position:absolute; 
            left:0px; top:0px; /* left and top are with respect to the parent div */ 
        }
    </style>
</head>
<body>

    <div id="my-image">
        <div id="my-text">some text</div>
    </div>

</body>
</html>
Moin Zaman
I changed left and top in the #my-text, but nothing the change.
demas
0 needs no unit. if you change it to a value other than 0 you need to add `px`.. so try something like this: `20px`
Moin Zaman
Excuse me :) It was my mistake.
demas
I can not edit my vote, because it is locked. But if you change your answer I'll do it.
demas
I added px to the 0's
Moin Zaman