<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Lea Verou &#187; Tips</title>
	<atom:link href="http://lea.verou.me/category/tips/feed/" rel="self" type="application/rss+xml" />
	<link>http://lea.verou.me</link>
	<description>Life at the bleeding edge (of web standards)</description>
	<lastBuildDate>Thu, 02 Feb 2012 14:08:08 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Easily keep gh-pages in sync with master</title>
		<link>http://lea.verou.me/2011/10/easily-keep-gh-pages-in-sync-with-master/</link>
		<comments>http://lea.verou.me/2011/10/easily-keep-gh-pages-in-sync-with-master/#comments</comments>
		<pubDate>Thu, 13 Oct 2011 14:50:55 +0000</pubDate>
		<dc:creator>Lea Verou</dc:creator>
				<category><![CDATA[Tips]]></category>
		<category><![CDATA[github]]></category>

		<guid isPermaLink="false">http://leaverou.me/?p=1368</guid>
		<description><![CDATA[I always loved Github&#8217;s ability to publish pages for a project and get the strain out of your server. However, every time I tried it, I struggled to keep the gh-pages branch up to date. Until I discovered the awesome git rebase. Usually my github workflow is like this: git add . git status // [...]]]></description>
			<content:encoded><![CDATA[
<div class="topsy_widget_data topsy_theme_hot-pink" style="float: right;margin-left: 0.75em; background: url(data:,%7B%20%22url%22%3A%20%22http%253A%252F%252Flea.verou.me%252F2011%252F10%252Feasily-keep-gh-pages-in-sync-with-master%252F%22%2C%20%22shorturl%22%3A%20%22http%3A%2F%2Fbit.ly%2Fpn9NUZ%22%2C%20%22style%22%3A%20%22big%22%2C%20%22title%22%3A%20%22Easily%20keep%20gh-pages%20in%20sync%20with%20master%22%20%7D);"></div>
<p>I always loved Github&#8217;s ability to publish pages for a project and get the strain out of your server. However, every time I tried it, I struggled to keep the gh-pages branch up to date. Until I discovered the awesome <code>git rebase</code>.</p>
<p>Usually my github workflow is like this:</p>
<pre>git add .
git status // to see what changes are going to be commited
git commit -m 'Some descriptive commit message'
git push origin master</pre>
<p>Now, when I use gh-pages, there are only a few more commands that I have to use after the above:</p>
<pre>git checkout gh-pages // go to the gh-pages branch
git rebase master // bring gh-pages up to date with master
git push origin gh-pages // commit the changes
git checkout master // return to the master branch</pre>
<p>I know this is old news to some of you (I&#8217;m a github n00b, struggling with basic stuff, so my advice is probably for other n00bs), but if I had read this a few months ago, it would&#8217;ve saved me big hassles, so I&#8217;m writing it for the others out there that are like me a few months ago. </p>
<p>Now if only I find an easy way to automate this&#8230; <img src='http://lea.verou.me/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>

]]></content:encoded>
			<wfw:commentRss>http://lea.verou.me/2011/10/easily-keep-gh-pages-in-sync-with-master/feed/</wfw:commentRss>
		<slash:comments>19</slash:comments>
		</item>
		<item>
		<title>To write good code, you sometimes have to write bad code</title>
		<link>http://lea.verou.me/2011/08/to-write-good-code-you-sometimes-have-to-write-bad-code/</link>
		<comments>http://lea.verou.me/2011/08/to-write-good-code-you-sometimes-have-to-write-bad-code/#comments</comments>
		<pubDate>Sat, 20 Aug 2011 12:51:45 +0000</pubDate>
		<dc:creator>Lea Verou</dc:creator>
				<category><![CDATA[Tips]]></category>
		<category><![CDATA[programming]]></category>

		<guid isPermaLink="false">http://leaverou.me/?p=1242</guid>
		<description><![CDATA[And I’m not referring to learning. For example, yesterday I was trying to write code for something and it ended up beng harder than I expected. It’s one of those rare cases where you can fully imagine how the solution should work, enough to tell it to another person, but you can’t put your thoughts [...]]]></description>
			<content:encoded><![CDATA[
<div class="topsy_widget_data topsy_theme_hot-pink" style="float: right;margin-left: 0.75em; background: url(data:,%7B%20%22url%22%3A%20%22http%253A%252F%252Flea.verou.me%252F2011%252F08%252Fto-write-good-code-you-sometimes-have-to-write-bad-code%252F%22%2C%20%22shorturl%22%3A%20%22http%3A%2F%2Fbit.ly%2FqjnxF9%22%2C%20%22style%22%3A%20%22big%22%2C%20%22title%22%3A%20%22To%20write%20good%20code%2C%20you%20sometimes%20have%20to%20write%20bad%20code%22%20%7D);"></div>
<p>And I’m not referring to learning.</p>
<p>For example, yesterday I was trying to write code for something and it ended up beng harder than I expected. It’s one of those rare cases where you can fully imagine how the solution should work, enough to tell it to another person, but you can’t put your thoughts to code and you feel you’re not smart enough.</p>
<p>I find that in those cases, it helps a lot to open a new editor window and try to write code that just works. Without being elegant, fast or maintainable. Just something that works properly. And after you manage to put your thoughts into (bad) code, it’s easy to refine it from there and end up with good code.</p>
<p>Just don’t stop at the bad code, like many beginners do. It’s like when designers sketch a rough draft for a logo, before drawing the digital version. Could you imagine how horrible it would be if they wanted to stop there and give the rough sketches to the client instead? <img src='http://lea.verou.me/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>

]]></content:encoded>
			<wfw:commentRss>http://lea.verou.me/2011/08/to-write-good-code-you-sometimes-have-to-write-bad-code/feed/</wfw:commentRss>
		<slash:comments>36</slash:comments>
		</item>
		<item>
		<title>Get your hash — the bulletproof way</title>
		<link>http://lea.verou.me/2011/05/get-your-hash-the-bulletproof-way/</link>
		<comments>http://lea.verou.me/2011/05/get-your-hash-the-bulletproof-way/#comments</comments>
		<pubDate>Mon, 23 May 2011 00:54:03 +0000</pubDate>
		<dc:creator>Lea Verou</dc:creator>
				<category><![CDATA[Tips]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[URLs]]></category>

		<guid isPermaLink="false">http://leaverou.me/?p=1065</guid>
		<description><![CDATA[This is probably one of the things that everyone thinks they know how to do but many end up doing it wrong. After coming accross yet one more super fragile snippet of code for this, I decided a blog post was in order. The problem You want to remove the pound sign (#) from location.hash. [...]]]></description>
			<content:encoded><![CDATA[
<div class="topsy_widget_data topsy_theme_hot-pink" style="float: right;margin-left: 0.75em; background: url(data:,%7B%20%22url%22%3A%20%22http%253A%252F%252Flea.verou.me%252F2011%252F05%252Fget-your-hash-the-bulletproof-way%252F%22%2C%20%22shorturl%22%3A%20%22http%3A%2F%2Fbit.ly%2FihtNtW%22%2C%20%22style%22%3A%20%22big%22%2C%20%22title%22%3A%20%22Get%20your%20hash%20%E2%80%94%20the%20bulletproof%20way%22%20%7D);"></div>
<p>This is probably one of the things that everyone thinks they know how to do but many end up doing it wrong. After coming accross yet one more super fragile snippet of code for this, I decided a blog post was in order.</p>
<h2>The problem</h2>
<p>You want to remove the pound sign (#) from <code>location.hash</code>. For example, when the hash is <code>"#foo"</code>, you want to get a string containing <code>"foo"</code>. That&#8217;s really simple, right?<span id="more-1065"></span></p>
<h2>Tricky cases</h2>
<p>What most developers seem to miss is that in modern, JavaScript-heavy applications, a hash can contain any unicode character. It doesn&#8217;t necessarily have to correspond to the value of an actual id attribute in the page. And even when it does, <a href="http://mathiasbynens.be/notes/html5-id-class" target="_blank">ID attributes can now contain almost any unicode character</a>. Another thing sometimes forgotten is that there might be no hash in the page. Even in a URL that ends in #, <code>location.hash</code> is actually equal to <code>""</code> (the empty string) and not <code>"#"</code>.</p>
<h2>Naive approaches</h2>
<p>This one is the most recent, found in a book I was tech reviewing:</p>
<pre>var hash = location.hash.match(/#(\w+)/)[1];</pre>
<p>which has quite a few issues:</p>
<ul>
<li>Returns wrong results when there is any non-latin or non-alphanumeric character in the hash. For example, for the hash <code>#foo@o#bar$%huh hello</code>, just <code>"foo"</code> would be returned.</li>
<li>Throws a <code>TypeError</code> when <code>location.hash</code> is empty, since <code>.match()</code> will return <code>null</code>.</li>
</ul>
<p>Other variations of this pattern I&#8217;ve seen include using explicitly defined character classes instead of <code>\w</code>, adding an anchor (<code>^</code>) before the pound sign (which is an excellent idea for performance) and checking if <code>.match()</code> actually returned something before using its result. However, they usually also fall into at least one of the 2 aforementioned issues.</p>
<p>Another approach a friend of mine once used was this:</p>
<pre>var hash = location.hash.split('#')[1];</pre>
<p>This also has its issues, which are ironically less than the first one, even though it seems a far more naive approach.</p>
<ul>
<li>With the same test hash, it would at least get the <code>"foo@o"</code> part, which means it only fails when the hash contains a pound sign</li>
<li>When there&#8217;s no hash, it doesn&#8217;t throw an error, although it returns <code>undefined</code> instead of the empty string.</li>
</ul>
<h2>Getting it right</h2>
<p>The approach I usually use is far simpler than both of the above and probably looks too loose:</p>
<pre>var hash = location.hash.substring(1);</pre>
<p>However, let&#8217;s examine it a bit:</p>
<ul>
<li>With our weird test hash, it actually returns the correct result: &#8221;foo@o#bar$%huh hello&#8221;</li>
<li>When no hash exists, it correctly returns the empty string</li>
</ul>
<p>&#8220;But it assumes there&#8217;s a pound sign at the start of the string!&#8221; I almost hear some of you cry. Well, that could be a real concern, if we were dealing with an arbitrary string. In that case, we would have to check if there&#8217;s actually a pound sign first or if the string even exists. However, with <code>location.hash</code> the only case when that is not true, is when there is no hash. And we got that case covered. <img src='http://lea.verou.me/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> </p>
<p><strong>Edit:</strong> As <a href="http://lea.verou.me/2011/05/get-your-hash-the-bulletproof-way/#comment-209660879">pointed out</a> in the comments, you may also use <code>location.hash.slice(1)</code> instead of <code>substring</code>. I kinda prefer it, since it&#8217;s 4 bytes shorter.</p>
<p>If however you&#8217;re obsessed with RegExps and want to do it with them no matter what, this is just as bulletproof and almost as short:</p>
<pre>var hash = location.hash.replace(/^#/, '');</pre>
<p>If for some reason (OCD?) you want to do it with <code>.match()</code> no matter what, you could do this:</p>
<pre>var match = location.hash.match(/^#?(.*)$/)[1];</pre>
<p>In that case, since the pound sign is optional, since <code>.match()</code> never returns <code>null</code>. And no, the pound sign never erroneously becomes part of the returned hash, because of the way regex engines work.</p>
<h2>&#8220;This is too basic, what a waste of my time!&#8221;</h2>
<p>Sorry for that. I know that for some of you, this is elementary. But the guy who wrote that book is very knowledgable (the book is really good, apart from that code snippet) so I thought this means there are many good developers out there who get this wrong, so this post was needed to be written. If you&#8217;re not one of them, you can take it as a compliment.</p>
<h2>&#8220;Hey, you missed something too!&#8221;</h2>
<p>In that case, I&#8217;d love to find out what it is, so please leave a comment! <img src='http://lea.verou.me/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>

]]></content:encoded>
			<wfw:commentRss>http://lea.verou.me/2011/05/get-your-hash-the-bulletproof-way/feed/</wfw:commentRss>
		<slash:comments>25</slash:comments>
		</item>
		<item>
		<title>Change URL hash without page jump</title>
		<link>http://lea.verou.me/2011/05/change-url-hash-without-page-jump/</link>
		<comments>http://lea.verou.me/2011/05/change-url-hash-without-page-jump/#comments</comments>
		<pubDate>Fri, 13 May 2011 21:40:05 +0000</pubDate>
		<dc:creator>Lea Verou</dc:creator>
				<category><![CDATA[Original]]></category>
		<category><![CDATA[Tips]]></category>
		<category><![CDATA[History API]]></category>
		<category><![CDATA[HTML5]]></category>
		<category><![CDATA[JavaScript]]></category>

		<guid isPermaLink="false">http://leaverou.me/?p=1053</guid>
		<description><![CDATA[In modern complex layouts, sometimes the point where a hash will transport you to will be entirely different than the one you actually wanted. If you prevent the default event, you will save yourself from the page jump, but the hash won&#8217;t change either. You can accept the regular behavior and change scrollTop after the [...]]]></description>
			<content:encoded><![CDATA[
<div class="topsy_widget_data topsy_theme_hot-pink" style="float: right;margin-left: 0.75em; background: url(data:,%7B%20%22url%22%3A%20%22http%253A%252F%252Flea.verou.me%252F2011%252F05%252Fchange-url-hash-without-page-jump%252F%22%2C%20%22shorturl%22%3A%20%22http%3A%2F%2Fbit.ly%2FiRDLcc%22%2C%20%22style%22%3A%20%22big%22%2C%20%22title%22%3A%20%22Change%20URL%20hash%20without%20page%20jump%22%20%7D);"></div>
<p>In modern complex layouts, sometimes the point where a hash will transport you to will be entirely different than the one you actually wanted. If you prevent the default event, you will save yourself from the page jump, but the hash won&#8217;t change either. You can accept the regular behavior and change scrollTop after the jump, but the user will still see a distracting flicker.<br />
<a href="http://css-tricks.com/" target="_blank">Chris Coyier</a> found <a href="http://css-tricks.com/hash-tag-links-padding/" target="_blank">a great workaround</a> last year but it&#8217;s not meant for every case.</p>
<h2>A different solution</h2>
<p>Turns out we can take advantage of the <a href="http://www.w3.org/TR/html5/history.html#the-history-interface" target="_blank">History API</a> to do that quite easily. It&#8217;s just one line of code:</p>
<pre>history.pushState(null, null, '#myhash');</pre>
<p>and we can combine it with the old method of setting location.hash to cater for older browsers as well:</p>
<pre>if(history.pushState) {
    history.pushState(null, null, '#myhash');
}
else {
    location.hash = '#myhash';
}
</pre>
<h2>Browser support?</h2>
<p>The History API is supported by:</p>
<ul>
<li>Firefox 4+</li>
<li>Safari 5+</li>
<li>Chrome 8+</li>
<li>Coming soon in Opera</li>
</ul>
<p>Enjoy <img src='http://lea.verou.me/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>

]]></content:encoded>
			<wfw:commentRss>http://lea.verou.me/2011/05/change-url-hash-without-page-jump/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>Rule filtering based on specific selector(s) support</title>
		<link>http://lea.verou.me/2011/05/rule-filtering-based-on-specific-selectors-support/</link>
		<comments>http://lea.verou.me/2011/05/rule-filtering-based-on-specific-selectors-support/#comments</comments>
		<pubDate>Mon, 02 May 2011 12:34:43 +0000</pubDate>
		<dc:creator>Lea Verou</dc:creator>
				<category><![CDATA[Tips]]></category>
		<category><![CDATA[CSS3]]></category>
		<category><![CDATA[CSS3 selectors]]></category>

		<guid isPermaLink="false">http://leaverou.me/?p=966</guid>
		<description><![CDATA[I&#8217;ve been using this trick for quite a while, but I never thought to blog about it. However, I recently realized that it might not be as common as I thought, so it might be a good idea to document it in a blog post. If you follow the discussions on www-style, you might have [...]]]></description>
			<content:encoded><![CDATA[
<div class="topsy_widget_data topsy_theme_hot-pink" style="float: right;margin-left: 0.75em; background: url(data:,%7B%20%22url%22%3A%20%22http%253A%252F%252Flea.verou.me%252F2011%252F05%252Frule-filtering-based-on-specific-selectors-support%252F%22%2C%20%22shorturl%22%3A%20%22http%3A%2F%2Fbit.ly%2FlmdArr%22%2C%20%22style%22%3A%20%22big%22%2C%20%22title%22%3A%20%22Rule%20filtering%20based%20on%20specific%20selector%28s%29%20support%22%20%7D);"></div>
<p>I&#8217;ve been using this trick for quite a while, but I never thought to blog about it. However, I recently realized that it might not be as common as I thought, so it might be a good idea to document it in a blog post.</p>
<p>If you follow the discussions on www-style, you might have noticed the <a href="http://lists.w3.org/Archives/Public/www-style/2011Apr/0428.html">proposal for a @supports rule</a> to query property and value support. Some people suggested that it should also test for selectors, for example whether a certain pseudo-class is supported. However, you can do that today, albeit in a limited manner (no OR and NOT support).</p>
<p>The main principle that you need to keep in mind is that <strong>browsers are expected to drop rules with selectors they don&#8217;t understand, even partially</strong>. So, if only one selector in a group cannot be parsed, the whole rule will be dropped. This means we can construct selector &#8220;tests&#8221;, which are use cases of the selector whose support we want to test, that will not match anything, even if the selector is supported. Then, we include that selector in the beginning of our selector group. If all this is unclear, don&#8217;t worry, as there&#8217;s an example coming next <img src='http://lea.verou.me/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> <span id="more-966"></span></p>
<h2>Example</h2>
<p>Suppose you want to apply the following CSS (for rudimentary custom checkboxes):</p>
<pre>input[type="checkbox"] {
    position:absolute;
    clip: rect(0,0,0,0);
    clip: rect(0 0 0 0);
}

input[type="checkbox"] + label::before {
    content: url('checkbox.png');
}

input[type="checkbox"]:checked + label::before {
    content: url('checkbox-checked.png');
}</pre>
<p>only in browsers that support the attribute equality selector, the <code>:checked</code> pseudo-class and the <code>::before</code> pseudo-element. We need to try to think of a selector that includes all of them but matches nothing. One such selector would be <code>#foo[type="checkbox"]:checked::before</code>. Even in supporting browsers, this matches nothing as there&#8217;s no element with id=&#8221;foo&#8221;. We can reduce the test for every rule to conserve bandwidth: For example, we don&#8217;t need to include tests for the attribute selector in any of them, since they are present anyway in all three rules. Also, we may eliminate <code>::before</code> from the second test and we don&#8217;t need any test for the 3rd one, since it includes all features we want to test for. To sum up:</p>
<pre><strong>#foo:checked::before,</strong>
input[type="checkbox"] {
    position:absolute;
    clip: rect(0,0,0,0);
    clip: rect(0 0 0 0);
}

<strong>#foo:checked,</strong>
input[type="checkbox"] + label::before {
    content: url('checkbox.png');
}

input[type="checkbox"]:checked + label::before {
    content: url('checkbox-checked.png');
}</pre>
<p>An important caveat of this technique is that <strong>Internet Explorer up to version 7</strong> will split selectors before parsing them, so it will completely ignore our filters <img src='http://lea.verou.me/wp-includes/images/smilies/icon_sad.gif' alt=':(' class='wp-smiley' />  (Thanks to <a href="http://www.thecssninja.com/">Ryan Seddon</a> for finding that out).</p>
<p>Disclaimer: The original idea about custom checkboxes belongs to <a href="http://www.thecssninja.com/css/custom-inputs-using-css">Ryan Seddon</a>, although his code was quite different.</p>

]]></content:encoded>
			<wfw:commentRss>http://lea.verou.me/2011/05/rule-filtering-based-on-specific-selectors-support/feed/</wfw:commentRss>
		<slash:comments>12</slash:comments>
		</item>
		<item>
		<title>Invert a whole webpage with CSS only</title>
		<link>http://lea.verou.me/2011/04/invert-a-whole-webpage-with-css-only/</link>
		<comments>http://lea.verou.me/2011/04/invert-a-whole-webpage-with-css-only/#comments</comments>
		<pubDate>Thu, 14 Apr 2011 20:28:51 +0000</pubDate>
		<dc:creator>Lea Verou</dc:creator>
				<category><![CDATA[Original]]></category>
		<category><![CDATA[Tips]]></category>
		<category><![CDATA[CSS]]></category>

		<guid isPermaLink="false">http://leaverou.me/?p=920</guid>
		<description><![CDATA[I recently saw Paul Irish&#8217;s jQuery invert page plugin. It inverts every color on a webpage including images or CSS. This reminded me of the invert color keyword that&#8217;s allowed on outlines (and sadly only supported by Opera and IE9+). So I wondered how it could be exploited to achieve the same effect through CSS [...]]]></description>
			<content:encoded><![CDATA[
<div class="topsy_widget_data topsy_theme_hot-pink" style="float: right;margin-left: 0.75em; background: url(data:,%7B%20%22url%22%3A%20%22http%253A%252F%252Flea.verou.me%252F2011%252F04%252Finvert-a-whole-webpage-with-css-only%252F%22%2C%20%22shorturl%22%3A%20%22http%3A%2F%2Fbit.ly%2Fh9xH7V%22%2C%20%22style%22%3A%20%22big%22%2C%20%22title%22%3A%20%22Invert%20a%20whole%20webpage%20with%20CSS%20only%22%20%7D);"></div>
<p><a href="http://lea.verou.me/wp-content/uploads/2011/04/Screen-shot-2011-04-14-at-22.24.18-.png"><img class="alignleft size-medium wp-image-922" title="Screenshot from the effect applied on my blog" src="http://lea.verou.me/wp-content/uploads/2011/04/Screen-shot-2011-04-14-at-22.24.18--300x199.png" alt="" width="300" height="199" /></a>I recently saw <a href="https://gist.github.com/373253" target="_blank">Paul Irish&#8217;s jQuery invert page plugin</a>. It inverts every color on a webpage including images or CSS. This reminded me of the invert color keyword that&#8217;s allowed on outlines (and sadly only supported by Opera and IE9+). So I wondered how it could be exploited to achieve the same effect through CSS alone. Turned out to be quite simple actually:</p>
<pre>body:before {
	content:"";
	position:fixed;
	top:50%; left: 50%;
	z-index:9999;
	width:1px; height: 1px;
	outline:2999px solid invert;
}</pre>
<p>Not even <code>pointer-events:none;</code> is needed, since outlines don&#8217;t receive pointer events anyway, and there&#8217;s no issue with scrollbars since they don&#8217;t contribute to scrolling. So this is not even CSS3, it&#8217;s just plain ol&#8217; CSS 2.1.</p>
<p>And here&#8217;s a bookmarklet to inject it into any given page: <a href="javascript:(function(){var%20style=document.createElement('style');style.innerHTML='body:before%20{%20content:%22%22;%20position:fixed;%20top:50%25;%20left:50%25;%20z-index:9999;%20width:1px;%20height:%201px;%20outline:2999px%20solid%20invert;%20}';document.body.appendChild(style)})();" class="view-demo">Invert page</a></p>
<p><strong>Note:</strong>This will only work on Opera and IE9+ since they&#8217;re currently the only ones supporting the color keyword &#8216;invert&#8217; on outlines. However, it&#8217;s probably possible to add Firefox support too with SVG filters, since they support them on HTML elements as well.</p>
<p>As for why would someone want to invert a page&#8230; I guess it could be useful for people that can read white text on dark backgrounds more easily, April fools jokes, konami code fun and stuff like that.</p>
<p><strong>Update:</strong> Mozilla is planning to <strong>never</strong> support <code>invert</code> because there&#8217;s a <a href="http://www.w3.org/TR/CSS21/ui.html#propdef-outline-color">loophole in the CSS 2.1 spec</a> that allows them to do that. However, you can push them to support it by <a href="https://bugzilla.mozilla.org/votes.cgi?action=show_user&#038;bug_id=359497#vote_359497">voting</a> on the <a href="https://bugzilla.mozilla.org/show_bug.cgi?id=359497">relevant issue</a>.</p>

]]></content:encoded>
			<wfw:commentRss>http://lea.verou.me/2011/04/invert-a-whole-webpage-with-css-only/feed/</wfw:commentRss>
		<slash:comments>22</slash:comments>
		</item>
		<item>
		<title>Create complex RegExps more easily</title>
		<link>http://lea.verou.me/2011/03/create-complex-regexps-more-easily/</link>
		<comments>http://lea.verou.me/2011/03/create-complex-regexps-more-easily/#comments</comments>
		<pubDate>Mon, 28 Mar 2011 16:05:45 +0000</pubDate>
		<dc:creator>Lea Verou</dc:creator>
				<category><![CDATA[Original]]></category>
		<category><![CDATA[Tips]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[RegExp]]></category>
		<category><![CDATA[regular expressions]]></category>

		<guid isPermaLink="false">http://leaverou.me/?p=916</guid>
		<description><![CDATA[When I was writing my linear-gradient() to -webkit-gradient() converter, I knew in advance that I would have to use a quite large regular expression to validate and parse the input. Such a regex would be incredibly hard to read and fix potential issues, so I tried to find a way to cut the process down [...]]]></description>
			<content:encoded><![CDATA[
<div class="topsy_widget_data topsy_theme_hot-pink" style="float: right;margin-left: 0.75em; background: url(data:,%7B%20%22url%22%3A%20%22http%253A%252F%252Flea.verou.me%252F2011%252F03%252Fcreate-complex-regexps-more-easily%252F%22%2C%20%22shorturl%22%3A%20%22http%3A%2F%2Fbit.ly%2FfEXA40%22%2C%20%22style%22%3A%20%22big%22%2C%20%22title%22%3A%20%22Create%20complex%20RegExps%20more%20easily%22%20%7D);"></div>
<p>When I was writing <a href="http://lea.verou.me/2011/03/convert-standard-gradient-syntax-to-webkit-gradient-and-others/">my linear-gradient() to -webkit-gradient() converter</a>, I knew in advance that I would have to use a quite large regular expression to validate and parse the input. Such a regex would be incredibly hard to read and fix potential issues, so I tried to find a way to cut the process down in reusable parts.</p>
<p>Turns out JavaScript regular expression objects have a .source property that can be used in the RegExp constructor to create a new RegExp out of another one. So I wrote a new function that takes a string with identifiers for regexp replacements in {{ and }} and replaces them with the corresponding sub-regexps, taken from an object literal as a second argument:</p>
<pre>/**
 * Create complex regexps in an easy to read way
 * @param str {String} Final regex with {{id}} for replacements
 * @param replacements {Object} Object with the replacements
 * @param flags {String} Just like the flags argument in the RegExp constructor
 */
RegExp.create = function(str, replacements, flags) {
	for(var id in replacements) {
		var replacement = replacements[id],
			idRegExp = RegExp('{{' + id + '}}', 'gi');

		if(replacement.source) {
			replacement = replacement.source.replace(/^\^|\$$/g, '');
		}

		// Don't add extra parentheses if they already exist
		str = str.replace(RegExp('\\(' + idRegExp.source + '\\)', 'gi'), '(' + replacement + ')');

		str = str.replace(idRegExp, '(?:' + replacement + ')');
	}

	return RegExp(str, flags);
};</pre>
<p>If you don&#8217;t like adding a function to the RegExp object, you can name it however you want. Here&#8217;s how I used it for my linear-gradient() parser:</p>
<pre>self.regex = {};

self.regex.number = /^-?[0-9]*\.?[0-9]+$/;
self.regex.keyword = /^(?:top\s+|bottom\s+)?(?:right|left)|(?:right\s+|left\s+)?(?:top|bottom)$/;

self.regex.direction = RegExp.create('^(?:{{keyword}}|{{number}}deg|0)$', {
	keyword: self.regex.keyword,
	number: self.regex.number
});

self.regex.color = RegExp.create('(?:{{keyword}}|{{func}}|{{hex}})', {
	keyword: /^(?:red|tan|grey|gray|lime|navy|blue|teal|aqua|cyan|gold|peru|pink|plum|snow|[a-z]{5,20})$/,
	func: RegExp.create('^(?:rgb|hsl)a?\\((?:\\s*{{number}}%?\\s*,?\\s*){3,4}\\)$', {
		number: self.regex.number
	}),
	hex: /^#(?:[0-9a-f]{1,2}){3}$/
});

self.regex.percentage = RegExp.create('^(?:{{number}}%|0)$', {
	number: self.regex.number
});

self.regex.length = RegExp.create('{{number}}{{unit}}|0', {
	number: self.regex.number,
	unit: /%|px|mm|cm|in|em|rem|en|ex|ch|vm|vw|vh/
});

self.regex.colorStop = RegExp.create('{{color}}\\s*{{length}}?', {
	color: self.regex.color,
	length: self.regex.length
}, 'g');

self.regex.linearGradient = RegExp.create('^linear-gradient\\(\\s*(?:({{direction}})\\s*,)?\\s*({{colorstop}}\\s*(?:,\\s*{{colorstop}}\\s*)+)\\)$', {
	direction: self.regex.direction,
	colorStop: self.regex.colorStop
}, 'i');</pre>
<p>(self in this case was a local variable, not the window object)</p>

]]></content:encoded>
			<wfw:commentRss>http://lea.verou.me/2011/03/create-complex-regexps-more-easily/feed/</wfw:commentRss>
		<slash:comments>16</slash:comments>
		</item>
		<item>
		<title>Beveled corners &amp; negative border-radius with CSS3 gradients</title>
		<link>http://lea.verou.me/2011/03/beveled-corners-negative-border-radius-with-css3-gradients/</link>
		<comments>http://lea.verou.me/2011/03/beveled-corners-negative-border-radius-with-css3-gradients/#comments</comments>
		<pubDate>Mon, 14 Mar 2011 15:53:56 +0000</pubDate>
		<dc:creator>Lea Verou</dc:creator>
				<category><![CDATA[Original]]></category>
		<category><![CDATA[Tips]]></category>
		<category><![CDATA[css3 gradients]]></category>

		<guid isPermaLink="false">http://leaverou.me/?p=892</guid>
		<description><![CDATA[Just found out how to do beveled corners and simulate negative border radius without images, by utilizing CSS3 gradients once again. It&#8217;s amazing how many CSS problems can be solved with gradients alone. Read the text in the fiddle to find out how (or just check the code): It also falls back to a solid [...]]]></description>
			<content:encoded><![CDATA[
<div class="topsy_widget_data topsy_theme_hot-pink" style="float: right;margin-left: 0.75em; background: url(data:,%7B%20%22url%22%3A%20%22http%253A%252F%252Flea.verou.me%252F2011%252F03%252Fbeveled-corners-negative-border-radius-with-css3-gradients%252F%22%2C%20%22shorturl%22%3A%20%22http%3A%2F%2Fbit.ly%2FhYU0F8%22%2C%20%22style%22%3A%20%22big%22%2C%20%22title%22%3A%20%22Beveled%20corners%20%26%20negative%20border-radius%20with%20CSS3%20gradients%22%20%7D);"></div>
<p>Just found out how to do beveled corners and simulate negative border radius without images, by utilizing CSS3 gradients once again. It&#8217;s amazing how many CSS problems can be solved with gradients alone. Read the text in the fiddle to find out how (or just check the code):</p>
<p><iframe style="width: 100%; height: 525px" src="http://jsfiddle.net/leaverou/EjE7c/embedded/result,css,html"></iframe></p>
<p>It also falls back to a solid color background if CSS gradients are not supported. It will work on Firefox 3.6, latest Webkit and —hopefully— Opera 11.10 (they <a href="http://www.opera.com/press/releases/2011/03/11/" target="_blank">announced that gradients support is coming</a>). You could add a <code>-webkit-gradient()</code> background too, to make it work in practically all versions of Webkit currently in use, but I warn you: It&#8217;s not going to be easy and I personally refuse to spend more than 5 minutes of my time messing with that non-standard sh*t.</p>
<p>PS: For my twitter friends, I had already written this when the robbers came and I was about to post it. I might have been really calm, but not as much as making CSS experiments the same day I was robbed and threatened by a gun <img src='http://lea.verou.me/wp-includes/images/smilies/icon_razz.gif' alt=':P' class='wp-smiley' /> </p>

]]></content:encoded>
			<wfw:commentRss>http://lea.verou.me/2011/03/beveled-corners-negative-border-radius-with-css3-gradients/feed/</wfw:commentRss>
		<slash:comments>34</slash:comments>
		</item>
		<item>
		<title>Custom &lt;select&gt; drop downs with CSS3</title>
		<link>http://lea.verou.me/2011/03/custom-select-drop-downs-with-css3/</link>
		<comments>http://lea.verou.me/2011/03/custom-select-drop-downs-with-css3/#comments</comments>
		<pubDate>Fri, 04 Mar 2011 19:26:20 +0000</pubDate>
		<dc:creator>Lea Verou</dc:creator>
				<category><![CDATA[Original]]></category>
		<category><![CDATA[Tips]]></category>
		<category><![CDATA[CSS3 properties]]></category>
		<category><![CDATA[Opera bugs]]></category>
		<category><![CDATA[pointer-events]]></category>

		<guid isPermaLink="false">http://leaverou.me/?p=856</guid>
		<description><![CDATA[The CSS3 Basic UI module defines pointer-events as: The pointer-events property allows authors to control whether or when an element may be the target of user pointing device (pointer, e.g. mouse) events. This property is used to specify under which circumstance (if any) a pointer event should go &#8220;through&#8221; an element and target whatever is [...]]]></description>
			<content:encoded><![CDATA[
<div class="topsy_widget_data topsy_theme_hot-pink" style="float: right;margin-left: 0.75em; background: url(data:,%7B%20%22url%22%3A%20%22http%253A%252F%252Flea.verou.me%252F2011%252F03%252Fcustom-select-drop-downs-with-css3%252F%22%2C%20%22shorturl%22%3A%20%22http%3A%2F%2Fbit.ly%2FhbYWRJ%22%2C%20%22style%22%3A%20%22big%22%2C%20%22title%22%3A%20%22Custom%20%26lt%3Bselect%26gt%3B%20drop%20downs%20with%20CSS3%22%20%7D);"></div>
<p>The CSS3 Basic UI module defines <a href="http://dev.w3.org/csswg/css3-ui/#pointer-events"><code>pointer-events</code></a> as:</p>
<blockquote><p>The <code>pointer-events</code> property allows authors to control whether or when an element may be the target of user pointing device (pointer, e.g. mouse) events. This property is used to specify under which circumstance (if any) a pointer event should go &#8220;through&#8221; an element and target whatever is &#8220;underneath&#8221; that element instead. This also applies to other &#8220;hit testing&#8221; behaviors such as dynamic pseudo-classes (:hover, :active, :focus), hyperlinks, and Document.elementFromPoint().</p></blockquote>
<p>The property was originally SVG-only, but eventually browsers and the W3C adopted a more limited version for HTML elements too.</p>
<p>It can be used in many use cases that weren&#8217;t possible before (or the solution was overly complicated), one of them being to create custom-looking &lt;select&gt; drop downs, by overlaying an element over the native drop down arrow (to create the custom one) and disallowing pointer events on it. Here&#8217;s a quick example:</p>
<p><iframe style="width: 100%; height: 200px" src="http://jsfiddle.net/leaverou/XxkSC/embedded/result,css"></iframe></p>
<p><code>-webkit-appearance: none</code> was needed in Webkit to turn off the native OSX appearance (in OSX and maybe Safari on Windows, I didn&#8217;t test that). However, since that also removes the native drop down arrow, our custom arrow now obscures part of the text, so we had to add a 30px padding-right to the select element, only in Webkit.<br />
<span id="more-856"></span><br />
You can easily detect if <code>pointer-events</code> is supported via JS and only apply this it if it is (eg by adding or removing a class from the body element):</p>
<pre>if(!('pointerEvents' in document.body.style)) {
    ...
}</pre>
<p>However, there is one caveat in this: Opera does include pointerEvents in HTML elements as well, but it does not actually support the property on HTML. There&#8217;s a more elaborate feature detection script <a href="https://github.com/ausi/Feature-detection-technique-for-pointer-events/blob/master/modernizr-pointerevents.js">here</a> as a Modernizr plugin (but the code is quite short, so you can adapt it to your needs).</p>
<p>Also, don&#8217;t try to replicate the behavior in JavaScript for browsers that don&#8217;t support this: it&#8217;s impossible to open a &lt;select&gt; drop down with JavaScript. Or, to put it differently, if you manage to do it, you&#8217;ll probably be the first to. Everything I could think of failed and I spent hours yesterday searching for a way, but no avail.</p>
<h3>References</h3>
<ul>
<li><a href="http://dev.w3.org/csswg/css3-ui/#pointer-events">W3C specification</a></li>
<li><a href="https://developer.mozilla.org/en/CSS/pointer-events">MDN article</a></li>
</ul>

]]></content:encoded>
			<wfw:commentRss>http://lea.verou.me/2011/03/custom-select-drop-downs-with-css3/feed/</wfw:commentRss>
		<slash:comments>35</slash:comments>
		</item>
		<item>
		<title>Checkerboard pattern with CSS3</title>
		<link>http://lea.verou.me/2011/02/checkerboard-pattern-with-css3/</link>
		<comments>http://lea.verou.me/2011/02/checkerboard-pattern-with-css3/#comments</comments>
		<pubDate>Wed, 16 Feb 2011 17:19:12 +0000</pubDate>
		<dc:creator>Lea Verou</dc:creator>
				<category><![CDATA[Original]]></category>
		<category><![CDATA[Tips]]></category>
		<category><![CDATA[CSS3]]></category>
		<category><![CDATA[css3 gradients]]></category>
		<category><![CDATA[CSS3 values]]></category>
		<category><![CDATA[multiple backgrounds]]></category>
		<category><![CDATA[patterns]]></category>

		<guid isPermaLink="false">http://leaverou.me/?p=850</guid>
		<description><![CDATA[A while ago, I wrote a post on creating simple patterns with CSS3 gradients. A common pattern I was unable to create was that of a regular, non-rotated checkerboard. However, I noticed today that by giving a different background-position to every triangle in the pattern tile, a checkerboard can be easily created: View in Gecko [...]]]></description>
			<content:encoded><![CDATA[
<div class="topsy_widget_data topsy_theme_hot-pink" style="float: right;margin-left: 0.75em; background: url(data:,%7B%20%22url%22%3A%20%22http%253A%252F%252Flea.verou.me%252F2011%252F02%252Fcheckerboard-pattern-with-css3%252F%22%2C%20%22shorturl%22%3A%20%22http%3A%2F%2Fbit.ly%2FgaHHGq%22%2C%20%22style%22%3A%20%22big%22%2C%20%22title%22%3A%20%22Checkerboard%20pattern%20with%20CSS3%22%20%7D);"></div>
<p>A while ago, I wrote a post on <a href="http://lea.verou.me/2010/12/checkered-stripes-other-background-patterns-with-css3-gradients/">creating simple patterns with CSS3 gradients</a>. A common pattern I was unable to create was that of a regular, non-rotated checkerboard. However, I noticed today that by giving a different background-position to every triangle in the pattern tile, a checkerboard can be easily created:</p>
<p><iframe style="width: 100%; height: 300px" src="http://jsfiddle.net/leaverou/SUgfD/embedded/result,css"></iframe></p>
<p>View in Gecko or Webkit.<br />
Webkit seems to have an odd rendering bug, so it needed a background-size override and it still doesn&#8217;t look perfect. Oh well, <a href="https://bugs.webkit.org/show_bug.cgi?id=54805">reported the bug</a> and moved on.</p>

]]></content:encoded>
			<wfw:commentRss>http://lea.verou.me/2011/02/checkerboard-pattern-with-css3/feed/</wfw:commentRss>
		<slash:comments>9</slash:comments>
		</item>
		<item>
		<title>Convert PHP serialized data to Unicode</title>
		<link>http://lea.verou.me/2011/02/convert-php-serialized-data-to-unicode/</link>
		<comments>http://lea.verou.me/2011/02/convert-php-serialized-data-to-unicode/#comments</comments>
		<pubDate>Sun, 13 Feb 2011 23:29:15 +0000</pubDate>
		<dc:creator>Lea Verou</dc:creator>
				<category><![CDATA[Original]]></category>
		<category><![CDATA[Tips]]></category>
		<category><![CDATA[latin1]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[serialized]]></category>
		<category><![CDATA[Unicode]]></category>
		<category><![CDATA[utf8]]></category>

		<guid isPermaLink="false">http://leaverou.me/?p=835</guid>
		<description><![CDATA[I recently had to convert a database of a large greek website from single-byte greek to Unicode (UTF-8). One of the problems I faced was the stored PHP serialized data: As PHP stores the length of the data (in bytes) inside the serialized string, the stored serialized strings could not be unserialized after the conversion. [...]]]></description>
			<content:encoded><![CDATA[
<div class="topsy_widget_data topsy_theme_hot-pink" style="float: right;margin-left: 0.75em; background: url(data:,%7B%20%22url%22%3A%20%22http%253A%252F%252Flea.verou.me%252F2011%252F02%252Fconvert-php-serialized-data-to-unicode%252F%22%2C%20%22shorturl%22%3A%20%22http%3A%2F%2Fbit.ly%2FdTf69m%22%2C%20%22style%22%3A%20%22big%22%2C%20%22title%22%3A%20%22Convert%20PHP%20serialized%20data%20to%20Unicode%22%20%7D);"></div>
<p>I recently had to convert a database of a large greek website from single-byte greek to Unicode (UTF-8). One of the problems I faced was the stored PHP serialized data: As PHP stores the length of the data (in bytes) inside the serialized string, the stored serialized strings could not be unserialized after the conversion.</p>
<p>I didn&#8217;t want anyone to go through the frustration I went through while searching for a solution, so here is a little function I wrote to recount the string lengths, since I couldn&#8217;t find anything on this:</p>
<pre>function recount_serialized_bytes($text) {
	mb_internal_encoding("UTF-8");
	mb_regex_encoding("UTF-8");

	mb_ereg_search_init($text, 's:[0-9]+:"');

	$offset = 0;

	while(preg_match('/s:([0-9]+):"/u', $text, $matches, PREG_OFFSET_CAPTURE, $offset) ||
		  preg_match('/s:([0-9]+):"/u', $text, $matches, PREG_OFFSET_CAPTURE, ++$offset)) {
		$number = $matches[1][0];
		$pos = $matches[1][1];

		$digits = strlen("$number");
		$pos_chars = mb_strlen(substr($text, 0, $pos)) + 2 + $digits;

		$str = mb_substr($text, $pos_chars, $number);

		$new_number = strlen($str);
		$new_digits = strlen($new_number);

		if($number != $new_number) {
			// Change stored number
			$text = substr_replace($text, $new_number, $pos, $digits);
			$pos += $new_digits - $digits;
		}

		$offset = $pos + 2 + $new_number;
	}

	return $text;
}</pre>
<p>My initial approach was to do it with regular expressions, but the PHP serialized data format is not a regular language and cannot be properly parsed with regular expressions. All approaches fail on edge cases, and I had lots of edge cases in my data (I even had nested serialized strings!).</p>
<p>Note that this will only work when converting <strong>from single-byte encoded data</strong>, since it assumes the stored lengths are the string lengths in characters. Admittedly, it&#8217;s not my best code, it could be optimized in many ways. It was something I had to write quickly and was only going to be used by me in a one-time conversion process. However, it works smoothly and has been tested with lots of different serialized data. I know that not many people will find it useful, but it&#8217;s going to be a lifesaver for the few ones that need it.</p>

]]></content:encoded>
			<wfw:commentRss>http://lea.verou.me/2011/02/convert-php-serialized-data-to-unicode/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Styling children based on their number, with CSS3</title>
		<link>http://lea.verou.me/2011/01/styling-children-based-on-their-number-with-css3/</link>
		<comments>http://lea.verou.me/2011/01/styling-children-based-on-their-number-with-css3/#comments</comments>
		<pubDate>Mon, 17 Jan 2011 20:40:35 +0000</pubDate>
		<dc:creator>Lea Verou</dc:creator>
				<category><![CDATA[Replies]]></category>
		<category><![CDATA[Tips]]></category>
		<category><![CDATA[CSS3]]></category>
		<category><![CDATA[CSS3 selectors]]></category>

		<guid isPermaLink="false">http://leaverou.me/?p=825</guid>
		<description><![CDATA[The original idea belongs to André Luís, but I think it could be improved to be much less verbose. André&#8217;s solution is like this: /* one item */ li:nth-child(1):nth-last-child(1) { width: 100%; } /* two items */ li:nth-child(1):nth-last-child(2), li:nth-child(2):nth-last-child(1) { width: 50%; } /* three items */ li:nth-child(1):nth-last-child(3), li:nth-child(2):nth-last-child(2), li:nth-child(3):nth-last-child(1) { width: 33.3333%; } /* [...]]]></description>
			<content:encoded><![CDATA[
<div class="topsy_widget_data topsy_theme_hot-pink" style="float: right;margin-left: 0.75em; background: url(data:,%7B%20%22url%22%3A%20%22http%253A%252F%252Flea.verou.me%252F2011%252F01%252Fstyling-children-based-on-their-number-with-css3%252F%22%2C%20%22shorturl%22%3A%20%22http%3A%2F%2Fbit.ly%2FekAbp3%22%2C%20%22style%22%3A%20%22big%22%2C%20%22title%22%3A%20%22Styling%20children%20based%20on%20their%20number%2C%20with%20CSS3%22%20%7D);"></div>
<p>The <a href="http://andr3.net/blog/post/142" target="_blank">original idea belongs to André Luís</a>, but I think it could be improved to be much less verbose.</p>
<p>André&#8217;s solution is like this:</p>
<pre>/* one item */
li:nth-child(1):nth-last-child(1) {
	width: 100%;
}

/* two items */
li:nth-child(1):nth-last-child(2),
li:nth-child(2):nth-last-child(1) {
	width: 50%;
}

/* three items */
li:nth-child(1):nth-last-child(3),
li:nth-child(2):nth-last-child(2),
li:nth-child(3):nth-last-child(1) {
	width: 33.3333%;
}

/* four items */
li:nth-child(1):nth-last-child(4),
li:nth-child(2):nth-last-child(3),
li:nth-child(3):nth-last-child(2),
li:nth-child(4):nth-last-child(1) {
	width: 25%;
}</pre>
<p>It&#8217;s based on the relationship between :nth-child and :nth-last-child. As you can see, the number of total rules is O(N) and the number of selectors in every rule is also O(N).</p>
<p>However, what you really want, is to just target the first element. The others can be targeted with just a sibling selector. With my improvement, the number of total rules is still O(N), but the number of selectors in every rule becomes just 2, making this trick practical for far larger numbers of children:</p>
<pre>/* one item */
li:first-child:nth-last-child(1) {
	width: 100%;
}

/* two items */
li:first-child:nth-last-child(2),
li:first-child:nth-last-child(2) ~ li {
	width: 50%;
}

/* three items */
li:first-child:nth-last-child(3),
li:first-child:nth-last-child(3) ~ li {
	width: 33.3333%;
}

/* four items */
li:first-child:nth-last-child(4),
li:first-child:nth-last-child(4) ~ li {
	width: 25%;
}</pre>
<p>And here&#8217;s a fiddle to prove it:<br />
<iframe style="width: 100%; height: 300px" src="http://jsfiddle.net/leaverou/HdfaT/embedded/result,css,html"></iframe></p>
<p>Yes, I know that with Flexbox and the other layout modules, techniques such as these are soon becoming obsolete, but I think they are still useful right now.<br />
I&#8217;m also aware that you can emulate this particular example with table display modes, but a) Table display modes have other implications that are sometimes undesirable and b) Widths are just an example, you could come up with other ways to style the elements based on their total count, which can&#8217;t be emulated by CSS tables.</p>

]]></content:encoded>
			<wfw:commentRss>http://lea.verou.me/2011/01/styling-children-based-on-their-number-with-css3/feed/</wfw:commentRss>
		<slash:comments>30</slash:comments>
		</item>
		<item>
		<title>Checkerboard, striped &amp; other background patterns with CSS3 gradients</title>
		<link>http://lea.verou.me/2010/12/checkered-stripes-other-background-patterns-with-css3-gradients/</link>
		<comments>http://lea.verou.me/2010/12/checkered-stripes-other-background-patterns-with-css3-gradients/#comments</comments>
		<pubDate>Wed, 22 Dec 2010 22:23:03 +0000</pubDate>
		<dc:creator>Lea Verou</dc:creator>
				<category><![CDATA[Original]]></category>
		<category><![CDATA[Tips]]></category>
		<category><![CDATA[background-size]]></category>
		<category><![CDATA[CSS3]]></category>
		<category><![CDATA[css3 gradients]]></category>
		<category><![CDATA[CSS3 values]]></category>
		<category><![CDATA[multiple backgrounds]]></category>
		<category><![CDATA[patterns]]></category>

		<guid isPermaLink="false">http://leaverou.me/?p=775</guid>
		<description><![CDATA[You&#8217;re probably familiar with CSS3 gradients by now, including the closer to the standard Mozilla syntax and the ugly verbose Webkit one. I assume you know how to add multiple color stops, make your gradients angled or create radial gradients. What you might not be aware of, is that CSS3 gradients can be used to [...]]]></description>
			<content:encoded><![CDATA[
<div class="topsy_widget_data topsy_theme_hot-pink" style="float: right;margin-left: 0.75em; background: url(data:,%7B%20%22url%22%3A%20%22http%253A%252F%252Flea.verou.me%252F2010%252F12%252Fcheckered-stripes-other-background-patterns-with-css3-gradients%252F%22%2C%20%22shorturl%22%3A%20%22http%3A%2F%2Fbit.ly%2FgI2SdY%22%2C%20%22style%22%3A%20%22big%22%2C%20%22title%22%3A%20%22Checkerboard%2C%20striped%20%26%20other%20background%20patterns%20with%20CSS3%20gradients%22%20%7D);"></div>
<p><a href="http://lea.verou.me/demos/css3-patterns.html"><img class="size-large alignleft" title="The CSS3 patterns I came up with" src="http://lea.verou.me/wp-content/uploads/2010/12/css3-patterns-1024x480.png" alt="Screenshot of the CSS3 patterns I came up with" width="300" /></a>You&#8217;re probably familiar with CSS3 gradients by now, including the closer to the standard Mozilla syntax and the ugly verbose Webkit one. I assume you know how to add multiple color stops, make your gradients angled or create radial gradients. What you might not be aware of, is that CSS3 gradients can be used to create many kinds of commonly needed patterns, including checkered patterns, stripes and more.</p>
<p><a class="view-demo" href="http://lea.verou.me/demos/css3-patterns.html">View demo</a> (Works in Webkit, Firefox 3.6+, Opera 11.50+ and IE10+)</p>
<p>The main idea behind the technique is the following, taken from the <a href="http://dev.w3.org/csswg/css3-images/#color-stop-syntax" target="_blank">CSS3 Images spec</a>:</p>
<blockquote><p>If multiple color-stops have the same position, they produce an infinitesimal transition from the one specified first in the rule to the one specified last. <strong>In effect, the color suddenly changes at that position rather than smoothly transitioning.</strong></p></blockquote>
<p>I guess this makes it obvious how to create the tile for the stripes (unless you&#8217;ve never created a striped background before, but teaching you this is beyond the scope of this post). For example the gradient for the horizontal stripes is:</p>
<pre>background-color: #0ae;
background-image: -webkit-gradient(linear, 0 0, 0 100%, color-stop(.5, rgba(255, 255, 255, .2)), color-stop(.5, transparent), to(transparent));
background-image: -moz-linear-gradient(rgba(255, 255, 255, .2) 50%, transparent 50%, transparent);
background-image: -o-linear-gradient(rgba(255, 255, 255, .2) 50%, transparent 50%, transparent);
background-image: linear-gradient(rgba(255, 255, 255, .2) 50%, transparent 50%, transparent);</pre>
<p>Why <code>transparent</code> instead of the actual colors we want? For flexibility. <code>background-color</code> serves two purposes here: Setting the color of half the stripes and serving as a fallback for browsers that don&#8217;t support gradients.</p>
<p>However, without anything else, the tile will occupy the whole container. To control the size of each tile, you can use background-size:</p>
<pre>-webkit-background-size: 50px 50px;
-moz-background-size: 50px 50px;
background-size: 50px 50px;</pre>
<p>To create the picnic-style pattern, you just overlay horizontal stripes on vertical stripes.</p>
<p>The hardest one to figure out was the checkered pattern. It consists of two 45° linear gradients and two -45° linear gradients, each containing ¼ of the dark squares. I still haven&#8217;t managed to think of a way to create a regular checkerboard (not at 45°) without needing an unacceptably large number of gradients. It will be very easily possible if conical gradients start being supported (currently they&#8217;re not even in the spec yet).</p>
<p>Can you think of any other popular patterns that can be created with CSS3 and no images? If so, let me know with a comment. Cheers! <img src='http://lea.verou.me/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<h3>Added afterwards: Other patterns</h3>
<p>There are far more pattern designs possible with CSS3 gradients than I originally thought. For more details, see <a href="http://lea.verou.me/2011/04/css3-patterns-gallery-and-a-new-pattern/">this later post</a>.</p>

]]></content:encoded>
			<wfw:commentRss>http://lea.verou.me/2010/12/checkered-stripes-other-background-patterns-with-css3-gradients/feed/</wfw:commentRss>
		<slash:comments>138</slash:comments>
		</item>
		<item>
		<title>MySQL: Are you actually utilizing your indexes?</title>
		<link>http://lea.verou.me/2010/03/mysql-are-you-actually-utilizing-your-indexes/</link>
		<comments>http://lea.verou.me/2010/03/mysql-are-you-actually-utilizing-your-indexes/#comments</comments>
		<pubDate>Thu, 25 Mar 2010 03:14:56 +0000</pubDate>
		<dc:creator>Lea Verou</dc:creator>
				<category><![CDATA[Tips]]></category>
		<category><![CDATA[MySQL]]></category>

		<guid isPermaLink="false">http://leaverou.me/?p=527</guid>
		<description><![CDATA[This might seem elementary to those of you that are DBAs or something similar, but it was fascinating to find out (not to mention it greatly helped what I had to do), so I decided to post it, in case it helps someone else too. A few moments ago I found out that whereas a [...]]]></description>
			<content:encoded><![CDATA[
<div class="topsy_widget_data topsy_theme_hot-pink" style="float: right;margin-left: 0.75em; background: url(data:,%7B%20%22url%22%3A%20%22http%253A%252F%252Flea.verou.me%252F2010%252F03%252Fmysql-are-you-actually-utilizing-your-indexes%252F%22%2C%20%22style%22%3A%20%22big%22%2C%20%22title%22%3A%20%22MySQL%3A%20Are%20you%20actually%20utilizing%20your%20indexes%3F%22%20%7D);"></div>
<p><em>This might seem elementary to those of you that are DBAs or something similar, but it was fascinating to find out (not to mention it greatly helped what I had to do), so I decided to post it, in case it helps someone else too.</em> A few moments ago I found out that whereas a query along the lines of&#8230;</p>
<pre>SELECT title, COUNT(1) as replies
FROM post INNER JOIN thread USING(threadid)
WHERE <strong>UNIX_TIMESTAMP(NOW()) - post.dateline &lt; 86400</strong>
GROUP BY threadid
ORDER BY replies DESC
LIMIT 5</pre>
<p>took a whopping <strong>~10 seconds</strong> on a post table of around 2,000,000 rows and a thread table of around 40,000 rows, the following:</p>
<pre>SELECT title, COUNT(1) as replies
FROM post INNER JOIN thread USING(threadid)
WHERE <strong>post.dateline &gt; UNIX_TIMESTAMP(NOW()) - 86400</strong>
GROUP BY threadid
ORDER BY replies DESC
LIMIT 5</pre>
<p>took a mere <strong>0.03 seconds</strong>!</p>
<p>Probably, MySQL wasn&#8217;t able to utilize the B+ tree index of the dateline column in the first query, whereas in the second, things were a bit more obvious to it. This can also be observed by examining the information about the execution plan that EXPLAIN provides:</p>
<pre>mysql&gt; explain select t.threadid, t.title, count(1) as replies from vb3_post as p inner join vb3_thread as t using(threadid) where unix_timestamp(now()) - p.dateline &lt; 86400 group by p.threadid order by replies desc limit 5;
+----+-------------+-------+------+---------------+----------+---------+------------+-------+---------------------------------+
| id | select_type | table | type | possible_keys | key      | key_len | re         | rows  | Extra                           |
+----+-------------+-------+------+---------------+----------+---------+------------+-------+---------------------------------+
|  1 | SIMPLE      | t     | ALL  | PRIMARY       | NULL     | NULL    | NULL       | 39859 | Using temporary; Using filesort |
|  1 | SIMPLE      | p     | ref  | threadid      | threadid | 4       | t.threadid |    49 | Using where                     |
+----+-------------+-------+------+---------------+----------+---------+------------+-------+---------------------------------+
2 rows in set (0.01 sec)

mysql&gt; explain select t.threadid, t.title, count(1) as replies from vb3_post as p inner join vb3_thread as t using(threadid) where p.dateline &gt; UNIX_TIMESTAMP(NOW()) - 86400 group by p.threadid order by replies desc limit 5;
+----+-------------+-------+--------+-------------------+----------+---------+------------+------+----------------------------------------------+
| id | select_type | table | type   | possible_keys     | key      | key_len | ref        | rows | Extra                                        |
+----+-------------+-------+--------+-------------------+----------+---------+------------+------+----------------------------------------------+
|  1 | SIMPLE      | p     | range  | threadid,dateline | dateline | 4       | NULL       | 1171 | Using where; Using temporary; Using filesort |
|  1 | SIMPLE      | t     | eq_ref | PRIMARY           | PRIMARY  | 4       | p.threadid |    1 |                                              |
+----+-------------+-------+--------+-------------------+----------+---------+------------+------+----------------------------------------------+
2 rows in set (0.00 sec)</pre>
<p>So, don&#8217;t rest assured that MySQL will use your indexes every time it should. It seems that sometimes you have to explicitly point it out.</p>

]]></content:encoded>
			<wfw:commentRss>http://lea.verou.me/2010/03/mysql-are-you-actually-utilizing-your-indexes/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Reading cookies the regular expression way</title>
		<link>http://lea.verou.me/2009/12/reading-cookies-the-regular-expression-way/</link>
		<comments>http://lea.verou.me/2009/12/reading-cookies-the-regular-expression-way/#comments</comments>
		<pubDate>Thu, 03 Dec 2009 22:21:17 +0000</pubDate>
		<dc:creator>Lea Verou</dc:creator>
				<category><![CDATA[Tips]]></category>
		<category><![CDATA[cookies]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[regular expressions]]></category>

		<guid isPermaLink="false">http://leaverou.me/?p=460</guid>
		<description><![CDATA[While taking a look on the 2nd 24ways article for 2009, I was really surprised to read that &#8220;The Web Storage API is basically cookies on steroids, a unhealthy dosage of steroids. Cookies are always a pain to work with. First of all you have the problem of setting, changing and deleting them. Typically solved [...]]]></description>
			<content:encoded><![CDATA[
<div class="topsy_widget_data topsy_theme_hot-pink" style="float: right;margin-left: 0.75em; background: url(data:,%7B%20%22url%22%3A%20%22http%253A%252F%252Flea.verou.me%252F2009%252F12%252Freading-cookies-the-regular-expression-way%252F%22%2C%20%22style%22%3A%20%22big%22%2C%20%22title%22%3A%20%22Reading%20cookies%20the%20regular%20expression%20way%22%20%7D);"></div>
<p>While taking a look on <a href="http://24ways.org/2009/breaking-out-the-edges-of-the-browser">the 2nd 24ways article for 2009</a>, I was really surprised to read that <em>&#8220;The Web Storage API is basically cookies on steroids, a unhealthy dosage of steroids. Cookies are always a pain to work with. First of all you have the problem of setting, changing and deleting them. <strong>Typically solved by Googling and blindly relying on PPK’s solution.</strong>&#8220;</em> (bold is mine)</p>
<p>Of course, there&#8217;s nothing wrong with <a href="http://www.quirksmode.org/js/cookies.html#script">PPK&#8217;s solution</a>. It works just fine. However, I always thought his readCookie() function was too verbose and complicated for no reason. It&#8217;s a very common example of someone desperately trying to avoid using a regular expression. I googled for &#8220;<a href="http://www.google.com/search?q=javascript+read+cookie">javascript read cookie</a>&#8221; and to my surprise, all examples found in the first results were very similar. I never understood why even experienced developers are so scared of regular expressions. Anyway, if anyone wants a shorter function to read a cookie, here&#8217;s what I use:</p>
<pre>function readCookie(name) {
    // Escape regexp special characters (thanks kangax!)
    name = name.replace(/([.*+?^=!:${}()|[\]\/\\])/g, '\\$1');

    var regex = new RegExp('(?:^|;)\\s?' + name + '=(.*?)(?:;|$)','i'),
        match = document.cookie.match(regex);

    return match &amp;&amp; unescape(match[1]); // thanks James!
}</pre>
<p><span style="color: #ff0000;"><strong>Update:</strong></span><strong> Function updated, see comments below</strong>.</p>
<p>I&#8217;ve been using it for years and it hasn&#8217;t let me down. <img src='http://lea.verou.me/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>Probably lots of other people have come up and posted something similar before me (I was actually very surprised that something like this isn&#8217;t mainstream), but I&#8217;m posting it just in case. <img src='http://lea.verou.me/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>

]]></content:encoded>
			<wfw:commentRss>http://lea.verou.me/2009/12/reading-cookies-the-regular-expression-way/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>Yet another email hiding technique?</title>
		<link>http://lea.verou.me/2009/11/yet-another-email-hiding-technique/</link>
		<comments>http://lea.verou.me/2009/11/yet-another-email-hiding-technique/#comments</comments>
		<pubDate>Sun, 29 Nov 2009 06:36:26 +0000</pubDate>
		<dc:creator>Lea Verou</dc:creator>
				<category><![CDATA[Original]]></category>
		<category><![CDATA[Tips]]></category>
		<category><![CDATA[spam]]></category>
		<category><![CDATA[Unicode]]></category>

		<guid isPermaLink="false">http://leaverou.me/?p=443</guid>
		<description><![CDATA[While exploring browser-supported Unicode characters, I noticed that apart from the usual @ and . (dot), there was another character that resembled an @ sign (0xFF20 or ＠) and various characters that resembled a period (I think 0&#215;2024 or ․ is closer, but feel free to argue). I&#8217;m wondering, if one could use this as [...]]]></description>
			<content:encoded><![CDATA[
<div class="topsy_widget_data topsy_theme_hot-pink" style="float: right;margin-left: 0.75em; background: url(data:,%7B%20%22url%22%3A%20%22http%253A%252F%252Flea.verou.me%252F2009%252F11%252Fyet-another-email-hiding-technique%252F%22%2C%20%22style%22%3A%20%22big%22%2C%20%22title%22%3A%20%22Yet%20another%20email%20hiding%20technique%3F%22%20%7D);"></div>
<p>While <a href="http://lea.verou.me/2009/11/exploring-browser-supported-unicode-characters-and-a-tweet-shortening-experiment/">exploring browser-supported Unicode characters</a>, I noticed that apart from the usual @ and . (dot), there was another character that resembled an @ sign (0xFF20 or ＠) and various characters that resembled a period (I think 0&#215;2024 or ․ is closer, but feel free to argue).</p>
<p>I&#8217;m wondering, if one could use this as another way of email hiding. It&#8217;s almost as easy as the foo [at] bar [dot] com technique, with the advantage of being far less common (I&#8217;ve never seen it before, so there&#8217;s a high chance that spambot developers haven&#8217;t either) and I think that the end result is more easily understood by newbies. To encode <a class="linkification-ext" title="Linkification: mailto:foo@bar.com" href="mailto:foo@bar.com">foo@bar.com</a> this way, we&#8217;d use (in an html page):</p>
<pre>foo&amp;#xFF20;bar&amp;#x2024;com</pre>
<p>and the result is: <span style="font-size:1.5em">foo＠bar․com</span></p>
<p>I used that technique on the <a href="http://lea.verou.me/demos/ligatweet/#conversions">ligatweet page</a>. Of course, if many people start using it, I guess spambot developers will notice, so it won&#8217;t be a good idea any more. However, for some reason I don&#8217;t think it will ever become that mainstream <img src='http://lea.verou.me/wp-includes/images/smilies/icon_razz.gif' alt=':P' class='wp-smiley' /> </p>
<p>By the way, if you&#8217;re interested in other ways of email hiding, <a href="http://www.csarven.ca/hiding-email-addresses">here&#8217;s an extensive article on the subject</a> that I came across after a quick googlesearch (to see if somebody else came up with this first &#8212; I didn&#8217;t find anything).</p>

]]></content:encoded>
			<wfw:commentRss>http://lea.verou.me/2009/11/yet-another-email-hiding-technique/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>(byte)size matters</title>
		<link>http://lea.verou.me/2009/07/bytesize-matters/</link>
		<comments>http://lea.verou.me/2009/07/bytesize-matters/#comments</comments>
		<pubDate>Fri, 31 Jul 2009 17:01:18 +0000</pubDate>
		<dc:creator>Lea Verou</dc:creator>
				<category><![CDATA[Personal]]></category>
		<category><![CDATA[Tips]]></category>
		<category><![CDATA[bytesizematters]]></category>

		<guid isPermaLink="false">http://leaverou.me/?p=378</guid>
		<description><![CDATA[Yesterday, I was editing a CSS file and I was wondering how many bytes/KB would a particular addition add to it, in order to decide if it was worth it. Since, I had found myself wondering about the exact same thing multiple times in the past, I decided to make a simple standalone HTML page [...]]]></description>
			<content:encoded><![CDATA[
<div class="topsy_widget_data topsy_theme_hot-pink" style="float: right;margin-left: 0.75em; background: url(data:,%7B%20%22url%22%3A%20%22http%253A%252F%252Flea.verou.me%252F2009%252F07%252Fbytesize-matters%252F%22%2C%20%22style%22%3A%20%22big%22%2C%20%22title%22%3A%20%22%28byte%29size%20matters%22%20%7D);"></div>
<p>Yesterday, I was editing a CSS file and I was wondering how many bytes/KB would a particular addition add to it, in order to decide if it was worth it. Since, I had found myself wondering about the exact same thing multiple times in the past, I decided to make a simple standalone HTML page that would compute the size of any entered text in bytes, KB, MB, etc (whatever was most appropriate). It should be simple and quick and it should account for line terminator differences across operating systems.</p>
<p>About half an hour later, I was done. And then it dawned on me: Someone else might need it too! Since .com domains are, so cheap, hey, let&#8217;s get a domain for it as well! There are <a href="http://kottke.org/08/02/single-serving-sites">several</a> <a href="http://dowebsitesneedtolookexactlythesameineverybrowser.com/">sites</a> <a href="http://amiawesome.com/">with</a> <a href="http://r33b.net/">a</a> <a href="http://isitchristmas.com/">domain</a> <a href="http://justfuckinggoogleit.com/">that</a> <a href="http://www.sometimesredsometimesblue.com/">are</a> <a href="http://www.d-e-f-i-n-i-t-e-l-y.com/">way</a> <a href="http://www.tired.com/">simpler</a> than that anyway. A friend that was sitting next to me suggested &#8220;sizematters.com&#8221; as a joke, but as it turned out, <a title="(byte)size matters!" href="http://bytesizematters.com">bytesizematters.com</a> was free, so we registered it. And there it is, less than a day after, <a href="http://bytesizematters.com">it&#8217;s aliiive</a>. <img src='http://lea.verou.me/wp-includes/images/smilies/icon_razz.gif' alt=':P' class='wp-smiley' /> </p>
<p>Any feedback or suggestions are greatly welcome!</p>
<p><span style="text-decoration: line-through;">For instance, should I implement a very simple minification algorithm and display bytesize for that as well, or is it too much and ruins the simplicity of it without being worth it? </span><strong>[edit: I did it anyway]</strong></p>
<p><span style="text-decoration: line-through;">Should I implement a way to compare two pieces of text and find out the difference in byte size (could be useful for JavaScript refactoring)?</span> <strong>[edit:</strong><strong> I did it anyway</strong><strong>]</strong></p>

]]></content:encoded>
			<wfw:commentRss>http://lea.verou.me/2009/07/bytesize-matters/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Bevels in CSS3</title>
		<link>http://lea.verou.me/2009/07/bevels-in-css3/</link>
		<comments>http://lea.verou.me/2009/07/bevels-in-css3/#comments</comments>
		<pubDate>Thu, 23 Jul 2009 19:24:22 +0000</pubDate>
		<dc:creator>Lea Verou</dc:creator>
				<category><![CDATA[Original]]></category>
		<category><![CDATA[Tips]]></category>
		<category><![CDATA[box-shadow]]></category>
		<category><![CDATA[CSS3]]></category>
		<category><![CDATA[CSS3 properties]]></category>

		<guid isPermaLink="false">http://leaverou.me/?p=368</guid>
		<description><![CDATA[Yeah, yeah I know, bevels are soooo 1996. And I agree. However, it&#8217;s always good to know the capabilities of your tools. Talented designers will know when it&#8217;s suitable to use a certain effect and incapable ones will abuse whatever is given to them, so after a lot of thought, I decided to blog about [...]]]></description>
			<content:encoded><![CDATA[
<div class="topsy_widget_data topsy_theme_hot-pink" style="float: right;margin-left: 0.75em; background: url(data:,%7B%20%22url%22%3A%20%22http%253A%252F%252Flea.verou.me%252F2009%252F07%252Fbevels-in-css3%252F%22%2C%20%22style%22%3A%20%22big%22%2C%20%22title%22%3A%20%22Bevels%20in%20CSS3%22%20%7D);"></div>
<p>Yeah, yeah I know, bevels are soooo 1996. And I agree. However, it&#8217;s always good to know the capabilities of your tools. Talented designers will know when it&#8217;s suitable to use a certain effect and incapable ones will abuse whatever is given to them, so after a lot of thought, I decided to blog about my discovery.<span id="more-368"></span></p>
<p>Even though not directly mentioned in the spec, CSS3 is capable of easily creating a bevel effect on any element. Moreover, if the element has rounded corners, the bevel follows that as well. Before explaining the technique, let&#8217;s think about how a bevel actually gets drawn. It&#8217;s essentially two inner shadows, that when combined, create the illusion of a 3d appearance: a light one from the top left corner and a dark one from the bottom right corner. CSS3 includes the ability to create inner shadows, if you specify the keyword &#8220;inset&#8221; in the box-shadow declaration (currently only supported by Firefox 3.5). Moreover, the CSS3 spec allows for multiple box shadows on the same elements.</p>
<p>Now, let&#8217;s examine an example (only works in Firefox 3.5):</p>
<pre>button {
 background:#f16;
 color:white;
 padding:6px 12px 8px 12px;
 border:none;
 font-size:18px;
 -moz-border-radius:10px;
 -moz-box-shadow: -2px -2px 10px rgba(0,0,0,.25) inset, 2px 2px 10px white inset;
}</pre>
<p>which produces this result:</p>
<p><img class="alignnone size-full wp-image-369" title="css3bevel" src="http://lea.verou.me/wp-content/uploads/2009/07/css3bevel.png" alt="css3bevel" width="110" height="52" /></p>
<p>If we want, we can also create a &#8220;pressed&#8221; button state, in a similar fashion:</p>
<pre>button:active {
 -moz-box-shadow: 2px 2px 10px rgba(0,0,0,.25) inset, -2px -2px 10px white inset;
 padding:7px 11px 7px 13px;
}

button::-moz-focus-inner { border: 0; }</pre>
<p>which produces this pressed state:</p>
<p><img class="alignnone size-full wp-image-370" title="css3bevel_pressed" src="http://lea.verou.me/wp-content/uploads/2009/07/css3bevel_pressed.png" alt="css3bevel_pressed" width="116" height="56" /></p>
<p>See it in action here (only for Firefox 3.5): <a class="linkification-ext" title="Linkification: http://lea.verou.me/demos/css3bevel.html" href="http://lea.verou.me/demos/css3bevel.html">http://lea.verou.me/demos/css3bevel.html</a></p>
<p>Of course, if implemented in a real world website, you should also add the -webkit- and -o- CSS3 properties to provide a closer effect for the other browsers and be ready for the time when the ones that aren&#8217;t implemented yet in them will finally make it (for instance, when Webkit implements inset box shadows, it will work in it as well).</p>
<p>Enjoy <strong>responsibly</strong>. <img src='http://lea.verou.me/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' /> </p>

]]></content:encoded>
			<wfw:commentRss>http://lea.verou.me/2009/07/bevels-in-css3/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>On password masking and usability</title>
		<link>http://lea.verou.me/2009/06/on-password-masking-and-usability/</link>
		<comments>http://lea.verou.me/2009/06/on-password-masking-and-usability/#comments</comments>
		<pubDate>Sun, 28 Jun 2009 16:24:50 +0000</pubDate>
		<dc:creator>Lea Verou</dc:creator>
				<category><![CDATA[Articles]]></category>
		<category><![CDATA[Tips]]></category>
		<category><![CDATA[Internet Explorer bugs]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[usability]]></category>

		<guid isPermaLink="false">http://leaverou.me/?p=351</guid>
		<description><![CDATA[I just read Jakob Nielsen&#8217;s recent post in which he urged web designers/developers to stop password masking due to it&#8217;s inherent usability issues. I found it an interesting read. Hey, at last, someone dared to talk about the elephant in the room! In most cases password masking is indeed useless, but still, there are several [...]]]></description>
			<content:encoded><![CDATA[
<div class="topsy_widget_data topsy_theme_hot-pink" style="float: right;margin-left: 0.75em; background: url(data:,%7B%20%22url%22%3A%20%22http%253A%252F%252Flea.verou.me%252F2009%252F06%252Fon-password-masking-and-usability%252F%22%2C%20%22style%22%3A%20%22big%22%2C%20%22title%22%3A%20%22On%20password%20masking%20and%20usability%22%20%7D);"></div>
<p>I just read <a href="http://www.useit.com/alertbox/passwords.html" target="_blank">Jakob Nielsen&#8217;s recent post in which he urged web designers/developers to stop password masking</a> due to it&#8217;s inherent usability issues. I found it an interesting read. Hey, at last, someone dared to talk about the elephant in the room!</p>
<p>In most cases password masking is indeed useless, but still, there are several cases where you need that kind of protection. He also points that out, suggesting a checkbox to enable the user to mask their entered password if they wish to do so. He also suggests that checkbox being enabled by default on sites that require high security.</p>
<p>I think the checkbox idea is really good, as long as it works in the opposite way: Password masking should <strong>always </strong>be the default and you should <strong>check</strong> the checkbox to <strong>show</strong> the characters you typed. This is in line with what Windows (Vista or newer) users are already accustomed to anyway<span id="more-351"></span>:</p>
<p><img class="alignnone size-full wp-image-356" title="Enter passphrase" src="http://lea.verou.me/wp-content/uploads/2009/06/vistawirelesspasscode.png" alt="Enter passphrase" width="362" height="246" /></p>
<p>This can (and should) be done with JavaScript alone: if the user has it turned off, no problem, just a regular old password field. Of course the checkbox should also be dynamically added, to prevent users with disabled JS from viewing a checkbox that does nothing at all.</p>
<p>This seems easy at first, even without a library (although, in this particular case, a library would greatly reduce the amount of code required, so much that I&#8217;m tempted to include a jQuery version as well):</p>
<pre>window.onload = function() {
	var passwords = document.getElementsByTagName('input');
	for(var i=0; i&lt;passwords.length; i++) {
		if(passwords[i].type == 'password') {
			var password = passwords[i];

			var showCharsCheckbox = document.createElement('input');
				showCharsCheckbox.type = 'checkbox';
				showCharsCheckbox.onclick = (function(input) {
					return function() {
						input.type = this.checked? 'text' : 'password';
					};
				})(password);

			var showCharsLabel = document.createElement('label');
				showCharsLabel.appendChild(showCharsCheckbox);
				showCharsLabel.appendChild(document.createTextNode('Show characters'));

			// If the password field is inside a &lt;label&gt; element, we don't want to insert our label in there as well!
			var previousSibling = /label/i.test(password.parentNode.nodeName)? password.parentNode : password;

			// Check whether it's the last child of it's parent
			if(previousSibling.nextSibling) {
				previousSibling.parentNode.insertBefore(showCharsLabel, previousSibling.nextSibling);
			}
			else {
				previousSibling.parentNode.appendChild(showCharsLabel);
			}
		}
	}
}</pre>
<p>However, nothing is ever simple, when you also need to support our <em>beloved </em>Internet Explorer. Most moderately experienced JavaScript developers have probably already understood what I&#8217;m talking about: The all time classic IE bug (still present in IE8&#8230;) in regards to setting an &lt;input /&gt; element&#8217;s type attribute. You can only set it once, for elements that are not already in the DOM. After that, it becomes read-only, and any attempt to set it results in a &#8220;The command is not supported&#8221; error. And when I say &#8220;any&#8221; attempt I mean it:</p>
<ul>
<li>element.setAttribute()</li>
<li>element.type</li>
<li>element.setAttributeNode()</li>
<li>element.removeAttribute() and then element.setAttribute()</li>
<li>element.cloneNode(), then one of the above, then replacing the node with the clone</li>
</ul>
<p><strong>everything</strong> fails miserably.</p>
<p>I&#8217;ve encountered this problem several times in the past as well, but I could always think of an alternative way to do what I wanted without having to work around it. In this case, I don&#8217;t think there is one. So we&#8217;re left with two possible scenarios:</p>
<ul>
<li>Perform an easy test in the beginning to see whether this bug exists and proceed only if the browser isn&#8217;t naughty. This could be done with the following:
<pre>var supportsChangingTypeAttribute = (function() {
	var input = document.createElement('input');
	try {
		input.type = 'password';
		input.type = 'text';
	} catch(e) {
		return false;
	}
	return input.type == 'text';
})();

if(supportsChangingTypeAttribute) {
	// do stuff...
}</pre>
</li>
<li>Wrap the statement that IE chokes on in a try&#8230;catch construct and in the catch(e) {&#8230;} block create a new input element, copy <strong>everything</strong> (where everything is <strong>at least</strong>: attributes, properties, event handlers &#8211; both traditional ones <strong>and </strong>listeners) from the password field into it (except the type attribute of course!) and replace the original password field with it. After the first time, the text field could also be reused, to improve performance.<br />
If you have a shortage of trouble in your life, you may attempt it, I currently do not. <img src='http://lea.verou.me/wp-includes/images/smilies/icon_razz.gif' alt=':P' class='wp-smiley' />  It can be a <strong>very </strong>simple task for particular cases, but a generic solution that would work in any site (or even in most sites) seems a really daunting, tedious and downright boring task. I also hope there might be a better solution, that I haven&#8217;t thought of. Any ideas?</li>
</ul>

]]></content:encoded>
			<wfw:commentRss>http://lea.verou.me/2009/06/on-password-masking-and-usability/feed/</wfw:commentRss>
		<slash:comments>11</slash:comments>
		</item>
		<item>
		<title>Tip: Multi-step form handling</title>
		<link>http://lea.verou.me/2009/06/tip-multi-step-form-handling/</link>
		<comments>http://lea.verou.me/2009/06/tip-multi-step-form-handling/#comments</comments>
		<pubDate>Tue, 16 Jun 2009 05:02:07 +0000</pubDate>
		<dc:creator>Lea Verou</dc:creator>
				<category><![CDATA[Articles]]></category>
		<category><![CDATA[Tips]]></category>
		<category><![CDATA[forms]]></category>
		<category><![CDATA[HTML]]></category>
		<category><![CDATA[usability]]></category>

		<guid isPermaLink="false">http://leaverou.me/?p=346</guid>
		<description><![CDATA[First of all, sorry for my long absence! I haven&#8217;t abandoned this blog, I was just really, really busy. I&#8217;m still busy, and this probably won&#8217;t change soon. However, I will still blog when I get too fed up with work or studying (this is one of these moments&#8230;). Now, let&#8217;s get to the meat. [...]]]></description>
			<content:encoded><![CDATA[
<div class="topsy_widget_data topsy_theme_hot-pink" style="float: right;margin-left: 0.75em; background: url(data:,%7B%20%22url%22%3A%20%22http%253A%252F%252Flea.verou.me%252F2009%252F06%252Ftip-multi-step-form-handling%252F%22%2C%20%22style%22%3A%20%22big%22%2C%20%22title%22%3A%20%22Tip%3A%20Multi-step%20form%20handling%22%20%7D);"></div>
<p>First of all, sorry for my long absence! I haven&#8217;t abandoned this blog, I was just really, really busy. I&#8217;m still busy, and this probably won&#8217;t change soon. However, I will still blog when I get too fed up with work or studying (this is one of these moments&#8230;). Now, let&#8217;s get to the meat.</p>
<h3>The situation</h3>
<p>In most web applications, even the simplest ones, the need for form handling will arise. There will be forms that need to be submitted, checked, processed or returned to the user informing them about any errors. A good empirical rule I try to follow is &#8220;Try not to produce URLs that don&#8217;t have a meaning if accessed directly&#8221;. It sounds simple and common-sense, doesn&#8217;t it? However, as Francois Voltaire said, &#8220;common sense is not so common&#8221;. I&#8217;ve seen variations of the following scenario several times, in several websites or even commercial web application software:<span id="more-346"></span></p>
<p>Lets assume we have a two step process, like a registration form with an arguably¹ bad usability. The hypothetical script is called register.php (PHP is just an example here, the exact language doesn&#8217;t matter, it could be register.jsp or anything else). The user fills in the information required for the first step, and if they get it right, they advance to something like register.php?step=2 to complete the rest of the information. They fill in their information there as well, and submit the form. Everything is fine.</p>
<h3>Or is it?</h3>
<p>What we have done this way is that we have effectively created a completely useless URL. If someone tries to access register.php?step=2 directly (via their history for instance), we don&#8217;t have the POST data from the first step, so we either have to redirect them to the first step or, even worse, assume they are actually coming from the first step and present it to them full of errors telling them they got everything wrong. In both cases we have duplicate content, and in the second one, usability suffers a great deal.</p>
<p>So, the right way is to pass step=2 via POST as well. This way, the URL stays as it was (register.php) and we avoid all the problems mentioned above. So, we end up doing something like this:</p>
<pre>... form fields here ...
&lt;input type="hidden" name="step" value="2" /&gt;
&lt;input type="submit" value="Create my account" /&gt;</pre>
<h3>Now we&#8217;re done. Or not?</h3>
<p>This works fine. However, there&#8217;s still room for improvement. We could get rid of the extra input element by utilizing the submit button. Yeah, it&#8217;s a form element too, even though we often overlook that and just focus on styling it. If we give it a name, it will get sent along with the other form fields. So instead of the html above, we can do that:</p>
<pre>... form fields here ...
&lt;input type="submit" name="step" value="2" /&gt;</pre>
<h3>But wait! What the f*ck is that<br />
<input name="step" type="submit" value="2" />???</h3>
<p>Now usability suffers! Instead of our nice &#8220;Create my account&#8221; button, the user now sees a cryptic &#8220;2&#8243;. Who cares if it works or if it requires less code, if nobody understands how to register, right? Luckily for us, we don&#8217;t <strong>have </strong>to use the <code>&lt;input /&gt;</code> tag to create submit buttons. A better (in terms of styling, semantics, markup clarity etc), albeit less known, alternative exists: The <code>&lt;button /&gt;</code> tag. When using the <code>&lt;button /&gt;</code> tag, the label of the button is derived from the markup within the start and end tags (yeah, we can also have html elements in there, not only text nodes, in case you&#8217;re wondering), not from the value attribute. So, we can set it&#8217;s name and value attributes to whatever we want, and the user won&#8217;t notice a thing:</p>
<pre>... form fields here ...
&lt;button type="submit" name="step" value="2"&gt;Create my account&lt;/button&gt;</pre>
<p>It&#8217;s really simple, although not done often. I guess it&#8217;s one of these &#8220;OMG how come I&#8217;ve never thought about this??&#8221; kind of things. <img src='http://lea.verou.me/wp-includes/images/smilies/icon_razz.gif' alt=':P' class='wp-smiley' /> </p>
<p>¹ <span style="font-size:smaller">I firmly believe we should eliminate the number of steps required in any procedure and especially in registration forms that users are bored to fill in anyway. However, there&#8217;s an exception to that: If the form <strong>has </strong>to be big for some reason, breaking it into steps actually makes it <strong>more </strong>usable, since the user is not overwhelmed with all these fields. Another situation when this approach is favorable is when the second step is determined according to the data from the first, although thanks to JavaScript and Ajax, this is becoming obsolete nowadays.</span></p>

]]></content:encoded>
			<wfw:commentRss>http://lea.verou.me/2009/06/tip-multi-step-form-handling/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Extend Math.log to allow for bases != e</title>
		<link>http://lea.verou.me/2009/03/extend-mathlog-to-allow-for-bases-e/</link>
		<comments>http://lea.verou.me/2009/03/extend-mathlog-to-allow-for-bases-e/#comments</comments>
		<pubDate>Thu, 26 Mar 2009 01:56:38 +0000</pubDate>
		<dc:creator>Lea Verou</dc:creator>
				<category><![CDATA[Tips]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[Math]]></category>

		<guid isPermaLink="false">http://leaverou.me/?p=265</guid>
		<description><![CDATA[As Math.log currently stands, it&#8217;s a bit useless. It only calculates natural logarithms (base e).  We can easily modify it however, to calculate logarithms of any base: Math.log = (function() { var log = Math.log; return function(n, a) { return log(n)/(a? log(a) : 1); } })(); We can now specify the base as a second [...]]]></description>
			<content:encoded><![CDATA[
<div class="topsy_widget_data topsy_theme_hot-pink" style="float: right;margin-left: 0.75em; background: url(data:,%7B%20%22url%22%3A%20%22http%253A%252F%252Flea.verou.me%252F2009%252F03%252Fextend-mathlog-to-allow-for-bases-e%252F%22%2C%20%22style%22%3A%20%22big%22%2C%20%22title%22%3A%20%22Extend%20Math.log%20to%20allow%20for%20bases%20%21%3D%20e%22%20%7D);"></div>
<p>As Math.log currently stands, it&#8217;s a bit useless. It only calculates natural logarithms (base e).  We can easily modify it however, to calculate logarithms of any base:</p>
<pre>Math.log = (function() {
	var log = Math.log;
	return function(n, a) {
		return log(n)/(a? log(a) : 1);
	}
})();</pre>
<p>We can now specify the base as a second parameter, or still use the default one (Math.E) if we don&#8217;t specify one, so older scripts won&#8217;t break or if we want a shortcut to the natural logarithm. <img src='http://lea.verou.me/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> </p>

]]></content:encoded>
			<wfw:commentRss>http://lea.verou.me/2009/03/extend-mathlog-to-allow-for-bases-e/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Mockup viewer bookmarklet</title>
		<link>http://lea.verou.me/2009/03/mockup-viewer-bookmarklet/</link>
		<comments>http://lea.verou.me/2009/03/mockup-viewer-bookmarklet/#comments</comments>
		<pubDate>Wed, 18 Mar 2009 21:23:37 +0000</pubDate>
		<dc:creator>Lea Verou</dc:creator>
				<category><![CDATA[Original]]></category>
		<category><![CDATA[Tips]]></category>
		<category><![CDATA[bookmarklets]]></category>
		<category><![CDATA[JavaScript]]></category>

		<guid isPermaLink="false">http://leaverou.me/?p=241</guid>
		<description><![CDATA[I usually view mockups in a browser, so that the impression I get is as close as possible to reality (I learned this the hard way: A mockup that seemed great in the neutral and minimalistic environment of a picture viewer, ended up looking way too fancy when viewed in a browser, something that I [...]]]></description>
			<content:encoded><![CDATA[
<div class="topsy_widget_data topsy_theme_hot-pink" style="float: right;margin-left: 0.75em; background: url(data:,%7B%20%22url%22%3A%20%22http%253A%252F%252Flea.verou.me%252F2009%252F03%252Fmockup-viewer-bookmarklet%252F%22%2C%20%22style%22%3A%20%22big%22%2C%20%22title%22%3A%20%22Mockup%20viewer%20bookmarklet%22%20%7D);"></div>
<p>I usually view mockups in a browser, so that the impression I get is as close as possible to reality (I learned this the hard way: A mockup that seemed great in the neutral and minimalistic environment of a picture viewer, ended up looking way too fancy when viewed in a browser, something that I realized after having worked for 6 months on the site). If you do the same, I&#8217;m sure you&#8217;ll feel my pain: Every time I do that, I have to carefully scroll down just as much as to hide the margin that the browser adds, and left just as much as to center the image. Not to mention the click required to enlarge the image to full-size.</p>
<p>Not any more!<span id="more-241"></span> I was so fed up today, that I wrote a little bookmarklet that does this. It enlarges the image to full size, removes the margins and scrolls the page left so that the image is centered. It works on any recent browser I&#8217;ve tested, and I think it will probably work in most browsers that web designers use (hint: not old IEs) <img src='http://lea.verou.me/wp-includes/images/smilies/icon_razz.gif' alt=':P' class='wp-smiley' /> </p>
<p>Enjoy.</p>
<p><a href="javascript:(function(){%20document.body.style.margin%20=%200;%20var%20inner%20=%20window.innerWidth%20||%20document.body.clientWidth,%20img%20=%20document.getElementsByTagName('img')[0];%20img.removeAttribute('width');%20img.removeAttribute('height');%20document.body.scrollLeft%20=%20(img.offsetWidth%20-%20inner)/2;%20})();">Mockup viewer</a></p>
<p>JS code:</p>
<pre>(function(){
	document.body.style.margin = 0;
	var inner = window.innerWidth || document.body.clientWidth, img = document.getElementsByTagName('img')[0];
	img.removeAttribute('width');
	img.removeAttribute('height');
	document.body.scrollLeft = (img.offsetWidth - inner)/2;
})();</pre>
<p>If only it could also write the XHTML &amp; CSS for the site&#8230; <img src='http://lea.verou.me/wp-includes/images/smilies/icon_razz.gif' alt=':P' class='wp-smiley' /> </p>

]]></content:encoded>
			<wfw:commentRss>http://lea.verou.me/2009/03/mockup-viewer-bookmarklet/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Check whether the browser supports RGBA (and other CSS3 values)</title>
		<link>http://lea.verou.me/2009/03/check-whether-the-browser-supports-rgba-and-other-css3-values/</link>
		<comments>http://lea.verou.me/2009/03/check-whether-the-browser-supports-rgba-and-other-css3-values/#comments</comments>
		<pubDate>Sun, 01 Mar 2009 14:44:14 +0000</pubDate>
		<dc:creator>Lea Verou</dc:creator>
				<category><![CDATA[Original]]></category>
		<category><![CDATA[Tips]]></category>
		<category><![CDATA[colors]]></category>
		<category><![CDATA[CSS3]]></category>
		<category><![CDATA[CSS3 values]]></category>
		<category><![CDATA[feature detection]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[RGBA]]></category>

		<guid isPermaLink="false">http://leaverou.me/?p=203</guid>
		<description><![CDATA[When using CSS, we can just include both declarations, one using rgba, and one without it, as mentioned in my post on cross-browser RGBA backgrounds. When writing JavaScript however, it&#8217;s a waste of resources to do that (and requires more verbose code), since we can easily check whether the browser is RGBA-capable, almost as easily [...]]]></description>
			<content:encoded><![CDATA[
<div class="topsy_widget_data topsy_theme_hot-pink" style="float: right;margin-left: 0.75em; background: url(data:,%7B%20%22url%22%3A%20%22http%253A%252F%252Flea.verou.me%252F2009%252F03%252Fcheck-whether-the-browser-supports-rgba-and-other-css3-values%252F%22%2C%20%22style%22%3A%20%22big%22%2C%20%22title%22%3A%20%22Check%20whether%20the%20browser%20supports%20RGBA%20%28and%20other%20CSS3%20values%29%22%20%7D);"></div>
<p>When using CSS, we can just include both declarations, one using rgba, and one without it, as mentioned in my post on <a href="http://lea.verou.me/2009/02/bulletproof-cross-browser-rgba-backgrounds/" target="_blank">cross-browser RGBA backgrounds</a>. When writing JavaScript however, it&#8217;s a waste of resources to do that (and requires more verbose code), since we can easily check whether the browser is RGBA-capable, almost as easily as we can check <a href="http://lea.verou.me/2009/02/check-if-a-css-property-is-supported/" target="_blank">whether it suppports a given property</a>. We can even follow the same technique to detect the support of other CSS3 values (for instance, <a href="http://www.css3.info/preview/multiple-backgrounds/">multiple backgrounds</a> support, <a href="http://www.css3.info/preview/hsla/" target="_blank">HSLA</a> support, etc).<span id="more-203"></span></p>
<p>The technique I&#8217;m going to present is based on the fact that when we assign a non-supported CSS value on any supported CSS property, the browser either throws an error AND ignores it (IE-style), or simply ignores it (Firefox-style). Concequently, to check whether RGBA is supported, the algorithm would be:</p>
<ol>
<li>Get the <code>color</code> (or <code>backgroundColor</code>, or <code>borderColor</code> or any property that is widely supported and accepts color values) value of the <code>style</code> object of any element that exists in the page for sure (for instance, the first script tag) and store it in a variable.</li>
<li>Assign an RGBA color to the <code>color</code> property of that element and catch any errors produced.</li>
<li>Assign to a variable whether the <code>color</code> of that element did change (boolean <code>true</code> or <code>false</code>).</li>
<li>Restore the previous color to the <code>color</code> property, so that our script doesn&#8217;t interfere with the designer&#8217;s decisions.</li>
<li>Return the stored result.</li>
</ol>
<p>and it would result in the following code:</p>
<pre>function supportsRGBA()
{
	var scriptElement = document.getElementsByTagName('script')[0];
	var prevColor = scriptElement.style.color;
	try {
		scriptElement.style.color = 'rgba(1,5,13,0.44)';
	} catch(e) {}
	var result = scriptElement.style.color != prevColor;
	scriptElement.style.color = prevColor;
	return result;
}</pre>
<h3>Performance improvements</h3>
<p>The code above works, but it wastes resources for no reason. Every time the function is called, it tests RGBA support again, even though the result will never change. So, we need a way to cache the result, and return the cached result after the first time the function is called.</p>
<p>This can be achieved in many ways. My personal preference is to store the result as a property of the function called, named <code>'result'</code>:</p>
<pre>function supportsRGBA()
{
	if(!('result' in arguments.callee))
	{
		var scriptElement = document.getElementsByTagName('script')[0];
		var prevColor = scriptElement.style.color;
		try {
			scriptElement.style.color = 'rgba(0, 0, 0, 0.5)';
		} catch(e) {}
		arguments.callee.result = scriptElement.style.color != prevColor;
		scriptElement.style.color = prevColor;
	}
	return arguments.callee.result;
}</pre>
<h3>Making it bulletproof</h3>
<p>There is a rare case where the script element might <strong>already</strong> have <code>rgba(0,0,0,0.5)</code> set as it&#8217;s color value (don&#8217;t ask me why would someone want to do that <img src='http://lea.verou.me/wp-includes/images/smilies/icon_razz.gif' alt=':P' class='wp-smiley' />  ), in which case our function will return <code>false</code> even if the browser actually supports RGBA. To prevent this, you might want to check whether the <code>color</code> property is already set to <code>rgba(0,0,0,0.5)</code> and return <code>true</code> if it is (because if the browser doesn&#8217;t support rgba, it will be blank):</p>
<pre>function supportsRGBA()
{
	if(!('result' in arguments.callee))
	{
		var scriptElement = document.getElementsByTagName('script')[0];
		var prevColor = scriptElement.style.color;
		var testColor = 'rgba(0, 0, 0, 0.5)';
		if(prevColor == testColor)
		{
			arguments.callee.result = true;
		}
		else
		{
			try {
				scriptElement.style.color = testColor;
			} catch(e) {}
			arguments.callee.result = scriptElement.style.color != prevColor;
			scriptElement.style.color = prevColor;
		}
	}
	return arguments.callee.result;
}</pre>
<p>Done!</p>

]]></content:encoded>
			<wfw:commentRss>http://lea.verou.me/2009/03/check-whether-the-browser-supports-rgba-and-other-css3-values/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Quick &amp; dirty way to run snippets of JavaScript anywhere</title>
		<link>http://lea.verou.me/2009/02/quick-dirty-way-to-run-snippets-of-javascript-anywhere/</link>
		<comments>http://lea.verou.me/2009/02/quick-dirty-way-to-run-snippets-of-javascript-anywhere/#comments</comments>
		<pubDate>Mon, 23 Feb 2009 07:41:52 +0000</pubDate>
		<dc:creator>Lea Verou</dc:creator>
				<category><![CDATA[Tips]]></category>
		<category><![CDATA[debugging]]></category>
		<category><![CDATA[JavaScript]]></category>

		<guid isPermaLink="false">http://leaverou.me/?p=172</guid>
		<description><![CDATA[Ever wanted to run a snippet of JavaScript on a browser that doesn&#8217;t support a console in order to debug something? (for instance, IE6, Opera etc) You probably know about Firebug Lite, but this either requires you to already have the bookmarklet, or include the script in the page. Although Firebug Lite is a great [...]]]></description>
			<content:encoded><![CDATA[
<div class="topsy_widget_data topsy_theme_hot-pink" style="float: right;margin-left: 0.75em; background: url(data:,%7B%20%22url%22%3A%20%22http%253A%252F%252Flea.verou.me%252F2009%252F02%252Fquick-dirty-way-to-run-snippets-of-javascript-anywhere%252F%22%2C%20%22style%22%3A%20%22big%22%2C%20%22title%22%3A%20%22Quick%20%26%20dirty%20way%20to%20run%20snippets%20of%20JavaScript%20anywhere%22%20%7D);"></div>
<p>Ever wanted to run a snippet of JavaScript on a browser that doesn&#8217;t support a console in order to debug something? (for instance, IE6, Opera etc)</p>
<p>You probably know about <a href="http://getfirebug.com/lite.html" target="_blank">Firebug Lite</a>, but this either requires you to already have the bookmarklet, or include the script in the page. Although Firebug Lite is a great tool for more in depth debugging, it can be tedious for simple tasks (eg. <em>&#8220;What&#8217;s the value of that property?&#8221;</em>).</p>
<p>Fortunately, there is a simpler way. <span id="more-172"></span>Do you remember the 2000 era and the <code>javascript:</code> URIs? Did you know that they also work from the address bar of <strong>any</strong> javascript-capable browser?</p>
<p>For instance, to find out the value of the global variable <code>foo</code>, you just type in the address bar <code>javascript:alert(foo)</code>. You can write any code you wish after the <code>javascript:</code> part, as long as you write it properly to fit in one line.</p>
<p>Of course these URIs are a no-no for websites, but they can be handy for simple debugging in browsers that don&#8217;t support a console. <img src='http://lea.verou.me/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> </p>

]]></content:encoded>
			<wfw:commentRss>http://lea.verou.me/2009/02/quick-dirty-way-to-run-snippets-of-javascript-anywhere/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Check whether a CSS property is supported</title>
		<link>http://lea.verou.me/2009/02/check-if-a-css-property-is-supported/</link>
		<comments>http://lea.verou.me/2009/02/check-if-a-css-property-is-supported/#comments</comments>
		<pubDate>Tue, 10 Feb 2009 22:42:45 +0000</pubDate>
		<dc:creator>Lea Verou</dc:creator>
				<category><![CDATA[Tips]]></category>
		<category><![CDATA[CSS properties]]></category>
		<category><![CDATA[feature detection]]></category>
		<category><![CDATA[JavaScript]]></category>

		<guid isPermaLink="false">http://leaverou.me/?p=3</guid>
		<description><![CDATA[Sometimes when using JavaScript, you need to determine whether a certain CSS property is supported by the current browser or not. For instance when setting opacity for an element, you need to find out whether the property that the browser supports is opacity, -moz-opacity (MozOpacity), -khtml-opacity (KhtmlOpacity) or the IE proprietary filter. Instead of performing [...]]]></description>
			<content:encoded><![CDATA[
<div class="topsy_widget_data topsy_theme_hot-pink" style="float: right;margin-left: 0.75em; background: url(data:,%7B%20%22url%22%3A%20%22http%253A%252F%252Flea.verou.me%252F2009%252F02%252Fcheck-if-a-css-property-is-supported%252F%22%2C%20%22style%22%3A%20%22big%22%2C%20%22title%22%3A%20%22Check%20whether%20a%20CSS%20property%20is%20supported%22%20%7D);"></div>
<p>Sometimes when using JavaScript, you need to determine whether a certain CSS property is supported by the current browser or not. For instance when setting opacity for an element, you need to find out whether the property that the browser supports is <code>opacity</code>, <code>-moz-opacity</code> (<code>MozOpacity</code>), <code>-khtml-opacity</code> (<code>KhtmlOpacity</code>) or the IE proprietary <code>filter</code>.</p>
<p>Instead of performing a forwards <strong>in</strong>compatible browser detect, you can easily check which property is supported with a simple conditional. <span id="more-3"></span>The only thing you&#8217;ll need is a DOM element that exists for sure. A DOM element that exists in every page and is also easily accessible via JS (no need for <code>getElementsByTagName</code>), is the <code>body</code> element, but you could use the <code>&lt;head&gt;</code> or even a <code>&lt;script&gt;</code> tag (since there is a script running in the page, a &lt;script&gt; tag surely exists). <strong>In this article we&#8217;ll use document.body, but it&#8217;s advised that you use the head or script elements, since document.body may not exist at the time your script is run.</strong></p>
<p>So, now that we have an element to test at, the test required is:</p>
<pre>if('opacity' in document.body.style)
{
	// do stuff
}</pre>
<p>Of course you&#8217;d change <code>document.body</code> with a reference to the element you&#8217;d like to test at (in case it&#8217;s not the body tag)  and <code>'opacity'</code> with the name of the actual property you want to test. You can even wrap up a function to use when you want to check about the support of a certain property:</p>
<pre>function isPropertySupported(property)
{
	return property in document.body.style;
}</pre>
<p>The only thing you should pay attention to, is using the JavaScript version of the CSS property (for example <code>backgroundColor</code> instead of <code>background-color</code>)</p>
<p>Wasn&#8217;t it easy?</p>

]]></content:encoded>
			<wfw:commentRss>http://lea.verou.me/2009/02/check-if-a-css-property-is-supported/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
	</channel>
</rss>

