tags:

views:

478

answers:

7

Does anyone know of an easy way to display text over top of an image? One solution I've found is to just do the following:

<img id='img1' src='myimage.jpg'>
<div style='position:relative; top:-30px'> MY TEXT TO DISPLAY ON IMAGE</div>

This will cause the Div and all of its contents to move up 30 pixels. The problem is that I do not know the height of my image. However, I do know the width. Also, there could be many dynamic images on my page that need to have overlaid text and I do not necessarily know the absolute position of the image at runtime on the server.

I also haven't had much success with making the image a background image because I haven't been able to get it to show the full image--it just shows enough of the image to put behind the div. If you have any suggestions or need more information please let me know.

PS: I'm open to a client-side solution like javascript/JQuery.

+1  A: 

With jQuery you could use the dimensions plug-in to get the size of the image. You could then easily modify the style of your div.

kgiannakakis
+2  A: 

What about this?

<div style="position: relative;">
  <img src="..." />
  <div style="position: absolute; top:0; left:0">!!! TEXT HERE !!!</div>
</div>
palig
This is an excellent answer if the size of the image is not known (which is what the original poster stated).
Traingamer
Thank you Traingamer, good to know that somebody really read original question. Background-image is a good solution but you need to know size of an image. And if you know it you can also use that - margin solution, don't you? :)
palig
Indeed--thanks for the feedback though. Since i do not know the height of my image this will not work consistently.
jakejgordon
Hmmmm did I miss something? You don't need to know image size with this solution.. Text will be moved to the top-left corner of your image.
palig
+2  A: 

Background images are best for this. Somethign like

<style>
.thing {
  background-image: url(myimage.jpg);
  background-repeat: no-repeat;
  padding: 10px;;
</style>


<div class="thing">My Text</div>
Just make sure to explicitly set the DIV's dimensions to those of the background image.
Pawel Krakowiak
Thanks for the feedback. Since i do not know the height of my image this will not work consistently because 10px may be 75% of my image or it may be 2% of my image--in which case the text looks bad.
jakejgordon
+1  A: 

Background images aren't the best - especially in case we don't exactly know also other parts of the page - so it's hard to tell how should they be used.

With background-images you are not able check image's size! Don't forget about that.

palig
A: 

If you want it to work with a background image, try this:

    .btn {position: relative; background-image: url('yourimagehere.png'); width: 100px; height: 20px;}
    .btn span {position: absolute; top:0; left:0; width: 100px; height: 20px; }

    <div class="btn">
         <a  href="#">My Text<span/></a>
    </div>

Note, you need to set the width and height as appropriate for your button in the css.

Martin Clarke
Thanks for the help. The problem (as mentioned in comments to the other answers) is that I do not know the height of the image--therefore this will not work.
jakejgordon
A: 

Well I was able to get around this after realizing that in the particular case in which I am interested, all images will in fact have the same height. Since that is the case, the following markup worked just fine for me:

<head>
<style type="text/css">
  .myClass {
    text-align:center;
    margin-top:-35px; /*set this to the distance from the bottom*/
    font-weight:bold;
    font-size:18px;
    color:#fff;
    background-color:#111;
    width:160px;
   }
</style>
</head>
<body>
<img src='My Fixed Size Image' />
<div class='myClass' runat='server'>MY TEXT HERE</div>
</body>
jakejgordon
A: 

Why not just use a div by itself?

<div style="display:inline;background:url('img.png');width:128px;height:128px;">text here</div>
a2h
Your solution would probably work fine now that I am able to assume that i have a fixed width and height. My original question wouldn't have allowed for that though as i did not know the height of my image.
jakejgordon