<?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; howto</title>
	<atom:link href="http://www.chromakode.com/tag/howto/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>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>
