<?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>chromakode &#187; code</title>
	<atom:link href="http://www.chromakode.com/category/code/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.chromakode.com</link>
	<description>Just another WordPress weblog</description>
	<lastBuildDate>Fri, 30 Apr 2010 17:50:39 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.1</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>WTF, CNN‽</title>
		<link>http://www.chromakode.com/2010/04/wtf-cnn/</link>
		<comments>http://www.chromakode.com/2010/04/wtf-cnn/#comments</comments>
		<pubDate>Wed, 28 Apr 2010 21:21:16 +0000</pubDate>
		<dc:creator>chromakode</dc:creator>
				<category><![CDATA[code]]></category>
		<category><![CDATA[breadpig]]></category>
		<category><![CDATA[cnn]]></category>
		<category><![CDATA[jquery]]></category>
		<category><![CDATA[mashup]]></category>
		<category><![CDATA[news]]></category>
		<category><![CDATA[wtf]]></category>

		<guid isPermaLink="false">http://www.chromakode.com/?p=10227</guid>
		<description><![CDATA[
Today, Alexis Ohanian and I released a new project: WTF CNN. We created the site to make it easy to compare CNN&#8217;s US front page with news sources from other countries. If you like what you see, pass it on, and check back the next time a big story emerges!
For the nerds: the site uses jQuery [...]]]></description>
			<content:encoded><![CDATA[<p style="text-align: center;"><a href="http://wtfcnn.com"><img class="size-medium wp-image-10258 aligncenter" title="wtfcnn" src="http://www.chromakode.com/wordpress/wp-content/uploads/2010/04/wtfcnn-550x376.png" alt="WTF CNN" width="550" height="376" /></a></p>
<p>Today, <a href="http://breadpig.com">Alexis Ohanian</a> and I released a new project: <a href="http://wtfcnn.com">WTF CNN</a>. We created the site to make it easy to compare CNN&#8217;s US front page with news sources from other countries. If you like what you see, pass it on, and check back the next time a big story emerges!</p>
<p>For the nerds: the site uses <a href="http://jquery.com">jQuery</a> for the animations. It also uses a lot of modern CSS3 styling with <a href="http://www.modernizr.com/">Modernizr</a> styles to ensure that things play nice in IE. This was my first time playing with Modernizr, and I&#8217;m super impressed by its simple approach to feature detection.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.chromakode.com/2010/04/wtf-cnn/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Hack: Wordpress cancel reply button</title>
		<link>http://www.chromakode.com/2009/02/wordpress-cancel-reply-button/</link>
		<comments>http://www.chromakode.com/2009/02/wordpress-cancel-reply-button/#comments</comments>
		<pubDate>Sat, 28 Feb 2009 08:34:21 +0000</pubDate>
		<dc:creator>chromakode</dc:creator>
				<category><![CDATA[code]]></category>
		<category><![CDATA[button]]></category>
		<category><![CDATA[comments]]></category>
		<category><![CDATA[hack]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[reply]]></category>
		<category><![CDATA[wordpress]]></category>

		<guid isPermaLink="false">http://www.chromakode.com/?p=783</guid>
		<description><![CDATA[I&#8217;m a huge fan of the commenting improvements introduced in Wordpress 2.7. It&#8217;s been a tremendous boon to have first-class threaded commenting features right out of the box.
However, one thing I&#8217;ve never understood is the decision to use an anchor element to cancel an open reply box. I&#8217;m far more accustomed to the reddit style [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;m a huge fan of the commenting improvements introduced in <a href="http://wordpress.org/development/2008/12/coltrane/">Wordpress 2.7</a>. It&#8217;s been a tremendous boon to have first-class threaded commenting features right out of the box.</p>
<p>However, one thing I&#8217;ve never understood is the decision to use an anchor element to cancel an open reply box. I&#8217;m far more accustomed to the reddit style of placing a cancel button next to the submit button, like this:</p>
<p><img class="aligncenter size-full wp-image-812 frame" title="A comment reply form from reddit" src="http://www.chromakode.com/wordpress/wp-content/uploads/2009/02/reddit-comment.png" alt="A comment reply form from reddit" width="411" height="159" /></p>
<p>The problem with the anchor element generated by Wordpress is that it looks really out of place when arranged next to the &#8220;submit comment&#8221; button. I had hoped there would be a parameter to change the anchor element into a button, but was disappointed to find that this particular HTML is hardcoded into the <code>get_cancel_comment_reply_link()</code> function.</p>
<p>However, the function does provide a filter hook, which I&#8217;ve used to make the change I desired. I simply intercept the HTML and return my own version with a button. It&#8217;s not very elegant, but it works.</p>
<p>If you&#8217;d like to use this hack, just add the following code to your theme&#8217;s <code>functions.php</code> file:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span class="co4">/**
 * Use an HTML button element for the cancel comment reply link.
 */</span>
<span class="kw2">function</span> cancel_comment_reply_button<span class="br0">&#40;</span><span class="re0">$html</span><span class="sy0">,</span> <span class="re0">$link</span><span class="sy0">,</span> <span class="re0">$text</span><span class="br0">&#41;</span> <span class="br0">&#123;</span>
    <span class="re0">$style</span> <span class="sy0">=</span> <span class="kw3">isset</span><span class="br0">&#40;</span><span class="re0">$_GET</span><span class="br0">&#91;</span><span class="st_h">'replytocom'</span><span class="br0">&#93;</span><span class="br0">&#41;</span> ? <span class="st_h">''</span> <span class="sy0">:</span> <span class="st_h">' style=&quot;display:none;&quot;'</span><span class="sy0">;</span>
    <span class="re0">$button</span> <span class="sy0">=</span> <span class="st_h">'&lt;button id=&quot;cancel-comment-reply-link&quot;'</span> <span class="sy0">.</span> <span class="re0">$style</span> <span class="sy0">.</span> <span class="st_h">'&gt;'</span><span class="sy0">;</span>
    <span class="kw1">return</span> <span class="re0">$button</span> <span class="sy0">.</span> <span class="re0">$text</span> <span class="sy0">.</span> <span class="st_h">'&lt;/button&gt;'</span><span class="sy0">;</span>
<span class="br0">&#125;</span>
&nbsp;
add_action<span class="br0">&#40;</span><span class="st_h">'cancel_comment_reply_link'</span><span class="sy0">,</span>
           <span class="st_h">'cancel_comment_reply_button'</span><span class="sy0">,</span> <span class="nu0">10</span><span class="sy0">,</span> <span class="nu0">3</span><span class="br0">&#41;</span><span class="sy0">;</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://www.chromakode.com/2009/02/wordpress-cancel-reply-button/feed/</wfw:commentRss>
		<slash:comments>9</slash:comments>
		</item>
		<item>
		<title>Socialite on AMO</title>
		<link>http://www.chromakode.com/2009/02/socialite-on-amo/</link>
		<comments>http://www.chromakode.com/2009/02/socialite-on-amo/#comments</comments>
		<pubDate>Mon, 02 Feb 2009 06:57:29 +0000</pubDate>
		<dc:creator>chromakode</dc:creator>
				<category><![CDATA[blog]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[firefox]]></category>
		<category><![CDATA[mozilla]]></category>
		<category><![CDATA[reddit]]></category>
		<category><![CDATA[socialite]]></category>

		<guid isPermaLink="false">http://www.chromakode.com/?p=742</guid>
		<description><![CDATA[As of approximately 7:08 am, Socialite is now publicly available on AMO (addons.mozilla.org).  It&#8217;s been a long time coming, and I&#8217;m excited to see how it&#8217;s received by the new users there.

]]></description>
			<content:encoded><![CDATA[<p style="text-align: left;">As of approximately 7:08 am, <a href="http://www.reddit.com/socialite/">Socialite</a> is now publicly <a href="https://addons.mozilla.org/en-US/firefox/addon/7799">available</a> on AMO (<a href="http://addons.mozilla.org">addons.mozilla.org</a>).  It&#8217;s been a long time coming, and I&#8217;m excited to see how it&#8217;s received by the new users there.</p>
<p style="text-align: center;"><a href="https://addons.mozilla.org/en-US/firefox/addon/7799"><img class="aligncenter size-full wp-image-744" title="Socialite in the Firefox add-ons manager" src="http://www.chromakode.com/wordpress/wp-content/uploads/2009/02/getsocialite.png" alt="Socialite in the Firefox add-ons manager" width="528" height="409" /></a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.chromakode.com/2009/02/socialite-on-amo/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>saidit, a Greasemonkey script for reddit</title>
		<link>http://www.chromakode.com/2009/01/saidit-a-greasemonkey-script-for-reddit/</link>
		<comments>http://www.chromakode.com/2009/01/saidit-a-greasemonkey-script-for-reddit/#comments</comments>
		<pubDate>Fri, 23 Jan 2009 08:06:13 +0000</pubDate>
		<dc:creator>chromakode</dc:creator>
				<category><![CDATA[code]]></category>
		<category><![CDATA[DOM storage]]></category>
		<category><![CDATA[greasemonkey]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[reddit]]></category>
		<category><![CDATA[saidit]]></category>
		<category><![CDATA[userscript]]></category>

		<guid isPermaLink="false">http://www.chromakode.com/?p=641</guid>
		<description><![CDATA[View and install the saidit Greasemonkey script on userscripts.org. Click here »
The saidit script enhances reddit comment pages by highlighting unread comments. It works by keeping track of the comments you&#8217;ve already viewed. Here&#8217;s what it looks like:

Features

Highlights unread reddit comments
Read comments are stored locally
Automatic removal of old data
Efficient implementation

Installation
If you&#8217;d like to give it [...]]]></description>
			<content:encoded><![CDATA[<div class="noticebox info">View and install the <a href="http://userscripts.org/scripts/show/40860">saidit</a> Greasemonkey script on userscripts.org. <a href="http://userscripts.org/scripts/show/40860">Click here »</a></div>
<p>The <a href="http://userscripts.org/scripts/show/40860">saidit</a> script enhances <a href="http://www.reddit.com">reddit</a> comment pages by highlighting unread comments. It works by keeping track of the comments you&#8217;ve already viewed. Here&#8217;s what it looks like:</p>
<p style="text-align: center;"><a href="http://www.reddit.com/r/funny/comments/7emij/original_drawing_of_a_7legged_spider_sells_for/c06gdav"><img class="aligncenter size-full wp-image-645 frame" title="Learning Gaucho theory with saidit" src="http://www.chromakode.com/wordpress/wp-content/uploads/2009/01/saidit.png" alt="saidit" width="500" height="210" /></a></p>
<h3>Features</h3>
<ul>
<li>Highlights unread reddit comments</li>
<li>Read comments are stored locally</li>
<li>Automatic removal of old data</li>
<li>Efficient implementation</li>
</ul>
<h3>Installation</h3>
<p>If you&#8217;d like to give it a spin, make sure you have <a href="https://addons.mozilla.org/en-US/firefox/addon/748">Greasemonkey</a> installed. Then, just install <a href="http://userscripts.org/scripts/show/40860">saidit</a> from userscripts.org. Have fun!</p>
<p class="note">
<p style="text-align: center;"><span id="more-641"></span></p>
<h3>How it works</h3>
<p>For every comment viewed on reddit, saidit records the comment id in a &#8220;seen comments list&#8221; using <a href="https://developer.mozilla.org/En/DOM/Storage">DOM Storage</a>. When a reddit comments page is loaded, saidit scans through each comment and checks if the id is in the list. If the id is not found, the class &#8220;unread&#8221;, is added to the comment div (this class can then be styled to emphasize unread comments). The seen comments list is then updated to include the new comments.</p>
<p>Since the script scans through hundreds of comments during the time-sensitive page loading process, I needed to make checking comment ids as fast as possible. Experimentation showed that if the seen comment ids were loaded into the fields of an <a href="https://developer.mozilla.org/En/Core_JavaScript_1.5_Reference/Global_Objects/Object">Object</a>, looking up ids was much faster than using an <a href="https://developer.mozilla.org/En/Core_JavaScript_1.5_Reference/Global_Objects/Array">Array</a>. Currently, saidit takes up to 2.5 seconds on the largest comment pages (200+) on my machine. I&#8217;m continuing to look into ways of making it faster.</p>
<h3>Interoperability</h3>
<p>An interesting side-effect of using DOM Storage is that read comment data is accessible to JavaScript on reddit and any other user scripts. If you can think of any clever ways to use this data, feel free to load it using the loading code below, or simply fork saidit to create a new script. Please use your new powers for good, not evil!</p>
<p>Here&#8217;s what the data loading code looks like, using Mozilla&#8217;s non-standard implementation of <a href="https://developer.mozilla.org/En/DOM/Storage">DOM Storage</a>:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span class="kw2">var</span> redditStorage <span class="sy0">=</span> globalStorage.<span class="me1">namedItem</span><span class="br0">&#40;</span><span class="st0">&quot;www.reddit.com&quot;</span><span class="br0">&#41;</span><span class="sy0">;</span>
<span class="kw2">var</span> pageThingID <span class="sy0">=</span> getThingID<span class="br0">&#40;</span>getPageThing<span class="br0">&#40;</span><span class="br0">&#41;</span><span class="br0">&#41;</span><span class="sy0">;</span>
<span class="kw2">var</span> seenArray <span class="sy0">=</span> <span class="br0">&#91;</span><span class="br0">&#93;</span><span class="sy0">;</span>
<span class="kw2">var</span> seenObject <span class="sy0">=</span> <span class="br0">&#123;</span><span class="br0">&#125;</span><span class="sy0">;</span>
&nbsp;
<span class="kw2">var</span> seenData <span class="sy0">=</span> redditStorage.<span class="me1">getItem</span><span class="br0">&#40;</span><span class="st0">&quot;seen-&quot;</span><span class="sy0">+</span>pageThingID<span class="br0">&#41;</span><span class="sy0">;</span>
seenArray <span class="sy0">=</span> seenData.<span class="me1">value</span>.<span class="me1">split</span><span class="br0">&#40;</span><span class="st0">&quot;,&quot;</span><span class="br0">&#41;</span><span class="sy0">;</span>
&nbsp;
<span class="co1">// Build an object for faster access</span>
seenArray.<span class="me1">forEach</span><span class="br0">&#40;</span><span class="kw2">function</span><span class="br0">&#40;</span>element<span class="br0">&#41;</span> <span class="br0">&#123;</span>
  seenObject<span class="br0">&#91;</span>element<span class="br0">&#93;</span> <span class="sy0">=</span> <span class="kw2">true</span><span class="sy0">;</span>
<span class="br0">&#125;</span><span class="br0">&#41;</span><span class="sy0">;</span></pre></div></div>

<p>The &#8220;unread&#8221; class can be styled in subreddit CSS to achieve different effects. Just use a CSS rule similar to the default (perhaps with !important added):</p>

<div class="wp_syntax"><div class="code"><pre class="css" style="font-family:monospace;"><span class="re1">.comment</span><span class="re1">.unread</span> <span class="re1">.tagline</span> <span class="br0">&#123;</span>
  <span class="kw1">font-weight</span><span class="sy0">:</span> <span class="kw2">bold</span><span class="sy0">;</span>
  <span class="kw1">background-color</span><span class="sy0">:</span> <span class="re0">#fea</span><span class="sy0">;</span>
  <span class="kw1">display</span><span class="sy0">:</span> <span class="kw2">inline</span><span class="sy0">;</span>
<span class="br0">&#125;</span></pre></div></div>

<p>If you discover any interesting ways to use these, please <a href="#respond">leave a comment</a>!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.chromakode.com/2009/01/saidit-a-greasemonkey-script-for-reddit/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Letting IE6 down easy</title>
		<link>http://www.chromakode.com/2008/12/letting-ie6-down-easy/</link>
		<comments>http://www.chromakode.com/2008/12/letting-ie6-down-easy/#comments</comments>
		<pubDate>Sun, 21 Dec 2008 10:50:50 +0000</pubDate>
		<dc:creator>chromakode</dc:creator>
				<category><![CDATA[code]]></category>
		<category><![CDATA[detect]]></category>
		<category><![CDATA[ie6]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[upgrade]]></category>
		<category><![CDATA[web]]></category>

		<guid isPermaLink="false">http://www.chromakode.com/?p=375</guid>
		<description><![CDATA[If you were browsing using Internet Explorer 6 right now, here&#8217;s what you&#8217;d see:

Note: Internet Explorer version 6 or lower has been detected.
Your browser cannot display this page properly. For compatibility, this page has been reduced to a low-graphics mode. To view this site the way it was intended, please upgrade Internet Explorer or switch [...]]]></description>
			<content:encoded><![CDATA[<p>If you were browsing using <a href="http://en.wikipedia.org/wiki/IE6">Internet Explorer 6</a> right now, here&#8217;s what you&#8217;d see:</p>
<div class="ie6alert">
<h1>Note: Internet Explorer version 6 or lower has been detected.</h1>
<p>Your browser cannot display this page properly. For compatibility, this page has been reduced to a low-graphics mode. To view this site the way it was intended, please upgrade Internet Explorer or <a href="http://browsehappy.com/browsers/">switch to another browser</a>.</div>
<p>This morning, I bit the bullet and finally took a look at my site in IE6. A few minutes later, after fruitlessly experimenting with compatibility scripts, I decided to formally drop IE6 support for this site. I&#8217;ve long felt that it&#8217;s a waste of effort to cater to browsers that don&#8217;t meet developers halfway, so I&#8217;ve typically put minimal effort into designing my pages around them. I&#8217;ve also felt that the best way to finally stab the stake into IE6&#8217;s heart is to simply let pages look bad and out of date in it. I&#8217;m pleased to see that this practice is finally gaining <a href="http://idroppedie6.com/">critical mass</a>.</p>
<p>Thus, I set out to find some JavaScript glue to formally explain my intentions to visitors. After surveying the options, some of which were a bit too <a href="http://www.stopie6.org/">preachy</a>, and others a bit too <a href="http://www.stopie6.com/">mild</a>, I decided to write my own little IE6 handler script.</p>
<p>My script, IE6unstyled.js, adds the header seen at the top of this post. The page is then displayed <strong>unstyled</strong> (based on <a href="http://dorward.me.uk/">David Dorward<a/>&#8217;s <a href="http://dorward.me.uk/software/disablecss/">bookmarklet</a>), which works well if your page degrades gracefully. Rather than allow IE6 to mangle the page layout, I choose to simply give it the raw content. This frees me from worrying about the presentation in IE6 and restores the layout to a basic, usable state. I feel that this projects the right message: to experience the current web to its fullest, you&#8217;d best switch from using IE6.</p>
<p><span id="more-375"></span><br />
Here&#8217;s the script.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">window.<span class="kw3">onload</span> <span class="sy0">=</span> <span class="kw2">function</span><span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span>
	<span class="kw1">for</span><span class="br0">&#40;</span>i<span class="sy0">=</span><span class="nu0">0</span><span class="sy0">;</span>i<span class="sy0">&lt;</span>document .<span class="me1">styleSheets</span>.<span class="me1">length</span><span class="sy0">;</span>i<span class="sy0">++</span><span class="br0">&#41;</span> <span class="br0">&#123;</span>
		document.<span class="me1">styleSheets</span>.<span class="kw1">item</span><span class="br0">&#40;</span>i<span class="br0">&#41;</span>.<span class="me1">disabled</span><span class="sy0">=</span><span class="kw2">true</span><span class="sy0">;</span>
	<span class="br0">&#125;</span>
	el <span class="sy0">=</span> document.<span class="me1">getElementsByTagName</span><span class="br0">&#40;</span><span class="st0">'*'</span><span class="br0">&#41;</span><span class="sy0">;</span>
	<span class="kw1">for</span> <span class="br0">&#40;</span> i<span class="sy0">=</span><span class="nu0">0</span><span class="sy0">;</span> i<span class="sy0">&lt;</span>el.<span class="me1">length</span><span class="sy0">;</span> i<span class="sy0">++</span><span class="br0">&#41;</span> <span class="br0">&#123;</span>
		el<span class="br0">&#91;</span>i<span class="br0">&#93;</span>.<span class="me1">style</span>.<span class="me1">cssText</span> <span class="sy0">=</span> <span class="st0">''</span><span class="sy0">;</span>
	<span class="br0">&#125;</span>
&nbsp;
	div <span class="sy0">=</span> document.<span class="me1">createElement</span><span class="br0">&#40;</span><span class="st0">'div'</span><span class="br0">&#41;</span><span class="sy0">;</span>
	div.<span class="me1">innerHTML</span> <span class="sy0">=</span> <span class="st0">'&lt;h1 style=&quot;font-size:18pt;&quot;&gt;Note: a version of Internet Explorer of 6 or lower has been detected.&lt;p&gt;Your browser cannot display this page properly. For compatibility, this page has been reduced to a low-graphics mode. To view this site the way it was intended, please upgrade Internet Explorer or &lt;a href=&quot;http://browsehappy.com/browsers/&quot;&gt;switch to another browser&lt;/a&gt;.&lt;/p&gt;'</span><span class="sy0">;</span>
	div.<span class="me1">style</span>.<span class="me1">cssText</span> <span class="sy0">=</span> <span class="st0">'color:black; background-color:yellow; border:5px solid black; padding:15px;'</span><span class="sy0">;</span>
	document.<span class="me1">body</span>.<span class="me1">insertBefore</span><span class="br0">&#40;</span>div<span class="sy0">,</span> document.<span class="me1">body</span>.<span class="me1">firstChild</span><span class="br0">&#41;</span><span class="sy0">;</span>
<span class="br0">&#125;</span><span class="sy0">;</span></pre></div></div>

<p>To use it, stash the file somewhere on your server. Then, add the following HTML to your header (edited to reflect the file location).</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span class="sc-1">&lt;!--[if lte IE 6]&gt;</span>
<span class="sc-1">	&lt;script src=&quot;path/to/IE6unstyled.js&quot; type=&quot;text/javascript&quot;&gt;&lt;/script&gt;</span>
<span class="sc-1">&lt;![endif]--&gt;</span></pre></div></div>

<p>Please feel free use this and make any modifications as you see fit.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.chromakode.com/2008/12/letting-ie6-down-easy/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>On clickable gradient effects using CSS</title>
		<link>http://www.chromakode.com/2008/12/fading-opacity-gradient-effect-using-css/</link>
		<comments>http://www.chromakode.com/2008/12/fading-opacity-gradient-effect-using-css/#comments</comments>
		<pubDate>Fri, 19 Dec 2008 06:06:26 +0000</pubDate>
		<dc:creator>chromakode</dc:creator>
				<category><![CDATA[code]]></category>
		<category><![CDATA[css]]></category>
		<category><![CDATA[howto]]></category>

		<guid isPermaLink="false">http://www.chromakode.com/?p=72</guid>
		<description><![CDATA[Alternatively, how to overlay an image on top of an element without interfering with clickable links underneath: 



How to makean element fade awayto transparencyusing CSS





Intro
I first saw this effect on a deviantART profile page following their &#8220;v6&#8243; redesign. Each profile has a long box of links to areas of interest regarding the profile owner. The [...]]]></description>
			<content:encoded><![CDATA[<p>Alternatively, how to overlay an image on top of an element without interfering with clickable links underneath: </p>
<div class="example clickable good">
<div class="container frame">
<div class="faded">
<a href="#">How to make</a><a href="#">an element fade away</a><a href="#">to transparency</a><a href="#">using CSS</a>
</div>
<div class="fade"></div>
</div>
</div>
<p><span id="more-72"></span></p>
<h3>Intro</h3>
<p>I first saw this effect on a deviantART profile page following their &#8220;v6&#8243; redesign. Each profile has a long box of links to areas of interest regarding the profile owner. The box is clipped and fades to transparency in its default state. When hovered over, the box expands to its full height, and the fade disappears.</p>
<p><img src="http://www.chromakode.com/wordpress/wp-content/uploads/2008/12/dahover.png" alt="deviantART profile links" title="deviantART profile links" width="550" height="300" class="aligncenter size-full wp-image-144" /></p>
<p>Honestly, I&#8217;m not crazy about the particular usage by deviantART. I think it obscures important links in a way that isn&#8217;t obvious to new users (most on-hover features are plagued by this disadvantage). However, the technique is useful whenever there is a long stream of continuous content that <em>won&#8217;t</em> be missed by your readers. For instance, I developed the trick behind this technique styling the &#8220;elsewhere&#8221; listing (by <a href="http://www.ibegin.com/labs/wp-lifestream/">Lifestream</a>) on the sidebar of this blog.</p>
<h3>First steps: the fade gradient</h3>
<p>The example I will present involves two divs adjacent in the markup: a faded element and an overlayed fade element. This was a constraint in my styled markup, though with a little tinkering, it could also work for an overlay element inside a container element. </p>
<p>Consider the following HTML markup:</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span class="sc3"><span class="re1">&lt;div</span> <span class="re0">class</span>=<span class="st0">&quot;faded&quot;</span><span class="re2">&gt;</span></span>
  <span class="sc3"><span class="re1">&lt;a</span> <span class="re0">href</span>=<span class="st0">&quot;#&quot;</span><span class="re2">&gt;</span></span>How to make<span class="sc3"><span class="re1">&lt;/a<span class="re2">&gt;</span></span></span>
  <span class="sc3"><span class="re1">&lt;a</span> <span class="re0">href</span>=<span class="st0">&quot;#&quot;</span><span class="re2">&gt;</span></span>an element fade away<span class="sc3"><span class="re1">&lt;/a<span class="re2">&gt;</span></span></span>
  <span class="sc3"><span class="re1">&lt;a</span> <span class="re0">href</span>=<span class="st0">&quot;#&quot;</span><span class="re2">&gt;</span></span>to transparency<span class="sc3"><span class="re1">&lt;/a<span class="re2">&gt;</span></span></span>
  <span class="sc3"><span class="re1">&lt;a</span> <span class="re0">href</span>=<span class="st0">&quot;#&quot;</span><span class="re2">&gt;</span></span>using CSS<span class="sc3"><span class="re1">&lt;/a<span class="re2">&gt;</span></span></span>
<span class="sc3"><span class="re1">&lt;/div<span class="re2">&gt;</span></span></span>
<span class="sc3"><span class="re1">&lt;div</span> <span class="re0">class</span>=<span class="st0">&quot;fade&quot;</span><span class="re2">&gt;</span><span class="re1">&lt;/div<span class="re2">&gt;</span></span></span></pre></div></div>

<p>With today&#8217;s CSS, I know of no standard way to mask the opacity of an element&#8217;s content with an image. Because of this, we&#8217;ll have to settle for merely the illusion of doing so. To produce the fade effect, we&#8217;ll give the faded element a solid background color and style the overlayed fade element with a gradient image from 0% opacity at the top to the background color at the bottom (<a href="http://www.chromakode.com/wordpress/wp-content/uploads/2008/12/fade.png">here&#8217;s an example</a>).</p>
<p>This way, any content within the faded element will transition to the background color, giving the illusion that it is fading away (while it is actually being obscured):</p>

<div class="wp_syntax"><div class="code"><pre class="css" style="font-family:monospace;"><span class="re1">.faded</span> <span class="br0">&#123;</span>
  <span class="kw1">background-color</span><span class="sy0">:</span> <span class="re0">#555</span><span class="sy0">;</span>
<span class="br0">&#125;</span>
&nbsp;
<span class="re1">.fade</span> <span class="br0">&#123;</span>
  <span class="kw1">height</span><span class="sy0">:</span> <span class="re3">100px</span><span class="sy0">;</span>
  <span class="kw1">background</span><span class="sy0">:</span> <span class="kw2">url</span><span class="br0">&#40;</span><span class="co2">/wordpress/wp-content/uploads/2008/12/fade.png</span><span class="br0">&#41;</span> <span class="kw2">repeat-x</span><span class="sy0">;</span>
<span class="br0">&#125;</span></pre></div></div>

<p>To position the fade on top of the faded content, I used a negative top margin. Since the height of my gradient is 100px, I displaced it upwards 100 pixels so that the bottom would be at the base of the target faded element.</p>

<div class="wp_syntax"><div class="code"><pre class="css" style="font-family:monospace;"><span class="re1">.fade</span> <span class="br0">&#123;</span> <span class="kw1">margin-top</span><span class="sy0">:</span> <span class="re3">-100px</span><span class="sy0">;</span> <span class="br0">&#125;</span></pre></div></div>

<p>Finally, to superimpose the fade element over the faded element, we&#8217;ll set a z-index value greater than 0. Since z-index only applies to positioned elements, we&#8217;ll need to also set both elements to have relative positioning.</p>

<div class="wp_syntax"><div class="code"><pre class="css" style="font-family:monospace;"><span class="re1">.faded</span> <span class="br0">&#123;</span>
  <span class="kw1">position</span><span class="sy0">:</span> <span class="kw2">relative</span><span class="sy0">;</span>
<span class="br0">&#125;</span>
&nbsp;
<span class="re1">.fade</span> <span class="br0">&#123;</span>
  <span class="kw1">position</span><span class="sy0">:</span> <span class="kw2">relative</span><span class="sy0">;</span>
  <span class="kw1">z-index</span><span class="sy0">:</span> <span class="nu0">10</span><span class="sy0">;</span>
<span class="br0">&#125;</span></pre></div></div>

<p>With these styles in place, we achieve a result that&#8217;s perfect visually:</p>
<div class="example bad">
<div class="container frame">
<div class="faded">
<a href="#">How to make</a><a href="#">an element fade away</a><a href="#">to transparency</a><a href="#">using CSS?</a>
</div>
<div class="fade"></div>
</div>
</div>
<p>Alas, the moment you try to click one of the links, you&#8217;ll realize that the fade element is obscuring the element below both visually and in terms of functionality. If you wanted to put clickable elements under the fade at this point, you&#8217;d be out of luck.</p>
<h3>Making links clickable</h3>
<p>A simple solution to this problem is to use CSS to remove the fade element when the mouse pointer is above it. We can achieve this by using the :hover pseudo-class to give the fade a <em>negative</em> z-index when it is hovered over, placing it underneath the faded element.</p>

<div class="wp_syntax"><div class="code"><pre class="css" style="font-family:monospace;"><span class="re1">.fade</span><span class="re2">:hover </span><span class="br0">&#123;</span> <span class="kw1">z-index</span><span class="sy0">:</span> -<span class="nu0">10</span><span class="sy0">;</span> <span class="br0">&#125;</span></pre></div></div>

<p>Unfortunately, there&#8217;s a problem with this proposal. If you&#8217;ve ever worked with selectors using the :hover pseudo-class, you may have encountered <strong>the flicker</strong>. As an illustrating example, try hovering your mouse pointer over this:</p>
<div class="example bad clickable">
<div class="container frame">
<div class="faded">
<a href="#">How <strong>not</strong> to make</a><a href="#">an element fade away</a><a href="#">to transparency</a><a href="#">using CSS</a>
</div>
<div class="fade"></div>
</div>
</div>
<p>The problem is that the instant the fade element falls beneath the faded element, it is no longer being hovered over by the pointer. It then switches back to its normal style, placing it over the faded element, where it is once again being hovered over. This creates an infinite loop: the browser rapidly switches between the :hover and non-:hover styles for the fade element.</p>
<h3>A solution</h3>
<p>Fortunately, there is a simple trick to stop the flicker and make this technique work. Ironically, the trick came to me as a direct result of my <a href="http://www.reddit.com/r/hurts_my_eyes/stylesheet.css">horrific abuse of :hover pseudo-classes</a> in the <a href="http://hurts_my_eyes.reddit.com">hurts_my_eyes subreddit</a>. The key is to ensure that the fade element cannot return to being above the faded element when it loses :hover status.</p>
<p>To break the flicker cycle, we&#8217;ll give the faded element z-index of 20 when it is hovered over:</p>

<div class="wp_syntax"><div class="code"><pre class="css" style="font-family:monospace;"><span class="re1">.faded</span><span class="re2">:hover </span><span class="br0">&#123;</span> <span class="kw1">z-index</span><span class="sy0">:</span> <span class="nu0">20</span><span class="sy0">;</span> <span class="br0">&#125;</span></pre></div></div>

<p>Like before, when the fade element is hovered over, it gets z-index: -10 and drops below, leaving the faded element hovered over. With this new rule since the faded element is hovered, it assumes a z-index of 20. Next, since the fade element is no longer hovered, it returns to its normal z-index of 10. Because the faded element now has higher z-index, and will as long as it is hovered over, no flicker cycle is created. The fade is effectively hidden until the mouse pointer leaves the area. Since the fade is displaced, links and other children of the faded element will be clickable.</p>
<p>Here&#8217;s an example employing this new rule, identical to the one at the top of this post.</p>
<div class="example clickable good">
<div class="container frame">
<div class="faded">
<a href="#">How to make</a><a href="#">an element fade away</a><a href="#">to transparency</a><a href="#">using CSS</a>
</div>
<div class="fade"></div>
</div>
</div>
<h3>Conclusion</h3>
<p>When dealing with the :hover pseudo-class, the straightforward way to style a behavior can sometimes introduce flicker. This happens when the :hover style results in the element no longer being hovered upon, creating an infinite loop. The trick to resolving the flicker is to find a way to break the cycle, ensuring that the element with :hover styling will not be hovered over twice. In the case of this howto, the addition of a single CSS rule made all the difference.</p>
<p>The core of the z-index trick is an interesting two-step process where the :hover style of the fade element on top triggers the :hover style of the faded element underneath. Utilizing this second :hover was the key to breaking the cycle. This technique enables us to complete the effect we were after using only simple CSS.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.chromakode.com/2008/12/fading-opacity-gradient-effect-using-css/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
