<?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>israeljernigan.com &#187; z-index</title>
	<atom:link href="http://www.israeljernigan.com/tag/z-index/feed" rel="self" type="application/rss+xml" />
	<link>http://www.israeljernigan.com</link>
	<description>personal web blogger</description>
	<lastBuildDate>Sat, 17 Oct 2009 16:28:47 +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>Reusing Images and Repetitive Content with CSS</title>
		<link>http://www.israeljernigan.com/reusing-images-and-repetitive-content-with-css</link>
		<comments>http://www.israeljernigan.com/reusing-images-and-repetitive-content-with-css#comments</comments>
		<pubDate>Fri, 16 Nov 2007 15:00:00 +0000</pubDate>
		<dc:creator>Israel Jernigan</dc:creator>
				<category><![CDATA[CSS]]></category>
		<category><![CDATA[accessability]]></category>
		<category><![CDATA[css]]></category>
		<category><![CDATA[image-background]]></category>
		<category><![CDATA[usability]]></category>
		<category><![CDATA[z-index]]></category>

		<guid isPermaLink="false">http://www.israeljernigan.com/reusing-images-and-repetitive-content-with-css</guid>
		<description><![CDATA[I work with websites that have a need for alot of images and repetitive content. One of the needs I&#8217;ve had to work with is that of not wanting to change the original image once it&#8217;s been uploaded, if the change is only temporary.
An example would be where you would need to make product links [...]]]></description>
			<content:encoded><![CDATA[<p>I work with websites that have a need for alot of images and repetitive content. One of the needs I&#8217;ve had to work with is that of not wanting to change the original image once it&#8217;s been uploaded, if the change is only temporary.</p>
<p>An example would be where you would need to make product links visually say &#8220;on sale&#8221;. And to do that without having to recreate thousands of images and reupload them.<br />
A preview of what we are trying to achieve.</p>
<p><img src="/post-content/onsale/saleExample.jpg" alt="Image change example" class="left" /></p>
<h3 class="clear">A few things that I want to keep in mind.</h3>
<ol>
<li>Usability for client</li>
<li>Reusable code/images</li>
<li>Accessibility</li>
<li>Valid xhtml</li>
</ol>
<p><span id="more-20"></span></p>
<p class="note">Note: Gladly you won&#8217;t need to compromise with any of these items to achieve the goal.</p>
<h3>Let&#8217;s start with the basic html we will need.</h3>
<p class="note">Note: &#8216;  »»&#8217; is for readability on this page, they are not part of the code.</p>
<pre>&lt;a href="#" title="View Item 602. On Sale Now!" class="box2"&gt;  »»&lt;img src="/post-content/onsale/602.jpg" alt="View Item 602. On Sale Now!"  /&gt;  »»

&lt;img src="/post-content/onsale/saleThmb.png" alt="This item is on sale!"   »»

height="0" width="0" class="sale" border="0"  /&gt;&lt;/a&gt;</pre>
<p>We have a <strong>link </strong>that has two images inside of it. The first image is the main image of the actual product we are trying to be linked. The second image is what we are going to use for the &#8220;on sale&#8221; part of the final link.</p>
<p>&#8220;Why does the second image have a <strong>height</strong> and <strong>width</strong> of 0&#8243;, you say? Ah, we&#8217;ll get to that in a bit.</p>
<p>Let&#8217;s get to the css first. We&#8217;ll start with the css for the <strong>link</strong>.</p>
<pre>.box2 {height:120px;

margin:6px 6px 0px 0px;

width:120px;

z-index:50;

position:relative;

float:left;

}</pre>
<p>We have a set <strong>height</strong> and <strong>width</strong> as well as a nice <strong>margin</strong> for the link. The margin is if there are going to be multiple links next to each other. They will line up nicely.</p>
<h3>What we are using z-index for</h3>
<p><img src="/post-content/onsale/z-index.jpg" alt="z-index layers example" class="left" /></p>
<p class="clear">I have set a <strong>z-index</strong> for the link. The reason that it is set at 50 is because I like to have some leeway with which way I can go on my z-index, and I don&#8217;t like to go into the negatives or go up or down by 1(it&#8217;s hard to check for errors). Also if you want z-index to work you will need to set a position for that element. I am using relative positioning.</p>
<h3>Now the code for the images</h3>
<p>Now let&#8217;s look at the css for the images. Starting with the first and then the second.</p>
<pre>
.box2 img{z-index:45;

position:relative;

height:120px;

width:120px;

border:0px;

}</pre>
<pre>
.box2 img.sale{position:absolute;

z-index:55;

top:0px;

left:0px;

border:0px;

height:120px;

width:120px;

}</pre>
<p>Now sense we didn&#8217;t want to recreate the image with the &#8220;on sale&#8221; on the original image we will need to put it on top of the other image. We accomplish this with a <strong>transparent png</strong>(or you could use gif) and <strong>z-index</strong>. We put the z-index for the image we want to be on top at a higher z-index, and the lower image to have a lower z-index. (Makes sense) We also set the position of the second image to <strong>absolute</strong>. This allows us to place that image wherever we want inside the link. And if we had not set the link to have a position the second image would not stay inside the link. Once we set the position we can place the image at the top and left of the link, with <strong>top</strong> and <strong>left</strong> set at <strong>0</strong>. Finally we set the <strong>height</strong> and <strong>width</strong> of the second image with css.</p>
<p>Now the reason we set the <strong>height</strong> and <strong>width</strong> of the second image in the xhtml to <strong>0</strong> is so that if the css or images are disabled the image will not be visible. I would rather my layout break, and break the way I want it to, than have an extra image laying around on the html page.</p>
<style type="text/css" media="screen">   a.box2 { float:left; height:120px; margin:6px 6px 0px 0px; width:120px; z-index:50; position:relative;} a.box2:link { border:none; }  a.box2 img { z-index:45; position:relative; height:120px; width:120px; border:0px; } a.box2 img.sale { position:absolute; z-index:55; top:0px; left:0px; border:0; height:120px; width:120px; } .footnote { color:#992200; } </style>
<p><a href="#" title="View Item 602." class="box2"><img src="/post-content/onsale/602.jpg" alt="View Item 602." /><img src="/post-content/onsale/saleThmb.png" alt="Item 602 is on sale!" class="sale" border="0" height="0" width="0" /></a></p>
<p class="clear">Viola! You know have reusable images and code for your product links. This will work in IE, Safari, and Mozilla browsers. The xhtml validates, and it&#8217;s totally accessable.</p>
<p>You can view the final result and copy the code <a href="/post-content/onsale/" title="The final result of this post is here">at this link.</a><br />
<a href="/post-content/onsale/" title="View the completed result of this post" class="big-button">View<br />
Final Result</a></p>
<p><a href="/post-content/onsale/saleExample.zip" title="Download example files here" class="big-button">Download<br />
Example Files</a></p>
<p>I am writing an article about a more advanced way to do this, and it will be posted soon. So check back within the next week!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.israeljernigan.com/reusing-images-and-repetitive-content-with-css/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
