<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" media="screen" href="/~d/styles/rss2full.xsl"?><?xml-stylesheet type="text/css" media="screen" href="http://feeds.feedburner.com/~d/styles/itemcontent.css"?><rss 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/" xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0" version="2.0">

<channel>
	<title>The CSS Ninja</title>
	
	<link>http://www.thecssninja.com</link>
	<description>All things CSS, Javascript &amp; xhtml</description>
	<lastBuildDate>Mon, 30 Aug 2010 10:21:46 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=abc</generator>
		<atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="self" type="application/rss+xml" href="http://feeds.feedburner.com/TheCSSNinja" /><feedburner:info uri="thecssninja" /><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="hub" href="http://pubsubhubbub.appspot.com/" /><item>
		<title>Drag out files like Gmail</title>
		<link>http://feedproxy.google.com/~r/TheCSSNinja/~3/k3WCZGruoTM/gmail-dragout</link>
		<comments>http://www.thecssninja.com/javascript/gmail-dragout#comments</comments>
		<pubDate>Mon, 16 Aug 2010 12:53:42 +0000</pubDate>
		<dc:creator>The Css Ninja</dc:creator>
				<category><![CDATA[html5]]></category>
		<category><![CDATA[javascript]]></category>

		<guid isPermaLink="false">http://www.thecssninja.com/?p=703</guid>
		<description><![CDATA[Google in their quest to keep me busy in trying to figure out how they do their innovative features in Gmail are at it again. First it was drag and drop uploading which used a clever trick to make it work in Chrome which currently doesn&#8217;t support the FileReader in their stable release. Now they&#8217;ve [...]]]></description>
			<content:encoded><![CDATA[<p>Google in their quest to keep me busy in trying to figure out how they do their innovative features in Gmail are at it again. First it was <a href="http://www.thecssninja.com/javascript/gmail-upload ">drag and drop uploading</a> which used a clever trick to make it work in Chrome which currently doesn&#8217;t support the FileReader in their stable release. Now they&#8217;ve added the ability to drag out attachments to your file system, allowing you to bypass the usual method of the save dialog.<span id="more-703"></span></p>
<p>While the first feature of drag and drop uploading I figured out quite easily, this drag out feature was a doozy.</p>
<h2 class="subtitle02">How did I figure it out?</h2>
<p>There were two clues which got me started. One it only works in Chrome so it had to be an extension to the current Drag and Drop API. Two, after some poking around in gmail, there was a custom attribute on the attachment link called download_url which colon separated the attachments mime type, file name and download link.</p>
<p>Since Gmails JavaScript is obfuscated to within in an inch of its life there was no easy way to attach the built in debugger to anything that might give it away. So I tried downloading the script and running it through various unobfuscaters which made it format nicely but I still had to work with function names like vHG etc.</p>
<p>My last hope was <a href="http://code.google.com/p/chromium/issues/list">chromiums bug tracker</a> and searching to see if any bugs or feature requests were filed that could help give it away. I knew the download_url attribute played a role and it would be set using setData method on the dataTransfer object. So I searched high and low on Chromiums bug tracker for matches to &#8220;download_url&#8221;, &#8220;downloadurl&#8221; &#038; &#8220;downloadurl setData&#8221; nothing nada, zip. So I turned to the <a href="https://bugs.webkit.org/">webkit bug tracker</a>, still nothing! So I thought maybe Mozilla had discussed it on their bug tracker, a long shot but worth a try. Bingo! This <a href="https://bugzilla.mozilla.org/show_bug.cgi?id=570164">bug</a> led me to this <a href="https://bugs.webkit.org/show_bug.cgi?id=31090">bug</a>, on webkits bug tracker don&#8217;t ask why the search didn&#8217;t bring this up, and then onto this <a href="http://lists.whatwg.org/htdig.cgi/whatwg-whatwg.org/2009-August/022118.html">proposal</a>. We&#8217;re in business!</p>
<h2 class="subtitle02">How does it work?</h2>
<p>So we&#8217;ve found the details let&#8217;s play with it.</p>
<div class="resources01"><a target="_blank" class="demo" title="How to drag out files like gmail" href="http://www.thecssninja.com/demo/gmail_dragout/">View a live demo</a> <a target="_blank" class="demo source" title="Download the source of the Gmail drag out demo" href="http://www.thecssninja.com/demo/gmail_dragout/gmail_dragout.zip">Download the source files</a></div>
<p>Update: I&#8217;ve rolled out drag to desktop across my site for all my demo source files. If you&#8217;re using Chrome 5+ just drag the &#8220;Download the source files&#8221; link to your desktop!</p>
<p>The above demo will, in Chrome 5+, allow you to drag any of the items to your desktop and save them without having to go through the usual save dialog process.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> file <span style="color: #339933;">=</span> document.<span style="color: #660066;">getElementById</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;dragout&quot;</span><span style="color: #009900;">&#41;</span>;
&nbsp;
file.<span style="color: #660066;">addEventListener</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;dragstart&quot;</span><span style="color: #339933;">,</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>evt<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
    evt.<span style="color: #660066;">dataTransfer</span>.<span style="color: #660066;">setData</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;DownloadURL&quot;</span><span style="color: #339933;">,</span>fileDetails<span style="color: #009900;">&#41;</span>;
<span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span><span style="color: #003366; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span>;</pre></div></div>

<p>From the code above you attach an ondragstart event listener to something you want to &#8220;drag out&#8221; and save to your file system. On the dragstart event you set the data using the new &#8220;DownloadURL&#8221; type and pass file information to it.</p>
<p>In order to pass the correct information to the event we access the download_url attribute and use that for our setData call. I&#8217;ve made one change that is slightly different to how Gmail sets and gets the attribute.</p>

<div class="wp_syntax"><div class="code"><pre class="htrml4strict" style="font-family:monospace;">&lt;a href=&quot;Eadui.ttf&quot; id=&quot;dragout&quot; draggable=&quot;true&quot; data-downloadurl=&quot;
    application/octet-stream
    :Eadui.ttf
    :http://thecssninja.com/gmail_dragout/Eadui.ttf&quot;&gt;Font file&lt;/a&gt;</pre></div></div>

<p>Instead of creating a new attribute I have instead used the new <a href="http://www.w3.org/TR/html5/common-dom-interfaces.html#domstringmap-0">custom data attributes</a> specified in the HTML5 spec. While not officially supported by Chrome yet we can still use it and add a simple test for support and fork the code either way.</p>
<p>The custom attribute needs three things specified that are separated by colons in order for it to work. The files mime type, the name you wish it to be saved as (this can be anything) and the url to where the file can be downloaded from.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> fileDetails;
&nbsp;
<span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">typeof</span> file.<span style="color: #660066;">dataset</span> <span style="color: #339933;">===</span> <span style="color: #3366CC;">&quot;undefined&quot;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #006600; font-style: italic;">// Grab it the old way</span>
    fileDetails <span style="color: #339933;">=</span> file.<span style="color: #660066;">getAttribute</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;data-downloadurl&quot;</span><span style="color: #009900;">&#41;</span>;
<span style="color: #009900;">&#125;</span> <span style="color: #000066; font-weight: bold;">else</span> <span style="color: #009900;">&#123;</span>
    fileDetails <span style="color: #339933;">=</span> file.<span style="color: #660066;">dataset</span>.<span style="color: #660066;">downloadurl</span>;
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Above I do a check to see if the element supports the dataset attribute if not use getAttribute to grab the value.</p>
<h2 class="subtitle02">A cool finding</h2>
<p>Playing around with this new functionality I did find a cool side effect that if I drag a file that&#8217;s set the DownloadURL type on the setData method into a page using Firefox 3.6+ that will capture a drop event the dataTransfer file attribute will act as though it&#8217;s captured a local file and can be manipulated with the FileAPI e.g. In my <a href="http://labs.thecssninja.com/font_dragr/">font dragr</a> web app if I drag a font file that&#8217;s been attached to an email it will render the font as though I&#8217;ve dragged it from my local file system! Doing the same with Chrome 6, which also supports file attribute on the dataTransfer property, will ignore the drop.</p>
<h2 class="subtitle02">Predicting the next challenge from Google</h2>
<p>So what do I think the Gmail team will do next? Since they announced, and are now starting, their 6 week release cycle of chrome. I foresee the ability to upload folders not just individual files, being able to capture a photo from your web cam straight into an embedded picture using the proposed <a href="http://www.w3.org/TR/html-media-capture/">media capture proposal</a>. Whatever it is I&#8217;m sure it&#8217;ll knock everyone&#8217;s socks off and make me rack my brain in trying to figure it out.</p>
<p class="shorty01">Short URL: <a href="http://cssn.in/ja/028">http://cssn.in/ja/028</a></p>
<div class="clear">&nbsp;</div>
]]></content:encoded>
			<wfw:commentRss>http://www.thecssninja.com/javascript/gmail-dragout/feed</wfw:commentRss>
		<slash:comments>20</slash:comments>
		<feedburner:origLink>http://www.thecssninja.com/javascript/gmail-dragout</feedburner:origLink></item>
		<item>
		<title>Pure CSS collapsible tree menu</title>
		<link>http://feedproxy.google.com/~r/TheCSSNinja/~3/ihKHkbujjCs/css-tree-menu</link>
		<comments>http://www.thecssninja.com/css/css-tree-menu#comments</comments>
		<pubDate>Wed, 14 Jul 2010 11:52:53 +0000</pubDate>
		<dc:creator>The Css Ninja</dc:creator>
				<category><![CDATA[css]]></category>
		<category><![CDATA[css3]]></category>
		<category><![CDATA[pure css]]></category>

		<guid isPermaLink="false">http://www.thecssninja.com/?p=653</guid>
		<description><![CDATA[The classic tree view, we all know it, it&#8217;s used everywhere and it definitely can be useful in the right context. I&#8217;ve seen various examples about doing it with CSS and they&#8217;ve all required JavaScript. Not content with any of those solutions I investigated doing it with pure CSS, I got a good head start [...]]]></description>
			<content:encoded><![CDATA[<p>The classic tree view, we all know it, it&#8217;s used everywhere and it definitely can be useful in the right context. I&#8217;ve seen various examples about doing it with CSS and they&#8217;ve all required JavaScript. Not content with any of those solutions I investigated doing it with pure CSS, I got a good head start from my <a href="http://www.thecssninja.com/css/custom-inputs-using-css">Custom Radio and Checkbox inputs</a> article. From there I&#8217;ve come up with a solution that works pretty well.<span id="more-653"></span></p>
<div class="clear">&nbsp;</div>
<h2 class="subtitle02">Another demo, another bug</h2>
<p>Everything I seem to investigate lately seems to present itself with an annoying bug/feature in various browsers. Last time it was the inconsistency between browsers and generated content on form elements. This time it is WebKit not being able to apply styles using the checked pseudo-class in conjunction with a general sibling combinator (<code>E ~ F</code>) or chained adjacent sibling combinator (<code>E + F + F</code>). Making it very hard, and probably the reason I haven&#8217;t seen a CSS solution that works in WebKit browsers. I did come across this <a href="http://bradclicks.com/CSSplay/foldingList.html">demo</a> but due to the bug mentioned above doesn&#8217;t work in WebKit browsers.</p>
<p>So I soldiered on and came up with a pretty decent attempt, and remember folks I&#8217;m not a designer so be kinder this time with design critiques all I&#8217;m doing is showing you how to do the technique <img src='http://www.thecssninja.com/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> . With that out of the way let&#8217;s dig into the inner workings and road blocks I faced.</p>
<h2 class="subtitle02">General sibling combinators are flaky</h2>
<p>The CSS3 selector module has a very useful addition to compliment the CSS2.1 adjacent sibling combinator. Unlike the adjacent selector the general selector gives us some flexibility in that it will match a sibling that isn&#8217;t immediately preceded by our first element.</p>
<p>Great, I have 3 elements an &lt;input&gt;, &lt;label&gt; &#038; &lt;ol&gt; the general sibling combinator is the perfect tool to do things like <code>input:checked ~ ol</code>. Check Firefox, awesome works, Opera too! Woo! Surely WebKit will have this&#8230;nope nothing. Let me try <code>input ~ ol</code>, yep works across the board, *face palm*.</p>
<p>So I dug into <a href="https://bugs.WebKit.org/">WebKits bug tracker</a> and came out with <a href="https://bugs.WebKit.org/show_bug.cgi?id=12520">this bug</a> which has been around since 2007. Stating that general sibling combinators in combination with dynamic CSS, ala <code>:checked</code>, won&#8217;t reflect changes. Nor will it do chained adjacent combinator which was going to be my next solution.</p>
<p>However doing <code>:checked</code> with a single adjacent sibling combinator works fine in post 2008 WebKit browsers. So using this information I went about and built a working demo that has good browser support.</p>
<div class="resources01"><a target="_blank" class="demo" title="Pure CSS collapsible tree menu demonstration" href="http://www.thecssninja.com/demo/css_tree/">View live demo</a> <a target="_blank" class="demo source" title="Download the source of the Pure CSS collapsible tree menu" href="http://www.thecssninja.com/demo/css_tree/css_tree.zip">Download the source files</a></div>
<p>The demo is built using an ordered list (ol) nested with further ordered lists to naturally represent a basic folder structure.</p>

<div class="wp_syntax"><div class="code"><pre class="html4strict html4strict" style="font-family:monospace;"><span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">ol</span>&gt;</span>
    <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">li</span> <span style="color: #000066;">class</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;file&quot;</span>&gt;&lt;<span style="color: #000000; font-weight: bold;">a</span> <span style="color: #000066;">href</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;document.pdf&quot;</span>&gt;</span>File 1<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">a</span>&gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">li</span>&gt;</span>
    <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">li</span>&gt;</span>
        <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">label</span> <span style="color: #000066;">for</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;subfolder1&quot;</span>&gt;</span>Subfolder 1<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">label</span>&gt;</span>
        <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">input</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;checkbox&quot;</span> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;subfolder1&quot;</span> <span style="color: #66cc66;">/</span>&gt;</span>
        <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">ol</span>&gt;</span>
            <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">li</span> <span style="color: #000066;">class</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;file&quot;</span>&gt;&lt;<span style="color: #000000; font-weight: bold;">a</span> <span style="color: #000066;">href</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;&quot;</span>&gt;</span>File 2<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">a</span>&gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">li</span>&gt;</span>
            <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">li</span> <span style="color: #000066;">class</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;file&quot;</span>&gt;&lt;<span style="color: #000000; font-weight: bold;">a</span> <span style="color: #000066;">href</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;&quot;</span>&gt;</span>File 2<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">a</span>&gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">li</span>&gt;</span>
            <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">li</span> <span style="color: #000066;">class</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;file&quot;</span>&gt;&lt;<span style="color: #000000; font-weight: bold;">a</span> <span style="color: #000066;">href</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;&quot;</span>&gt;</span>File 2<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">a</span>&gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">li</span>&gt;</span>
        <span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">ol</span>&gt;</span>
    <span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">li</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">ol</span>&gt;</span></pre></div></div>

<p>As you can see, in order to get around the general combinator issue in WebKit based browsers I have switched the label to come first then the input so the &#8220;folders&#8221; could be expanded/collapsed by checking/unchecking the checkbox.</p>

<div class="wp_syntax"><div class="code"><pre class="css css" style="font-family:monospace;">li input
<span style="color: #00AA00;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">position</span><span style="color: #00AA00;">:</span> <span style="color: #993333;">absolute</span>;
    <span style="color: #000000; font-weight: bold;">left</span><span style="color: #00AA00;">:</span> 0;
    <span style="color: #000000; font-weight: bold;">margin-left</span><span style="color: #00AA00;">:</span> 0;
    opacity<span style="color: #00AA00;">:</span> 0;
    <span style="color: #000000; font-weight: bold;">z-index</span><span style="color: #00AA00;">:</span> <span style="color: #cc66cc;">2</span>;
    <span style="color: #000000; font-weight: bold;">cursor</span><span style="color: #00AA00;">:</span> <span style="color: #993333;">pointer</span>;
    <span style="color: #000000; font-weight: bold;">height</span><span style="color: #00AA00;">:</span> <span style="color: #933;">1em</span>;
    <span style="color: #000000; font-weight: bold;">width</span><span style="color: #00AA00;">:</span> <span style="color: #933;">1em</span>;
    <span style="color: #000000; font-weight: bold;">top</span><span style="color: #00AA00;">:</span> 0;
<span style="color: #00AA00;">&#125;</span>
li label
<span style="color: #00AA00;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">background</span><span style="color: #00AA00;">:</span> <span style="color: #993333;">url</span><span style="color: #00AA00;">&#40;</span>img<span style="color: #00AA00;">&#41;</span> <span style="color: #933;">15px</span> <span style="color: #933;">1px</span> <span style="color: #993333;">no-repeat</span>;
    <span style="color: #000000; font-weight: bold;">cursor</span><span style="color: #00AA00;">:</span> <span style="color: #993333;">pointer</span>;
    <span style="color: #000000; font-weight: bold;">display</span><span style="color: #00AA00;">:</span> <span style="color: #993333;">block</span>;
    <span style="color: #000000; font-weight: bold;">padding-left</span><span style="color: #00AA00;">:</span> <span style="color: #933;">37px</span>;
<span style="color: #00AA00;">&#125;</span></pre></div></div>

<p>To sit the input and label in the right visual order I absolutely positioned the input and applied a left padding to the label to push it out. That way changing the styles of the child ol, when the input is checked, can be done using the adjacent sibling combinator. I&#8217;ve also set the cursor to pointer when hovered over the input or label to visual show they&#8217;re clickable.</p>

<div class="wp_syntax"><div class="code"><pre class="css css" style="font-family:monospace;">li input <span style="color: #00AA00;">+</span> ol
<span style="color: #00AA00;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">background</span><span style="color: #00AA00;">:</span> <span style="color: #993333;">url</span><span style="color: #00AA00;">&#40;</span><span style="color: #ff0000; font-style: italic;">toggle-small-expand.png</span><span style="color: #00AA00;">&#41;</span> <span style="color: #933;">40px</span> 0 <span style="color: #993333;">no-repeat</span>;
    <span style="color: #000000; font-weight: bold;">margin</span><span style="color: #00AA00;">:</span> <span style="color: #933;">-0.938em</span> 0 0 <span style="color: #933;">-44px</span>; <span style="color: #808080; font-style: italic;">/* 15px */</span>
    <span style="color: #000000; font-weight: bold;">display</span><span style="color: #00AA00;">:</span> <span style="color: #993333;">block</span>;
    <span style="color: #000000; font-weight: bold;">height</span><span style="color: #00AA00;">:</span> <span style="color: #933;">1em</span>;
<span style="color: #00AA00;">&#125;</span></pre></div></div>

<p>Unlike my custom radio and checkbox article where I added the background image on the label, this time I had to do some trickery and apply it to the direct sibling ol of an input. Applying a sprite image to the ol wouldn&#8217;t be possible in this situation due to it being applied to the ol which would make it difficult to effectively hide other images within the sprite image.</p>
<p>To position the ol correctly I use a negative margin to pull it into the right location so it will sit next to the label and underneath the invisible checkbox.</p>

<div class="wp_syntax"><div class="code"><pre class="css css" style="font-family:monospace;">li input <span style="color: #00AA00;">+</span> ol <span style="color: #00AA00;">&gt;</span> li
<span style="color: #00AA00;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">height</span><span style="color: #00AA00;">:</span> 0;
    <span style="color: #000000; font-weight: bold;">overflow</span><span style="color: #00AA00;">:</span> <span style="color: #993333;">hidden</span>;
    <span style="color: #000000; font-weight: bold;">margin-left</span><span style="color: #00AA00;">:</span> <span style="color: #933;">-14px</span> !important;
    <span style="color: #000000; font-weight: bold;">padding-left</span><span style="color: #00AA00;">:</span> <span style="color: #933;">1px</span>;
<span style="color: #00AA00;">&#125;</span></pre></div></div>

<p>To hide the sub folders so they don&#8217;t appear when the parent folder is collapsed I target the child list items and set them to a zero height and hide any overflow.</p>

<div class="wp_syntax"><div class="code"><pre class="css css" style="font-family:monospace;">li label
<span style="color: #00AA00;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">background</span><span style="color: #00AA00;">:</span> <span style="color: #993333;">url</span><span style="color: #00AA00;">&#40;</span><span style="color: #ff0000; font-style: italic;">folder.png</span><span style="color: #00AA00;">&#41;</span> <span style="color: #933;">15px</span> <span style="color: #933;">1px</span> <span style="color: #993333;">no-repeat</span>;
<span style="color: #00AA00;">&#125;</span>
li<span style="color: #6666ff;">.file</span> a
<span style="color: #00AA00;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">background</span><span style="color: #00AA00;">:</span> <span style="color: #993333;">url</span><span style="color: #00AA00;">&#40;</span><span style="color: #ff0000; font-style: italic;">document.png</span><span style="color: #00AA00;">&#41;</span> 0 <span style="color: #933;">-1px</span> <span style="color: #993333;">no-repeat</span>;
    <span style="color: #000000; font-weight: bold;">color</span><span style="color: #00AA00;">:</span> <span style="color: #cc00cc;">#fff</span>;
    <span style="color: #000000; font-weight: bold;">padding-left</span><span style="color: #00AA00;">:</span> <span style="color: #933;">21px</span>;
    <span style="color: #000000; font-weight: bold;">text-decoration</span><span style="color: #00AA00;">:</span> <span style="color: #993333;">none</span>;
    <span style="color: #000000; font-weight: bold;">display</span><span style="color: #00AA00;">:</span> <span style="color: #993333;">block</span>;
<span style="color: #00AA00;">&#125;</span></pre></div></div>

<p>To differentiate between folders and files I applied a background image to either the label or to an anchor within a list item for files.</p>

<div class="wp_syntax"><div class="code"><pre class="css css" style="font-family:monospace;">li
<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: #000000; font-weight: bold;">margin-left</span><span style="color: #00AA00;">:</span> <span style="color: #933;">-15px</span>;
    <span style="color: #000000; font-weight: bold;">list-style</span><span style="color: #00AA00;">:</span> <span style="color: #993333;">none</span>;
<span style="color: #00AA00;">&#125;</span>
li<span style="color: #6666ff;">.file</span>
<span style="color: #00AA00;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">margin-left</span><span style="color: #00AA00;">:</span> <span style="color: #933;">-1px</span> !important;
<span style="color: #00AA00;">&#125;</span></pre></div></div>

<p>To pull out the folder list items I apply a larger negative margin so the folder will line up with any of the file icons, and for file based list items I reset the left margin so they sit flush.</p>
<h2 class="subtitle02">Change icon based on file extension</h2>
<p>With some CSS3 attribute selectors we can determine an anchor links file format and change the icon accordingly.</p>

<div class="wp_syntax"><div class="code"><pre class="css css" style="font-family:monospace;">li<span style="color: #6666ff;">.file</span> a<span style="color: #00AA00;">&#91;</span>href $<span style="color: #00AA00;">=</span> <span style="color: #ff0000;">'.pdf'</span><span style="color: #00AA00;">&#93;</span>     <span style="color: #00AA00;">&#123;</span> <span style="color: #000000; font-weight: bold;">background-position</span><span style="color: #00AA00;">:</span> <span style="color: #933;">-16px</span> <span style="color: #933;">-1px</span>; <span style="color: #00AA00;">&#125;</span>
li<span style="color: #6666ff;">.file</span> a<span style="color: #00AA00;">&#91;</span>href $<span style="color: #00AA00;">=</span> <span style="color: #ff0000;">'.html'</span><span style="color: #00AA00;">&#93;</span>   <span style="color: #00AA00;">&#123;</span> <span style="color: #000000; font-weight: bold;">background-position</span><span style="color: #00AA00;">:</span> <span style="color: #933;">-32px</span> <span style="color: #933;">-1px</span>; <span style="color: #00AA00;">&#125;</span>
li<span style="color: #6666ff;">.file</span> a<span style="color: #00AA00;">&#91;</span>href $<span style="color: #00AA00;">=</span> <span style="color: #ff0000;">'.css'</span><span style="color: #00AA00;">&#93;</span>    <span style="color: #00AA00;">&#123;</span> <span style="color: #000000; font-weight: bold;">background-position</span><span style="color: #00AA00;">:</span> <span style="color: #933;">-48px</span> <span style="color: #933;">-1px</span>; <span style="color: #00AA00;">&#125;</span>
li<span style="color: #6666ff;">.file</span> a<span style="color: #00AA00;">&#91;</span>href $<span style="color: #00AA00;">=</span> <span style="color: #ff0000;">'.js'</span><span style="color: #00AA00;">&#93;</span>      <span style="color: #00AA00;">&#123;</span> <span style="color: #000000; font-weight: bold;">background-position</span><span style="color: #00AA00;">:</span> <span style="color: #933;">-64px</span> <span style="color: #933;">-1px</span>; <span style="color: #00AA00;">&#125;</span></pre></div></div>

<p>Using the <code>$=</code> CSS attribute selector allows us to check the end of an attribute exactly ends in .pdf, .html etc.</p>
<p>If for some reason you attribute doesn&#8217;t end with your file extension, your anchor may have a query string on the end. We can still match file types.</p>

<div class="wp_syntax"><div class="code"><pre class="css css" style="font-family:monospace;">li<span style="color: #6666ff;">.file</span> a<span style="color: #00AA00;">&#91;</span>href <span style="color: #00AA00;">*=</span> <span style="color: #ff0000;">'.pdf'</span><span style="color: #00AA00;">&#93;</span>   <span style="color: #00AA00;">&#123;</span> <span style="color: #000000; font-weight: bold;">background-position</span><span style="color: #00AA00;">:</span> <span style="color: #933;">-16px</span> <span style="color: #933;">-1px</span>; <span style="color: #00AA00;">&#125;</span>
li<span style="color: #6666ff;">.file</span> a<span style="color: #00AA00;">&#91;</span>href <span style="color: #00AA00;">*=</span> <span style="color: #ff0000;">'.html'</span><span style="color: #00AA00;">&#93;</span>  <span style="color: #00AA00;">&#123;</span> <span style="color: #000000; font-weight: bold;">background-position</span><span style="color: #00AA00;">:</span> <span style="color: #933;">-32px</span> <span style="color: #933;">-1px</span>; <span style="color: #00AA00;">&#125;</span></pre></div></div>

<p>The <code>*=</code> will match a substring that contains .pdf or .html anywhere within the attribute and if a href has a query string we can still match our file extension without issue. This does have the slight disadvantage that it will match it anywhere e.g. if you have a file called file.html.pdf it will match both file types and the one with the higher CSS specificity will be applied or incase of the example above their CSS specificity is the same so the html background will be applied.</p>
<h2 class="subtitle02">Checkbox attributes</h2>
<p>In the demo by default the first folder is open, this is done by adding the checked attribute to the checkbox which will trigger our styles thanks to the checked pseudo-class and reveal its sub files and folders.</p>
<p>We can also add the disabled attribute to the checkbox to stop a user from opening a folder as the input can neither be checked or unchecked.</p>
<p>Lastly using a combination of both disabled and checked will allow us to reveal the sub files and folders but not allow the user to close the top level folder.</p>
<h2 class="subtitle02">Browser support</h2>
<p>Based on testing this will work in any CSS3 selector supporting browser. The following have been tested and known to work</p>
<ul>
<li>Firefox 1+</li>
<li>Opera 9.6+</li>
<li>Safari 4+</li>
<li>iPhone/iPod Safari</li>
<li>Chrome 1+</li>
<li>Android</li>
<li>IE9+</li>
</ul>
<p>This could very well work in IE8 but would require some JavaScript to get IE8 to interpret the checked pseudo-class, which I won&#8217;t be going into.</p>
<p>Right now I use conditional comments to hide the stylesheet from all versions of IE and another conditional comment to load the stylesheet for IE9 and greater.</p>

<div class="wp_syntax"><div class="code"><pre class="html4strict html4strict" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">&lt;!--[if gte IE 9 ]&gt;</span>
<span style="color: #808080; font-style: italic;">    &lt;link rel=&quot;stylesheet&quot; type=&quot;text/css&quot; href=&quot;_styles.css&quot; media=&quot;screen&quot;&gt;</span>
<span style="color: #808080; font-style: italic;">&lt;![endif]--&gt;</span>
<span style="color: #808080; font-style: italic;">&lt;!--[if !IE]&gt;--&gt;</span>
    <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">link</span> <span style="color: #000066;">rel</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;stylesheet&quot;</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;text/css&quot;</span> <span style="color: #000066;">href</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;_styles.css&quot;</span> <span style="color: #000066;">media</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;screen&quot;</span>&gt;</span>
<span style="color: #808080; font-style: italic;">&lt;!--&lt;![endif]--&gt;</span></pre></div></div>

<h2 class="subtitle02">Highly scalable</h2>
<p>This technique will cater for a large amount of sub folders and files. It&#8217;s governed by your screen real estate and even then it&#8217;ll apply scroll bars to the document when the tree structure gets too long or wide.</p>
<p>Any questions/comments/suggestions leave a comment.</p>
<p class="shorty01">Short URL: <a href="http://cssn.in/ja/026">http://cssn.in/ja/026</a></p>
<div class="clear">&nbsp;</div>
]]></content:encoded>
			<wfw:commentRss>http://www.thecssninja.com/css/css-tree-menu/feed</wfw:commentRss>
		<slash:comments>17</slash:comments>
		<feedburner:origLink>http://www.thecssninja.com/css/css-tree-menu</feedburner:origLink></item>
		<item>
		<title>How Gmail’s drag and drop works and why it’s not supported in Safari</title>
		<link>http://feedproxy.google.com/~r/TheCSSNinja/~3/tXqQc8DSJcw/gmail-upload</link>
		<comments>http://www.thecssninja.com/javascript/gmail-upload#comments</comments>
		<pubDate>Wed, 21 Apr 2010 12:42:51 +0000</pubDate>
		<dc:creator>The Css Ninja</dc:creator>
				<category><![CDATA[javascript]]></category>

		<guid isPermaLink="false">http://www.thecssninja.com/?p=682</guid>
		<description><![CDATA[Recently Gmail pushed out an update that allowed users to drag and drop files from desktop to Gmail and have them automatically uploaded. Being the web geek I am I had to figure out how it functioned. Firefox was easy and I have covered drag and drop uploading already. They also mentioned in their post [...]]]></description>
			<content:encoded><![CDATA[<p>Recently Gmail pushed out an update that allowed users to drag and drop files from desktop to Gmail and have them automatically uploaded. Being the web geek I am I had to figure out how it functioned. Firefox was easy and I have covered <a href="http://www.thecssninja.com/javascript/fileapi">drag and drop uploading</a> already. They also mentioned in their post that Chrome was supported but I know Chrome is yet to implement the File API. Most intriguing was that it doesn&#8217;t work in Safari?<span id="more-682"></span></p>
<div class="resources01"><a href="http://www.thecssninja.com/demo/gmail_upload/" title="How Gmail's drag and drop works and why it's not supported in Safari " class="demo" target="_blank">View a live demo</a> <a href="http://www.thecssninja.com/demo/gmail_upload/gmail_upload.zip" title="Download the source of the Gmail drag and drop upload demo" class="demo source" target="_blank">Download the source files</a></div>
<p>In the above demo I&#8217;ve recreated a mock Gmail UI that works the same as the actual Gmail with drag drop uploading. It feature detects the browsers capabilities and depending on it will use the File API or another method, which I will delve into. The beauty of feature detection is once Chrome and other browsers add support for the File API the alternative method will become defunct and would require no maintenance for the code.</p>
<p>I have intentionally left out the File API code that would allow Firefox to function as I wanted to focus on Chrome’s implementation.</p>
<h2 class="subtitle02">So how does Chrome do it?</h2>
<p>Using a bit of CSS trickery we can create the illusion of having File API support and still allow users to drag and drop files from the desktop into Gmail. I accomplish this by having the drop zone, which becomes visible on a <code>dragenter</code> event, contain an invisible file input with a 100% width and height of the drop zone area. The file input also has the attribute multiple on it allowing a user to drop multiple files.</p>

<div class="wp_syntax"><div class="code"><pre class="html4strict html4strict" style="font-family:monospace;"><span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">div</span> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;drop&quot;</span>&gt;</span>
    <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">h1</span>&gt;</span>Drop files here<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">h1</span>&gt;</span>
    <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">p</span>&gt;</span>To add them as attachments<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">p</span>&gt;</span>
    <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">input</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;file&quot;</span> <span style="color: #000066;">multiple</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;true&quot;</span> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;filesUpload&quot;</span> <span style="color: #66cc66;">/</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">div</span>&gt;</span></pre></div></div>

<p>The drop zone is hidden by default and only appears when the body detects the <code>dragenter</code> event.</p>

<div class="wp_syntax"><div class="code"><pre class="css css" style="font-family:monospace;"><span style="color: #cc00cc;">#drop</span> <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;">&#125;</span>
<span style="color: #cc00cc;">#drop</span> input <span style="color: #00AA00;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">position</span><span style="color: #00AA00;">:</span> <span style="color: #993333;">absolute</span>;
    <span style="color: #000000; font-weight: bold;">width</span><span style="color: #00AA00;">:</span> <span style="color: #933;"><span style="color: #cc66cc;">100</span>%</span>;
    <span style="color: #000000; font-weight: bold;">height</span><span style="color: #00AA00;">:</span> <span style="color: #933;"><span style="color: #cc66cc;">100</span>%</span>;
    <span style="color: #000000; font-weight: bold;">top</span><span style="color: #00AA00;">:</span> 0;
    <span style="color: #000000; font-weight: bold;">left</span><span style="color: #00AA00;">:</span> 0;
    opacity<span style="color: #00AA00;">:</span> 0;
<span style="color: #00AA00;">&#125;</span></pre></div></div>

<p>The file input inside the drop zone has position absolute, width and height of 100% and its opacity set to 0 so it can still be interacted with but not seen.</p>
<h2 class="subtitle02">Handling the drop</h2>
<p>Unlike Firefox 3.6 we don&#8217;t bother listening for the drop event in the drop zone as Chrome is using the file input. We instead attach a change event to the file input which will trigger when a file is selected through the browse screen or in our case when a file is dropped on the input, and since we added the multiple attribute we can drop more than one file at a time.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript javascript" style="font-family:monospace;">dropArea.<span style="color: #660066;">addEventListener</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;change&quot;</span><span style="color: #339933;">,</span> TCNDDU.<span style="color: #660066;">handleDrop</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span>;
&nbsp;
TCNDDU.<span style="color: #660066;">handleDrop</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span> <span style="color: #009900;">&#40;</span>evt<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #003366; font-weight: bold;">var</span> files <span style="color: #339933;">=</span> evt.<span style="color: #660066;">target</span>.<span style="color: #660066;">files</span>;
    dropArea.<span style="color: #660066;">style</span>.<span style="color: #660066;">display</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;none&quot;</span>;
    <span style="color: #000066; font-weight: bold;">for</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">var</span> i <span style="color: #339933;">=</span> 0<span style="color: #339933;">,</span> len <span style="color: #339933;">=</span> files.<span style="color: #660066;">length</span>; i <span style="color: #339933;">&lt;</span> len; i<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #006600; font-style: italic;">// iterate over file(s) and process them for uploading</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>;</pre></div></div>

<p>The function that handles the change event looks at the javascript event target to access the files array which contains the dropped file(s) name, size and type. From there we can iterate over the files and queue them to be uploaded.</p>
<h2 class="subtitle02">Uploading the file</h2>
<p>I&#8217;ve updated the demo to now upload the file. Using XHR2 and the <a href="http://www.w3.org/TR/2009/WD-XMLHttpRequest2-20090820/#the-upload-attribute">upload attribute</a> to attach progress events so we can do a real progress bar that indicates to the user how much the file has uploaded.</p>
<p>Drop the file or files into the drop zone in Chrome or Safari and the files will be uploaded.</p>
<p>Have a read of Andrea Giammarchi <a href="http://webreflection.blogspot.com/2009/03/safari-4-multiple-upload-with-progress.html">Safari 4 Multiple Upload With Progress Bar</a> article. I used the PHP from that article to handle the upload.</p>
<h2 class="subtitle02">Why doesn&#8217;t it work in Safari?</h2>
<p>Technically it does work in Safari but only if you drop one file at a time which isn&#8217;t what users would expect. Dropping multiple files will populate the files array in the file input, but selecting say 3 files and dragging them into Safari will register 3 files have been dragged but if you iterate over the files array those 3 files will all be the same file&#8230;</p>
<p>To explain it better let&#8217;s say you have 3 files each one called a.jpg, b.jpg and c.jpg selecting those 3 files and then clicking and dragging on the b.jpg file will add 3 files to the input. The file you did the drag operation on will be added 3 times to the input and it will ignore the other files, iterating over the files array will have b.jpg 3 times?!</p>
<p>To add to the strangeness if I have an input that is visible and I click the choose file/browse button and select multiple files in the dialog box upon clicking open the files array will correctly be populated with all the files selected and won&#8217;t have the duplication issue.</p>
<p>That folks is why I believe Safari isn&#8217;t supported in the new drag and drop upload feature in Gmail. I&#8217;m sure Google has tapped Apple on the shoulder to get that fixed.</p>
<p><strong>Update:</strong> <a href="#comment-1480">Paul</a> has filed a <a href="https://bugs.webkit.org/show_bug.cgi?id=37957">ticket</a> in webkits bug tracker.</p>
<h2 class="subtitle02">A work around?</h2>
<p>The only work around that Google could do is to break out of the loop if they detect multiple instances of the same file being referenced. This work around would limit Safari to only 1 file per drag and drop operation.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript javascript" style="font-family:monospace;">TCNDDU.<span style="color: #660066;">handleDrop</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span> <span style="color: #009900;">&#40;</span>evt<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #003366; font-weight: bold;">var</span> files <span style="color: #339933;">=</span> evt.<span style="color: #660066;">target</span>.<span style="color: #660066;">files</span>;
&nbsp;
    <span style="color: #000066; font-weight: bold;">for</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">var</span> i <span style="color: #339933;">=</span> 0<span style="color: #339933;">,</span> len <span style="color: #339933;">=</span> files.<span style="color: #660066;">length</span>; i <span style="color: #339933;">&lt;</span> len; i<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>i <span style="color: #339933;">!=</span> 0 <span style="color: #339933;">&amp;&amp;</span> files<span style="color: #009900;">&#91;</span>0<span style="color: #009900;">&#93;</span>.<span style="color: #660066;">fileName</span> <span style="color: #339933;">===</span> files<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span>.<span style="color: #660066;">fileName</span><span style="color: #009900;">&#41;</span> <span style="color: #000066; font-weight: bold;">continue</span>;
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>;</pre></div></div>

<p>Checking the first file name against other file names and making sure they don&#8217;t match, if they do skip that iteration and go to the next. If the file names are all the same only the first will be processed but you still get the benefit of drag and drop action in Safari. Not ideal and an error message could be shown to the user letting them know why only 1 file appears.</p>
<h2 class="subtitle02">Still sometime to go</h2>
<p>From my investigation it seems Safari isn&#8217;t far off from getting some drag and drop love, they just need to push out a fix for the bug mentioned above or Google could limit drag and drop to first file only for Safari.</p>
<p>I don&#8217;t expect to see this functionality in any version of IE until they support the File API. Dragging and dropping a file into a file input doesn&#8217;t work nor does IE have a files array for the file input.</p>
<p>Opera is a long way from having this functionality they have yet to implement the Drag and Drop API and their file inputs work the same as IE.</p>
<p>Our best bet of getting cross browser drag and drop uploading is with the Drag and Drop and File API. Let&#8217;s hope we see some other browser vendors add this soon.</p>
<p class="shorty01">Short URL: <a href="http://cssn.in/ja/027">http://cssn.in/ja/027</a></p>
<div class="clear">&nbsp;</div>
]]></content:encoded>
			<wfw:commentRss>http://www.thecssninja.com/javascript/gmail-upload/feed</wfw:commentRss>
		<slash:comments>25</slash:comments>
		<feedburner:origLink>http://www.thecssninja.com/javascript/gmail-upload</feedburner:origLink></item>
		<item>
		<title>Futurebox, lightbox without the javascript and target pseudo-class</title>
		<link>http://feedproxy.google.com/~r/TheCSSNinja/~3/6TYx4NCy35w/futurebox3</link>
		<comments>http://www.thecssninja.com/css/futurebox3#comments</comments>
		<pubDate>Sat, 10 Apr 2010 02:05:14 +0000</pubDate>
		<dc:creator>The Css Ninja</dc:creator>
				<category><![CDATA[css]]></category>
		<category><![CDATA[css3]]></category>
		<category><![CDATA[futurebox]]></category>
		<category><![CDATA[modals]]></category>

		<guid isPermaLink="false">http://www.thecssninja.com/?p=633</guid>
		<description><![CDATA[So I recently released a revised version of Futurebox which added a lot of functionality. But one thing was nagging me, the fact that it utilised the target pseudo-class to hijack in page anchors which has a few side effects that can create some major drawbacks to the technique. One being that if you click [...]]]></description>
			<content:encoded><![CDATA[<p>So I recently released a <a href="http://www.thecssninja.com/xhtml/futurebox">revised version of Futurebox</a> which added a lot of functionality. But one thing was nagging me, the fact that it utilised the <a href="http://www.w3.org/TR/css3-selectors/#target-pseudo">target pseudo-class</a> to hijack in page anchors which has a few side effects that can create some major drawbacks to the technique. One being that if you click multiple futurebox links and then click the browser back button it will go through all the previous overlays that were activated due to the natural behaviour of in page anchors. The other drawback, clicking an in page anchor can cause the page to abruptly jump as it tries to bring the anchor location to the top of the page.<span id="more-633"></span></p>
<p>But you’re thinking I&#8217;m mad, there can&#8217;t possibly be any other way to trigger a pure CSS overlay.</p>
<div class="resources01"><a target="_blank" class="demo" title="Futurebox, lightbox without the javascript and target pseudo-class" href="http://www.thecssninja.com/demo/futurebox/v3">View a live demo</a> <a target="_blank" class="demo source" title="Download the source of the Futurebox demo" href="http://www.thecssninja.com/demo/futurebox/v3/futurebox3.zip">Download the source files</a></div>
<h2 class="subtitle02">Radio buttons?!</h2>
<p>Yep this technique uses radio inputs along with the checked CSS3 pseudo-class, which I described in my <a href="http://www.thecssninja.com/css/custom-inputs-using-css">custom checkbox/radio inputs</a> article, to show/hide the futurebox modals.</p>

<div class="wp_syntax"><div class="code"><pre class="html4strict html4strict" style="font-family:monospace;"><span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">ul</span>&gt;</span>
    <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">li</span>&gt;</span>
        <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">label</span> <span style="color: #000066;">for</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;futurebox01&quot;</span>&gt;</span>
            <span style="color: #009900;">&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;image.gif&quot;</span> <span style="color: #000066;">width</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;100&quot;</span> <span style="color: #000066;">height</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;102&quot;</span> <span style="color: #000066;">alt</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;The CSS Ninja&quot;</span> <span style="color: #66cc66;">/</span>&gt;</span>
        <span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">label</span>&gt;</span>
        <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">input</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;radio&quot;</span> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;futurebox01&quot;</span> <span style="color: #000066;">name</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;gallery&quot;</span> <span style="color: #66cc66;">/</span>&gt;</span>
        <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;overlay&quot;</span>&gt;</span>
            <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">label</span> <span style="color: #000066;">for</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;close&quot;</span> <span style="color: #000066;">title</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;Close futurebox&quot;</span>&gt;</span>
                <span style="color: #009900;">&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;image.png&quot;</span> <span style="color: #000066;">alt</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;The Css Ninja&quot;</span> <span style="color: #000066;">width</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;469&quot;</span> <span style="color: #000066;">height</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;500&quot;</span> <span style="color: #66cc66;">/</span>&gt;</span>
            <span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">label</span>&gt;</span>
        <span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">div</span>&gt;</span>
    <span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">li</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">ul</span>&gt;</span></pre></div></div>

<p>The mark-up differs from the <a href="http://www.thecssninja.com/demo/futurebox/">original futurebox</a>. The overlay now needs to appear directly below the image, the images are wrapped in a label as well as the overlay, we set the &#8220;for&#8221; attribute on the label so we can check the radio without having to actually click the radio. We also use that to our advantage so we can actually close the modal once it&#8217;s been opened by having the large image wrapped in a label with a &#8220;for&#8221; attribute to a hidden radio button.</p>
<p>Having the same name value on all the inputs makes sure only one of the radio buttons can be checked at any one time.</p>

<div class="wp_syntax"><div class="code"><pre class="css css" style="font-family:monospace;">ul li input <span style="color: #00AA00;">&#123;</span> <span style="color: #000000; font-weight: bold;">display</span><span style="color: #00AA00;">:</span> <span style="color: #993333;">none</span>; <span style="color: #00AA00;">&#125;</span>
ul li input<span style="color: #3333ff;">:checked </span><span style="color: #00AA00;">+</span> <span style="color: #6666ff;">.overlay</span> <span style="color: #00AA00;">&#123;</span> <span style="color: #000000; font-weight: bold;">display</span><span style="color: #00AA00;">:</span> table; <span style="color: #00AA00;">&#125;</span></pre></div></div>

<p>Since we don&#8217;t actually want or need to see the radio button we hide it. As mentioned before the &#8220;for&#8221; attribute controls the checking and unchecking of the input.</p>
<p>Unfortunately due to a <a href="http://connect.microsoft.com/IE/feedback/details/542479/label-wont-trigger-input-focus-if-i-use-anything-other-than-text-inside-a-label">bug in IE</a> (you&#8217;ll need to login to view it, or just change your UA string to googlebot <img src='http://www.thecssninja.com/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> ) only labels with text trigger the input. Since IE9 is the first IE to support the checked pseudo-class if this bug is fixed this technique will work, until then it won&#8217;t. Let&#8217;s hope a fix makes it into IE9 final release.</p>
<p>The second line uses the checked pseudo-class to apply display: table to the inputs direct sibling with a class of overlay.</p>

<div class="wp_syntax"><div class="code"><pre class="css css" style="font-family:monospace;"><span style="color: #6666ff;">.overlay</span>
<span style="color: #00AA00;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">width</span><span style="color: #00AA00;">:</span> <span style="color: #933;"><span style="color: #cc66cc;">100</span>%</span>;
    <span style="color: #000000; font-weight: bold;">height</span><span style="color: #00AA00;">:</span> <span style="color: #933;"><span style="color: #cc66cc;">100</span>%</span>;
    <span style="color: #000000; font-weight: bold;">position</span><span style="color: #00AA00;">:</span> <span style="color: #993333;">absolute</span>;
    <span style="color: #000000; font-weight: bold;">top</span><span style="color: #00AA00;">:</span> 0;
    <span style="color: #000000; font-weight: bold;">left</span><span style="color: #00AA00;">:</span> 0;
    <span style="color: #000000; font-weight: bold;">display</span><span style="color: #00AA00;">:</span> <span style="color: #993333;">none</span>;
    <span style="color: #000000; font-weight: bold;">z-index</span><span style="color: #00AA00;">:</span> <span style="color: #cc66cc;">999</span>;
    <span style="color: #000000; font-weight: bold;">background</span><span style="color: #00AA00;">:</span> <span style="color: #993333;">rgb</span><span style="color: #00AA00;">&#40;</span>0<span style="color: #00AA00;">,</span>0<span style="color: #00AA00;">,</span>0<span style="color: #00AA00;">&#41;</span>;
    <span style="color: #000000; font-weight: bold;">background</span><span style="color: #00AA00;">:</span> rgba<span style="color: #00AA00;">&#40;</span>0<span style="color: #00AA00;">,</span>0<span style="color: #00AA00;">,</span>0<span style="color: #00AA00;">,</span><span style="color: #cc66cc;">0.7</span><span style="color: #00AA00;">&#41;</span>;
<span style="color: #00AA00;">&#125;</span>
    <span style="color: #6666ff;">.overlay</span> label
    <span style="color: #00AA00;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">display</span><span style="color: #00AA00;">:</span> <span style="color: #993333;">table-cell</span>;
        <span style="color: #000000; font-weight: bold;">vertical-align</span><span style="color: #00AA00;">:</span> <span style="color: #993333;">middle</span>;
        <span style="color: #000000; font-weight: bold;">text-align</span><span style="color: #00AA00;">:</span> <span style="color: #993333;">center</span>;
    <span style="color: #00AA00;">&#125;</span></pre></div></div>

<p>The overlay styles don&#8217;t differ at all to the original demo, except that instead of an anchor tag wrapping the image it&#8217;s now a label. When the input is checked the overlay sibling of that input becomes visible, by setting it to display: table, as shown in the previous CSS block. The label has display: table-cell applied to it so it&#8217;ll act like a table cell and naturally fill all height and width available to it. Applying vertical-align: middle will make the image vertically center and to horizontally center the image we take advantage of the fact that images are inline elements and just set the label to have text-align: center.</p>
<h2 class="subtitle02">Not just for images</h2>
<p>Since futurebox was only ever originally meant for loading images I thought I would extend this new version to also load static and dynamic content (iframes). In the demo the second listing shows 2 examples of static content and 2 examples of loading an iframe.</p>
<h2 class="subtitle02">Static content</h2>
<p>In order to show static content within a futurebox overlay the mark-up differs slightly.</p>

<div class="wp_syntax"><div class="code"><pre class="html4strict html4strict" style="font-family:monospace;"><span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">ul</span>&gt;</span>
    <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">li</span>&gt;</span>
        <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">label</span> <span style="color: #000066;">for</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;futurebox01&quot;</span>&gt;</span>
            <span style="color: #009900;">&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;image.gif&quot;</span> <span style="color: #000066;">width</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;100&quot;</span> <span style="color: #000066;">height</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;102&quot;</span> <span style="color: #000066;">alt</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;The CSS Ninja&quot;</span> <span style="color: #66cc66;">/</span>&gt;</span>
        <span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">label</span>&gt;</span>
        <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">input</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;radio&quot;</span> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;futurebox01&quot;</span> <span style="color: #000066;">name</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;gallery&quot;</span> <span style="color: #66cc66;">/</span>&gt;</span>
        <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;overlay&quot;</span>&gt;</span>
            <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">label</span> <span style="color: #000066;">for</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;close&quot;</span> <span style="color: #000066;">title</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;Close futurebox&quot;</span>&gt;</span>
                <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">span</span> <span style="color: #000066;">class</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;content01&quot;</span>&gt;</span>
                    <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">span</span> <span style="color: #000066;">class</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;inner_content&quot;</span>&gt;</span>
                        // content goes here
                    <span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">span</span>&gt;</span>
                <span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">span</span>&gt;</span>
            <span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">label</span>&gt;</span>
        <span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">div</span>&gt;</span>
    <span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">li</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">ul</span>&gt;</span></pre></div></div>

<p>Rather than have the image we have 2 spans, although 1 is fine I used 2 to make the scrolling look nicer.</p>

<div class="wp_syntax"><div class="code"><pre class="css css" style="font-family:monospace;"><span style="color: #6666ff;">.content01</span>
<span style="color: #00AA00;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">display</span><span style="color: #00AA00;">:</span> <span style="color: #993333;">block</span>;
        <span style="color: #000000; font-weight: bold;">width</span><span style="color: #00AA00;">:</span> <span style="color: #933;">460px</span>;
        <span style="color: #000000; font-weight: bold;">padding</span><span style="color: #00AA00;">:</span> <span style="color: #933;">20px</span>;
        <span style="color: #000000; font-weight: bold;">background</span><span style="color: #00AA00;">:</span> <span style="color: #cc00cc;">#fff</span>;
        <span style="color: #000000; font-weight: bold;">color</span><span style="color: #00AA00;">:</span> <span style="color: #cc00cc;">#000</span>;
        <span style="color: #000000; font-weight: bold;">margin</span><span style="color: #00AA00;">:</span> 0 <span style="color: #993333;">auto</span>;
        <span style="color: #000000; font-weight: bold;">text-align</span><span style="color: #00AA00;">:</span> <span style="color: #000000; font-weight: bold;">left</span>;
        <span style="color: #000000; font-weight: bold;">cursor</span><span style="color: #00AA00;">:</span> <span style="color: #993333;">auto</span>;
        -webkit-border-radius<span style="color: #00AA00;">:</span> <span style="color: #933;">10px</span>;
        -moz-border-radius<span style="color: #00AA00;">:</span> <span style="color: #933;">10px</span>;
        border-radius<span style="color: #00AA00;">:</span> <span style="color: #933;">10px</span>;
<span style="color: #00AA00;">&#125;</span>
    <span style="color: #6666ff;">.content01</span> <span style="color: #6666ff;">.inner_content</span>
    <span style="color: #00AA00;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">display</span><span style="color: #00AA00;">:</span> <span style="color: #993333;">block</span>;
        <span style="color: #000000; font-weight: bold;">height</span><span style="color: #00AA00;">:</span> <span style="color: #933;">500px</span>;
        overflow-y<span style="color: #00AA00;">:</span> <span style="color: #993333;">auto</span>;
    <span style="color: #00AA00;">&#125;</span></pre></div></div>

<p>The content span I apply a base width and some padding along with applying some CSS3 to give the modal some nice rounded corners.</p>
<p>The inner content span I set a fixed height and apply overflow-y to auto so any content that extends further will show a vertical scrollbar otherwise if the content fits it will hide the scrollbar.</p>
<p>The reason I use spans and not divs is because a label is a inline element and putting divs inside it is not only invalid but will also cause render issues in some browsers. So using a span and setting it to display: block handles that dilemma.</p>
<h2 class="subtitle02">Change the height of individual modals</h2>
<p>Since the base height of the static content container is quite high putting in a small amount of content will create alot of empty white space. You can get around it by targeting specific futurebox modals and set the height to something else.</p>

<div class="wp_syntax"><div class="code"><pre class="css css" style="font-family:monospace;"><span style="color: #cc00cc;">#futurebox07</span> <span style="color: #00AA00;">+</span> <span style="color: #6666ff;">.overlay</span> <span style="color: #6666ff;">.inner_content</span> <span style="color: #00AA00;">&#123;</span> <span style="color: #000000; font-weight: bold;">height</span><span style="color: #00AA00;">:</span> <span style="color: #933;">265px</span>; <span style="color: #00AA00;">&#125;</span></pre></div></div>

<p>Each input has their own id so the &#8220;for&#8221; attribute will work on the labels, we can then use that id to target specific modals and change their height.</p>
<h2 class="subtitle02">It&#8217;s an iframe</h2>
<p>Loading static content has a lot of great uses but sometimes only an iframe can handle some situations such as submitting a form within a modal or loading an external resource.</p>
<p>The first iframe example loads up my contact page, feel free contact me, so you can submit a form without affecting the modal or causing a post back on the parent page.</p>
<p>The second iframe loads a Google search for futurebox.</p>
<p>Since iframes are naturally inline placing it inside a label isn&#8217;t invalid and won&#8217;t cause any browser issues.</p>

<div class="wp_syntax"><div class="code"><pre class="css css" style="font-family:monospace;"><span style="color: #6666ff;">.overlay</span> iframe
<span style="color: #00AA00;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">background</span><span style="color: #00AA00;">:</span> <span style="color: #cc00cc;">#ffffff</span>;
    <span style="color: #000000; font-weight: bold;">border</span><span style="color: #00AA00;">:</span> <span style="color: #993333;">none</span>;
    <span style="color: #000000; font-weight: bold;">padding</span><span style="color: #00AA00;">:</span> <span style="color: #933;">10px</span>;
    -webkit-border-radius<span style="color: #00AA00;">:</span> <span style="color: #933;">10px</span>;
    -moz-border-radius<span style="color: #00AA00;">:</span> <span style="color: #933;">10px</span>;
    border-radius<span style="color: #00AA00;">:</span> <span style="color: #933;">10px</span>;
<span style="color: #00AA00;">&#125;</span></pre></div></div>

<p>To give the iframe the same padding and rounded corners as the images and static content modals, we add a few styles to reset the iframes border, add some padding, set the background and apply some border-radius values to give it rounded corners.</p>
<h2 class="subtitle02">Close buttons</h2>
<p>I&#8217;ve also added close buttons on the static and iframe modals, these are purely for show and don&#8217;t technically close the modal since clicking anywhere on the overlay will close the modal. It&#8217;s purely there to give the user the feeling they are actually closing something by clicking a button, rather than the background.</p>

<div class="wp_syntax"><div class="code"><pre class="html4strict html4strict" style="font-family:monospace;"><span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">label</span> <span style="color: #000066;">for</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;close&quot;</span> <span style="color: #000066;">title</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;Close futurebox&quot;</span>&gt;</span>
    <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">span</span> <span style="color: #000066;">class</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;content01 content02&quot;</span>&gt;</span>
        <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">strong</span> <span style="color: #000066;">class</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;closebutton&quot;</span> <span style="color: #000066;">title</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;Close futurebox&quot;</span>&gt;</span>X<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">strong</span>&gt;</span>
        // content
    <span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">span</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">label</span>&gt;</span></pre></div></div>

<p>To add the close button I&#8217;ve added a strong tag within the content container. I&#8217;ve just added an X inside to represent the close action and added a title attribute so it makes sense.</p>

<div class="wp_syntax"><div class="code"><pre class="css css" style="font-family:monospace;"><span style="color: #6666ff;">.closebutton</span>
<span style="color: #00AA00;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">background</span><span style="color: #00AA00;">:</span> <span style="color: #cc00cc;">#606061</span>;
    <span style="color: #000000; font-weight: bold;">color</span><span style="color: #00AA00;">:</span> <span style="color: #cc00cc;">#FFFFFF</span>;
    <span style="color: #000000; font-weight: bold;">cursor</span><span style="color: #00AA00;">:</span> <span style="color: #993333;">pointer</span>;
    <span style="color: #000000; font-weight: bold;">height</span><span style="color: #00AA00;">:</span> <span style="color: #933;">24px</span>;
    <span style="color: #000000; font-weight: bold;">line-height</span><span style="color: #00AA00;">:</span> <span style="color: #933;">24px</span>;
    <span style="color: #000000; font-weight: bold;">overflow</span><span style="color: #00AA00;">:</span> <span style="color: #993333;">hidden</span>;
    <span style="color: #000000; font-weight: bold;">position</span><span style="color: #00AA00;">:</span> <span style="color: #993333;">absolute</span>;
    <span style="color: #000000; font-weight: bold;">right</span><span style="color: #00AA00;">:</span> <span style="color: #933;">-12px</span>;
    <span style="color: #000000; font-weight: bold;">text-align</span><span style="color: #00AA00;">:</span> <span style="color: #993333;">center</span>;
    <span style="color: #000000; font-weight: bold;">top</span><span style="color: #00AA00;">:</span> <span style="color: #933;">-12px</span>;
    <span style="color: #000000; font-weight: bold;">width</span><span style="color: #00AA00;">:</span> <span style="color: #933;">24px</span>;
    -webkit-border-radius<span style="color: #00AA00;">:</span> <span style="color: #933;">12px</span>;
    -moz-border-radius<span style="color: #00AA00;">:</span> <span style="color: #933;">12px</span>;
    border-radius<span style="color: #00AA00;">:</span> <span style="color: #933;">12px</span>;
    -moz-box-shadow<span style="color: #00AA00;">:</span> <span style="color: #933;">1px</span> <span style="color: #933;">1px</span> <span style="color: #933;">3px</span> <span style="color: #cc00cc;">#000</span>;
    -webkit-box-shadow<span style="color: #00AA00;">:</span> <span style="color: #933;">1px</span> <span style="color: #933;">1px</span> <span style="color: #933;">3px</span> <span style="color: #cc00cc;">#000</span>;
    box-shadow<span style="color: #00AA00;">:</span> <span style="color: #933;">1px</span> <span style="color: #933;">1px</span> <span style="color: #933;">3px</span> <span style="color: #cc00cc;">#000</span>;
<span style="color: #00AA00;">&#125;</span></pre></div></div>

<p>Since we have fixed width content it makes it possible to position it correctly while still keeping the futurebox horizontally centred, that&#8217;s the reason there is no close on the image based modals as the width can vary from image to image. To sit the button to the right of the futurebox I absolutely position it and set top and right to -12px so it will sit slightly offset from the container.</p>
<p>To create the circle effect I set the width and height to 24px and apply a border-radius of 12px for either side which will create our circle.</p>

<div class="wp_syntax"><div class="code"><pre class="css css" style="font-family:monospace;"><span style="color: #6666ff;">.closebutton</span><span style="color: #3333ff;"><span style="color: #00AA00;">:</span>hover
</span><span style="color: #00AA00;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">background</span><span style="color: #00AA00;">:</span> <span style="color: #cc00cc;">#318DF2</span>;
        <span style="color: #000000; font-weight: bold;">text-shadow</span><span style="color: #00AA00;">:</span> <span style="color: #933;">1px</span> <span style="color: #933;">1px</span> <span style="color: #933;">1px</span> <span style="color: #cc00cc;">#ccc</span>;
<span style="color: #00AA00;">&#125;</span></pre></div></div>

<p>When the user hovers over the close button I change the background-color and give the X a light drop shadow.</p>
<h2 class="subtitle02">Mix it up</h2>
<p>We can mix up the styles of the futurebox by adding a class to the parent ul, in the last list example I&#8217;ve added a class to do just that.</p>

<div class="wp_syntax"><div class="code"><pre class="css css" style="font-family:monospace;"><span style="color: #6666ff;">.theme01</span> <span style="color: #6666ff;">.overlay</span>
<span style="color: #00AA00;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">background</span><span style="color: #00AA00;">:</span> <span style="color: #993333;">none</span>;
<span style="color: #00AA00;">&#125;</span>
    <span style="color: #6666ff;">.theme01</span> <span style="color: #6666ff;">.overlay</span> label <span style="color: #00AA00;">&gt;</span> img<span style="color: #00AA00;">,</span>
    <span style="color: #6666ff;">.theme01</span> <span style="color: #6666ff;">.overlay</span> iframe<span style="color: #00AA00;">,</span>
    theme01 <span style="color: #6666ff;">.content01</span>
    <span style="color: #00AA00;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">border</span><span style="color: #00AA00;">:</span> <span style="color: #933;">1px</span> <span style="color: #993333;">solid</span> <span style="color: #cc00cc;">#ccc</span>;
        -moz-box-shadow<span style="color: #00AA00;">:</span> <span style="color: #933;">5px</span> <span style="color: #933;">5px</span> <span style="color: #933;">10px</span> <span style="color: #cc00cc;">#000</span><span style="color: #00AA00;">,</span> <span style="color: #933;">-5px</span> <span style="color: #933;">-5px</span> <span style="color: #933;">10px</span> <span style="color: #cc00cc;">#000</span>;
        -webkit-box-shadow<span style="color: #00AA00;">:</span> <span style="color: #933;">5px</span> <span style="color: #933;">5px</span> <span style="color: #933;">10px</span> <span style="color: #cc00cc;">#000</span><span style="color: #00AA00;">,</span> <span style="color: #933;">-5px</span> <span style="color: #933;">-5px</span> <span style="color: #933;">10px</span> <span style="color: #cc00cc;">#000</span>;
        box-shadow<span style="color: #00AA00;">:</span> <span style="color: #933;">5px</span> <span style="color: #933;">5px</span> <span style="color: #933;">10px</span> <span style="color: #cc00cc;">#000</span><span style="color: #00AA00;">,</span> <span style="color: #933;">-5px</span> <span style="color: #933;">-5px</span> <span style="color: #933;">10px</span> <span style="color: #cc00cc;">#000</span>;
    <span style="color: #00AA00;">&#125;</span></pre></div></div>

<p>Instead of a semi transparent rgba background, I&#8217;ve reset the background to none and applied a dual box shadow to the overlay items.</p>
<h2 class="subtitle02">Love it, hate it?</h2>
<p>Think it&#8217;s awesome, think it&#8217;s an abomination? Let me know what you think or if you have any suggestions on how you think it could work better make sure to leave a comment.</p>
<p class="shorty01">Short URL: <a href="http://cssn.in/ja/025">http://cssn.in/ja/025</a></p>
<div class="clear">&nbsp;</div>
]]></content:encoded>
			<wfw:commentRss>http://www.thecssninja.com/css/futurebox3/feed</wfw:commentRss>
		<slash:comments>24</slash:comments>
		<feedburner:origLink>http://www.thecssninja.com/css/futurebox3</feedburner:origLink></item>
		<item>
		<title>Is that a Speedo in your pocket?</title>
		<link>http://feedproxy.google.com/~r/TheCSSNinja/~3/h-6ToqtxZwE/pocket-speedo</link>
		<comments>http://www.thecssninja.com/javascript/pocket-speedo#comments</comments>
		<pubDate>Sat, 13 Mar 2010 04:11:25 +0000</pubDate>
		<dc:creator>The Css Ninja</dc:creator>
				<category><![CDATA[css]]></category>
		<category><![CDATA[html5]]></category>
		<category><![CDATA[javascript]]></category>

		<guid isPermaLink="false">http://www.thecssninja.com/?p=623</guid>
		<description><![CDATA[Why yes it is&#8230; I&#8217;ve been sitting on this little idea for a while and as a bit of fun I finally got around to putting it together and properly testing it. Basically on an iPhone with geolocation support (3.0+), I have set up a little web app that will get the speed from the [...]]]></description>
			<content:encoded><![CDATA[<p>Why yes it is&#8230;</p>
<p>I&#8217;ve been sitting on this little idea for a while and as a bit of fun I finally got around to putting it together and properly testing it. Basically on an iPhone with geolocation support (3.0+), I have set up a little web app that will get the speed from the GPS and move the speedometer needle according to your current speed in kilometres.<span id="more-623"></span></p>
<h2 class="subtitle02">But speed returns null?</h2>
<p>When I originally played around with the <a href="http://www.thecssninja.com/javascript/geolocation-iphone">geolocation API on the iPhone</a> checking the speed attribute always returned null, what I failed to actually do was test this value while actually moving. The speed attribute is capable of getting speeds as low as 2km/h so walking with the phone will start to register speeds.</p>
<div class="resources01"><a target="_blank" class="demo" title="Pocket speedo web app" href="http://labs.thecssninja.com/pocket_speedo/">View a live demo</a> <a target="_blank" class="demo source" title="Download the source of the Pocket speedo web app demo" href="http://labs.thecssninja.com/pocket_speedo/pocket_speedo.zip">Download the source files</a></div>
<h2 class="subtitle02">So we can get the speed let&#8217;s use it</h2>

<div class="wp_syntax"><div class="code"><pre class="javascript javascript" style="font-family:monospace;"><span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>navigator.<span style="color: #660066;">geolocation</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    navigator.<span style="color: #660066;">geolocation</span>.<span style="color: #660066;">getCurrentPosition</span><span style="color: #009900;">&#40;</span>getSpeed<span style="color: #339933;">,</span> handleError<span style="color: #009900;">&#41;</span>;
<span style="color: #009900;">&#125;</span>
<span style="color: #003366; font-weight: bold;">function</span> getSpeed<span style="color: #009900;">&#40;</span>pos<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    updateStats<span style="color: #009900;">&#40;</span>pos<span style="color: #009900;">&#41;</span>;
&nbsp;
    <span style="color: #003366; font-weight: bold;">var</span> coordsListener <span style="color: #339933;">=</span> navigator.<span style="color: #660066;">geolocation</span>.<span style="color: #660066;">watchPosition</span><span style="color: #009900;">&#40;</span>updateStats<span style="color: #339933;">,</span> handleError<span style="color: #009900;">&#41;</span>;
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Here we get the current position and upon success tell it to execute the <code>getSpeed</code> function. Inside that function I call another function which does all the hard work which I&#8217;ll explain below, next I call the watchPosition method and pass it the same updateStats function so we can get constant updates from the GPS about our speed and adjust our speedometer needle.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">function</span> updateStats<span style="color: #009900;">&#40;</span>pos<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #003366; font-weight: bold;">var</span> speed <span style="color: #339933;">=</span> pos.<span style="color: #660066;">coords</span>.<span style="color: #660066;">speed</span><span style="color: #339933;">,</span>
         speedVariation <span style="color: #339933;">=</span> <span style="color: #CC0000;">3.6</span><span style="color: #339933;">,</span>
         currentSpeed <span style="color: #339933;">=</span> Math.<span style="color: #660066;">round</span><span style="color: #009900;">&#40;</span>speed<span style="color: #339933;">*</span>speedVariation<span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
         angleVariation <span style="color: #339933;">=</span> <span style="color: #CC0000;">0.8</span><span style="color: #339933;">,</span>
         initSpeedAngle <span style="color: #339933;">=</span> <span style="color: #339933;">-</span><span style="color: #CC0000;">105</span><span style="color: #339933;">,</span>
         speedAngle <span style="color: #339933;">=</span> initSpeedAngle <span style="color: #339933;">+</span> <span style="color: #009900;">&#40;</span>currentSpeed <span style="color: #339933;">*</span> angleVariation<span style="color: #009900;">&#41;</span>;
&nbsp;
    needle.<span style="color: #660066;">style</span>.<span style="color: #660066;">webkitTransitionDuration</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">'500ms'</span>;
    needle.<span style="color: #660066;">style</span>.<span style="color: #660066;">webkitTransform</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;rotate(&quot;</span> <span style="color: #339933;">+</span> speedAngle <span style="color: #339933;">+</span> <span style="color: #3366CC;">&quot;deg)&quot;</span>;
    speedVal.<span style="color: #660066;">textContent</span> <span style="color: #339933;">=</span> currentSpeed <span style="color: #339933;">+</span> <span style="color: #3366CC;">&quot;km/h&quot;</span>;
<span style="color: #009900;">&#125;</span></pre></div></div>


<div class="wp_syntax"><div class="code"><pre class="javascript javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> speed <span style="color: #339933;">=</span> pos.<span style="color: #660066;">coords</span>.<span style="color: #660066;">speed</span><span style="color: #339933;">,</span>
     speedVariation <span style="color: #339933;">=</span> <span style="color: #CC0000;">3.6</span><span style="color: #339933;">,</span>
     currentSpeed <span style="color: #339933;">=</span> Math.<span style="color: #660066;">round</span><span style="color: #009900;">&#40;</span>speed<span style="color: #339933;">*</span>speedVariation<span style="color: #009900;">&#41;</span>;</pre></div></div>

<p>When I get my initial position and my subsequent position updates through the watchPosition method, I access the current speed, in metres, through the coords attribute. To work out the rough speed I times the speed by the speedVariation var which is 3.6 which will give me the speed in km/h, for miles/hour the variation is 2.237. I do this calculation inside the Math.round function so I get a whole rounded number with no decimal places.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> angleVariation <span style="color: #339933;">=</span> <span style="color: #CC0000;">0.8</span><span style="color: #339933;">,</span>
     initSpeedAngle <span style="color: #339933;">=</span> <span style="color: #339933;">-</span><span style="color: #CC0000;">105</span><span style="color: #339933;">,</span>
     speedAngle <span style="color: #339933;">=</span> initSpeedAngle <span style="color: #339933;">+</span> <span style="color: #009900;">&#40;</span>currentSpeed <span style="color: #339933;">*</span> angleVariation<span style="color: #009900;">&#41;</span>;</pre></div></div>

<p>To calculate the corresponding angle to the equivalent speed we are currently getting from the GPS I had to work out the angle variation per kilometre. I knew there was 16&deg; between each 20km/h block. The angle variation then could be worked out to be 0.8&deg; per kilometre, I also needed to have the starting angle of 0km/h and that is exactly -105&deg; from our middle point. With those 2 values plus the currentSpeed I could then work out the angle so the speedometer needle could be correctly rotated to the right spot.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript javascript" style="font-family:monospace;">needle.<span style="color: #660066;">style</span>.<span style="color: #660066;">webkitTransitionDuration</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">'500ms'</span>;
needle.<span style="color: #660066;">style</span>.<span style="color: #660066;">webkitTransform</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;rotate(&quot;</span> <span style="color: #339933;">+</span> speedAngle <span style="color: #339933;">+</span> <span style="color: #3366CC;">&quot;deg)&quot;</span>;
speedVal.<span style="color: #660066;">textContent</span> <span style="color: #339933;">=</span> currentSpeed <span style="color: #339933;">+</span> <span style="color: #3366CC;">&quot;km/h&quot;</span>;</pre></div></div>

<p>To move the needle when the speed changes and to make that change animate between the two points I use CSS3 transforms and transitions. By setting our <code>-webkit-transition-duration</code> any changes done using the <code>-webkit-transform</code> property will be transitioned smoothly between the current angle and the new angle. The last bit is just to print out the speed so we can compare the needle position to the actual speed being read.</p>
<h2 class="subtitle02">Some CSS3</h2>
<p>I&#8217;ve added some extra CSS3 to the speedo including animations and box-shadow</p>

<div class="wp_syntax"><div class="code"><pre class="css css" style="font-family:monospace;"><span style="color: #cc00cc;">#dial</span> <span style="color: #cc00cc;">#needle</span>
<span style="color: #00AA00;">&#123;</span>
    -moz-box-shadow<span style="color: #00AA00;">:</span> <span style="color: #933;">1px</span> <span style="color: #933;">1px</span> <span style="color: #933;">3px</span> <span style="color: #cc00cc;">#ffd712</span><span style="color: #00AA00;">,</span> <span style="color: #933;">-1px</span> <span style="color: #933;">-1px</span> <span style="color: #933;">3px</span> <span style="color: #cc00cc;">#feb018</span>;
    -webkit-box-shadow<span style="color: #00AA00;">:</span> <span style="color: #933;">1px</span> <span style="color: #933;">1px</span> <span style="color: #933;">3px</span> <span style="color: #cc00cc;">#ffd712</span><span style="color: #00AA00;">,</span> <span style="color: #933;">-1px</span> <span style="color: #933;">-1px</span> <span style="color: #933;">3px</span> <span style="color: #cc00cc;">#feb018</span>;
&nbsp;
    -webkit-animation-name<span style="color: #00AA00;">:</span> speedorgo;
    -webkit-animation-duration<span style="color: #00AA00;">:</span> 2s;
<span style="color: #00AA00;">&#125;</span></pre></div></div>

<p>To add the slight glow around speedo needle by using a dual box-shadow so it appears on both edges. The <a href="http://webkit.org/blog/324/css-animation-2/">-webkit-animation-</a> properties allows me to specify a predefined animation that can be applied to different elements.</p>

<div class="wp_syntax"><div class="code"><pre class="css css" style="font-family:monospace;"><span style="color: #a1a100;">@-webkit-keyframes speedorgo {</span>
    <span style="color: #933;">0%</span>   <span style="color: #00AA00;">&#123;</span> -webkit-transform<span style="color: #00AA00;">:</span> rotate<span style="color: #00AA00;">&#40;</span>-105deg<span style="color: #00AA00;">&#41;</span>; <span style="color: #00AA00;">&#125;</span>
    <span style="color: #933;"><span style="color: #cc66cc;">20</span>%</span>  <span style="color: #00AA00;">&#123;</span> -webkit-transform<span style="color: #00AA00;">:</span> rotate<span style="color: #00AA00;">&#40;</span>41deg<span style="color: #00AA00;">&#41;</span>; <span style="color: #00AA00;">&#125;</span>
    <span style="color: #933;"><span style="color: #cc66cc;">100</span>%</span> <span style="color: #00AA00;">&#123;</span> -webkit-transform<span style="color: #00AA00;">:</span> rotate<span style="color: #00AA00;">&#40;</span>-105deg<span style="color: #00AA00;">&#41;</span>; <span style="color: #00AA00;">&#125;</span>
<span style="color: #00AA00;">&#125;</span></pre></div></div>

<p>By using webkits @-webkit-keyframes rule and giving it a unique name which we applied to our speedo needle in the previous CSS using the -webkit-animation-name property we can do some pretty cool animations. Inside this animation rule I set up the animation to rotate any element it&#8217;s applied to to go from -105&deg; to 41&deg; then back to -105&deg; when you rotate the phone into landscape mode it&#8217;ll trigger the animation and do the animation over a 2 second period.</p>
<h2 class="subtitle02">Pfft! Who needs the internet&#8230;</h2>
<p>Well technically we initially need the internet but after we load the web app for the first time no connection is required for this to work. By taking advantage of the <a href="http://www.thecssninja.com/javascript/how-to-create-offline-webapps-on-the-iphone">application cache</a> we can store all our assets on the phone, and since the GPS can still work without a network connection the speedo will still function fine.</p>
<h2 class="subtitle02">A clear sky</h2>
<p>In order to pick up a consistent speed reading from the GPS you will need to have the phone with a good clear view of the sky. I&#8217;ve found when using it while walking the speed reading will fluctuate quite a lot, going from 0 to the actual speed your doing and back to 0 and back up etc. It becomes more reliable the faster you are moving. I&#8217;m sure with some adjustments to the code getting a more consistent speed reading will be possible.</p>
<h2 class="subtitle02">Go forth and hack</h2>
<p>We have a fun little tech demo that shows what can with some simple JavaScript and CSS3. I don&#8217;t see why this couldn&#8217;t work on an Android, Palm Pre or a Blackberry with the new webkit browser, all of these should or should very soon support the features required for the web app to work. If you get this working in any other smart phone please leave a comment.</p>
<p class="shorty01">Short URL: <a href="http://cssn.in/ja/024">http://cssn.in/ja/024</a></p>
<div class="clear">&nbsp;</div>
]]></content:encoded>
			<wfw:commentRss>http://www.thecssninja.com/javascript/pocket-speedo/feed</wfw:commentRss>
		<slash:comments>12</slash:comments>
		<feedburner:origLink>http://www.thecssninja.com/javascript/pocket-speedo</feedburner:origLink></item>
		<item>
		<title>Custom radio and checkbox inputs using CSS</title>
		<link>http://feedproxy.google.com/~r/TheCSSNinja/~3/SFPY0ne9h6E/custom-inputs-using-css</link>
		<comments>http://www.thecssninja.com/css/custom-inputs-using-css#comments</comments>
		<pubDate>Fri, 19 Feb 2010 05:13:08 +0000</pubDate>
		<dc:creator>The Css Ninja</dc:creator>
				<category><![CDATA[css]]></category>
		<category><![CDATA[css3]]></category>
		<category><![CDATA[selectors]]></category>

		<guid isPermaLink="false">http://www.thecssninja.com/?p=598</guid>
		<description><![CDATA[In my never ending quest to find weird and wonderful ways to abuse CSS and all its little intricacies, I have come up with a pretty good way of using CSS to create custom radio and checkbox inputs without JavaScript, that are accessible, keyboard controlled, don&#8217;t use any hacks and degrade nicely in non supporting [...]]]></description>
			<content:encoded><![CDATA[<p>In my never ending quest to find weird and wonderful ways to abuse CSS and all its little intricacies, I have come up with a pretty good way of using CSS to create custom radio and checkbox inputs without JavaScript, that are accessible, keyboard controlled, don&#8217;t use any hacks and degrade nicely in non supporting browsers. The journey wasn&#8217;t easy and I was on the brink of filing it in the &#8220;to crazy&#8221; folder, never to be seen again. Luckily I had a brain wave that paid off and actually allowed this to be a very viable solution that degrades beautifully and works in 80% of the browsers. This is my story.<span id="more-598"></span></p>
<h2 class="subtitle02">It&#8217;s a bug, no it&#8217;s intended&#8230;well actually it&#8217;s still a draft</h2>
<p>So upon my initial investigation of how doing something like this would be possible, I came across what I initially thought was a <a href="https://bugzilla.mozilla.org/show_bug.cgi?id=546367">bug with Firefox</a> and IE8. Applying CSS generated content to form elements doesn&#8217;t work, turns out Firefox and IE8 are following the CSS3 draft specification as specified in the <a href="http://www.w3.org/TR/css3-content/">CSS3 Generated and Replaced Content Module</a>.</p>
<blockquote cite="http://www.w3.org/TR/css3-content/#replacedContent"><p>The box model defines different rules for the layout of replaced elements than normal elements. Replaced elements do not have &#8216;<code>::before</code>&#8216; and &#8216;<code>::after</code>&#8216; pseudo-elements;&#8230;</p>
</blockquote>
<p>Form elements fall under the &#8220;replaced elements&#8221; category and therefore don&#8217;t allow <code>:before</code> and <code>:after</code> pseudo-elements on them. However Safari, Chrome and Opera all allow generated content on form elements. Further to the odd behaviour Chrome and Safari will only apply generated content on checkboxes, radios and file inputs whereas Opera will allow it for all form elements, see <a href="http://thecssninja.com/demo/form_generated-content/">test case</a>.</p>
<p>You can see my <a href="http://thecssninja.com/demo/css_custom-forms/fail.html">initial attempt</a> at creating custom radios and checkboxes. This applied the generated content directly to the inputs themselves. Open up the demo in Safari or Chrome to see it working. This presented yet another problem this time with Opera, clicking the radio or checkbox directly would never actually check the box whereas with Chrome and Safari the box would be checked and the custom replacement would change state accordingly. But if you clicked the label, which uses the for attribute to create the relationship to the corresponding input, it would check the input in Opera and change the custom check state correctly.</p>
<p>I then thought I would see if I could fix it by using some JavaScript to check the input <code>onclick</code>, this worked for Opera but stopped it working in Chrome and Safari as it was putting it in an indefinite indeterminate state, as it was checked then quickly unchecked by the JavaScript.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript javascript" style="font-family:monospace;">document.<span style="color: #660066;">body</span>.<span style="color: #660066;">onclick</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span> <span style="color: #009900;">&#40;</span>evt<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #003366; font-weight: bold;">var</span> node <span style="color: #339933;">=</span> evt.<span style="color: #660066;">target</span>;
&nbsp;
    <span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>node.<span style="color: #660066;">nodeName</span> <span style="color: #339933;">===</span> <span style="color: #3366CC;">&quot;INPUT&quot;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #003366; font-weight: bold;">var</span> isChecked <span style="color: #339933;">=</span> node.<span style="color: #660066;">checked</span>;
        <span style="color: #009900;">&#40;</span>isChecked<span style="color: #009900;">&#41;</span> <span style="color: #339933;">?</span>
            isChecked <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">false</span> <span style="color: #339933;">:</span>
	    isChecked <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">true</span>;
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>As there was no safe obvious way that didn&#8217;t involve browser sniffing or using bad object detection to determine if the browser behaved in this way. And the fact it was taking away from the whole point of this demo, to create a CSS only solution for custom forms, I scrapped my idea and went back to the drawing board.</p>
<h2 class="subtitle02">Selectors, pseudo-classes &amp; pseudo-elements</h2>
<div class="resources01"><a href="http://www.thecssninja.com/demo/css_custom-forms/" title="Custom radio and checkbox inputs demonstration" class="demo" target="_blank">View live demo</a> <a href="http://www.thecssninja.com/demo/css_custom-forms/css_custom-forms.zip" title="Download the source of the web custom radio and checkbox inputs demo" class="demo source" target="_blank">Download the source files</a></div>
<p>As you can see from the demo each radio and checkbox is replaced with a custom one. The difference here from my initial attempt, and to get around the fact you can&#8217;t apply generated content to form elements consistently, is to apply the generated content to the label rather than the input itself, and using some clever selectors I can determine the state of the radio/checkbox is in and adjust the custom replaced one accordingly. I also set the original input opacity to 0 so it won&#8217;t show through our custom one.</p>

<div class="wp_syntax"><div class="code"><pre class="html4strict html4strict" style="font-family:monospace;"><span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">p</span>&gt;</span>
    <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">input</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;radio&quot;</span> <span style="color: #000066;">value</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;male&quot;</span> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;male&quot;</span> <span style="color: #000066;">name</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;gender&quot;</span> <span style="color: #66cc66;">/</span>&gt;</span>
    <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">label</span> <span style="color: #000066;">for</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;male&quot;</span>&gt;</span>Male<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">label</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">p</span>&gt;</span></pre></div></div>

<p><small class="caption01">The HTML isn&#8217;t bloated and needs no extra mark-up</small></p>

<div class="wp_syntax"><div class="code"><pre class="css css" style="font-family:monospace;">p<span style="color: #00AA00;">:</span>not<span style="color: #00AA00;">&#40;</span><span style="color: #cc00cc;">#foo</span><span style="color: #00AA00;">&#41;</span> <span style="color: #00AA00;">&gt;</span> input <span style="color: #00AA00;">+</span> label
<span style="color: #00AA00;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">background</span><span style="color: #00AA00;">:</span> <span style="color: #993333;">url</span><span style="color: #00AA00;">&#40;</span><span style="color: #ff0000; font-style: italic;">gr_custom-inputs.png</span><span style="color: #00AA00;">&#41;</span> 0 <span style="color: #933;">-1px</span> <span style="color: #993333;">no-repeat</span>;
    <span style="color: #000000; font-weight: bold;">height</span><span style="color: #00AA00;">:</span> <span style="color: #933;">16px</span>;
    <span style="color: #000000; font-weight: bold;">padding</span><span style="color: #00AA00;">:</span> 0 0 0 <span style="color: #933;">18px</span>;
<span style="color: #00AA00;">&#125;</span></pre></div></div>

<p>Basically this selector is looking for a label that is immediately preceded by a sibling input, that is a child of a paragraph that doesn&#8217;t have an id of foo and <del datetime="2010-03-12T01:40:46+00:00">lastly it has the <code>:before</code> pseudo-element so we can add our custom radio input.</del> The reason for the not selector is to <strong>not</strong> apply these styles in IE8, I&#8217;ll explain why further down.</p>
<p><del datetime="2010-03-12T01:40:46+00:00"><strong id="sprite">Update:</strong> Thanks to <a href="#comment-1182">Lea</a> and <a href="#comment-1184">Mr.MoOx</a> for their suggestions this now uses a sprite image. Rather than insert the image in the <code>content</code> property, I apply a background and insert 3 non-breaking space characters, since the content property doesn&#8217;t accept named entities I used the unicode equivalent of <code>\00a0</code>. The reason I insert 3 is because Firefox 1.5 won&#8217;t show the full background unless I put 3 in there and using a fullstop and applying <code>color: transparent</code> doesn&#8217;t work in FF 1.5.</del></p>
<p><del datetime="2010-03-12T01:40:46+00:00">To get this working in older browsers such as Firefox 3 and down I use a negative margin to get it to sit in the right place. Support for absolutely positioned generated content was only recently added in Firefox 3.5+. I then adjust the newer browsers by offsetting the left value to be equivalent of the left margin.</del></p>
<p><strong id="nogeneratedcontent">Update 2:</strong> Thanks to <a href="#comment-1272">Marius</a> for demonstrating that the :before pseudo-element is superfluous and adding the background directly to the label itself with a few adjustments works just fine and removes the hacky need for using the content property with 3 non-breaking space.</p>
<p>I moved the previous <a href="http://www.thecssninja.com/demo/css_custom-forms/legacy/" target="_blank" title="Legacy demo that uses CSS generated content">demo</a> and it&#8217;s <a href="http://www.thecssninja.com/demo/css_custom-forms/legacy/css_custom-forms.zip" target="_blank" title="Legacy source files for demo that uses CSS generated content">source files</a> to a legacy folder so you can still get them if you want.</p>
<p>Now all the radios have been replaced by our custom versions we need to be able to tell if the radio is checked so we can adjust our custom state.</p>

<div class="wp_syntax"><div class="code"><pre class="css css" style="font-family:monospace;">p<span style="color: #00AA00;">:</span>not<span style="color: #00AA00;">&#40;</span><span style="color: #cc00cc;">#foo</span><span style="color: #00AA00;">&#41;</span> <span style="color: #00AA00;">&gt;</span> input<span style="color: #00AA00;">&#91;</span>type<span style="color: #00AA00;">=</span>radio<span style="color: #00AA00;">&#93;</span><span style="color: #3333ff;">:checked </span><span style="color: #00AA00;">+</span> label <span style="color: #00AA00;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">background-position</span><span style="color: #00AA00;">:</span> 0 <span style="color: #933;">-241px</span>;
<span style="color: #00AA00;">&#125;</span></pre></div></div>

<p>Again this is the same as the previous selector with one difference, we add the <code>:checked</code> pseudo-class available in the <a href="http://www.w3.org/TR/css3-selectors/#checked">CSS3 selectors module</a> to determine the radios state in CSS and change it upon the user checking the radio.</p>
<p>Of course the control doesn&#8217;t end there we also utilise the <code>:hover</code>, <code>:focus</code>, <code>:active</code> &#038; <code>:disabled</code> pseudo-classes to change the radio on hover, when it has focus (for keyboard support), when the input is active (click and hold your mouse cursor to see this state change) and when it&#8217;s disabled. Mixing this with the <code>:checked</code> pseudo-class lets us control all possible states of the radio input and gives us great control and flexibility that doesn&#8217;t require a mouse nor JavaScript to control the states.</p>

<div class="wp_syntax"><div class="code"><pre class="css css" style="font-family:monospace;">p<span style="color: #00AA00;">:</span>not<span style="color: #00AA00;">&#40;</span><span style="color: #cc00cc;">#foo</span><span style="color: #00AA00;">&#41;</span> <span style="color: #00AA00;">&gt;</span> input<span style="color: #00AA00;">&#91;</span>type<span style="color: #00AA00;">=</span>radio<span style="color: #00AA00;">&#93;</span><span style="color: #3333ff;">:hover </span><span style="color: #00AA00;">+</span> label<span style="color: #00AA00;">,</span>
p<span style="color: #00AA00;">:</span>not<span style="color: #00AA00;">&#40;</span><span style="color: #cc00cc;">#foo</span><span style="color: #00AA00;">&#41;</span> <span style="color: #00AA00;">&gt;</span> input<span style="color: #00AA00;">&#91;</span>type<span style="color: #00AA00;">=</span>radio<span style="color: #00AA00;">&#93;</span><span style="color: #3333ff;">:focus </span><span style="color: #00AA00;">+</span> label<span style="color: #00AA00;">,</span>
p<span style="color: #00AA00;">:</span>not<span style="color: #00AA00;">&#40;</span><span style="color: #cc00cc;">#foo</span><span style="color: #00AA00;">&#41;</span> <span style="color: #00AA00;">&gt;</span> input<span style="color: #00AA00;">&#91;</span>type<span style="color: #00AA00;">=</span>radio<span style="color: #00AA00;">&#93;</span> <span style="color: #00AA00;">+</span> label<span style="color: #3333ff;">:hover </span><span style="color: #00AA00;">&#123;</span> 
    <span style="color: #000000; font-weight: bold;">background-position</span><span style="color: #00AA00;">:</span> 0 <span style="color: #933;">-181px</span>; 
<span style="color: #00AA00;">&#125;</span>
p<span style="color: #00AA00;">:</span>not<span style="color: #00AA00;">&#40;</span><span style="color: #cc00cc;">#foo</span><span style="color: #00AA00;">&#41;</span> <span style="color: #00AA00;">&gt;</span> input<span style="color: #00AA00;">&#91;</span>type<span style="color: #00AA00;">=</span>radio<span style="color: #00AA00;">&#93;</span><span style="color: #3333ff;">:hover</span><span style="color: #3333ff;">:checked </span><span style="color: #00AA00;">+</span> label<span style="color: #00AA00;">,</span>
p<span style="color: #00AA00;">:</span>not<span style="color: #00AA00;">&#40;</span><span style="color: #cc00cc;">#foo</span><span style="color: #00AA00;">&#41;</span> <span style="color: #00AA00;">&gt;</span> input<span style="color: #00AA00;">&#91;</span>type<span style="color: #00AA00;">=</span>radio<span style="color: #00AA00;">&#93;</span><span style="color: #3333ff;">:focus</span><span style="color: #3333ff;">:checked </span><span style="color: #00AA00;">+</span> label<span style="color: #00AA00;">,</span>
p<span style="color: #00AA00;">:</span>not<span style="color: #00AA00;">&#40;</span><span style="color: #cc00cc;">#foo</span><span style="color: #00AA00;">&#41;</span> <span style="color: #00AA00;">&gt;</span> input<span style="color: #00AA00;">&#91;</span>type<span style="color: #00AA00;">=</span>radio<span style="color: #00AA00;">&#93;</span><span style="color: #3333ff;">:checked </span><span style="color: #00AA00;">+</span> label<span style="color: #3333ff;">:hover  </span><span style="color: #00AA00;">&#123;</span> 
    <span style="color: #000000; font-weight: bold;">background-position</span><span style="color: #00AA00;">:</span> 0 <span style="color: #933;">-261px</span>;
 <span style="color: #00AA00;">&#125;</span>
p<span style="color: #00AA00;">:</span>not<span style="color: #00AA00;">&#40;</span><span style="color: #cc00cc;">#foo</span><span style="color: #00AA00;">&#41;</span> <span style="color: #00AA00;">&gt;</span> input<span style="color: #00AA00;">&#91;</span>type<span style="color: #00AA00;">=</span>radio<span style="color: #00AA00;">&#93;</span><span style="color: #3333ff;">:disabled </span><span style="color: #00AA00;">+</span> label<span style="color: #00AA00;">,</span>
p<span style="color: #00AA00;">:</span>not<span style="color: #00AA00;">&#40;</span><span style="color: #cc00cc;">#foo</span><span style="color: #00AA00;">&#41;</span> <span style="color: #00AA00;">&gt;</span> input<span style="color: #00AA00;">&#91;</span>type<span style="color: #00AA00;">=</span>radio<span style="color: #00AA00;">&#93;</span><span style="color: #3333ff;">:hover</span><span style="color: #3333ff;">:disabled </span><span style="color: #00AA00;">+</span> label<span style="color: #00AA00;">,</span>
p<span style="color: #00AA00;">:</span>not<span style="color: #00AA00;">&#40;</span><span style="color: #cc00cc;">#foo</span><span style="color: #00AA00;">&#41;</span> <span style="color: #00AA00;">&gt;</span> input<span style="color: #00AA00;">&#91;</span>type<span style="color: #00AA00;">=</span>radio<span style="color: #00AA00;">&#93;</span><span style="color: #3333ff;">:focus</span><span style="color: #3333ff;">:disabled </span><span style="color: #00AA00;">+</span> label<span style="color: #00AA00;">,</span>
p<span style="color: #00AA00;">:</span>not<span style="color: #00AA00;">&#40;</span><span style="color: #cc00cc;">#foo</span><span style="color: #00AA00;">&#41;</span> <span style="color: #00AA00;">&gt;</span> input<span style="color: #00AA00;">&#91;</span>type<span style="color: #00AA00;">=</span>radio<span style="color: #00AA00;">&#93;</span><span style="color: #3333ff;">:disabled </span><span style="color: #00AA00;">+</span> label<span style="color: #3333ff;">:hover</span><span style="color: #00AA00;">,</span>
p<span style="color: #00AA00;">:</span>not<span style="color: #00AA00;">&#40;</span><span style="color: #cc00cc;">#foo</span><span style="color: #00AA00;">&#41;</span> <span style="color: #00AA00;">&gt;</span> input<span style="color: #00AA00;">&#91;</span>type<span style="color: #00AA00;">=</span>radio<span style="color: #00AA00;">&#93;</span><span style="color: #3333ff;">:disabled </span><span style="color: #00AA00;">+</span> label<span style="color: #3333ff;">:hover</span><span style="color: #3333ff;">:active </span><span style="color: #00AA00;">&#123;</span> 
    <span style="color: #000000; font-weight: bold;">background-position</span><span style="color: #00AA00;">:</span> 0 <span style="color: #933;">-221px</span>; 
<span style="color: #00AA00;">&#125;</span>
p<span style="color: #00AA00;">:</span>not<span style="color: #00AA00;">&#40;</span><span style="color: #cc00cc;">#foo</span><span style="color: #00AA00;">&#41;</span> <span style="color: #00AA00;">&gt;</span> input<span style="color: #00AA00;">&#91;</span>type<span style="color: #00AA00;">=</span>radio<span style="color: #00AA00;">&#93;</span><span style="color: #3333ff;">:disabled</span><span style="color: #3333ff;">:checked </span><span style="color: #00AA00;">+</span> label<span style="color: #00AA00;">,</span>
p<span style="color: #00AA00;">:</span>not<span style="color: #00AA00;">&#40;</span><span style="color: #cc00cc;">#foo</span><span style="color: #00AA00;">&#41;</span> <span style="color: #00AA00;">&gt;</span> input<span style="color: #00AA00;">&#91;</span>type<span style="color: #00AA00;">=</span>radio<span style="color: #00AA00;">&#93;</span><span style="color: #3333ff;">:hover</span><span style="color: #3333ff;">:disabled</span><span style="color: #3333ff;">:checked </span><span style="color: #00AA00;">+</span> label<span style="color: #00AA00;">,</span>
p<span style="color: #00AA00;">:</span>not<span style="color: #00AA00;">&#40;</span><span style="color: #cc00cc;">#foo</span><span style="color: #00AA00;">&#41;</span> <span style="color: #00AA00;">&gt;</span> input<span style="color: #00AA00;">&#91;</span>type<span style="color: #00AA00;">=</span>radio<span style="color: #00AA00;">&#93;</span><span style="color: #3333ff;">:focus</span><span style="color: #3333ff;">:disabled</span><span style="color: #3333ff;">:checked </span><span style="color: #00AA00;">+</span> label<span style="color: #00AA00;">,</span>
p<span style="color: #00AA00;">:</span>not<span style="color: #00AA00;">&#40;</span><span style="color: #cc00cc;">#foo</span><span style="color: #00AA00;">&#41;</span> <span style="color: #00AA00;">&gt;</span> input<span style="color: #00AA00;">&#91;</span>type<span style="color: #00AA00;">=</span>radio<span style="color: #00AA00;">&#93;</span><span style="color: #3333ff;">:disabled</span><span style="color: #3333ff;">:checked </span><span style="color: #00AA00;">+</span> label<span style="color: #3333ff;">:hover</span><span style="color: #00AA00;">,</span>
p<span style="color: #00AA00;">:</span>not<span style="color: #00AA00;">&#40;</span><span style="color: #cc00cc;">#foo</span><span style="color: #00AA00;">&#41;</span> <span style="color: #00AA00;">&gt;</span> input<span style="color: #00AA00;">&#91;</span>type<span style="color: #00AA00;">=</span>radio<span style="color: #00AA00;">&#93;</span><span style="color: #3333ff;">:disabled</span><span style="color: #3333ff;">:checked </span><span style="color: #00AA00;">+</span> label<span style="color: #3333ff;">:hover</span><span style="color: #3333ff;">:active </span><span style="color: #00AA00;">&#123;</span>
     <span style="color: #000000; font-weight: bold;">background-position</span><span style="color: #00AA00;">:</span> 0 <span style="color: #933;">-301px</span>; 
<span style="color: #00AA00;">&#125;</span>
p<span style="color: #00AA00;">:</span>not<span style="color: #00AA00;">&#40;</span><span style="color: #cc00cc;">#foo</span><span style="color: #00AA00;">&#41;</span> <span style="color: #00AA00;">&gt;</span> input<span style="color: #00AA00;">&#91;</span>type<span style="color: #00AA00;">=</span>radio<span style="color: #00AA00;">&#93;</span><span style="color: #3333ff;">:active </span><span style="color: #00AA00;">+</span> label<span style="color: #00AA00;">,</span>
p<span style="color: #00AA00;">:</span>not<span style="color: #00AA00;">&#40;</span><span style="color: #cc00cc;">#foo</span><span style="color: #00AA00;">&#41;</span> <span style="color: #00AA00;">&gt;</span> input<span style="color: #00AA00;">&#91;</span>type<span style="color: #00AA00;">=</span>radio<span style="color: #00AA00;">&#93;</span> <span style="color: #00AA00;">+</span> label<span style="color: #3333ff;">:hover</span><span style="color: #3333ff;">:active </span><span style="color: #00AA00;">&#123;</span> 
    <span style="color: #000000; font-weight: bold;">background-position</span><span style="color: #00AA00;">:</span> 0 <span style="color: #933;">-201px</span>; 
<span style="color: #00AA00;">&#125;</span>
p<span style="color: #00AA00;">:</span>not<span style="color: #00AA00;">&#40;</span><span style="color: #cc00cc;">#foo</span><span style="color: #00AA00;">&#41;</span> <span style="color: #00AA00;">&gt;</span> input<span style="color: #00AA00;">&#91;</span>type<span style="color: #00AA00;">=</span>radio<span style="color: #00AA00;">&#93;</span><span style="color: #3333ff;">:active</span><span style="color: #3333ff;">:checked </span><span style="color: #00AA00;">+</span> label<span style="color: #00AA00;">,</span>
p<span style="color: #00AA00;">:</span>not<span style="color: #00AA00;">&#40;</span><span style="color: #cc00cc;">#foo</span><span style="color: #00AA00;">&#41;</span> <span style="color: #00AA00;">&gt;</span> input<span style="color: #00AA00;">&#91;</span>type<span style="color: #00AA00;">=</span>radio<span style="color: #00AA00;">&#93;</span><span style="color: #3333ff;">:checked </span><span style="color: #00AA00;">+</span> label<span style="color: #3333ff;">:hover</span><span style="color: #3333ff;">:active </span><span style="color: #00AA00;">&#123;</span>
     <span style="color: #000000; font-weight: bold;">background-position</span><span style="color: #00AA00;">:</span> 0 <span style="color: #933;">-281px</span>; 
<span style="color: #00AA00;">&#125;</span></pre></div></div>

<p>Another addition I have made to the CSS is also changing the input states when the user hovers, clicks or focuses on the label it will now change the input to reflect those actions. </p>
<h2 class="subtitle02">IE8 is almost there</h2>
<p>IE8 can do everything, except it doesn&#8217;t support the <code>:checked</code> pseudo-class and therefore makes this technique useless. So I use the <code>not()</code> pseudo-class, which IE8 doesn&#8217;t support, to work around this unfortunate lack of ability. Let&#8217;s hope IE9 adds the CSS3 selector module<sup>1</sup>. This of course degrades nicely in non-supporting browsers and fallback to the browser default form elements.</p>
<p><sup>1</sup> IE9 does support the CSS3 selector module so this technique has across the board support for all major browsers.</p>
<h2 class="subtitle02">Accessible and friendly</h2>
<p><del datetime="2010-03-12T01:40:46+00:00">CSS generated content doesn&#8217;t get in the way of a screen reader and they have a clear view of the form elements</del> CSS generated content is no longer used it&#8217;s now a background image on the label rather than doing it on the generated content <a href="#nogeneratedcontent" title="The need for CSS generated content is no longer needed see update for further details">see update</a> (I would appreciate any accessibility experts or screen reader users to please comment to correct or agree with me). Users, who have trouble operating a mouse or, like me, prefer navigating forms with the keyboard as it&#8217;s faster, aren&#8217;t left out. Tabbing through the inputs changes the states, pressing spacebar to check a radio or checkbox also changes the state. I also change the label colour and give it a text-shadow to give a nicer indication that the current input has focus.</p>
<p>The disabled and checked attributes work as intended with this solution and don&#8217;t require any trickery to achieve that.</p>

<div class="wp_syntax"><div class="code"><pre class="html4strict html4strict" style="font-family:monospace;"><span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">p</span>&gt;</span>
    <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">input</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;radio&quot;</span> <span style="color: #000066;">disabled</span> <span style="color: #000066;">value</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;male&quot;</span> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;male&quot;</span> <span style="color: #000066;">name</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;gender&quot;</span> <span style="color: #66cc66;">/</span>&gt;</span>
    <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">label</span> <span style="color: #000066;">for</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;male&quot;</span>&gt;</span>Male<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">label</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">p</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">p</span>&gt;</span>
    <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">input</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;radio&quot;</span> <span style="color: #000066;">checked</span> <span style="color: #000066;">value</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;Female&quot;</span> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;female&quot;</span> <span style="color: #000066;">name</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;gender&quot;</span> <span style="color: #66cc66;">/</span>&gt;</span>
    <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">label</span> <span style="color: #000066;">for</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;female&quot;</span>&gt;</span>Female<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">label</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">p</span>&gt;</span></pre></div></div>

<p>As you can see from the above mark-up adding the disabled or checked attributes (disabled=&#8221;disabled&#8221; &#038; checked=&#8221;checked&#8221; also work) will allow us to target those states using the <code>:checked</code> and <code>:disabled</code> pseudo-classes. As well as change the style if we use JavaScript to disable any inputs based on users actions.</p>
<h2 class="subtitle02">Browser support and notes</h2>
<p>As of writing the following browsers have been tested and known to work with the demo:</p>
<ul>
<li>Firefox 1.5+</li>
<li>Opera 9.6+</li>
<li>Safari 3.2+*</li>
<li>iPhone/iPod Safari**</li>
<li>Chrome 4+</li>
<li>IE9+</li>
</ul>
<p>Will run this demo through browsershots and update it accordingly if the browser support works in any lower versions mentioned</p>
<p>* This would work in Safari 3.2 except due to a <a href="https://bugs.webkit.org/show_bug.cgi?id=17680">bug</a>. The checked pseudo-class will work just fine if it has the checked attribute on the input but won&#8217;t change the input state if the user checks the input with their mouse or keyboard (the actual input will check but the CSS state won&#8217;t update the custom input image).</p>
<p>* This now works in Safari 3.2 but, and a big but, the behaviour is quite bizarre. Clicking a radio or checkbox will change the custom state but clicking it again to uncheck won’t show until you have hovered away from or taken focus away from the input. If you have Safari 3.2 installed try the new demo to get a better understanding of what’s happening.</p>
<p>** <del datetime="2010-03-12T01:40:46+00:00">To get the iPhone to work with this demo I had to apply the <a href="https://developer.mozilla.org/en/CSS/pointer-events"><code>pointer-events</code></a> CSS property.</del> Since the input is actually sitting on top of the background image we no longer need to apply pointer events to the label.</p>

<div class="wp_syntax"><div class="code"><pre class="css css" style="font-family:monospace;">p<span style="color: #00AA00;">:</span>not<span style="color: #00AA00;">&#40;</span><span style="color: #cc00cc;">#foo</span><span style="color: #00AA00;">&#41;</span> <span style="color: #00AA00;">&gt;</span> input <span style="color: #00AA00;">+</span> label
<span style="color: #00AA00;">&#123;</span>
    pointer-events<span style="color: #00AA00;">:</span> <span style="color: #993333;">none</span>;
<span style="color: #00AA00;">&#125;</span></pre></div></div>

<p><small class="caption01">Here for legacy reasons no longer needed for iPhone support</small></p>
<p><del datetime="2010-03-12T01:40:46+00:00">Since having the generated content sit over the top of the actual checkbox, iPhone Safari needed a kick up the bum with the <code>pointer-events</code> property so we could click through the custom checkbox to the actual checkbox. Desktop Safari works fine without it.</del></p>
<h2 class="subtitle02">Drawbacks</h2>
<p>Resolved <a href="#sprite">see update above</a>. <del datetime="2010-02-20T05:37:40+00:00">This technique has only 1 drawback I can think of, IE support is not a drawback for me, you can&#8217;t use a big sprite image to save all the radio and checkbox states, they need to be individual images. Using CSS generated content to insert an image doesn&#8217;t give you control of the image position like a background image does. The images are small in size and the initial load of each state shouldn&#8217;t be noticed. If however you wish to preload all the images take a look at my CSS based <a href="http://www.thecssninja.com/css/even-better-image-preloading-with-css2">image preload technique</a>. That technique will create quite a few http request depending on how many images you preload but since they&#8217;re small in size it shouldn&#8217;t be a major concern.</del></p>
<p class="shorty01">Short URL: <a href="http://cssn.in/ja/023">http://cssn.in/ja/023</a></p>
<div class="clear">&nbsp;</div>
]]></content:encoded>
			<wfw:commentRss>http://www.thecssninja.com/css/custom-inputs-using-css/feed</wfw:commentRss>
		<slash:comments>67</slash:comments>
		<feedburner:origLink>http://www.thecssninja.com/css/custom-inputs-using-css</feedburner:origLink></item>
		<item>
		<title>Web Notifications</title>
		<link>http://feedproxy.google.com/~r/TheCSSNinja/~3/oc8njVSneys/web-notifications</link>
		<comments>http://www.thecssninja.com/javascript/web-notifications#comments</comments>
		<pubDate>Sat, 06 Feb 2010 06:40:58 +0000</pubDate>
		<dc:creator>The Css Ninja</dc:creator>
				<category><![CDATA[html5]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[chrome]]></category>
		<category><![CDATA[editor draft]]></category>
		<category><![CDATA[web notifications]]></category>

		<guid isPermaLink="false">http://www.thecssninja.com/?p=584</guid>
		<description><![CDATA[Google Chrome has recently updated their stable release with some extra goodies; probably most notable is the inclusion of @font-face support which is very welcome. Another not so well know proposal by the Chromium team is to have web notifications which was shipped with the stable release (v4.0.249.78). Web Notifications allows users to get updates [...]]]></description>
			<content:encoded><![CDATA[<p>Google Chrome has recently updated their stable release with some extra goodies; probably most notable is the inclusion of @font-face support which is very welcome. Another not so well know proposal by the Chromium team is to have <a href="http://dev.w3.org/2006/webapi/WebNotifications/publish/">web notifications</a> which was shipped with the stable release (v4.0.249.78).<span id="more-584"></span></p>
<p>Web Notifications allows users to get updates on a webpage even if they&#8217;re not looking at it, shown to them through small notification boxes, think <a href="http://growl.info/screenshots.php">growl</a>. This opens up some great potential for the current web apps out there. When you get a new email it could popup a little notification much like our desktop email clients do now or your twitter page could let you know when new @replies have come in, the possibilities are endless.</p>
<p>Interaction with your web app no longer needs the focus of the user to be informed that something has occurred, I know I would find notifications very useful.</p>
<p>Since this is a proposal and neither a working draft nor an agreed upon API, anything written about in this article is almost guaranteed to change. I will however try to make updates when changes are made. We have our <a href="http://code.google.com/p/chromium/issues/detail?id=34961">first bug</a>. If you&#8217;re using the latest dev version (5.0.317.2) Web Notifications will crash the browser, all previous versions still work fine.</p>
<div id="usermessagea" class="cf_info failure"><strong>Update:</strong> Web notifications spec has been updated <a href="#specchange01">see here for changes</a>.</div>
<h2 class="subtitle02">You&#8217;ve got mail</h2>
<p>I&#8217;ll go through a few examples I have put together on how to setup and start using notifications today.</p>
<p><img src="http://www.thecssninja.com/wp-content/uploads/2010/02/notification01.png" alt="First example of Web Notifications in Chrome" title="First example of Web Notifications in Chrome" width="609" height="353" class="article-img main-img" /><br />
<small style="margin: -30px 0pt 0pt; display: block;">This is what a notification looks like using the <code>createNotification()</code> method</small></p>
<div class="resources01"><a target="_blank" class="demo" title="HTML5 web notifications" href="http://www.thecssninja.com/demo/web_notifications/">View live demos</a> <a target="_blank" class="demo source" title="Download the source of the web notification demo" href="http://www.thecssninja.com/demo/web_notifications/web_notifications.zip">Download the source files</a></div>
<p>Everyone loves the classic &#8220;you&#8217;ve got mail&#8221; voice, so let&#8217;s go through how to do that with Chromes Web Notifications.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript javascript" style="font-family:monospace;">TCNWN.<span style="color: #660066;">myNotifications</span> <span style="color: #339933;">=</span> window.<span style="color: #660066;">webkitNotifications</span>;
&nbsp;
TCNWN.<span style="color: #660066;">setup</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>TCNWN.<span style="color: #660066;">myNotifications</span>.<span style="color: #660066;">checkPermission</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">===</span> <span style="color: #CC0000;">1</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #006600; font-style: italic;">// 1 = Permission is uknown so we request permission</span>
        TCNWN.<span style="color: #660066;">myNotifications</span>.<span style="color: #660066;">requestPermission</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>;
    <span style="color: #009900;">&#125;</span> <span style="color: #000066; font-weight: bold;">else</span> <span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>TCNWN.<span style="color: #660066;">myNotifications</span>.<span style="color: #660066;">checkPermission</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">===</span> 0<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #006600; font-style: italic;">// 0 = Permission has been granted to show notifications</span>
    <span style="color: #009900;">&#125;</span> <span style="color: #000066; font-weight: bold;">else</span> <span style="color: #009900;">&#123;</span>  
        <span style="color: #006600; font-style: italic;">// 2 = Permission has been denied	</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>;</pre></div></div>

<p>A few things are happening here, for notifications to ever work you need to ask the user for permission. So we use the <code>checkPermission()</code> method to see what status web notifications are in and act on the status accordingly. Once permission has been granted notifications can then be used. <strong id="specchange01">Update:</strong> <code>checkPermission()</code> has been changed to <code>permissionLevel()</code> in the spec, this hasn&#8217;t been pushed out in any release of Chrome so <code>checkPermission()</code> still works.</p>
<p>A bizarre bug with web notifications is asking for permission by calling the <code>requestPermission()</code> method within the page will throw a security exception and won&#8217;t display the permission window where the user can either deny, allow or ignore the notifications request. To get around this I have to run the <code>requestPermission()</code> method as either <del datetime="2010-02-08T00:44:29+00:00">a javascript: URL as this code only works when run in the address bar</del> a <code>javascript:</code> link or by using an <code>onclick</code> function, but still cannot be called programatically (see <a href="http://code.google.com/p/chromium/issues/detail?id=31736">Issue 31736</a>). That&#8217;s why you need to click the first link in the demo. Once permission has been granted we no longer need to resort to that god awful hack, hopefully this is fixed ASAP. You&#8217;ll also notice the notifications object is prefixed with webkit, for similar reason why they prefix new CSS3 properties.</p>
<p>Once permission is allowed the <code>requestPermission()</code> method has a callback parameter where we can setup all our notifications. In this case we pass a function to display our got mail message.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript javascript" style="font-family:monospace;">TCNWN.<span style="color: #660066;">gotMail</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>TCNWN.<span style="color: #660066;">myNotifications</span>.<span style="color: #660066;">checkPermission</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">===</span> 0<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		TCNWN.<span style="color: #660066;">notification</span> <span style="color: #339933;">=</span> TCNWN.<span style="color: #660066;">myNotifications</span>.<span style="color: #660066;">createNotification</span><span style="color: #009900;">&#40;</span>
			<span style="color: #3366CC;">&quot;http://www.thecssninja.com/demo/web_notifications/icon.png&quot;</span><span style="color: #339933;">,</span>
			<span style="color: #3366CC;">&quot;You've got mail&quot;</span><span style="color: #339933;">,</span>
			<span style="color: #3366CC;">&quot;The CSS Ninja has sent you an email&quot;</span>
		<span style="color: #009900;">&#41;</span>;
		TCNWN.<span style="color: #660066;">notification</span>.<span style="color: #660066;">ondisplay</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			<span style="color: #003366; font-weight: bold;">var</span> audio <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> Audio<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;youvegotmail.mp3&quot;</span><span style="color: #009900;">&#41;</span>;
			audio.<span style="color: #660066;">play</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>;
			window.<span style="color: #660066;">setTimeout</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;TCNWN.notification.cancel()&quot;</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">5000</span><span style="color: #009900;">&#41;</span>;
		<span style="color: #009900;">&#125;</span>
		TCNWN.<span style="color: #660066;">notification</span>.<span style="color: #660066;">show</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>;
	<span style="color: #009900;">&#125;</span> <span style="color: #000066; font-weight: bold;">else</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000066;">alert</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;You need to allow web notifications for this to work&quot;</span><span style="color: #009900;">&#41;</span>;
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>;</pre></div></div>

<p>In the callback function we create our new notification object using <code>createNotification()</code> method which accepts an icon url, title and body text. To display nothing for any one of these parameters passing in empty quotes (&#8220;&#8221;) will work, otherwise it will display undefined. We then attach an <code>ondisplay</code> event listener so we can run some code when the notification displays. For this demo we load an mp3 to play &#8220;you&#8217;ve got mail&#8221; using HTML5 audio. We then create a <code>setTimeout</code> function to close the notification after 5 seconds. Lastly we execute the <code>show()</code> method so the actual notification displays.</p>
<h2 class="subtitle02">Custom notifications</h2>
<p>Along with the <code>createNotification()</code> we also have the <code>createHTMLNotification()</code> method which allows us to pass an url to a html page to be displayed as the notification. The second example on the demo page will load a notification that fetches a new tweet every 15 seconds and dynamically updates the content.</p>
<p><img src="http://www.thecssninja.com/wp-content/uploads/2010/02/notification02.png" alt="The second example using custom notifications method" title="notification02" width="609" height="353" class="article-img main-img" /><br />
<small style="margin: -30px 0pt 0pt; display: block;">Using <code>createHTMLNotification()</code> method, we can pass a url to be loaded in the notification window</small></p>

<div class="wp_syntax"><div class="code"><pre class="javascript javascript" style="font-family:monospace;">TCNWN.<span style="color: #660066;">loadTweet</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>TCNWN.<span style="color: #660066;">myNotifications</span>.<span style="color: #660066;">checkPermission</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">===</span> 0<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        TCNWN.<span style="color: #660066;">notification</span> <span style="color: #339933;">=</span> TCNWN.<span style="color: #660066;">myNotifications</span>.<span style="color: #660066;">createHTMLNotification</span><span style="color: #009900;">&#40;</span>url<span style="color: #009900;">&#41;</span>;
        TCNWN.<span style="color: #660066;">notification</span>.<span style="color: #660066;">show</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>;
    <span style="color: #009900;">&#125;</span> <span style="color: #000066; font-weight: bold;">else</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000066;">alert</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;You need to allow web notifications for this to work&quot;</span><span style="color: #009900;">&#41;</span>;
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>;</pre></div></div>

<p>The only difference here is we specify 1 parameter for the <code>createHTMLNotification()</code> method which is the url for the notification we wish to display. I won&#8217;t be going into detail on loading the tweets, to touch on it quickly it uses a JSONP call.</p>
<h2 class="subtitle02">Some observations</h2>
<p>Since playing around with this and trying to figure out how it works I have come across some quirks/bugs. </p>
<p>If you go to the first example that does the &#8220;you&#8217;ve got mail&#8221; notification and click back in the browser before the audio finishes playing, it will stop the audio and the setTimeout function that closes the notification will never fire.</p>
<p>As discussed earlier in the article you can&#8217;t actually request permission to display web notifications from within your code, you have to create a <code>javascript:</code> link that will execute the code. I tried programmatically changing the window.location to a JavaScript url which would fire the <code>requestPermission()</code> method but that wouldn&#8217;t work.</p>
<p>Bringing focus back to the window that called the notification, as stated in the proposal by using <code>window.opener.focus()</code>, doesn&#8217;t currently work.</p>
<p>Finally the last quirk I came across with the twitter demo is that for some tweets with long unbroken strings it will cause the notification window to flicker uncontrollably. Setting <code>white-space: pre-wrap</code> to force long string to be broken can help fix the issue.</p>
<h2 class="subtitle02">Looks promising</h2>
<p>Web notifications do look like a promising addition to the browser land and can create some really useful feedback to a user. With today’s web applications pushing the limits of the current technology available in the browser, I&#8217;m sure many developers will embrace web notifications in clever and exciting new ways.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.thecssninja.com/javascript/web-notifications/feed</wfw:commentRss>
		<slash:comments>6</slash:comments>
		<feedburner:origLink>http://www.thecssninja.com/javascript/web-notifications</feedburner:origLink></item>
		<item>
		<title>Even better image preloading with CSS2</title>
		<link>http://feedproxy.google.com/~r/TheCSSNinja/~3/jEiYFgQpD4U/even-better-image-preloading-with-css2</link>
		<comments>http://www.thecssninja.com/css/even-better-image-preloading-with-css2#comments</comments>
		<pubDate>Tue, 05 Jan 2010 08:35:17 +0000</pubDate>
		<dc:creator>The Css Ninja</dc:creator>
				<category><![CDATA[css]]></category>

		<guid isPermaLink="false">http://www.thecssninja.com/?p=566</guid>
		<description><![CDATA[Recently I read an article on better image preloading using CSS3 which presented a clever idea using CSS3 multiple background images to preload images on one element as opposed to another method of having containers for each image. As of writing the support for multiple backgrounds is fairly sparse with webkit having the best support [...]]]></description>
			<content:encoded><![CDATA[<p>Recently I read an article on <a href="http://perishablepress.com/press/2010/01/04/preload-images-css3/">better image preloading using CSS3</a> which presented a clever idea using CSS3 <a href="http://www.w3.org/TR/2009/CR-css3-background-20091217/#layering">multiple background images</a> to preload images on one element as opposed to another method of having containers for each image. As of writing the support for multiple backgrounds is fairly sparse with webkit having the best support (Safari 3+ &#038; Chrome 1+), Firefox is introducing this in the upcoming 3.6 release.<br />
<span id="more-566"></span></p>
<div class="clear"> </div>
<h2 class="subtitle02">There&#8217;s a better way</h2>
<p>The article instantly got me thinking on a potential alternate method to accomplish the same task, but using a widely available CSS2.1 property. Which if possible will up the browser support significantly. Thankfully the idea actually worked.</p>
<div class="resources01"><a href="http://www.thecssninja.com/demo/css_preload/" title="Preload images using CSS" class="demo" target="_blank">View a live demo</a> <a href="http://www.thecssninja.com/demo/css_preload/css_preload.zip" title="Download the source of the CSS preload demo" class="demo source" target="_blank">Download the source files</a></div>
<h2 class="subtitle02">CSS generated content</h2>
<p>In CSS2 a <a href="http://www.w3.org/TR/CSS2/generate.html#content">content property</a> was introduced, which in conjunction with the <a href="http://www.w3.org/TR/CSS2/generate.html#before-after-content"><em>:before</em> and <em>:after</em></a> pseudo-elements allows us to generate content in a document. The most interesting value that can be used with the content property is <em>url()</em> which allows us to pass an image.</p>

<div class="wp_syntax"><div class="code"><pre class="css css" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">content</span><span style="color: #00AA00;">:</span> <span style="color: #993333;">url</span><span style="color: #00AA00;">&#40;</span>img01.jpg<span style="color: #00AA00;">&#41;</span>;</pre></div></div>

<p>That&#8217;s all great than we can add a single preloaded image using the content property but what about multiple images? That&#8217;s where the content property shows its true power, we can pass multiple url&#8217;s.</p>

<div class="wp_syntax"><div class="code"><pre class="css css" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">content</span><span style="color: #00AA00;">:</span> <span style="color: #993333;">url</span><span style="color: #00AA00;">&#40;</span>img01.jpg<span style="color: #00AA00;">&#41;</span> <span style="color: #993333;">url</span><span style="color: #00AA00;">&#40;</span>img02.jpg<span style="color: #00AA00;">&#41;</span> <span style="color: #993333;">url</span><span style="color: #00AA00;">&#40;</span>img02.jpg<span style="color: #00AA00;">&#41;</span></pre></div></div>

<p>Brilliant, we can now preload multiple images using one line of CSS. We have all the benefits of the CSS3 method without the CSS3 and the Browser support grows exponentially.</p>
<ul>
<li>IE8+</li>
<li>FF2+</li>
<li>Safari 3+</li>
<li>Chrome 1+</li>
<li>Opera 9.62+</li>
</ul>
<h2 class="subtitle02">Putting it all together</h2>
<p>Now we know how to preload multiple images using CSS2, lets put it all together:</p>

<div class="wp_syntax"><div class="code"><pre class="css css" style="font-family:monospace;">body<span style="color: #3333ff;"><span style="color: #00AA00;">:</span>after
</span><span style="color: #00AA00;">&#123;</span>
<span style="color: #000000; font-weight: bold;">content</span><span style="color: #00AA00;">:</span> <span style="color: #993333;">url</span><span style="color: #00AA00;">&#40;</span>img01.jpg<span style="color: #00AA00;">&#41;</span> <span style="color: #993333;">url</span><span style="color: #00AA00;">&#40;</span>img02.jpg<span style="color: #00AA00;">&#41;</span> <span style="color: #993333;">url</span><span style="color: #00AA00;">&#40;</span>img03.jpg<span style="color: #00AA00;">&#41;</span>;
<span style="color: #000000; font-weight: bold;">display</span><span style="color: #00AA00;">:</span> <span style="color: #993333;">none</span>;
<span style="color: #00AA00;">&#125;</span></pre></div></div>

<p>Instead of creating an empty div with a class on it, we can utilise the requirement of the :after (or :before) pseudo-element. This needs to be there for the content property to work. We apply it to generate the content after the body tag and then set it to <em>display: none</em>, that way the preloaded images aren&#8217;t shown but are still loaded. We know have a pure CSS solution that requires no mark-up.</p>
<h2 class="subtitle02">Almost perfect</h2>
<p>IE6 and 7 do not support CSS generated content and therefore will not preload any images, but that&#8217;s a minor trade off. Users of IE6/7 will just have to load the images when they are called rather than getting them from the cache. </p>
<p>A possible work around for their lack of support is to use <a href="http://msdn.microsoft.com/en-us/library/ms537512%28VS.85%29.aspx">conditional comments</a> to load the images. This solution will require extra mark-up.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.thecssninja.com/css/even-better-image-preloading-with-css2/feed</wfw:commentRss>
		<slash:comments>15</slash:comments>
		<feedburner:origLink>http://www.thecssninja.com/css/even-better-image-preloading-with-css2</feedburner:origLink></item>
		<item>
		<title>Futurebox, revisited</title>
		<link>http://feedproxy.google.com/~r/TheCSSNinja/~3/UnEFhd9yBb4/futurebox2</link>
		<comments>http://www.thecssninja.com/css/futurebox2#comments</comments>
		<pubDate>Sat, 19 Dec 2009 05:12:01 +0000</pubDate>
		<dc:creator>The Css Ninja</dc:creator>
				<category><![CDATA[css]]></category>
		<category><![CDATA[css3]]></category>

		<guid isPermaLink="false">http://www.thecssninja.com/?p=523</guid>
		<description><![CDATA[Earlier this year I unleashed futurebox into the wild. It got a whole lot more attention than I anticipated and I got some great feedback. Since then, I have been slowly working on a new version of futurebox which incorporates many new features. &#160; Images are lazy loaded so they are only called when the [...]]]></description>
			<content:encoded><![CDATA[<p>Earlier this year I unleashed <a href="http://www.thecssninja.com/xhtml/futurebox">futurebox</a> into the wild. It got a whole lot more attention than I anticipated and I got some great feedback. Since then, I have been slowly working on a new version of futurebox which incorporates many new features.<span id="more-523"></span>
<div class="clear">&nbsp;</div>
<ul>
<li>Images are lazy loaded so they are only called when the futurebox overlay is activated.</li>
<li>You can scroll through using next/previous buttons</li>
<li>Use the keyboard to activate or close futurebox. Overlays can be opened in any order using the keyboard.</li>
<li>Generated content for image description and counters</li>
<li>CSS transitions for webkit browsers</li>
</ul>
<div class="cf_info failure" id="usermessagea">
<p><strong>Update:</strong> A new take on futurebox that doesn&#8217;t use the target pseudo-class. <a href="http://www.thecssninja.com/css/futurebox3">Futurebox v3</a>.</p>
</div>
<div class="resources01"><a target="_blank" class="demo" title="Futurebox, lightbox without the javascript" href="http://www.thecssninja.com/demo/futurebox/revisited/">View a live demo</a> <a target="_blank" class="demo source" title="Download the source of the Futurebox demo" href="http://www.thecssninja.com/demo/futurebox/revisited/futurebox2.zip">Download the source files</a></div>
<h2 class="subtitle02">It&#8217;s back with a vengeance</h2>
<p>The overall markup hasn&#8217;t been altered all that much compared to the original it just adds useful functionality.</p>

<div class="wp_syntax"><div class="code"><pre class="html4strict html4strict" style="font-family:monospace;"><span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">ul</span> <span style="color: #000066;">class</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;overlay_container&quot;</span>&gt;</span>
	<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">li</span> <span style="color: #000066;">class</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;overlay&quot;</span> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;futurebox_img2&quot;</span>&gt;</span>
		<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">a</span> <span style="color: #000066;">href</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;#close&quot;</span> <span style="color: #000066;">title</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;Close future box&quot;</span>&gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">a</span>&gt;</span>
		<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">p</span> <span style="color: #000066;">title</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;Image description goes here&quot;</span>&gt;</span>
			<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">a</span> <span style="color: #000066;">href</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;#futurebox_img1&quot;</span> <span style="color: #000066;">rel</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;prev&quot;</span> <span style="color: #000066;">title</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;Previous&quot;</span>&gt;</span><span style="color: #ddbb00;">&amp;lt;</span><span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">a</span>&gt;</span>
			<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">a</span> <span style="color: #000066;">href</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;#futurebox_img3&quot;</span> <span style="color: #000066;">accesskey</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;3&quot;</span> <span style="color: #000066;">rel</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;next&quot;</span> <span style="color: #000066;">title</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;Next&quot;</span>&gt;</span><span style="color: #ddbb00;">&amp;gt;</span><span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">a</span>&gt;</span>
		<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">p</span>&gt;</span>
	<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">li</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">ul</span>&gt;</span></pre></div></div>

<p>The anchor tag is now empty as we load the image as a background. A paragraph tag containing our next and previous links along with a title attribute so we can give the image a small description. The rest of the markup and foundation CSS stays pretty much the same.</p>
<p>Due to the complex nature of this updated version browser support is not a great as the original version. I will run this through a <a href="http://browsershots.org/websites/2790502/">browser screenshot service</a> and confirm browser support. If you come across any weird behaviour please make a comment about it.</p>
<h2 class="subtitle02">Lazy loading images</h2>
<p>In the <a href="http://www.thecssninja.com/demo/futurebox/">original demo</a> the images were loaded in the page essentially slowing down the load time. I&#8217;ve fixed that by using a lazy load technique, rather than having an &lt;img&gt; tag I set a background on the overlay anchor tag. The image is only downloaded when the user clicks on a thumbnail.</p>

<div class="wp_syntax"><div class="code"><pre class="css css" style="font-family:monospace;"><span style="color: #6666ff;">.overlay</span><span style="color: #3333ff;">:target </span><span style="color: #00AA00;">&gt;</span> a <span style="color: #00AA00;">&#123;</span> <span style="color: #000000; font-weight: bold;">background</span><span style="color: #00AA00;">:</span> <span style="color: #993333;">url</span><span style="color: #00AA00;">&#40;</span><span style="color: #ff0000; font-style: italic;">gr_cssninja_lrg.png</span><span style="color: #00AA00;">&#41;</span> <span style="color: #933;"><span style="color: #cc66cc;">50</span>%</span> <span style="color: #933;"><span style="color: #cc66cc;">50</span>%</span> <span style="color: #993333;">no-repeat</span>; <span style="color: #00AA00;">&#125;</span></pre></div></div>

<p>We use the <em>#futurebox_img2.overlay</em> selector for all other overlays to target each item so we can change the background image correspondingly.</p>
<h2 class="subtitle02">Scroll through images</h2>
<p>I have added ability to scroll forwards and backwards through the images using buttons. These form part of the markup in each overlay item essentially just applying the previous images id in the href and the same for the next button.</p>

<div class="wp_syntax"><div class="code"><pre class="html4strict html4strict" style="font-family:monospace;"><span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">a</span> <span style="color: #000066;">href</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;#futurebox_img1&quot;</span> <span style="color: #000066;">rel</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;prev&quot;</span> <span style="color: #000066;">title</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;Previous&quot;</span>&gt;</span><span style="color: #ddbb00;">&amp;lt;</span><span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">a</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">a</span> <span style="color: #000066;">href</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;#futurebox_img3&quot;</span> <span style="color: #000066;">accesskey</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;3&quot;</span> <span style="color: #000066;">rel</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;next&quot;</span> <span style="color: #000066;">title</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;Next&quot;</span>&gt;</span><span style="color: #ddbb00;">&amp;gt;</span><span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">a</span>&gt;</span></pre></div></div>

<p>On each next/previous link we use the <em>rel</em> attribute so we can target it in CSS using attribute selectors</p>

<div class="wp_syntax"><div class="code"><pre class="css css" style="font-family:monospace;"><span style="color: #6666ff;">.overlay</span> <span style="color: #00AA00;">&gt;</span> p a<span style="color: #00AA00;">,</span>
<span style="color: #6666ff;">.overlay</span> <span style="color: #00AA00;">&gt;</span> p a<span style="color: #00AA00;">&#91;</span>rel<span style="color: #00AA00;">=</span>next<span style="color: #00AA00;">&#93;</span>
<span style="color: #00AA00;">&#123;</span>
	<span style="color: #808080; font-style: italic;">/* styles here */</span>
<span style="color: #00AA00;">&#125;</span>
<span style="color: #6666ff;">.overlay</span> <span style="color: #00AA00;">&gt;</span> p a<span style="color: #00AA00;">&#91;</span>rel<span style="color: #00AA00;">=</span>prev<span style="color: #00AA00;">&#93;</span>
<span style="color: #00AA00;">&#123;</span>
	<span style="color: #808080; font-style: italic;">/* styles here */</span>
<span style="color: #00AA00;">&#125;</span></pre></div></div>

<h2 class="subtitle02">Keyboard navigation</h2>
<p>Along with navigation buttons to scroll through the images you can use the keyboard to scroll through in any order. Closing futurebox is also possible using the keyboard. This is done using accesskeys and controls vary in different browsers and platforms.</p>

<div class="wp_syntax"><div class="code"><pre class="html4strict html4strict" style="font-family:monospace;"><span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">a</span> <span style="color: #000066;">href</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;#futurebox_img3&quot;</span> <span style="color: #000066;">accesskey</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;3&quot;</span> <span style="color: #000066;">rel</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;next&quot;</span> <span style="color: #000066;">title</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;Next&quot;</span>&gt;</span><span style="color: #ddbb00;">&amp;gt;</span><span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">a</span>&gt;</span></pre></div></div>

<p>Using the numbers you can scroll through up to 10 images using the 0-9 numbers and the corresponding keyboard combination in your browser:</p>
<ul>
<li>Firefox Windows: Alt + Shift + 0-9</li>
<li>Safari/Chrome Windows: Alt + 0-9</li>
<li>Opera Windows: Shift + Esc (This brings up a menu where you press the accesskey you wish to activate)</li>
</ul>
<p>To close futurebox the access key is <strong>x</strong>. If you know of any other keyboard combinations, especially the Mac equivalents, I&#8217;ll update the article.</p>
<h2 class="subtitle02">CSS generated content</h2>
<p>It is now also possible to add a small description below each image using the CSS <a href="http://www.w3.org/TR/CSS2/generate.html#content">content property</a>.</p>

<div class="wp_syntax"><div class="code"><pre class="css css" style="font-family:monospace;"><span style="color: #6666ff;">.overlay</span> <span style="color: #00AA00;">&gt;</span> a <span style="color: #00AA00;">+</span> p<span style="color: #3333ff;"><span style="color: #00AA00;">:</span>after
</span><span style="color: #00AA00;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">content</span><span style="color: #00AA00;">:</span> <span style="color: #ff0000;">&quot;No. &quot;</span> counter<span style="color: #00AA00;">&#40;</span>futurebox<span style="color: #00AA00;">&#41;</span> <span style="color: #ff0000;">&quot; &quot;</span> attr<span style="color: #00AA00;">&#40;</span>title<span style="color: #00AA00;">&#41;</span>;
<span style="color: #00AA00;">&#125;</span></pre></div></div>

<p>That snippet will get the paragraph tag that is a direct sibling of the anchor tag that is a direct child of the overlay class. Using the <em>:after</em> pseudo-element is how we can append the content after the paragraph.</p>
<p>We also use generated content to do automatic numbering of each image using the <a href="http://www.w3.org/TR/CSS2/generate.html#counters">counter-reset and counter-increment</a> properties in conjunction with the <em>counter()</em> function used in the <em>content</em> property.</p>

<div class="wp_syntax"><div class="code"><pre class="css css" style="font-family:monospace;"><span style="color: #6666ff;">.overlay_container</span> 	<span style="color: #00AA00;">&#123;</span> <span style="color: #000000; font-weight: bold;">counter-reset</span><span style="color: #00AA00;">:</span> futurebox; <span style="color: #00AA00;">&#125;</span>
<span style="color: #6666ff;">.overlay_container</span> <span style="color: #00AA00;">&gt;</span> li 	<span style="color: #00AA00;">&#123;</span> <span style="color: #000000; font-weight: bold;">counter-increment</span><span style="color: #00AA00;">:</span> futurebox; <span style="color: #00AA00;">&#125;</span></pre></div></div>

<p>We first reset the counter so we know it will start from 1, we also give it a unique name e.g. futurebox but this can be anything. Then in the following line we increment the counter for each list item inside the <em>overlay_container</em>, we use the child selector to make sure it only counts top level list items. The counter increment idea came from this blog post about doing a <a href="http://habrahabr.ru/blogs/css/66120/">css based gallery</a> (it&#8217;s in Russian so you&#8217;ll need to run it through a translator).</p>
<h2 class="subtitle02">Animation without the JavaScript</h2>
<p>One of the most requested features for futurebox was using webkits CSS transitions property to animate scrolling between images. So in this version I have added fading between image overlays. In Chrome and Safari we animate the opacity of the image so it will fade in upon activation.</p>

<div class="wp_syntax"><div class="code"><pre class="css css" style="font-family:monospace;"><span style="color: #6666ff;">.overlay</span> a
<span style="color: #00AA00;">&#123;</span>
	opacity<span style="color: #00AA00;">:</span> 0;
	-webkit-transition<span style="color: #00AA00;">:</span> opacity 1s ease;
<span style="color: #00AA00;">&#125;</span>
<span style="color: #6666ff;">.overlay</span><span style="color: #3333ff;">:hover </span>a <span style="color: #00AA00;">&#123;</span> opacity<span style="color: #00AA00;">:</span> <span style="color: #cc66cc;">1</span>; <span style="color: #00AA00;">&#125;</span></pre></div></div>

<p>To setup the transition we set the opacity to 0 and tell the transition to ease the opacity over 1 second. The reason I activate the transition on <em>:hover</em> and not <em>:target</em> is that target will not transition upon activation, whether this is a bug will take some further investigation.</p>
<p>To stop issues in browsers that don’t support transitions. I wrapped the transition styles in a media query.</p>

<div class="wp_syntax"><div class="code"><pre class="css css" style="font-family:monospace;"><span style="color: #a1a100;">@media (-webkit-transition) {</span>
	<span style="color: #808080; font-style: italic;">/* Styles here */</span>
<span style="color: #00AA00;">&#125;</span></pre></div></div>

<p>This will only target webkit browsers that support the <em>-webkit-transition</em> property. When Firefox &#038; Opera add support for transitions hopefully they will add support for this media query.</p>
<p>Another issue with transitions is hover will fail to activate if you launch futurebox using the keyboard, moving your mouse will then trigger the transition to execute. Only the initial launch is affected using the keyboard to scroll through other images will trigger the transition correctly.</p>
<h2 class="subtitle02">Some things to note</h2>
<p>While this does have good browser support it won&#8217;t work in IE and I don&#8217;t see it working without considerable use of hacks. There is however a fantastic article over at Carsonified on <a href="http://carsonified.com/blog/design/css/how-to-create-a-valid-non-javascript-lightbox/">creating a lighbox effect that works in IE</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.thecssninja.com/css/futurebox2/feed</wfw:commentRss>
		<slash:comments>2</slash:comments>
		<feedburner:origLink>http://www.thecssninja.com/css/futurebox2</feedburner:origLink></item>
		<item>
		<title>The File API has changed</title>
		<link>http://feedproxy.google.com/~r/TheCSSNinja/~3/IAgy9HJuq5s/fileapi</link>
		<comments>http://www.thecssninja.com/javascript/fileapi#comments</comments>
		<pubDate>Thu, 10 Dec 2009 06:53:52 +0000</pubDate>
		<dc:creator>The Css Ninja</dc:creator>
				<category><![CDATA[html5]]></category>
		<category><![CDATA[javascript]]></category>

		<guid isPermaLink="false">http://www.thecssninja.com/?p=530</guid>
		<description><![CDATA[Recently I have been touting how awesome and revolutionary the File API is through a few demo&#8217;s. After some feedback on webapps mailing list there have been some major changes to the API and how it works. I have updated my previous drag and drop upload demo to reflect the API changes, as of Firefox [...]]]></description>
			<content:encoded><![CDATA[<p>Recently I have been touting how awesome and revolutionary the <a href="http://www.w3.org/TR/FileAPI/">File API</a> is through a <a href="http://www.thecssninja.com/javascript/drag-and-drop-upload">few</a> <a href="http://www.thecssninja.com/javascript/font-dragr">demo&#8217;s</a>. After some feedback on webapps mailing list there have been some major changes to the API and how it works.<span id="more-530"></span></p>
<p>I have updated my <a href="http://www.thecssninja.com/demo/drag-drop_upload/">previous drag and drop upload demo</a> to reflect the API changes, as of Firefox 3.6b3 the API supports both the original API and the updated one. The older model will eventually be dropped, it&#8217;s only in there for legacy purposes. To use the demo you will need <a href="http://www.mozilla.com/en-US/firefox/all-beta.html">Firefox 3.6</a> installed. You can also watch the <a href="http://screenr.com/i18">screencast of it in action</a>.</p>
<div class="resources01"><a target="_blank" class="demo" title="Drag and drop files from your computer and they're uploaded using a XMLHttpRequest" href="http://www.thecssninja.com/demo/drag-drop_upload/v2/">View a live demo</a> <a target="_blank" class="demo source" title="Download the source of the Drag and drop upload demo" href="http://www.thecssninja.com/demo/drag-drop_upload/v2/drag-drop_upload.zip">Download the source files</a></div>
<p>The biggest change is with the file handling, it is now processed asynchronously with progress events so we can attach listeners. The advantage of this is if a user drags in many files or a large file the UI won&#8217;t be locked up while it&#8217;s processing the data, much like XmlHttpRequest works.</p>
<h2 class="subtitle02">The File object</h2>
<p>The <a href="https://developer.mozilla.org/en/DOM/File">File object</a> has been updated to reflect the new specs changes and has deprecated all the previous methods we used to get the file in various formats e.g. <em>getAsDataUrl()</em>, <em>getAsText()</em>, <em>getAsBinary()</em>. We now handle these methods in the new <a href="https://developer.mozilla.org/en/DOM/FileReader">FileReader object</a>.</p>
<p>It has also renamed the 2 properties for accessing the files name and size from <em>fileName</em>/<em>fileSize</em> to <em>name</em>/<em>size</em> respectively.</p>
<h2 class="subtitle02">The FileReader object</h2>
<p>This new object allows us to asynchronously read the contents of a file from a drop event.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> reader <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> FileReader<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>;
&nbsp;
reader.<span style="color: #660066;">addEventListener</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;loadend&quot;</span><span style="color: #339933;">,</span> TCNDDU.<span style="color: #660066;">buildImageListItem</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span>;
reader.<span style="color: #660066;">readAsDataURL</span><span style="color: #009900;">&#40;</span>file<span style="color: #009900;">&#41;</span>;</pre></div></div>

<p>Instead of locking the UI while we wait for it to loop through all the dropped files and then convert them to a DataUrl. The <em>FileReader</em> does this asynchronously. We attach to the <em>onloadend</em> event handler which will fire once the current file has been read into the <em>result</em> attribute. Upon the event firing we then take the event <em>result</em> and add the DataURL to the source of the image to be displayed to the user while it uploads.</p>
<h2 class="subtitle02">Send the binary data</h2>
<p>Once we have the file we send it to be processed for an XHR call so we can upload it to the server. Same as above we need to create a file reader and attach to the <em>onloadend</em> event which we then pass to the <em>sendAsBinary()</em> method.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> getBinaryDataReader <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> FileReader<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>;
&nbsp;
getBinaryDataReader.<span style="color: #660066;">addEventListener</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;loadend&quot;</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>evt<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>xhr.<span style="color: #660066;">sendAsBinary</span><span style="color: #009900;">&#40;</span>evt.<span style="color: #660066;">target</span>.<span style="color: #660066;">result</span><span style="color: #009900;">&#41;</span>;<span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span>;
getBinaryDataReader.<span style="color: #660066;">readAsBinaryString</span><span style="color: #009900;">&#40;</span>file<span style="color: #009900;">&#41;</span>;</pre></div></div>

<p>Similar code we used for the image display handling, but we use the <em>readAsBinaryString()</em> method to return the files binary data for uploading.</p>
<h2 class="subtitle02">Further reading</h2>
<p>As the 3.6 final release date is coming closer Mozilla has been ramping up demos and documentation about the File API. Some good documentation on <a href="https://developer.mozilla.org/en/Using_files_from_web_applications">handling files in web applications</a>.</p>
<p>The <a href="http://hacks.mozilla.org/">hacks blog</a> has recently put up some <a href="http://hacks.mozilla.org/2009/12/w3c-fileapi-in-firefox-3-6/">great information about the File API</a> along with an excellent demo extracting <a href="http://hacks.mozilla.org/2009/12/firefox-36-fileapi-demo-reading-exif-data-from-a-local-jpeg-file/">EXIF data from an image</a>. </p>
<p>Now we just need webkit to push out the File API to their nightly builds.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.thecssninja.com/javascript/fileapi/feed</wfw:commentRss>
		<slash:comments>29</slash:comments>
		<feedburner:origLink>http://www.thecssninja.com/javascript/fileapi</feedburner:origLink></item>
	</channel>
</rss><!-- Dynamic page generated in 0.850 seconds. --><!-- Cached page generated by WP-Super-Cache on 2010-08-30 20:21:59 -->
