Thursday, June 12, 2008

Placing text over image using CSS position property

There are a few techniques available to perform this task using css. But I am going to show you the most effective one in this tutorial - this involves css position property.

I will be using this image (a cow) in this tutorial.
In our html code, there needs to be three div elements
1. container (for both image and text)
2. image (the cow)
3. text (Moooooo...)

Here is the code
<html>
<head>
<title>frinity.blogspot.com</title>
</head>
<body>
<div id="container">
<div><img src="cow.jpg" /></div>
<div style="position: absolute; left: 20px; top: 160px;">
<span style="font-weight: bold; color: #fff;">Moooooo...</span>
</div>
</div>
</body>
</html>


Its the CSS positioning that makes it happen.

Output (screenshot):

19 comments:

  1. nice tutorial. i have browsing similar tutorials. others used techniques like having the image as the element's background, but css positioning seems a better option to me.

    thanks!

    ReplyDelete
  2. needed this badly! thanks dude

    ReplyDelete
  3. you can also put in a z-index just to make sure that other elements do not cover the text...

    ReplyDelete
  4. not all the browsers support z-indexing (like mozilla). the method described here is the best..i guess!!

    ReplyDelete
  5. Just what I needed to know!

    Many many thanks

    ReplyDelete
  6. thnx .. this is wat i need...

    ReplyDelete
  7. Here's a much simper way to do the same thing:

    <div><img src="cow.jpg" />/div>

    <div style="position: absolute; left: 20px; top: 160px; font-weight: bold; color: #fff;">
    Moooooo...>/<div>

    ReplyDelete
  8. ok. but why wouldn't you make the image a background image of the div containing the 'mooo' text and position the text in the div by using the padding property of the div? thus text and image would stick together and could be handled more easily. or am i wrong here?

    ReplyDelete
  9. Wow thank you very very much !

    ReplyDelete
  10. the better use is
    style="position: relative;"

    ReplyDelete
  11. Needed this a lot, so thanks (:

    ReplyDelete
  12. can you help me with my css slicing? i need to put text on image.

    ReplyDelete
  13. this help a lot. tnx for the post.

    ReplyDelete
  14. Thank you so much for this tutorial, I had to float some text over a header image and all the other tutorials I found were so long winded I just got lost. Your tutorial was quick and to the point. Again, thank you!

    ReplyDelete
  15. This was completely unhelpful. Thanks.

    ReplyDelete