<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>html tips &#187; CSS</title>
	<atom:link href="http://www.html-tips.net/css/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.html-tips.net</link>
	<description>css / html tips and techniques</description>
	<lastBuildDate>Wed, 21 Oct 2009 10:20:43 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.5</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>CSS: Text Over an Image</title>
		<link>http://www.html-tips.net/css-text-over-an-image/</link>
		<comments>http://www.html-tips.net/css-text-over-an-image/#comments</comments>
		<pubDate>Wed, 21 Oct 2009 09:31:54 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[CSS]]></category>

		<guid isPermaLink="false">http://www.html-tips.net/?p=171</guid>
		<description><![CDATA[There are a few different ways using CSS to display text over an image. This technique is widely used in templates, for example, if you have a look at the sidebar of this website, you will see that it has text over a gradient image! I have done this by setting a background image in [...]]]></description>
			<content:encoded><![CDATA[<p>There are a few different ways using CSS to display <strong>text over an image</strong>. This technique is widely used in templates, for example, if you have a look at the sidebar of this website, you will see that it has text over a gradient image! I have done this by setting a background image in CSS, this will be the first way I show you how to display text over an image. Another way to display some text over an image is to use relative positioning.</p>

<span id="more-171"></span>

<h3>Placing Text Over an Image Using background-image</h3>

<p>By utilizing the background-image CSS property, we can display the image in the background and text (or anything else, such as another image) in the foreground. This method is quite simple and straight forward, I recommend using this method over relative positioning, there are some benefits of using relative positioning though, so don't rule that out just yet! Okay, here is the code and a preview:<br /><br /></p>

<p><strong>CSS:</strong></p>


<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
</pre></td><td class="code"><pre class="css" style="font-family:monospace;"><span style="color: #6666ff;">.textimage</span> <span style="color: #00AA00;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">background-image</span><span style="color: #00AA00;">:</span> <span style="color: #993333;">url</span><span style="color: #00AA00;">&#40;</span><span style="color: #ff0000;">'http://www.html-tips.net/images/la-photo.jpg'</span><span style="color: #00AA00;">&#41;</span><span style="color: #00AA00;">;</span>
  <span style="color: #000000; font-weight: bold;">height</span><span style="color: #00AA00;">:</span> <span style="color: #933;">225px</span><span style="color: #00AA00;">;</span>
  <span style="color: #000000; font-weight: bold;">width</span><span style="color: #00AA00;">:</span> <span style="color: #933;">300px</span><span style="color: #00AA00;">;</span>
<span style="color: #00AA00;">&#125;</span></pre></td></tr></table></div>




<p>In the CSS, I defined the width and height of the image because I want the full image to be shown. If you want the size of the image to be defined by how much content is inside the div, just take out either the height, width or both, depending on which way you want the content to flow.<br /><br /><strong>HTML:</strong></p>


<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="html4strict" style="font-family:monospace;"><span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">div</span> <span style="color: #000066;">class</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;textimage&quot;</span>&gt;</span>Los Angeles, Taken From Grand Central Market<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">div</span>&gt;</span></pre></td></tr></table></div>




<p><strong>Preview:</strong></p>
<div style="height: 225px;width: 300px;background: url('http://www.html-tips.net/wp-content/uploads/2009/10/los-angeles-taken-from-grand-central-market-300x225.jpg');">Los Angeles, Taken From Grand Central Market</div>

<h4>Positioning Text Over an Image Using background-image</h4>

<p>Positioning can be done many ways, including the use of HTML line break tags (&lt;br /&gt;), but you can also do this in a more precise way, using CSS:<br /><br /></p>

<p><strong>CSS:</strong></p>


<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
</pre></td><td class="code"><pre class="css" style="font-family:monospace;"><span style="color: #6666ff;">.textimage</span> <span style="color: #00AA00;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">background-image</span><span style="color: #00AA00;">:</span> <span style="color: #993333;">url</span><span style="color: #00AA00;">&#40;</span><span style="color: #ff0000;">'http://www.html-tips.net/images/la-photo.jpg'</span><span style="color: #00AA00;">&#41;</span><span style="color: #00AA00;">;</span>
  <span style="color: #000000; font-weight: bold;">height</span><span style="color: #00AA00;">:</span> <span style="color: #933;">225px</span><span style="color: #00AA00;">;</span>
  <span style="color: #000000; font-weight: bold;">width</span><span style="color: #00AA00;">:</span> <span style="color: #933;">300px</span><span style="color: #00AA00;">;</span>
<span style="color: #00AA00;">&#125;</span>
<span style="color: #6666ff;">.textimage</span> p <span style="color: #00AA00;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">padding</span><span style="color: #00AA00;">:</span> <span style="color: #933;">120px</span> <span style="color: #933;">20px</span> <span style="color: #cc66cc;">0</span> <span style="color: #933;">80px</span><span style="color: #00AA00;">;</span>
  <span style="color: #000000; font-weight: bold;">color</span><span style="color: #00AA00;">:</span> <span style="color: #cc00cc;">#ffffff</span><span style="color: #00AA00;">;</span>
<span style="color: #00AA00;">&#125;</span></pre></td></tr></table></div>




<p>Simply add a paragraph tag to the HTML and CSS class and give that class some padding. When defining CSS padding, specify values in a clock-wise manner (top,right,bottom,left). I also made the text white, so the text is a bit more legible.<br /><br /><strong>HTML:</strong></p>


<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="html4strict" style="font-family:monospace;"><span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">div</span> <span style="color: #000066;">class</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;textimage&quot;</span>&gt;&lt;<span style="color: #000000; font-weight: bold;">p</span>&gt;</span>Los Angeles, Taken From Grand Central Market<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">p</span>&gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">div</span>&gt;</span></pre></td></tr></table></div>




<p><strong>Preview:</strong></p>
<div style="height: 225px;width: 300px;background: url('http://www.html-tips.net/wp-content/uploads/2009/10/los-angeles-taken-from-grand-central-market-300x225.jpg');"><p style="padding: 120px 20px 0 80px;color: #ffffff;">Los Angeles, Taken From Grand Central Market</p></div>

<h3>Placing Text Over an Image Using position relative</h3>

<p>This method allows you to keep the img tag and the alt attribute of your image, if that is needed. If it is not needed, I recommend using the background-image method. Here is the code:<br /><br /></p>

<p><strong>CSS:</strong></p>


<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
</pre></td><td class="code"><pre class="css" style="font-family:monospace;"><span style="color: #6666ff;">.textimage</span> p <span style="color: #00AA00;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">position</span><span style="color: #00AA00;">:</span> <span style="color: #993333;">relative</span><span style="color: #00AA00;">;</span>
  <span style="color: #000000; font-weight: bold;">top</span><span style="color: #00AA00;">:</span> <span style="color: #933;">120px</span><span style="color: #00AA00;">;</span>
  <span style="color: #000000; font-weight: bold;">left</span><span style="color: #00AA00;">:</span> <span style="color: #933;">80px</span><span style="color: #00AA00;">;</span>
  <span style="color: #000000; font-weight: bold;">right</span><span style="color: #00AA00;">:</span> <span style="color: #933;">20px</span><span style="color: #00AA00;">;</span>
  <span style="color: #000000; font-weight: bold;">width</span><span style="color: #00AA00;">:</span> <span style="color: #933;">200px</span><span style="color: #00AA00;">;</span>
  <span style="color: #000000; font-weight: bold;">height</span><span style="color: #00AA00;">:</span> <span style="color: #933;">40px</span><span style="color: #00AA00;">;</span> <span style="color: #808080; font-style: italic;">/* For getting rid of white space */</span>
  <span style="color: #000000; font-weight: bold;">color</span><span style="color: #00AA00;">:</span> <span style="color: #cc00cc;">#ffffff</span><span style="color: #00AA00;">;</span>
<span style="color: #00AA00;">&#125;</span>
<span style="color: #6666ff;">.textimage</span> img <span style="color: #00AA00;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">margin-top</span><span style="color: #00AA00;">:</span> <span style="color: #933;">-40px</span><span style="color: #00AA00;">;</span> <span style="color: #808080; font-style: italic;">/* For getting rid of white space */</span>
  <span style="color: #000000; font-weight: bold;">height</span><span style="color: #00AA00;">:</span> <span style="color: #933;">225px</span><span style="color: #00AA00;">;</span>
  <span style="color: #000000; font-weight: bold;">width</span><span style="color: #00AA00;">:</span> <span style="color: #933;">300px</span><span style="color: #00AA00;">;</span>
<span style="color: #00AA00;">&#125;</span></pre></td></tr></table></div>




<p>In the CSS, you will only need to style the p and img tags. Use top, right, bottom, left to position the paragraph tag. If you want the text to wrap around onto a new line, use the width property. When you define position as relative, the height of the original position of the p tag stays there as white space. To get rid of this white space, define a height for the p tag and then on the img tag, specify a margin-top to the negative value of the height defined for the p tag.<br /><br /><strong>HTML:</strong></p>


<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="html4strict" style="font-family:monospace;"><span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">div</span> <span style="color: #000066;">class</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;textimage&quot;</span>&gt;&lt;<span style="color: #000000; font-weight: bold;">img</span> <span style="color: #000066;">src</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;http://www.html-tips.net/images/la-photo.jpg&quot;</span> <span style="color: #000066;">alt</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;Los Angeles&quot;</span> <span style="color: #66cc66;">/</span>&gt;&lt;<span style="color: #000000; font-weight: bold;">p</span>&gt;</span>Los Angeles, Taken From Grand Central Market<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">p</span>&gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">div</span>&gt;</span></pre></td></tr></table></div>




<p><strong>Preview:</strong></p>
<p style="position: relative;top: 120px;left: 80px;right: 20px;width: 200px;height: 40px;color: #ffffff;">Los Angeles, Taken From Grand Central Market</p><img style="border: none;padding: 0;margin: 0;margin-top: -40px;height: 225px;width: 300px;" src="http://www.html-tips.net/wp-content/uploads/2009/10/los-angeles-taken-from-grand-central-market-300x225.jpg" alt="Los Angeles" />

<p><br /><br />That should just about wrap it up for the basics of placing text over an image.</p><div class="sexy-bookmarks sexy-bookmarks-expand sexy-bookmarks-bg-enjoy"><ul class="socials"><li class="sexy-twitter"><a href="http://twitter.com/home?status=CSS%3A+Text+Over+an+Image+-+http://b2l.me/syg7+" rel="nofollow" title="Tweet This!">Tweet This!</a></li><li class="sexy-facebook"><a href="http://www.facebook.com/share.php?u=http://www.html-tips.net/css-text-over-an-image/&amp;t=CSS%3A+Text+Over+an+Image" rel="nofollow" title="Share this on Facebook">Share this on Facebook</a></li><li class="sexy-delicious"><a href="http://del.icio.us/post?url=http://www.html-tips.net/css-text-over-an-image/&amp;title=CSS%3A+Text+Over+an+Image" rel="nofollow" title="Share this on del.icio.us">Share this on del.icio.us</a></li><li class="sexy-digg"><a href="http://digg.com/submit?phase=2&amp;url=http://www.html-tips.net/css-text-over-an-image/&amp;title=CSS%3A+Text+Over+an+Image" rel="nofollow" title="Digg this!">Digg this!</a></li><li class="sexy-reddit"><a href="http://reddit.com/submit?url=http://www.html-tips.net/css-text-over-an-image/&amp;title=CSS%3A+Text+Over+an+Image" rel="nofollow" title="Share this on Reddit">Share this on Reddit</a></li><li class="sexy-stumbleupon"><a href="http://www.stumbleupon.com/submit?url=http://www.html-tips.net/css-text-over-an-image/&amp;title=CSS%3A+Text+Over+an+Image" rel="nofollow" title="Stumble upon something good? Share it on StumbleUpon">Stumble upon something good? Share it on StumbleUpon</a></li><li class="sexy-myspace"><a href="http://www.myspace.com/Modules/PostTo/Pages/?u=http://www.html-tips.net/css-text-over-an-image/&amp;t=CSS%3A+Text+Over+an+Image" rel="nofollow" title="Post this to MySpace">Post this to MySpace</a></li><li class="sexy-technorati"><a href="http://technorati.com/faves?add=http://www.html-tips.net/css-text-over-an-image/" rel="nofollow" title="Share this on Technorati">Share this on Technorati</a></li><li class="sexy-mail"><a href="mailto:?subject=%22CSS%3A%20Text%20Over%20an%20Image%22&amp;body=I%20thought%20this%20article%20might%20interest%20you.%0A%0A%22There%20are%20a%20few%20different%20ways%20using%20CSS%20to%20display%20text%20over%20an%20image.%20This%20technique%20is%20widely%20used%20in%20templates%2C%20for%20example%2C%20if%20you%20have%20a%20look%20at%20the%20sidebar%20of%20this%20website%2C%20you%20will%20see%20that%20it%20has%20text%20over%20a%20gradient%20image%21%20I%20have%20done%20this%20by%20setting%20a%20background%20image%20in%20CSS%2C%20this%20will%20be%22%0A%0AYou%20can%20read%20the%20full%20article%20here%3A%20http://www.html-tips.net/css-text-over-an-image/" rel="nofollow" title="Email this to a friend?">Email this to a friend?</a></li><li class="sexy-google"><a href="http://www.google.com/bookmarks/mark?op=add&amp;bkmk=http://www.html-tips.net/css-text-over-an-image/&amp;title=CSS%3A+Text+Over+an+Image" rel="nofollow" title="Add this to Google Bookmarks">Add this to Google Bookmarks</a></li></ul><div style="clear:both;"></div></div>]]></content:encoded>
			<wfw:commentRss>http://www.html-tips.net/css-text-over-an-image/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
