<?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>Sat, 27 Feb 2010 01:28:10 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=abc</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<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>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: #3333ff;"><span style="color: #00AA00;">:</span>before
</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;">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;">position</span><span style="color: #00AA00;">:</span> <span style="color: #993333;">absolute</span>;
    <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;">left</span><span style="color: #00AA00;">:</span> <span style="color: #933;">16px</span>;
    <span style="color: #000000; font-weight: bold;">content</span><span style="color: #00AA00;">:</span> <span style="color: #ff0000;">&quot;<span style="color: #000099; font-weight: bold;">\0</span>0a0<span style="color: #000099; font-weight: bold;">\0</span>0a0<span style="color: #000099; font-weight: bold;">\0</span>0a0&quot;</span>; <span style="color: #808080; font-style: italic;">/* 3x &amp;nbsp; */</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;">width</span><span style="color: #00AA00;">:</span> <span style="color: #933;">16px</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;">margin</span><span style="color: #00AA00;">:</span> 0 <span style="color: #933;">3px</span> 0 <span style="color: #933;">-16px</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 lastly it has the <code>:before</code> pseudo-element so we can add our custom radio input. 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><strong id="sprite">Update:</strong> Thanks to <a href="#comment-1182">Lea</a> and <a href="#comment-1184">Mr.MoOx</a> for there suggestions this now uses a sprite image. Rather then 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.</p>
<p>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.</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: #3333ff;">:before </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;">-73px</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;">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: #3333ff;">:before</span><span style="color: #00AA00;">,</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: #3333ff;">:before </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;">-19px</span>; 
<span style="color: #00AA00;">&#125;</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: #3333ff;">:before</span><span style="color: #00AA00;">,</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: #3333ff;">:before </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;">-91px</span>; 
<span style="color: #00AA00;">&#125;</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: #3333ff;">:before</span><span style="color: #00AA00;">,</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: #3333ff;">:before </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;">-55px</span>; 
<span style="color: #00AA00;">&#125;</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: #3333ff;">:before</span><span style="color: #00AA00;">,</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: #3333ff;">:before </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;">-127px</span>; 
<span style="color: #00AA00;">&#125;</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: #3333ff;">:before </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;">-37px</span>; 
<span style="color: #00AA00;">&#125;</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: #3333ff;">:before </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;">-109px</span>; 
<span style="color: #00AA00;">&#125;</span></pre></div></div>

<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. This of course degrades nicely in non-supporting browsers and fallback to the browser default form elements.</p>
<h2 class="subtitle02">Accessible and friendly</h2>
<p>CSS generated content doesn&#8217;t get in the way of a screen reader and they have a clear view of the form elements (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 4+*</li>
<li>iPhone/iPod Safari**</li>
<li>Chrome 4+</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>** 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.</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: #3333ff;"><span style="color: #00AA00;">:</span>before
</span><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>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.</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>33</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 on [...]]]></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>1</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>11</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 futurebox overlay [...]]]></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="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 3.6b3 [...]]]></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>6</slash:comments>
		<feedburner:origLink>http://www.thecssninja.com/javascript/fileapi</feedburner:origLink></item>
		<item>
		<title>Font Dragr: A drag and drop font tester</title>
		<link>http://feedproxy.google.com/~r/TheCSSNinja/~3/ablbi-pQdbo/font-dragr</link>
		<comments>http://www.thecssninja.com/javascript/font-dragr#comments</comments>
		<pubDate>Tue, 13 Oct 2009 07:39:27 +0000</pubDate>
		<dc:creator>The Css Ninja</dc:creator>
				<category><![CDATA[html5]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[@font-face]]></category>
		<category><![CDATA[Firefox]]></category>

		<guid isPermaLink="false">http://www.thecssninja.com/?p=484</guid>
		<description><![CDATA[After playing with the new file API draft spec available in Firefox 3.6 with my drag and drop upload article. I had another idea when I was playing with custom fonts, @font-face, in the browser. What if you could drag an drop a font file (otf, ttf, svg, woff) from your desktop into the browser [...]]]></description>
			<content:encoded><![CDATA[<p>After playing with the new file API draft spec available in Firefox 3.6 with my <a href="http://www.thecssninja.com/javascript/drag-and-drop-upload">drag and drop upload article</a>. I had another idea when I was playing with custom fonts, @font-face, in the browser. What if you could drag an drop a font file (otf, ttf, svg, woff) from your desktop into the browser and have text rendered on the fly using any available valid font.<span id="more-484"></span></p>
<p>In conjunction with Mozilla&#8217;s official announcement for the <a href="http://hacks.mozilla.org/2009/10/woff/">Web Open Font Format (WOFF)</a> and continuing the wanky tradition of removing vowels from web app names, I introduce font dragr.</p>
<div class="resources01"><a href="http://labs.thecssninja.com/font_dragr/" title="HTML5 web app for testing custom font files, drag and drop font testing" class="demo" target="_blank">View the web app</a></div>
<h2 class="subtitle02">What is it?</h2>
<p><a href="http://labs.thecssninja.com/font_dragr/" title="HTML5 web app for testing custom font files, drag and drop font testing" class="demo" target="_blank">Font dragr</a> is an experimental web app that uses HTML5 &#038; CSS3 to create a useful standalone web based application for testing custom fonts, once you visit it for the first time you don&#8217;t need to be online to use it after the initial visit. It allows you, in Firefox 3.6+, to drag and drop font files from your file system into the drop area. The browser will then create a data URL encoded copy to use in the page and render the content in the dropped font.</p>
<h2 class="subtitle02">See it in action</h2>
<p><object classid='clsid:d27cdb6e-ae6d-11cf-96b8-444553540000' codebase='http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,115,0' width='609' height='366'><param name='movie' value='http://screenr.com/Content/assets/screenr_0817090731.swf' /><param name='flashvars' value='i=20449' /><param name='allowFullScreen' value='true' /><embed src='http://screenr.com/Content/assets/screenr_0817090731.swf' flashvars='i=20449' allowFullScreen='true' width='609' height='366' pluginspage='http://www.macromedia.com/go/getflashplayer'></embed></object></p>
<h2 class="subtitle02">Encoded files</h2>
<p>I went about testing out a few theories on how I could dynamically add a dropped font to be used in the page. I saw this article on embedding <a href="http://robert.accettura.com/blog/2009/07/03/optimizing-font-face-for-performance/">custom fonts using a base64</a> encoded string. In the file API we have the <em>getAsDataURL()</em> method to return a file as a data url for using in a page, I used the same method in my <a href="http://www.thecssninja.com/demo/drag-drop_upload/" class="external-link">drag drop upload demo</a> to display the images.</p>
<h2 class="subtitle02">Inserting into the stylesheet</h2>
<p>The next hurdle was taking that data url and inserting it into the style sheet referenced on the page.</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> styleSheet <span style="color: #339933;">=</span> document.<span style="color: #660066;">styleSheets</span><span style="color: #009900;">&#91;</span>0<span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span>
	fontFaceStyle <span style="color: #339933;">=</span> 
	<span style="color: #3366CC;">&quot;@font-face{font-family: CustomFont;src:url(&quot;</span><span style="color: #339933;">+</span>files<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span>.<span style="color: #660066;">getAsDataURL</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">+</span><span style="color: #3366CC;">&quot;);}&quot;</span>;
&nbsp;
styleSheet.<span style="color: #660066;">insertRule</span><span style="color: #009900;">&#40;</span>fontFaceStyle<span style="color: #339933;">,</span> 0<span style="color: #009900;">&#41;</span>;</pre></div></div>

<p>In the above code we reference our style sheet using <em>document.styleSheet[0]</em>. We then build our style from the dropped file that we want to insert, notice the <em>files[i].getAsDataURL()</em> is where the magic happens and the current file is converted to a dataURL. We then use the <em>insertRule</em> method to prepend it to the style sheet.</p>
<h2 class="subtitle02">Getting file information and applying the font</h2>
<p>We also get the <em>fileSize</em> of the font and the <em>fileName</em> so we can populate the list item that we create on drop. With the file name I also do some crude validation and I also sanitise the file name in case there is any non alpha numeric characters, it will stop the font from being applied to the container if we have invalid characters.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript javascript" style="font-family:monospace;">acceptedFileExtensions <span style="color: #339933;">=</span> <span style="color: #009966; font-style: italic;">/^.*\.(ttf|otf|svg|woff)$/i</span>
&nbsp;
droppedFileName <span style="color: #339933;">=</span> droppedFullFileName.<span style="color: #660066;">replace</span><span style="color: #009900;">&#40;</span><span style="color: #009966; font-style: italic;">/\..+$/</span><span style="color: #339933;">,</span><span style="color: #3366CC;">&quot;&quot;</span><span style="color: #009900;">&#41;</span>;
droppedFileName <span style="color: #339933;">=</span> droppedFileName.<span style="color: #660066;">replace</span><span style="color: #009900;">&#40;</span><span style="color: #009966; font-style: italic;">/\W+/g</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;-&quot;</span><span style="color: #009900;">&#41;</span>;</pre></div></div>

<p>For the validation I have a regexp of accepted file extensions so it will only accept font types that work in Firefox. I remove the file extension from the fileName and then replace any non alphanumeric characters, <strong>\W</strong>, with hyphens.</p>
<p>Once we have a semi-friendly name we use that to apply the font family style to the newly created list item and the content container.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript javascript" style="font-family:monospace;">displayContainer.<span style="color: #660066;">style</span>.<span style="color: #660066;">fontFamily</span> <span style="color: #339933;">=</span> droppedFileName;</pre></div></div>

<h2 class="subtitle02">International font friendly</h2>
<p>Dragging in an international based font file will cause the text to render in a default sans serif font. To fix that I have made the main content area editable, you can type some text in your own language which should render in your international based font, this hasn&#8217;t been thoroughly tested. I would appreciate any feedback on using this demo with international fonts.</p>
<h2 class="subtitle02">Work in progress</h2>
<p>I am still very much actively refining and adding more features to this web app. If you have any of your own features/ideas please feel free to suggest/request any improvements or changes. Some I&#8217;m working on now.</p>
<ul>
<li>Using localStorage object to store users dropped fonts and <del datetime="2009-10-28T06:04:17+00:00">save edits to main content area</del>. Main content edits are now stored in localStorage, hit size limit capacity with dropped fonts will look into webDB for font storage.</li>
<li>Better indicating to the user that the content can be edited</li>
<li>Making the drop area highlight on drag</li>
</ul>
<p>I built this to fill a need I had, to quickly test custom fonts, and the idea has grown in to something much bigger and better than I thought it would be. I can see this as not only something cool to show off where HTML5 is heading but something genuinely useful.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.thecssninja.com/javascript/font-dragr/feed</wfw:commentRss>
		<slash:comments>22</slash:comments>
		<feedburner:origLink>http://www.thecssninja.com/javascript/font-dragr</feedburner:origLink></item>
		<item>
		<title>Make IE awesome by turning it into Google Chrome</title>
		<link>http://feedproxy.google.com/~r/TheCSSNinja/~3/k5h5wRXbgy8/make-ie-awesome</link>
		<comments>http://www.thecssninja.com/javascript/make-ie-awesome#comments</comments>
		<pubDate>Wed, 23 Sep 2009 12:15:15 +0000</pubDate>
		<dc:creator>The Css Ninja</dc:creator>
				<category><![CDATA[html5]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[canvas]]></category>
		<category><![CDATA[chrome]]></category>
		<category><![CDATA[google]]></category>

		<guid isPermaLink="false">http://www.thecssninja.com/?p=455</guid>
		<description><![CDATA[As I&#8217;m sure you all know by now Google made an announcement about their Chrome Frame plugin for IE that turns the Trident rendering engine into the Chrome rendering engine giving IE access to the awesome power that is Chrome. No longer will IE miss out on those awesome new features in CSS3 &#038; HTML5 [...]]]></description>
			<content:encoded><![CDATA[<p>As I&#8217;m sure you all know by now Google made an announcement about their <a href="http://blog.chromium.org/2009/09/introducing-google-chrome-frame.html">Chrome Frame plugin for IE</a> that turns the Trident rendering engine into the Chrome rendering engine giving IE access to the awesome power that is <a href="http://code.google.com/chrome/chromeframe/">Chrome</a>. No longer will IE miss out on those awesome new features in CSS3 &#038; HTML5 the more competent browsers enjoy today such as the soon to be released <a href="http://wave.google.com/">Google Wave</a>.<span id="more-455"></span></p>
<p>I did a post a while back on how <a href="http://www.thecssninja.com/rant/chroming-how-google-is-changing-the-browser">Google Chrome is changing the browser</a>, the landscape is rapidly progressing thanks to Google and the likes of Mozilla, Webkit &#038; Opera. <a href="http://code.google.com/chrome/chromeframe/">This plugin</a> helps push that final frontier into fruition bringing the next set of tools to all major browsers.</p>
<div class="resources01"><a target="_blank" class="demo" title="A demo using canvas that's works in IE with Chrome Frame installed" href="http://www.thecssninja.com/demo/particle_fountain/">View a live demo</a> <a target="_blank" class="demo source" title="Download the source of the canvas demo" href="http://www.thecssninja.com/demo/particle_fountain/particle_fountain.zip">Download the source files</a></div>
<p><small>This demo was slightly modified from the awesome work of http://iterationsix.com/posts/16 particle fountain.</small></p>
<h2 class="subtitle02">How does it work?</h2>
<p>The above demo uses the HTML5 canvas tag to simulate particle effects. If you load this in IE you will be prompted to install Chrome Frame. Setting up a site to make it render using Chrome Frame is a simple meta tag.</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;">meta</span> <span style="color: #000066;">http-equiv</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;X-UA-Compatible&quot;</span> <span style="color: #000066;">content</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;chrome=1&quot;</span>&gt;</span></pre></div></div>

<p>Look familiar? It&#8217;s the same meta tag that IE uses to force IE8 to render in <a href="http://msdn.microsoft.com/en-au/library/cc817574.aspx" target="_blank">compatibility mode</a>. </p>
<p>If IE goes to a site that has this meta tag it will kick-in and render the page using Chrome Frame giving the full power of Google Chrome in IE. Pretty simple and incredibly powerful.</p>
<p><object classid='clsid:d27cdb6e-ae6d-11cf-96b8-444553540000' codebase='http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,115,0' width='609' height='366'><param name='movie' value='http://screenr.com/Content/assets/screenr_0817090731.swf' /><param name='flashvars' value='i=14065' /><param name='allowFullScreen' value='true' /><embed src='http://screenr.com/Content/assets/screenr_0817090731.swf' flashvars='i=14065' allowFullScreen='true' width='609' height='366' pluginspage='http://www.macromedia.com/go/getflashplayer'></embed></object><br />
<small>IE8 vs IE8 with <em>cf:</em> protocol to force use of Chrome Frame</small></p>
<h2 class="subtitle02">Prompt the user to install Chrome Frame</h2>
<p>What if the user doesn&#8217;t know about or have the Chrome Frame installed on their machine? There&#8217;s a solution for that. Using a script available on Google Ajax APIs</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;">script</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;text/javascript&quot;</span> <span style="color: #000066;">src</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;http://ajax.googleapis.com/ajax/libs/chrome-frame/1/CFInstall.min.js&quot;</span>&gt;</span> <span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">script</span>&gt;</span>
&nbsp;
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">script</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;text/javascript&quot;</span>&gt;</span>
CFInstall.check();
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">script</span>&gt;</span></pre></div></div>

<p>The above code will check if Chrome Frame is installed if not it will prompt the user to install it by loading up an iframe with the Chrome Frame site. The <em>check</em> method gives you quite a <a href="http://code.google.com/chrome/chromeframe/developers_guide.html#CFInstall_check_Options">few options</a> all of them are optional the previous link gives a good run down of all the options and what they do.</p>
<h2 class="subtitle02">Force it</h2>
<p>If your are keen to use it before sites start adding the meta tag you can force the site to render using Chrome Frame by appending the <b>cf:</b> protocol to the beginning of any url e.g. <a href="cf:http://bespin.mozilla.com/">cf:http://bespin.mozilla.com/</a> if loaded in IE with Chrome Frame installed it will force the website to render using the Chrome engine.</p>
<h2 class="subtitle02">Early stages</h2>
<p>Right now this plugin is in early development stages but I&#8217;m sure it will move along rapidly to coincide with Google Wave public release.</p>
<p>This is truly an exciting plugin that opens the flood gates to a massive user base being able to use the latest and greatest web apps.<!--more--></p>
]]></content:encoded>
			<wfw:commentRss>http://www.thecssninja.com/javascript/make-ie-awesome/feed</wfw:commentRss>
		<slash:comments>4</slash:comments>
		<feedburner:origLink>http://www.thecssninja.com/javascript/make-ie-awesome</feedburner:origLink></item>
		<item>
		<title>Drag and drop file uploading using JavaScript</title>
		<link>http://feedproxy.google.com/~r/TheCSSNinja/~3/hUBjdsNRKsc/drag-and-drop-upload</link>
		<comments>http://www.thecssninja.com/javascript/drag-and-drop-upload#comments</comments>
		<pubDate>Wed, 26 Aug 2009 08:52:39 +0000</pubDate>
		<dc:creator>The Css Ninja</dc:creator>
				<category><![CDATA[html5]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[draganddrop]]></category>
		<category><![CDATA[fileapi]]></category>
		<category><![CDATA[xhr2]]></category>

		<guid isPermaLink="false">http://www.thecssninja.com/?p=430</guid>
		<description><![CDATA[With the recent announcement of the File API draft specification being published I&#8217;m sure a lot of people were confused as to what it could really do and why it is truly a powerful API. Firefox&#8217;s latest alpha release of their 3.6 browser, aka Namoroka, is the first to implement this new draft specification.
One of [...]]]></description>
			<content:encoded><![CDATA[<p>With the recent announcement of the <a href="http://dev.w3.org/2006/webapi/FileUpload/publish/FileAPI.html">File API</a> draft specification being published I&#8217;m sure a lot of people were confused as to what it could really do and why it is truly a powerful API. Firefox&#8217;s latest alpha release of their 3.6 browser, aka Namoroka, is the first to implement this new draft specification.<span id="more-430"></span></p>
<p>One of those powerful things we can do with it is create a file uploader where the user can drag &#038; drop multiple files from their desktop straight into the browser avoiding the previous method of using the file input creating the ultimate killer feature that browsers so badly need.</p>
<p>The below demo only works in <a href="http://ftp.mozilla.org/pub/mozilla.org/firefox/nightly/3.6a1-candidates/build1/">Firefox 3.6 Alpha 1</a> if you don&#8217;t want to install it you can watch the screencast below.</p>
<div class="cf_info failure" id="usermessagea"><strong>Update:</strong> File API has changed see new article on changes. <a href="http://www.thecssninja.com/javascript/fileapi">The File API has changed</a></div>
<p><object classid='clsid:d27cdb6e-ae6d-11cf-96b8-444553540000' codebase='http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,115,0' width='609' height='366'><param name='movie' value='http://screenr.com/Content/assets/screenr_0817090731.swf' /><param name='flashvars' value='i=8363' /><param name='allowFullScreen' value='true' /><embed src='http://screenr.com/Content/assets/screenr_0817090731.swf' flashvars='i=8363' allowFullScreen='true' width='609' height='366' pluginspage='http://www.macromedia.com/go/getflashplayer'></embed></object></p>
<div class="resources01"><a href="http://www.thecssninja.com/demo/drag-drop_upload/" title="Drag and drop files from your computer and they're uploaded using a XMLHttpRequest" class="demo" target="_blank">View a live demo</a> <a href="http://www.thecssninja.com/demo/drag-drop_upload/drag-drop_upload.zip" title="Download the source of the Drag and drop upload demo" class="demo source" target="_blank">Download the source files</a></div>
<h2 class="subtitle02">Drag &#038; drop it like it&#8217;s hot</h2>
<p>Now I&#8217;m sure a few people would have seen this functionality already if they watched the Google Wave presentation where they demonstrated drag and drop file uploading, but they used Google Gears to accomplish this. They did mention they will be working on a draft spec to get this functionality into HTML5. This hasn&#8217;t happened yet and instead <a href="http://arunranga.com/blog/about/">Arun Ranganathan</a> of Mozilla wrote the first draft spec for such functionality to be possible.</p>
<p><object width="609" height="366"><param name="movie" value="http://www.youtube.com/v/v_UyVmITiYQ&#038;hl=en&#038;fs=1&#038;start=922"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="http://www.youtube.com/v/v_UyVmITiYQ&#038;hl=en&#038;fs=1&#038;start=922" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="609" height="366"></embed></object><br />
<small>Video will automatically start at 15m22s, where the drag drop is shown</small></p>
<h2 class="subtitle02">The File API</h2>
<p>The File API is what makes this whole thing possible, on the <em>dataTransfer</em> object it has been extended with the <em>file</em> attribute so it can read and convert the files you are dropping which then sends the file information as binary using an xhr <em>upload</em> attribute creating a desktop like behaviour but in the browser, opening great possibilities and much needed functionality that a user will find more intuitive.</p>
<h2 class="subtitle02">XMLHttpRequest 2</h2>
<p>The second revision of the <a href="http://www.w3.org/TR/2009/WD-XMLHttpRequest2-20090820/">XMLHTTPRequest specification</a> adds further functionality so we can actually send our dropped files to the server asynchronously. There are several additions which I use in this demo such as the <a href="http://www.w3.org/TR/2009/WD-XMLHttpRequest2-20090820/#the-upload-attribute"><em>upload</em> attribute</a> and the <a href="http://www.w3.org/TR/2009/WD-XMLHttpRequest2-20090820/#events">progress events</a> like <em>progress</em> and <em>load</em>. With those events we can give the user some detailed feedback such as a percentage loader used in this example.</p>
<h2 class="subtitle02">How it works</h2>
<p>This example uses a few emerging technologies such as xhr2, local file access and the drag and drop API. The order in which the events happen are as follows:</p>
<ul>
<li>The user drags images from their desktop to the drop area in the browser and fires the <em>TCNDDU.handleDrop</em> function.</li>
<li>The <em>dataTransfer</em> object passes through the local files dragged over through the <em>files</em> attribute</li>
<li>Using the <em>getAsDataURL</em> method we can convert the file to a base64 encoded string create an image and sets its source to that string.</li>
<li>The file is then passed into an xhr request where we use the new <em>sendAsBinary</em> method available since Firefox 3.0 and pass in the file as binary data using the <em>getAsBinary</em> method</li>
<li>We attach some <a href="http://www.w3.org/TR/XMLHttpRequest2/#events">progress events</a> to the <a href="http://www.w3.org/TR/XMLHttpRequest2/#the-upload-attribute">upload</a> attribute so we can create a progress bar with percentage feedback and a load progress event so we can remove the progress bar once the image has uploaded successfully</li>
</ul>
<p>I&#8217;ll go through some of the code in the demo to explain a few things in more detail.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript javascript" style="font-family:monospace;"><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; i <span style="color: #339933;">&lt;</span> count; i<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	domElements <span style="color: #339933;">=</span> <span style="color: #009900;">&#91;</span>
		document.<span style="color: #660066;">createElement</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'li'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
		document.<span style="color: #660066;">createElement</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'a'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
		document.<span style="color: #660066;">createElement</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'img'</span><span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#93;</span>;
&nbsp;
	domElements<span style="color: #009900;">&#91;</span><span style="color: #CC0000;">2</span><span style="color: #009900;">&#93;</span>.<span style="color: #660066;">src</span> <span style="color: #339933;">=</span> files<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span>.<span style="color: #660066;">getAsDataURL</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>; <span style="color: #006600; font-style: italic;">// base64 encodes local file(s)</span>
	domElements<span style="color: #009900;">&#91;</span><span style="color: #CC0000;">2</span><span style="color: #009900;">&#93;</span>.<span style="color: #660066;">width</span> <span style="color: #339933;">=</span> <span style="color: #CC0000;">300</span>;
	domElements<span style="color: #009900;">&#91;</span><span style="color: #CC0000;">2</span><span style="color: #009900;">&#93;</span>.<span style="color: #660066;">height</span> <span style="color: #339933;">=</span> <span style="color: #CC0000;">200</span>;
	domElements<span style="color: #009900;">&#91;</span><span style="color: #CC0000;">1</span><span style="color: #009900;">&#93;</span>.<span style="color: #660066;">appendChild</span><span style="color: #009900;">&#40;</span>domElements<span style="color: #009900;">&#91;</span><span style="color: #CC0000;">2</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span>;
	domElements<span style="color: #009900;">&#91;</span>0<span style="color: #009900;">&#93;</span>.<span style="color: #660066;">id</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;item&quot;</span><span style="color: #339933;">+</span>i;
	domElements<span style="color: #009900;">&#91;</span>0<span style="color: #009900;">&#93;</span>.<span style="color: #660066;">appendChild</span><span style="color: #009900;">&#40;</span>domElements<span style="color: #009900;">&#91;</span><span style="color: #CC0000;">1</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span>;
&nbsp;
	imgPreviewFragment.<span style="color: #660066;">appendChild</span><span style="color: #009900;">&#40;</span>domElements<span style="color: #009900;">&#91;</span>0<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span>;
&nbsp;
	dropListing.<span style="color: #660066;">appendChild</span><span style="color: #009900;">&#40;</span>imgPreviewFragment<span style="color: #009900;">&#41;</span>;
&nbsp;
	TCNDDU.<span style="color: #660066;">processXHR</span><span style="color: #009900;">&#40;</span>files.<span style="color: #000066; font-weight: bold;">item</span><span style="color: #009900;">&#40;</span>i<span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> i<span style="color: #009900;">&#41;</span>;
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>This for loop is inside the <em>TCNDDU.handleDrop</em> function which will loop through the files, the count var in the for loop is pointing to <em>event.dataTransfer.files.length</em> so we know how many files we are working with.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript javascript" style="font-family:monospace;">domElements<span style="color: #009900;">&#91;</span><span style="color: #CC0000;">2</span><span style="color: #009900;">&#93;</span>.<span style="color: #660066;">src</span> <span style="color: #339933;">=</span> files<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span>.<span style="color: #660066;">getAsDataURL</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>;</pre></div></div>

<p>This line sets the source of the image as a base64 encoded string so we can display the local file to the user straight away.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript javascript" style="font-family:monospace;">fileUpload.<span style="color: #660066;">addEventListener</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;progress&quot;</span><span style="color: #339933;">,</span> TCNDDU.<span style="color: #660066;">uploadProgressXHR</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span>;
fileUpload.<span style="color: #660066;">addEventListener</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;load&quot;</span><span style="color: #339933;">,</span> TCNDDU.<span style="color: #660066;">loadedXHR</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span>;
fileUpload.<span style="color: #660066;">addEventListener</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;error&quot;</span><span style="color: #339933;">,</span> TCNDDU.<span style="color: #660066;">uploadError</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>These lines are in the <em>TCNDDU.processXHR</em> that gets fired for each file dragged into the window. The <em>fileUpload</em> points to a <em>new XMLHttpRequest().upload</em> which we attach a few event listeners for progress, load and error so we can give useful feedback to the user.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript javascript" style="font-family:monospace;">xhr.<span style="color: #000066;">open</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;POST&quot;</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;upload.php&quot;</span><span style="color: #009900;">&#41;</span>;
xhr.<span style="color: #660066;">overrideMimeType</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'text/plain; charset=x-user-defined-binary'</span><span style="color: #009900;">&#41;</span>;
xhr.<span style="color: #660066;">sendAsBinary</span><span style="color: #009900;">&#40;</span>file.<span style="color: #660066;">getAsBinary</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>;</pre></div></div>

<p>Here we post the data to the PHP file for possible further processing etc. We also use the <em>overrideMimeType</em> method to user defined binary and finally use the new <em>sendAsBinary</em> method which has the local file passed in as binary.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript javascript" style="font-family:monospace;">TCNDDU.<span style="color: #660066;">uploadProgressXHR</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span> <span style="color: #009900;">&#40;</span>event<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>event.<span style="color: #660066;">lengthComputable</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #003366; font-weight: bold;">var</span> percentage <span style="color: #339933;">=</span> Math.<span style="color: #660066;">round</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span>event.<span style="color: #660066;">loaded</span> <span style="color: #339933;">*</span> <span style="color: #CC0000;">100</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">/</span> event.<span style="color: #660066;">total</span><span style="color: #009900;">&#41;</span>;
		<span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>percentage <span style="color: #339933;">&lt;</span> <span style="color: #CC0000;">100</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			event.<span style="color: #660066;">target</span>.<span style="color: #660066;">log</span>.<span style="color: #660066;">firstChild</span>.<span style="color: #660066;">nextSibling</span>.<span style="color: #660066;">firstChild</span>.<span style="color: #660066;">style</span>.<span style="color: #660066;">width</span> <span style="color: #339933;">=</span> 
			<span style="color: #009900;">&#40;</span>percentage<span style="color: #339933;">*</span><span style="color: #CC0000;">2</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span> <span style="color: #3366CC;">&quot;px&quot;</span>;
&nbsp;
			event.<span style="color: #660066;">target</span>.<span style="color: #660066;">log</span>.<span style="color: #660066;">firstChild</span>.<span style="color: #660066;">nextSibling</span>.<span style="color: #660066;">firstChild</span>.<span style="color: #660066;">textContent</span> <span style="color: #339933;">=</span> 
			percentage <span style="color: #339933;">+</span> <span style="color: #3366CC;">&quot;%&quot;</span>;
		<span style="color: #009900;">&#125;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>;</pre></div></div>

<p>For the progress event we attached earlier we can check if it will return the right information so we can do a progress bar by checking for the <em>lengthComputable</em> property if that’s available we know the progress event will return two values of <em>loaded</em> and <em>total</em> from there we can work out the percentage that has loaded and adjust our visual cues, in our case a progress bar.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript javascript" style="font-family:monospace;">event.<span style="color: #660066;">target</span>.<span style="color: #660066;">log</span>.<span style="color: #660066;">firstChild</span>.<span style="color: #660066;">nextSibling</span>.<span style="color: #660066;">firstChild</span></pre></div></div>

<p>This line allows us to get access to the current container of the image that is being calculated. <em>event.target</em> will always point back to the xhr object in the <em>TCNDDU.processXHR</em> we added a link to the container by adding <em>log</em> to <em>event.target</em> and pointing it to container variable.</p>
<h2 class="subtitle02">Not without issues</h2>
<p>There are two issues I came across with this demo and most likely are something I have done wrong or it could possibly be a bug.</p>
<p>The first issue I came across was the <em>event.target.log</em> suddenly losing its reference to itself and no longer being able to update the progress bar as the link to the current container is suddenly undefined, this also causes the load event to never get fired and the progress bar never gets removed.</p>
<p>The other issue is the <em>progress</em> event won&#8217;t fire if the file size is below around 140-150kb so no feedback will be given to the user. I&#8217;m not sure if this is intentional or a bug. I hope it&#8217;s the latter as feedback on any sized file would be necessary. You can see this happening on the toucan image in the screencast above.</p>
<p>I took a sceencast of the issues you can watch below.</p>
<p><object classid='clsid:d27cdb6e-ae6d-11cf-96b8-444553540000' codebase='http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,115,0' width='609' height='366'><param name='movie' value='http://screenr.com/Content/assets/screenr_0817090731.swf' /><param name='flashvars' value='i=8364' /><param name='allowFullScreen' value='true' /><embed src='http://screenr.com/Content/assets/screenr_0817090731.swf' flashvars='i=8364' allowFullScreen='true' width='609' height='366' pluginspage='http://www.macromedia.com/go/getflashplayer'></embed></object></p>
<h2 class="subtitle02">Heading in a great direction</h2>
<p>This functionality is exactly what the web needs going forward and will hopefully see it in other browsers very soon, Google Wave could benefit from this greatly and would make one of their coolest features work without any need for external plugins.</p>
<h2 class="subtitle02">Resources</h2>
<p>This article was inspired from a few examples I have seen throughout my searches for such functionality, what sparked my imagination and got me developing a drag and drop uploader was <a href="https://bug503598.bugzilla.mozilla.org/attachment.cgi?id=388413">this demo</a> found on the Mozilla bug list.</p>
<p>A few people have already figured out and <a href="http://webreflection.blogspot.com/2009/03/safari-4-multiple-upload-with-progress.html">demonstrated multiple file uploading</a> in safari 4 and a <a href="http://hacks.mozilla.org/2009/06/xhr-progress-and-richer-file-uploading-feedback/">similar version</a> that works in Firefox 3.5, although you can&#8217;t select multiple files you can upload more than one at a time. That example I used a slightly modified version in this demo to send binary data to the server.</p>
<p>There is also another great article on <a href="http://blog.igstan.ro/2009/01/pure-javascript-file-upload.html">uploading files and posting forms</a> using Ajax. This articles demo works in Firefox 3+.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.thecssninja.com/javascript/drag-and-drop-upload/feed</wfw:commentRss>
		<slash:comments>31</slash:comments>
		<feedburner:origLink>http://www.thecssninja.com/javascript/drag-and-drop-upload</feedburner:origLink></item>
		<item>
		<title>Create the accordion effect using CSS3</title>
		<link>http://feedproxy.google.com/~r/TheCSSNinja/~3/WvsbxJyHNT4/accordian-effect-using-css</link>
		<comments>http://www.thecssninja.com/css/accordian-effect-using-css#comments</comments>
		<pubDate>Wed, 05 Aug 2009 13:26:25 +0000</pubDate>
		<dc:creator>The Css Ninja</dc:creator>
				<category><![CDATA[css]]></category>
		<category><![CDATA[animation]]></category>
		<category><![CDATA[css3]]></category>
		<category><![CDATA[webkit]]></category>

		<guid isPermaLink="false">http://www.thecssninja.com/?p=407</guid>
		<description><![CDATA[Recently I have been playing around with CSS transitions and animations as implemented in webkit based browsers such as Safari and Chrome. They have been submitted to the W3C for consideration in the CSS3 spec so hopefully we should see more browsers support this soon, Firefox 3.5 supports CSS transforms which was developed by the [...]]]></description>
			<content:encoded><![CDATA[<p>Recently I have been playing around with <a href="http://webkit.org/blog/324/css-animation-2/">CSS transitions and animations</a> as implemented in webkit based browsers such as Safari and Chrome. They have been submitted to the W3C for consideration in the <a href="http://www.w3.org/TR/css3-transitions/">CSS3 spec</a> so hopefully we should see more browsers support this soon, Firefox 3.5 supports <a href="https://developer.mozilla.org/En/CSS/Using_CSS_transforms">CSS transforms</a> which was developed by the webkit people to work alongside CSS animations &#038; transitions.<span id="more-407"></span></p>
<p>To continue my effort to accomplish tasks in CSS that are usually reserved for JavaScript, such as my <a href="http://www.thecssninja.com/xhtml/futurebox">Futurebox</a> and <a href="http://www.thecssninja.com/css/iphone-orientation-css">CSS based iPhone orientation detection</a>. I have developed a CSS based version of the popular <a href="http://tutorialblog.org/10-javascript-accordion-scripts/">&#8220;accordion effect&#8221;</a> that utilises the webkit CSS transitions. Like the <a href="http://www.thecssninja.com/demo/futurebox/">Futurebox demo</a> I&#8217;m utilising the CSS3 <a href="http://www.w3.org/TR/css3-selectors/#target-pseudo"><em>:target</em></a> pseudo class to know which item to show based the URI fragment identifier (the # in the url).</p>
<div class="resources01"><a target="_blank" class="demo" title="Accordion effect using CSS3" href="http://www.thecssninja.com/demo/css_accordion/">View a live demo</a> <a target="_blank" class="demo source" title="Download the source of the CSS accordion demo" href="http://www.thecssninja.com/demo/css_accordion/css_accordion.zip">Download the source files</a></div>
<p>It should be noted that this works best in a webkit based browser such as Safari 3+, Chrome or iPhone. Other browsers that support the <em>:target</em> pseudo class will still function on the core level but won&#8217;t animate the showing and hiding. The following browsers have been tested and work with this demo.</p>
<ul>
<li>Firefox 1.5+</li>
<li>Opera 9.6+</li>
<li>Safari 3+</li>
<li>Chrome 1+</li>
<li>IE6+ &#8211; <a href="#ie-solution">IE solution</a></li>
</ul>
<h2 class="subtitle02">The xhtml</h2>

<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;">dl</span>&gt;</span>
	<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">dt</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;#Section1&quot;</span>&gt;</span>Section 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;">dt</span>&gt;</span>
	<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">dd</span> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;Section1&quot;</span>&gt;</span>
		<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">p</span>&gt;</span>
			Lorem ipsum dolor sit amet...
		<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;">dd</span>&gt;</span>
	<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">dt</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;#Section2&quot;</span>&gt;</span>Section 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;">dt</span>&gt;</span>
	<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">dd</span> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;Section2&quot;</span>&gt;</span>
		<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">p</span>&gt;</span>
			Lorem ipsum dolor sit amet...
	<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">dd</span>&gt;</span>
	...
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">dl</span>&gt;</span></pre></div></div>

<p>We setup the accordion using a <a href="http://www.w3.org/MarkUp/html3/deflists.html">definition list</a> to create the foundation so we can show and hide the definition data (dd) tag when the user clicks the anchor link inside the definition title (dt) tag.</p>
<h2 class="subtitle02">The CSS</h2>

<div class="wp_syntax"><div class="code"><pre class="css css" style="font-family:monospace;">dl
<span style="color: #00AA00;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">padding</span><span style="color: #00AA00;">:</span> <span style="color: #933;">10px</span>;
	<span style="color: #000000; font-weight: bold;">min-width</span><span style="color: #00AA00;">:</span> <span style="color: #933;">960px</span>;
<span style="color: #00AA00;">&#125;</span>
	dl dt
	<span style="color: #00AA00;">&#123;</span>
		-webkit-border-radius<span style="color: #00AA00;">:</span> <span style="color: #933;">5px</span>;
		-moz-border-radius<span style="color: #00AA00;">:</span> <span style="color: #933;">5px</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;">#cccccc</span>;
		<span style="color: #000000; font-weight: bold;">margin</span><span style="color: #00AA00;">:</span> 0;
	<span style="color: #00AA00;">&#125;</span>
		dl dt a
		<span style="color: #00AA00;">&#123;</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;">font-weight</span><span style="color: #00AA00;">:</span> <span style="color: #993333;">bold</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;">padding</span><span style="color: #00AA00;">:</span> <span style="color: #933;">10px</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>
	dl dd
	<span style="color: #00AA00;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">margin</span><span style="color: #00AA00;">:</span> 0;
		<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>;
		-webkit-transition<span style="color: #00AA00;">:</span> <span style="color: #000000; font-weight: bold;">height</span> 1s ease;
	<span style="color: #00AA00;">&#125;</span>
		dl dd p
		<span style="color: #00AA00;">&#123;</span>
			<span style="color: #000000; font-weight: bold;">padding</span><span style="color: #00AA00;">:</span> <span style="color: #933;">10px</span>;
			<span style="color: #000000; font-weight: bold;">margin</span><span style="color: #00AA00;">:</span> 0;
		<span style="color: #00AA00;">&#125;</span>
	dl dd<span style="color: #3333ff;"><span style="color: #00AA00;">:</span>target
	</span><span style="color: #00AA00;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">height</span><span style="color: #00AA00;">:</span> <span style="color: #993333;">auto</span>;
	<span style="color: #00AA00;">&#125;</span>
&nbsp;
<span style="color: #a1a100;">@media (-webkit-transition) {</span>
	dl dd<span style="color: #3333ff;"><span style="color: #00AA00;">:</span>target
	</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;">6.667em</span>;
	<span style="color: #00AA00;">&#125;</span>
<span style="color: #00AA00;">&#125;</span></pre></div></div>

<p>Pretty simple CSS involved, the <em>dd</em> tag is hidden by setting the <em>height</em> to 0 and the <em>overflow</em> to hidden.</p>

<div class="wp_syntax"><div class="code"><pre class="css css" style="font-family:monospace;">-webkit-transition<span style="color: #00AA00;">:</span> <span style="color: #000000; font-weight: bold;">height</span> 1s ease;</pre></div></div>

<p>This property on the <em>dd</em> tag lets webkit browsers know we wish to transition the height value over 1 second period using the <em>ease</em> timing function this transition will only happen when the <em>height</em> of the <em>dd</em> tag is changed. We can also express this in a long hand version.</p>

<div class="wp_syntax"><div class="code"><pre class="css css" style="font-family:monospace;">-webkit-transition-property<span style="color: #00AA00;">:</span> <span style="color: #000000; font-weight: bold;">height</span>;
-webkit-transition-duration<span style="color: #00AA00;">:</span> 1s;
-webkit-transition-timing-function<span style="color: #00AA00;">:</span> ease;</pre></div></div>

<p>To change the height we use the <em>:target</em> pseudo class to set the height of the dd tag to auto so the right content will show based the URI fragment identifier. For webkit browsers it&#8217;s a little different.</p>
<h2 class="subtitle02">Webkit media queries</h2>
<p>In webkit browsers there are additional <a href="http://webkit.org/specs/MediaQueriesExtensions.html">media queries available</a> so we can target browsers that support the extended features such as transitions and not affect other browsers. In this demo I use the <em>@media (transition)</em> media query.</p>
<p>Webkit implements this feature by using their -webkit vendor extension so the media query looks like the following</p>

<div class="wp_syntax"><div class="code"><pre class="css css" style="font-family:monospace;"><span style="color: #a1a100;">@media (-webkit-transition) {</span>
	dl dd<span style="color: #3333ff;"><span style="color: #00AA00;">:</span>target
	</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;">6.667em</span>;
	<span style="color: #00AA00;">&#125;</span>
<span style="color: #00AA00;">&#125;</span></pre></div></div>

<p>Unfortunately setting the height of the <em>dd</em> tag to <em>auto</em> will not make it animate although this would be ideal and much more capable of catering for different sized content it&#8217;s not possible at the moment. For now we have to set the height to an actual value, to keep the height in line with any text resizing I set the height using em based value so if the user has larger text the height will adjust and won&#8217;t cut of any content. The height is 80px we divide by the base font size, which is 12, and we get 6.667em.</p>
<h2 class="subtitle02" id="ie-solution">What about IE</h2>
<p>Unfortunately IE doesn&#8217;t support the <em>:target</em> pseudo class and won&#8217;t work as describe above, but that didn&#8217;t stop me! Take a look at working example that functions in IE6 and up.</p>
<div class="resources01"><a target="_blank" class="demo" title="Accordion effect using CSS that works in IE6+" href="http://www.thecssninja.com/demo/css_accordion/ie_solution.html">View a live demo</a> <a target="_blank" class="demo source" title="Download the source of the CSS IE accordion demo" href="http://www.thecssninja.com/demo/css_accordion/css_accordion_ie.zip">Download the source files</a></div>
<p>This is quite hacky and involves a bit of IE conditional comments.</p>
<h2 class="subtitle02">IE xhtml</h2>

<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;">dl</span>&gt;</span>
	<span style="color: #808080; font-style: italic;">&lt;!--[if IE]&gt;</span>
<span style="color: #808080; font-style: italic;">		&lt;a href=&quot;#Section1&quot; class=&quot;ie&quot;&gt;&lt;div&gt;</span>
<span style="color: #808080; font-style: italic;">	&lt;![endif]--&gt;</span>
	<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">dt</span>&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;">a</span> <span style="color: #000066;">href</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;#Section1&quot;</span>&gt;</span>
				<span style="color: #808080; font-style: italic;">&lt;!--&lt;![endif]--&gt;</span>Section 1<span style="color: #808080; font-style: italic;">&lt;!--[if !IE]&gt;--&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: #808080; font-style: italic;">&lt;!--&lt;![endif]--&gt;</span>
	<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">dt</span>&gt;</span>
	<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">dd</span> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;Section1&quot;</span>&gt;</span>
		<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">p</span>&gt;</span>
			Lorem ipsum dolor sit amet...
		<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;">dd</span>&gt;</span>
	<span style="color: #808080; font-style: italic;">&lt;!--[if IE]&gt;</span>
<span style="color: #808080; font-style: italic;">		&lt;/div&gt;&lt;/a&gt;</span>
<span style="color: #808080; font-style: italic;">	&lt;![endif]--&gt;</span>
	...
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">dl</span>&gt;</span></pre></div></div>

<p>As you can see there is <a href="http://msdn.microsoft.com/en-us/library/ms537512%28VS.85%29.aspx">conditional comments</a> so I can wrap the <em>dt</em> and <em>dd</em> tag in an anchor so we can get it functioning in IE using the following CSS. I also use the conditional comments to hide the anchor that appears in the <em>dt</em> tag only for IE browsers. IE6 was not functioning with just the anchor around the <em>dd</em> &#038; <em>dt</em> so I added a <em>div</em> inside the anchor. In IE6 the first anchor would surround all the items, the div fixes that. Demo, demo files and example code has been updated to reflect that.</p>
<h2 class="subtitle02">IE CSS</h2>

<div class="wp_syntax"><div class="code"><pre class="css css" style="font-family:monospace;">dl a<span style="color: #6666ff;">.ie</span> <span style="color: #00AA00;">&#123;</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: #00AA00;">&#125;</span>
	dl a<span style="color: #6666ff;">.ie</span> dd <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>
&nbsp;
<span style="color: #808080; font-style: italic;">/* Fix IE6 hover bug */</span>
dl a<span style="color: #6666ff;">.ie</span><span style="color: #3333ff;">:hover </span><span style="color: #00AA00;">&#123;</span> <span style="color: #000000; font-weight: bold;">background-color</span><span style="color: #00AA00;">:</span> <span style="color: #cc00cc;">#606061</span> !important; <span style="color: #00AA00;">&#125;</span>
&nbsp;
dl a<span style="color: #6666ff;">.ie</span> dt
<span style="color: #00AA00;">&#123;</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;">font-weight</span><span style="color: #00AA00;">:</span> <span style="color: #993333;">bold</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;">padding</span><span style="color: #00AA00;">:</span> <span style="color: #933;">10px</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>
&nbsp;
dl a<span style="color: #6666ff;">.ie</span><span style="color: #3333ff;">:hover </span>dd<span style="color: #00AA00;">,</span>
dl a<span style="color: #6666ff;">.ie</span><span style="color: #3333ff;">:active </span>dd<span style="color: #00AA00;">,</span>
dl a<span style="color: #6666ff;">.ie</span><span style="color: #3333ff;">:focus </span>dd
<span style="color: #00AA00;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">height</span><span style="color: #00AA00;">:</span> <span style="color: #993333;">auto</span>;
	<span style="color: #000000; font-weight: bold;">color</span><span style="color: #00AA00;">:</span> <span style="color: #cc00cc;">#cccccc</span> !important;
	<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>Pretty simple stuff, set the <em>text-decoration</em> so the content isn&#8217;t underlined. We need to hide the <em>dd</em> tag as it causes issues in IE7 and below when trying to hover over any items below the first section. Next a <em>background-color</em> is applied to the <em>:hover</em> pseudo class of the surrounding anchor to fix an issue in IE6 that won&#8217;t trigger a hover unless something like a background-color is applied it. To make it work in IE we utilise the <em>:hover</em>, <em>:focus</em> and <em>:active</em> pseudo classes. That way when the user hovers in IE the content gets revealed, we also simulate a &#8220;click&#8221; by using the <em>:active</em> pseudo class. The :focus pseudo class allows us to make it work by using keyboard navigation, tabbing to the anchor will reveal the content. All the mark-up is XHTML 1.0 Strict complaint.</p>
<p>I think this is a pretty good attempt and best of all it works in all major browser so it can be potentially be used in a production environment.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.thecssninja.com/css/accordian-effect-using-css/feed</wfw:commentRss>
		<slash:comments>29</slash:comments>
		<feedburner:origLink>http://www.thecssninja.com/css/accordian-effect-using-css</feedburner:origLink></item>
		<item>
		<title>Real text rotation with CSS</title>
		<link>http://feedproxy.google.com/~r/TheCSSNinja/~3/sfGOg7OiBbo/real-text-rotation-with-css</link>
		<comments>http://www.thecssninja.com/css/real-text-rotation-with-css#comments</comments>
		<pubDate>Wed, 29 Jul 2009 11:27:22 +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=388</guid>
		<description><![CDATA[Just saw a great post on Jonathan Snooks&#8217; blog about doing text rotation with CSS and how to accomplish it in IE using IE propriety filter basic image property to rotate a text block. But there is a better way using CSS3 writing-mode property that has been in IE since version 5.5.
writing-mode which is currently [...]]]></description>
			<content:encoded><![CDATA[<p>Just saw a great post on <a href="http://snook.ca/s/952">Jonathan Snooks&#8217; blog</a> about doing text rotation with CSS and how to accomplish it in IE using IE propriety <a href="http://msdn.microsoft.com/en-us/library/ms532918%28VS.85%29.aspx"><em>filter</em> basic image</a> property to rotate a text block. But there is a better way using CSS3 <em>writing-mode</em> property that has been in IE since version 5.5.<span id="more-388"></span></p>
<p><a href="http://www.w3.org/TR/2001/WD-css3-text-20010517/#PrimaryTextAdvanceDirection"><em>writing-mode</em></a> which is currently in the CSS3 draft specification allows us to accomplish text rotation without using propriety properties, effectively future proofing the concept as more browsers adopt the CSS3 draft spec.</p>
<div class="resources01"><a href="http://www.thecssninja.com/demo/css_text-rotation/" title="Simple demo of rotating text with CSS3" class="demo" target="_blank">View a live demo</a><a href="http://www.thecssninja.com/demo/css_text-rotation/css_text-rotation.zip" title="Download the source of the CSS text rotation demo" class="demo source" target="_blank">Download the source files</a></div>
<h2 class="subtitle02">The CSS</h2>

<div class="wp_syntax"><div class="code"><pre class="css css" style="font-family:monospace;">p <span style="color: #00AA00;">&#123;</span> writing-mode<span style="color: #00AA00;">:</span> tb-rl; <span style="color: #00AA00;">&#125;</span></pre></div></div>

<p>That&#8217;s it incredibly simple CSS technique that will eventually work with all browsers as their CSS3 support gets better. This is one of the handful of CSS3 supported properties in IE. The <em>tb-rl</em> value tells the browser to display paragraphs with the text flowing from top to bottom, right to left. Essentially rotating the text 90 degrees clockwise and aligning to the right.</p>
<p>This properties true intention is for displaying other languages in their correct &#8220;writing mode&#8221; such as Japanese right to left or Arabic &#038; Hebrew which display right to left &#038; top to bottom (<em>rl-tb</em>).</p>
<h2 class="subtitle02">Support</h2>
<p>At the moment IE is the only browser to support it starting from IE5.5 and up, IE8 adds further values through using the <em>-ms</em> extension. There are 4 values available from IE5.5+ and an additional 4 values for IE8+ through the -ms extension.</p>
<ul>
<li>lr-tb &#8211; This is the default value, left to right, top to bottom</li>
<li>rl-tb &#8211; This flows the text from right to left, top to bottom</li>
<li>tb-rl &#8211; Flows the text vertically from top to bottom, right to left</li>
<li>bt-rl &#8211; bottom to top, right to left</li>
<li>tb-lr &#8211; This and the followings value are only available in IE8+ using <em>-ms-writing-mode</em>. Text flows top to bottom, left to right</li>
<li>bt-lr &#8211; Bottom to top, left to right</li>
<li>lr-bt &#8211; Left to right, bottom to top</li>
<li>rl-bt &#8211; Right to left, bottom to top</li>
</ul>
<h2 class="subtitle02">Rotate text in other browsers?</h2>
<p>As stated in Snooks&#8217; article we can do this in Webkit based browsers and Firefox 3.5+ using the <em>transform</em> property.</p>

<div class="wp_syntax"><div class="code"><pre class="css css" style="font-family:monospace;">-webkit-transform<span style="color: #00AA00;">:</span> rotate<span style="color: #00AA00;">&#40;</span>90deg<span style="color: #00AA00;">&#41;</span>; 
-moz-transform<span style="color: #00AA00;">:</span> rotate<span style="color: #00AA00;">&#40;</span>90deg<span style="color: #00AA00;">&#41;</span>;</pre></div></div>

<p>For both we need to use the vendor extensions <em>-moz</em> &#038; <em>-webkit</em>. As for Opera it neither yet supports <em>transforms</em> or <em>writing-mode</em> properties but I&#8217;m sure that will change with the upcoming release of version 10.</p>
<p>In the article example we need to position the element differently for browsers that support the <em>transform</em> property compared to ones that support <em>writing-mode</em> as the elements rotated still exist as horizontal elements where as with <em>writing-mode</em> the element is truly rotated, see screenshot below.</p>
<p><img src="http://www.thecssninja.com/wp-content/uploads/2009/07/gr_css-compare.png" alt="CSS writing-mode on the left and CSS transform on the right" title="CSS writing-mode on the left and CSS transform on the right" width="608" height="156" class="article-img main-img" /><br />
<small class="caption01">Element that is rotated is outlined and highlighted, writing-mode on left transform on right</small></p>

<div class="wp_syntax"><div class="code"><pre class="css css" style="font-family:monospace;"><span style="color: #6666ff;">.datebox</span> span<span style="color: #3333ff;">:nth-</span>child<span style="color: #00AA00;">&#40;</span><span style="color: #cc66cc;">3</span><span style="color: #00AA00;">&#41;</span>
<span style="color: #00AA00;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">right</span><span style="color: #00AA00;">:</span> <span style="color: #933;">0px</span>;
	<span style="color: #000000; font-weight: bold;">top</span><span style="color: #00AA00;">:</span> <span style="color: #933;">0px</span>;
<span style="color: #00AA00;">&#125;</span></pre></div></div>

<p>To target the browsers that potentially support the <em>transform</em> property we use the CSS3 <em>nth-child()</em> psuedo class.</p>
<p>A pretty cool alternative to an already great idea by <a href="http://snook.ca/">snook</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.thecssninja.com/css/real-text-rotation-with-css/feed</wfw:commentRss>
		<slash:comments>7</slash:comments>
		<feedburner:origLink>http://www.thecssninja.com/css/real-text-rotation-with-css</feedburner:origLink></item>
	</channel>
</rss><!-- Dynamic page generated in 0.630 seconds. --><!-- Cached page generated by WP-Super-Cache on 2010-02-27 11:50:04 -->
