<?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>User Agent Man &#187; CSS</title>
	<atom:link href="http://www.useragentman.com/blog/category/technologies/css/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.useragentman.com/blog</link>
	<description>A Blog about Client Side Web Technology</description>
	<lastBuildDate>Wed, 08 Sep 2010 14:02:09 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>Giving Challenged @font-face Fonts The Italics Makeover</title>
		<link>http://www.useragentman.com/blog/2010/09/07/giving-challenged-font-face-fonts-the-italics-makeover/</link>
		<comments>http://www.useragentman.com/blog/2010/09/07/giving-challenged-font-face-fonts-the-italics-makeover/#comments</comments>
		<pubDate>Wed, 08 Sep 2010 03:59:34 +0000</pubDate>
		<dc:creator>zoltan</dc:creator>
				<category><![CDATA[@font-face]]></category>
		<category><![CDATA[CSS]]></category>
		<category><![CDATA[CSS3]]></category>
		<category><![CDATA[Fonts]]></category>

		<guid isPermaLink="false">http://www.useragentman.com/blog/?p=1560</guid>
		<description><![CDATA[Have a font that doesn't have an italics variant? There is a simple solution to this problem using CSS3 Transforms.]]></description>
			<content:encoded><![CDATA[<p>See the nice italicized text in the heading of this article?  It is not so obvious to style as you may think.  There are many great free and commercial fonts out there that allow @font-face embedding.  Unfortunately, not all of them have an italics variant for one reason or another.  For example, the font I use for my blogs headlines, <a href="http://www.fonts.info/info/press/free-fonts-for-font-face-embedding.htm">Graublau Sans Web</a>, does not have a true italics variant included with it, while the font I use for the blog&#8217;s copy, <a href="http://www.fontsquirrel.com/fonts/Droid-Sans">Droid Sans</a>, does.  When a font doesn&#8217;t have an italics variant, and you try to italicize it in a web-page using <code>&lt;i&gt;</code> and <code>&lt;em&gt;</code> tags, the results depend on the browser you are using:</p>
<ul>
<li>Safari, Chrome and Opera will render the font normally <strong>without</strong> italicizing it.</li>
<li>Internet Explorer and Firefox will attempt to italicize the font, but Internet Explorer will slant the font at a 20&deg; angle while Firefox implements a 10&deg; slant.</li>
</ul>
<table class="dataTable">
<thead>
<tr>
<th style="width: 33%">Internet Explorer 6.0</th>
<th style="width: 33%">Firefox 3.6</th>
<th style="width: 33%">Safari 4/Chrome 5/Opera 10</th>
</tr>
</thead>
<tbody>
<tr>
<td><a href="http://www.useragentman.com/blog/wp-content/uploads/2010/09/badItalics-IE.png"><img src="http://www.useragentman.com/blog/wp-content/uploads/2010/09/badItalics-IE.png" alt="Default Italics for Graublau Web in IE." width="106" height="38" class="alignnone size-full wp-image-1567" /></a></td>
<td><img src="http://www.useragentman.com/blog/wp-content/uploads/2010/09/badItalics-firefox.png" alt="Default Italics for Graublau Web in Firefox." width="106" height="38" class="alignnone size-full wp-image-1567" /></td>
<td><img src="http://www.useragentman.com/blog/wp-content/uploads/2010/09/badItalics-safari.png" alt="Default Italics for Graublau Web in Safari." width="106" height="38" class="alignnone size-full wp-image-1567" /></td>
</tr>
</table>
<p>This inconsistency is quite annoying from a user-experience point of view.  However, there is a way of leveling the playing field by using the CSS3 <code>transform</code> property.  And even though Internet Explorer doesn&#8217;t recognize <code>transform</code>, there is a workaround <strong>without using JavaScript</strong> using the <code>Matrix</code> Visual Filter.</p>
<h2>The CSS</h2>
<p>When I was a calligraphy student, I learned that the Italics script was to be written on a 10&deg; angle.  Using the CSS3 <code>transform</code> property, this can be achieved by using the <code>skewX()</code> function:</p>
<blockquote class="code">
<pre>
#content h1 em {
    font-family: "Graublau Web Bold", Arial, sans-serif;
    font-style: normal;

<span class="hilite">    -moz-transform:    skewX(-10deg);
    -o-transform:      skewX(-10deg);
    -webkit-transform: skewX(-10deg);
    transform:         skewX(-10deg);

    display: inline-block;
</span>
}
	</pre>
</blockquote>
<p>The <code>display: inline-block</code> is there since Safari, Chrome and Opera will not allow transformations on an <code>inline</code> element.</p>
<p>But what about Internet Explorer?  I <strong>could</strong> use my cssSandpaper script, but it is <strong>really</strong> simple to do without it:</p>
<blockquote class="code">
<pre>
#content h1 em {
    font-family: "Graublau Web Bold", Arial, sans-serif;
    font-style: normal;

    -moz-transform:    skewX(-10deg);
    -o-transform:      skewX(-10deg);
    -webkit-transform: skewX(-10deg);
    transform:         skewX(-10deg);

    display: inline-block;

<span class="hilite">    /* IE8+: must be on one line. */
    -ms-filter: "progid:DXImageTransform.Microsoft.Matrix(M11=1, M12=-0.1763269807084645, M21=0, M22=1, SizingMethod='auto expand')";

    /* IE6 and 7 */
    filter: progid:DXImageTransform.Microsoft.Matrix(
             M11=1,
             M12=-0.1763269807084645,
             M21=0,
             M22=1,
             SizingMethod='auto expand');</span>
}
	</pre>
</blockquote>
<p>This is the Microsoft <code>Matrix</code> Filter equivalent of <code>skewX(10deg)</code>. Let&#8217;s take a look at how this works in all the major browsers:</p>
<table class="dataTable">
<thead>
<tr>
<th style="width: 20%">Internet Explorer 6.0+</th>
<th style="width: 20%">Firefox 3.5+</th>
<th style="width: 20%">Safari 4+</th>
<th style="width: 20%">Chrome 5+</th>
<th style="width: 20%">Opera 10.5+</th>
</tr>
</thead>
<tbody>
<tr>
<td><img src="http://www.useragentman.com/blog/wp-content/uploads/2010/09/goodItalics-IE.png" alt="Italics Fix in IE" width="106" height="39" class="alignnone size-full wp-image-1574" /></td>
<td><img src="http://www.useragentman.com/blog/wp-content/uploads/2010/09/goodItalics-firefox.png" alt="Italics Fix in Firefox" width="106" height="39" class="alignnone size-full wp-image-1574" /></td>
<td><img src="http://www.useragentman.com/blog/wp-content/uploads/2010/09/goodItalics-safari.png" alt="Italics Fix in Safari" width="106" height="39" class="alignnone size-full wp-image-1574" /></td>
<td><img src="http://www.useragentman.com/blog/wp-content/uploads/2010/09/goodItalics-chrome.png" alt="Italics Fix in Chrome" width="106" height="39" class="alignnone size-full wp-image-1574" /></td>
<td><img src="http://www.useragentman.com/blog/wp-content/uploads/2010/09/goodItalics-opera.png" alt="Italics Fix in Opera" width="106" height="39" class="alignnone size-full wp-image-1574" /></td>
</tr>
</table>
<p>Isn&#8217;t that nice and consistent?  The screenshots are from the Windows versions of the browsers, but you can expect the same results for Mac and Linux versions as well.</p>
<h2>Is This <em>Really</em> Italics?</h2>
<p>Not really.  <strong>Technically, we are creating an oblique variant of this font.</strong> <a href="http://en.wikipedia.org/wiki/Oblique_type">The Wikipedia entry for Oblique fonts</a> states that many sans-serif fonts do not have italic variants, so this technique will be a useful one in a web developers toolkit.  </p>
<h2>Caveats</h2>
<ul>
<li>If you are going to make a few words in a row oblique, make sure you wrap the <code>&lt;i&gt;</code> and <code>&lt;em&gt;</code> tags around each word.  This is because the <code>display: inline-block</code> rule treats multiple words wrapped in one set of these tags as one word, which will cause problems when you want the words to wrap to the next line.<br />
<table>
<thead>
<tr>
<th>Wrapping multiple words with <strong>one</strong> <code>&lt;em&gt;</code> tag</th>
</tr>
</thead>
<tbody>
<tr>
<td><img src="http://www.useragentman.com/blog/wp-content/uploads/2010/09/badWordWrap.png" alt="Bad Word Wrap Example" title="badWordWrap" class="aligncenter size-full wp-image-1580" /></td>
</tr>
</tbody>
</table>
<table>
<thead>
<tr>
<th>Wrapping multiple words with separate <code>&lt;em&gt;</code> tags</th>
</tr>
</thead>
<tbody>
<tr>
<td>
<img src="http://www.useragentman.com/blog/wp-content/uploads/2010/09/goodWordWrap.png" alt="Good Word Wrap Example" title="goodWordWrap"  class="aligncenter size-full wp-image-1582" /></td>
</tr>
</tbody>
</table>
</li>
<li>If the oblique text does not have a solid background color, it will appear &#8220;blocky&#8221; or &#8220;pixely&#8221; in IE.  To fix this, you must use IE&#8217;s Chroma Visual Filter, as described in my last blog post, <a href="/blog/2010/09/02/how-to-make-cleartype-font-face-fonts-and-css-visual-filters-play-nicely-together/">How to Make ClearType, @font-face Fonts and CSS Visual Filters Play Nicely Together</a>.</li>
<li>In IE, multiple oblique words may appear to have more white space than the other browsers.  In this case you may need to create IE-only CSS rules that implement negative left margin on each word, using a solution like <a href="http://paulirish.com/2008/conditional-stylesheets-vs-css-hacks-answer-neither/">Paul Irish’s Conditional CSS Pattern</a>.</li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://www.useragentman.com/blog/2010/09/07/giving-challenged-font-face-fonts-the-italics-makeover/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>How to Make ClearType, @font-face Fonts and CSS Visual Filters Play Nicely Together</title>
		<link>http://www.useragentman.com/blog/2010/09/02/how-to-make-cleartype-font-face-fonts-and-css-visual-filters-play-nicely-together/</link>
		<comments>http://www.useragentman.com/blog/2010/09/02/how-to-make-cleartype-font-face-fonts-and-css-visual-filters-play-nicely-together/#comments</comments>
		<pubDate>Thu, 02 Sep 2010 20:49:51 +0000</pubDate>
		<dc:creator>zoltan</dc:creator>
				<category><![CDATA[CSS]]></category>
		<category><![CDATA[CSS3]]></category>
		<category><![CDATA[ClearType]]></category>
		<category><![CDATA[Fonts]]></category>
		<category><![CDATA[anti-aliased text]]></category>
		<category><![CDATA[blocky text]]></category>
		<category><![CDATA[cleartype]]></category>
		<category><![CDATA[CSS Filters]]></category>
		<category><![CDATA[IE]]></category>
		<category><![CDATA[ie filters]]></category>
		<category><![CDATA[Internet Explorers]]></category>
		<category><![CDATA[matrix]]></category>
		<category><![CDATA[opacity]]></category>
		<category><![CDATA[Visual Filters]]></category>

		<guid isPermaLink="false">http://www.useragentman.com/blog/?p=1526</guid>
		<description><![CDATA[Ever had a problem with using IE's Alpha Visual Filter and getting blocky text? A solution has been found, and it doesn't use JavaScript.  I expect to hear a sigh of relief from many developers.]]></description>
			<content:encoded><![CDATA[<div class="importantNotes">
<h3>Update (Sept. 3, 2010)</h3>
<ul>
<li>This article originally has the <code>filter</code> and <code>-ms-filter</code> rules reversed, which is against <a href="http://blogs.msdn.com/b/ie/archive/2008/09/08/microsoft-css-vendor-extensions.aspx">Microsoft&#8217;s best practices</a> for IE8.  This error has been corrected.</li>
<li>Thanks to <a href="http://www.zomigi.com">Zoe Mickley Gillenwater</a> for pointing out that IE8 needs to have all the filter functions (i.e. all the stuff in the double quotes) on one line.</li>
</ul>
</div>
<p>The modern web developer wants to be able to use a variety of CSS3 effects together, but sometimes doing this in IE can be challenging, especially if you don&#8217;t want to use JavaScript.  Let&#8217;s take a look a common problem with IE: blocky text in IE when using IE Visual filters on a block of text.  For example, if you&#8217;ve ever tried to do <a href="http://davidwalsh.name/css-opacity">the cross-browser CSS opacity trick</a> to implement opacity in IE6, this problem manifests itself when the <strong>text is styled with no background color</strong>.  A developer uses this well-known cross browser CSS-foo to make this work:</p>
<blockquote class="code">
<pre>
#badAntiAliasing {
	opacity: 0.5;

        /* IE 8 */
        -ms-filter: "progid:DXImageTransform.Microsoft.Alpha(opacity=50)";

        /* IE 6,7 */
	filter: progid:DXImageTransform.Microsoft.Alpha(opacity=50);

}
</pre>
</blockquote>
<p>And it looks great in every browser, but <strong>it looks like crap in Internet Explorer 6 with ClearType turned on.</strong>  It is especially pronounced when using @font-face embedding with fonts that assume that some sort of anti-aliasing technology is being used.  The example below uses <a href="http://www.fonts.info/info/press/free-fonts-for-font-face-embedding.htm">Graublau Sans Web</a> with the Alpha filter to illustrate the issue:</p>
<table class="dataTable" style="margin-top: 1em">
<thead>
<tr>
<th>Opacity Solution in IE</th>
<th>Opacity Solution in every other browser</th>
</tr>
</thead>
<tbody>
<tr>
<td><a href="http://www.useragentman.com/blog/wp-content/uploads/2010/09/firefoxAntiAlias.png"><img src="http://www.useragentman.com/blog/wp-content/uploads/2010/09/firefoxAntiAlias.png" alt="" title="firefoxAntiAlias" width="360" height="50" class="alignnone size-full wp-image-1529" /></a></td>
<td><a href="http://www.useragentman.com/blog/wp-content/uploads/2010/09/ieBadAntiAlias.png"><img src="http://www.useragentman.com/blog/wp-content/uploads/2010/09/ieBadAntiAlias.png" alt="" title="ieBadAntiAlias" width="361" height="58" class="alignnone size-full wp-image-1530" /></a></td>
</tr>
</tbody>
</table>
<p>This has been a known problem with IE ever since 2006 when ClearType&#8217;s product manager, Peter Gurevich, <a href="http://blogs.msdn.com/b/ie/archive/2006/08/31/730887.aspx">announced on the IEBlog their decision to disable ClearType on elements that use any Visual Filter</a>.  If you read the comments on this blog article, you will see many upset developers.  I was one of them, since this not only affects the <a href="http://msdn.microsoft.com/en-us/library/ms532967%28v=VS.85%29.aspx">Alpha Filter</a>, but all of <a href="http://msdn.microsoft.com/en-us/library/ms532849%28v=VS.85%29.aspx">IE&#8217;s Visual Filters</a>, some of which I use in cssSandpaper to get IE to support CSS3 properties like <code>transform</code>.</p>
<p>Today, however, we can rejoice.  There is a <strong>usable workaround that will make ClearType rendered fonts look nice when using Visual Filters</strong>.  In our example above, here is what needs to be done:</p>
<blockquote class="code">
<pre>
body.ie6 #goodAntiAliasing,
body.ie7 #goodAntiAliasing,
body.ie8 #goodAntiAliasing {
	background-color: <span class="hilite">white</span>;
}

#goodAntiAliasing {
	opacity: 0.5;

        /* IE 8: yes, it is ugly but it has to be on one line. :-( */
        -ms-filter: "progid:DXImageTransform.Microsoft.Chroma(<span class="hilite">color='white'</span>) progid:DXImageTransform.Microsoft.Alpha(opacity=50)";

        /* IE 6,7 is more flexible: it can be on multiple lines. */
	filter: progid:DXImageTransform.Microsoft.Chroma(<span class="hilite">color='white'</span>)
                progid:DXImageTransform.Microsoft.Alpha(opacity=50);

}
</pre>
</blockquote>
<p>What is going on here?</p>
<ol>
<li>The first rule sets the background color of the container to a color that is represents the color of the majority of the background image on the page.  <strong>Note that this rule uses <a href="http://paulirish.com/2008/conditional-stylesheets-vs-css-hacks-answer-neither/">Paul Irish&#8217;s Conditional CSS Pattern</a> to serve it only to IE.</strong></li>
<li>The second rule executes two of IE&#8217;s Visual Filters:
<ul>
<li>the Chroma Visual Filter, which tells IE &#8220;Please make this color transparent&#8221;.  The color we choose to make transparent is the one we selected in the first rule.</li>
<li>the Alpha Visual Filter, which does the opacity trick</li>
</ul>
</ol>
<table class="dataTable">
<thead>
<tr>
<th>New Opacity Solution in IE</th>
<th>New Opacity Solution in every other browser</th>
</tr>
</thead>
<tbody>
<tr>
<td><a href="http://www.useragentman.com/blog/wp-content/uploads/2010/09/ieBadAntiAlias.png"><img src="http://www.useragentman.com/blog/wp-content/uploads/2010/09/ieBadAntiAlias.png" alt="" title="ieBadAntiAlias" width="361" height="58" class="alignnone size-full wp-image-1530" /></a></td>
<td><a href="http://www.useragentman.com/blog/wp-content/uploads/2010/09/ieBadAntiAlias.png"><img src="http://www.useragentman.com/blog/wp-content/uploads/2010/09/ieGoodAntiAlias.png" alt="" title="ieBadAntiAlias" width="361" height="58" class="alignnone size-full wp-image-1530" /></a></td>
</tr>
</tbody>
</table>
<p><strong>These filters must be in the order given</strong>, and not the other way around.  Note also that care must be given that <strong>the color chosen to be transparent should be the average color of what is behind the text being styled</strong>, otherwise you will get less than optimal results.  If this background is a complex image, just use the colour that&#8217;s used most often.</p>
<p>This fixes not only the IE opacity problem, but also issues with other Visual Filters, such as <code>Matrix</code>.  I will be updating cssSandpaper to use this trick so that transformed objects in IE won&#8217;t look so blocky.</p>
<p><a class="exampleLink" href="/tests/fontFacePlusFilter/">Take a look at the solution in action (make sure you look at it in IE in order to get the full effect)</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.useragentman.com/blog/2010/09/02/how-to-make-cleartype-font-face-fonts-and-css-visual-filters-play-nicely-together/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>Coding Colors Easily Using CSS3 hsl() Notation</title>
		<link>http://www.useragentman.com/blog/2010/08/28/coding-colors-easily-using-css3-hsl-notation/</link>
		<comments>http://www.useragentman.com/blog/2010/08/28/coding-colors-easily-using-css3-hsl-notation/#comments</comments>
		<pubDate>Sat, 28 Aug 2010 12:11:59 +0000</pubDate>
		<dc:creator>zoltan</dc:creator>
				<category><![CDATA[CSS]]></category>
		<category><![CDATA[CSS3]]></category>

		<guid isPermaLink="false">http://www.useragentman.com/blog/?p=1443</guid>
		<description><![CDATA[The seemingly impossible task of coming up with color codes off the top of your head can be done easily using CSS3's <code>hsl</code> color notation.  Read how you can use this "human-friendly" and how it can work in the few browsers that don't support it natively.]]></description>
			<content:encoded><![CDATA[<div >
<dl id="attachment_822" class="wp-caption alignleft" style="width: 267px;">
<dt class="wp-caption-dt"><img class="size-full wp-image-822 " title="CSS3 Cube Layout Example" src="http://www.useragentman.com/blog/wp-content/uploads/2010/08/colorWheel.jpg" alt="" width="257" /></dt>
<dd class="wp-caption-dd">
<div>A remix of <a href="http://commons.wikimedia.org/wiki/File:Bezold_Farbentafel_1874.jpg">Wilhelm von Bezold&#8217;s Farbentafel Color Wheel</a>.</div>
</dd>
</dl>
</div>
<p>I have heard stories about <strong>&#8220;<em>those</em> people&#8221;</strong> that see a color and can immediately calculate the RGB hex code in their head, without any tool whatsoever. Since I have never actually met one of <strong>&#8220;<em>those</em> people&#8221;</strong>, I don&#8217;t think they really exist.  However, it <strong>would</strong> be a nice skill to have, especially when presenting a new design to a client and they say &#8220;Perfect!  But can make that box a brighter orange, and that do-hickey a less pronounced purple?  And can you show us how it would look <strong>right now?</strong>&#8221;   </p>
<p>This seemingly impossible task of coming up with color codes off the top of your head can be done using CSS3&#8242;s <code>hsl</code> color notation.  <strong>HSL is a much more &#8220;human-friendly&#8221; notation than RGB</strong>, and with it you can code colors in your head easily <strong>without using The GIMP&#8217;s or Photoshop&#8217;s color-wheel</strong>.</p>
<p>Don&#8217;t believe me?  Read on and I&#8217;ll show you.  You&#8217;ll never want to go back to RGB again.  And even though IE 6-8 doesn&#8217;t support HSL natively, we will cover how support can be added.</p>
<h2>Notation</h2>
<p>An HSL color code uses the following CSS3 notation:</p>
<blockquote class="code"><pre>#myObject {
    background: <span class="hilite">hsl(120, 50%, 50%);</span>
}
</pre>
</blockquote>
<p>The first number is the <strong>hue</strong>, the second number is the <strong>saturation</strong>, and the third is the <strong>lightness</strong>.  These three terms are what HSL stand for.  But how does one manipulate these numbers?  Easily.</p>
<h2>Step 1: &#8220;Young Guys Can Be Messy Rascals&#8221;</h2>
<div id="colorwheel">
<div class="testBlock"  id="o1">
				<span><strong>R</strong>ed: 360&deg;</span>
			</div>
<div class="testBlock"  id="o2">
				<span><strong>Y</strong>ellow: 60&deg;</span>
			</div>
<div class="testBlock"  id="o3">
				<span><strong>G</strong>reen: 120&deg;</span>
			</div>
<div class="testBlock"  id="o4">
				<span><strong>C</strong>yan: 180&deg;</span>
			</div>
<div class="testBlock"  id="o5">
				<span><strong>B</strong>lue: 240&deg;</span>
			</div>
<div class="testBlock"  id="o6">
				<span><strong>M</strong>agenta: 300&deg;</span>
			</div>
<p>This diagram is not an image.  It was generated with HTML using <a href="/blog/2010/03/09/cross-browser-css-transforms-even-in-ie/">CSS3 <code>transform</code> functions</a> and <code>hsl()</code> color notation, and cssSandpaper for browsers that don&#8217;t support it.</p>
</p></div>
<p><strong>Memorize this sentence.</strong>  If you do, you will be to remember the six major colors in the HSV color wheel: Yellow, Green, Cyan, Blue, Magenta and Red.  <strong>These colors are spaced out by angles of 60 degrees.</strong>  If you follow the mnemonic, you should be able to remember the diagram on the left very easily.</p>
<p>So, if you want to pick green, you would code:</p>
<blockquote class="code smaller"><pre>hsl(<span class="hilite">120</span>, 100%, 50%)</pre>
</blockquote>
<p>(Don&#8217;t worry about the second and third parameters for now &#8230; we will get to that in a second).</p>
<p>Blue would be:</p>
<blockquote class="code smaller"><pre>hsl(<span class="hilite">240</span>, 100%, 50%)</pre>
</blockquote>
<p>Let&#8217;s say you wanted a color that is not on the wheel, like purple.  Purple is halfway between blue (240&deg;) and magenta (300&deg;) so we should pick something in between:</p>
<div class="example purple">background-color: hsl(270, 100%, 50%)</div>
<p>Think it should be more blue?  Choose an angle closer to blue!</p>
<div class="example bluePurple">background-color: hsl(255, 100%, 50%);</div>
<p>Want more magenta instead?  Then choose angle closer to magenta:</p>
<div class="example magentaPurple">background-color: hsl(285, 100%, 50%);</div>
<p>Note that you do not put use the <code>deg</code> keyword link in CSS3 Transforms.  It seems a little inconsistent, but at least it&#8217;s less typing. </p>
<h2>Step 2: Choose Your Intensity</h2>
<p>The second parameter is the colorfulness, or <a href="http://en.wikipedia.org/wiki/Colorfulness">saturation</a>.  The larger the percentage, the more &#8220;colorful&#8221; this color is.  Let&#8217;s take a look at what happens when we change green&#8217;s (120&deg;) colorfulness:</p>
<table id="colorfulnessTable" class="dataTable">
<thead>
<tr>
<th>hsl(120, 0%, 50%)</th>
<th>hsl(120, 25%, 50%)</th>
<th>hsl(120, 50%, 50%)</th>
<th>hsl(120, 75%, 50%)</th>
<th>hsl(120, 100%, 50%)</th>
</tr>
</thead>
<tbody>
<tr>
<td class="c1"></td>
<td class="c2"></td>
<td class="c3"></td>
<td class="c4"></td>
<td class="c5"></td>
</tr>
</table>
<p>The more the saturation percentage, the less &#8220;gray&#8221; it is.</p>
<h2>Step 3: Lighten it up</h2>
<p>The final step is to lighten up the color to the right level.  That can be done with the final parameter: </p>
<table id="brightnessTable" class="dataTable">
<thead>
<tr>
<th>hsl(120, 50%, 0%)</th>
<th>hsl(120, 50%, 25%)</th>
<th>hsl(120, 50%, 50%)</th>
<th>hsl(120, 50%, 75%)</th>
<th>hsl(120, 50%, 100%)</th>
</tr>
</thead>
<tbody>
<tr>
<td class="c1"></td>
<td class="c2"></td>
<td class="c3"></td>
<td class="c4"></td>
<td class="c5"></td>
</tr>
</table>
<h2>That&#8217;s It!</h2>
<p>Try it when you are using color on your next project.  <strong>You&#8217;ll find using the HSL color-space is <em>much</em> easier than using RGB</strong></p>
<h2>Browser Support</h2>
<p>HSL is supported in almost all the major browsers: Firefox 3.0+, Safari 3.0+, Chrome 5.0+, Opera 10+ and Explorer 9.0+.  <strong>For the browsers that don&#8217;t support it, I have updated <a href="/blog/2010/03/09/cross-browser-css-transforms-even-in-ie/">cssSandpaper</a> to implement HSL with the CSS <code>color</code> property</strong>, as well as the <code>border</code>, <code>background</code> family of properties (i.e. <code>border-right</code>, <code>background-color</code>, etc).</p>
<p>Also supported by cssSandpaper is HSLA, which is to HSL, as RGBA is to RGB.  It contains a fourth element, the alpha channel, that is a number from 0 to 1.  To illustrate how this works, let&#8217;s say an HTML element has a grid as a background image.  How can you shade that background image so that it looks, say, more green?</p>
<table id="alphaTable" class="dataTable">
<thead>
<tr>
<th>hsla(120, 50%, 50%, 0.25)</th>
<th>hsla(120, 50%, 50%, 0.5)</th>
<th>hsla(120, 50%, 50%, 0.75)</th>
<th>hsla(120, 50%, 50%, 1)</th>
</tr>
</thead>
<tbody>
<tr>
<td class="c2"></td>
<td class="c3"></td>
<td class="c4"></td>
<td class="c5"></td>
</tr>
</table>
<p>The table above is not an image &mdash; it is a real example of <code>hsla()</code> in action. Pretty neat, huh?  You can also use HSL and HSLA color in gradients.  The <a href="/blog/2010/03/09/cross-browser-css-transforms-even-in-ie/">cssSandpaper</a> library has supported WebKit style gradients with it&#8217;s own <code>-sand-gradient</code> property.  Now it supports HSLA gradients, like this 80% opaque yellow to 20% opaque blue linear gradient:</p>
<table id="gradientTable" class="dataTable">
<thead>
<tr>
<th>	-sand-gradient(linear, center top, center bottom, from(hsla(60, 100%, 50%, 0.8)), to(hsla(255, 100%, 50%, 0.2)));
</th>
</tr>
</thead>
<tbody>
<tr>
<td class="c1"></td>
</tr>
</tbody>
</table>
<p>Unfortunately, cssSandpaper only supports HSLA for background coloring only, not border or text coloring, but one would just use progressive enhancement to take care of that edge case, since what it does support, in my humble opinion, is rather cool.  Download the latest version from the link below and use HSL and HSLA color notation now!  You&#8217;ll impress your developer friends with your off-the-top-of-your-head color-coding skillz.  :-)</p>
<p><a class="exampleLink" href="/blog/2010/03/09/cross-browser-css-transforms-even-in-ie/">Get the latest version of cssSandpaper from the official documentation page</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.useragentman.com/blog/2010/08/28/coding-colors-easily-using-css3-hsl-notation/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>cssSandpaper Now Supports transform: translate() and rgba() Gradients</title>
		<link>http://www.useragentman.com/blog/2010/05/06/csssandpaper-now-supports-transform-translate-and-rgba-gradients/</link>
		<comments>http://www.useragentman.com/blog/2010/05/06/csssandpaper-now-supports-transform-translate-and-rgba-gradients/#comments</comments>
		<pubDate>Thu, 06 May 2010 22:01:22 +0000</pubDate>
		<dc:creator>zoltan</dc:creator>
				<category><![CDATA[CSS]]></category>
		<category><![CDATA[JavaScript]]></category>

		<guid isPermaLink="false">http://www.useragentman.com/blog/?p=1060</guid>
		<description><![CDATA[In the first in a planned series of posts, I update cssSandpaper with new features.  This week I add IE support for <code>translate()</code> support to CSS transforms and alpha channel support to linear gradients.  ]]></description>
			<content:encoded><![CDATA[<p>I have been really happy with the great feedback I&#8217;ve been getting about <a href="http://www.useragentman.com/blog/csssandpaper-a-css3-javascript-library/">cssSandpaper</a>. <strong> CSS3 gives web developers  some great design tools to work with</strong> and it is my hope that this library will encourage more developers to use the <strong>use of these new features today</strong>, even if they have to code <strong>in older browsers.</strong> Even though Internet Explorer 9 will be supporting a few of these features, <strong>it will be quite a while before the number of users of IE8 will be at an ignorable level</strong> and we won&#8217;t need cssSandpaper any longer.  Until then, I will work hard to make cssSandpaper better.</p>
<p><a href="http://www.useragentman.com/blog/2010/03/09/cross-browser-css-transforms-even-in-ie/">When I released cssSandpaper</a> a few months ago, I was disappointed that I couldn&#8217;t support CSS3 transform&#8217;s <code>translate()</code> function in IE due to a limitation in the <a href="http://msdn.microsoft.com/en-us/library/ms533014%28VS.85%29.aspx"><code>DXImageTransform.Microsoft.Matrix</code> CSS <code>filter</code></a>.  However, with the gracious help of <a href="http://www.nicolarizzo.com/">Nicola Rizzo</a> (who has already used his Matrix filter skills coding <a href="http://archive.dojotoolkit.org/nightly/dojotoolkit/dojox/html/tests/test_style-html.html">a really cool dojox transformation plugin</a>) and Tomasz Tunik, we now have a version of cssSandpaper that supports it.  I also got a few requests to have IE support for CSS3 gradients that can use <a href="http://www.css3.info/preview/rgba/">the <code>rgba()</code> color space</a>, and with the help of information on <a href="http://css3please.com">CSS3 Please</a>, it was easily added.</p>
<h2>transform: translate</h2>
<p>The CSS <code>transform</code> property allows for 2D translations of a box.  What this means is, no matter which way you want to rotate or skew the object, you can move the object horizontally and vertically from the object&#8217;s perspective.  Some of you may ask &#8220;How is this different from changing the object&#8217;s  <code>top</code> and <code>left</code> properties?  Let&#8217;s take a look at an example below to see how it differs:</p>
<div id="attachment_1063" class="wp-caption aligncenter" style="width: 478px"><a href="http://www.useragentman.com/tests/cssSandpaper/simpleTranslate.html"><img class="size-full wp-image-1063" title="translateExample1" src="http://www.useragentman.com/blog/wp-content/uploads/2010/05/translateExample1.png" alt="" width="468" height="458" /></a><p class="wp-caption-text">An example of CSS3 transform: translate() using cssSandpaper (click the image above to see it in action)</p></div>
<p>In the example above you&#8217;ll see two white boxes, one without any transforms performed, and the other translated 200 pixels to the right.  Just what you would expect.  Now take a look at the two red boxes &#8230; they are both rotated 45 degrees, but the second one is then<strong> translated 200 pixels to the right from the object&#8217;s perspective</strong>.</p>
<p>The translations are done with some very simple CSS:</p>
<blockquote class="code">
<pre>/*
 * This object is rotated 45 degrees and then translated 200 pixels to the
 * right and 100 pixels down, with respect to the object's perspective
 */

#o2 {
	-sand-transform: translate(200px, 100px);
}</pre>
</blockquote>
<p><code>translate()</code> takes two parameters, the x and y translation offsets (currently, cssSandpaper only supports <code>px</code> units for these offsets).  There are also <code>translateX()</code> and <code>translateY()</code> if a developer only wants to translate in one direction.</p>
<h2>rgba() Gradients</h2>
<p>The rgba color space adds alpha channeling to the rgb color space you know and love.  When using it in gradients, developers can make parts of an elements background-color semi-transparent.  The two examples below illustrate the effects that be done with <code>rgba()</code>.</p>
<table>
<tbody>
<tr>
<td>
<p><div id="attachment_1073" class="wp-caption aligncenter" style="width: 244px"><a href="/tests/cssSandpaper/rgbaGradientTest.html"><img class="size-full wp-image-1073 " title="gradients" src="http://www.useragentman.com/blog/wp-content/uploads/2010/05/gradients.png" alt="" width="234" height="330" /></a><p class="wp-caption-text">A few rgba() gradients on top of each other to demonstrate their semi-transparency.</p></div></td>
<td>
<p><div id="attachment_1072" class="wp-caption aligncenter" style="width: 244px"><a href="/tests/cssSandpaper/rgbaGradient.html"><img class="size-full wp-image-1072 " title="gradientAnimation" src="http://www.useragentman.com/blog/wp-content/uploads/2010/05/gradientAnimation.png" alt="" width="234" height="330" /></a><p class="wp-caption-text">An example of an animation using rgba() gradients (click on the above images to show the examples in action).</p></div></td>
</tr>
</tbody>
</table>
<p>The following code illustrates how to perform an rgba() gradient:</p>
<blockquote class="code">
<pre>/*
 * The following is a horizontal gradient that blends red to blue, with a 50% opacity level
 */

#o2 {
   background-image: -sand-gradient(linear, left top, right top,
      from(rgba(255, 0, 0,  0.5)), to(rgba(0, 0, 255, 0.5)));
}</pre>
</blockquote>
<p>Note that as before, IE only supports two color gradients that are strictly horizontal or vertical.  I hope to support multiple color stops in IE in a future release.  Also note that in IE, the alpha channel is removed from an object if you apply a transform on it. This may be a limitation of <a href="http://msdn.microsoft.com/en-us/library/ms532997%28VS.85%29.aspx">IE&#8217;s  Gradient filter</a>, which cssSandpaper uses to implement CSS3 gradients.</p>
<p>In order to translate <code>rgba()</code> notation to ARGB notation with IE&#8217;s gradient filter understands (e.g. <code>#aarrggbb</code>, where <code>aa</code> are two hex digits representing the amount of opacity), I modified <a href="http://www.phpied.com/">Stoyan Stefanov</a>&#8216;s <a href="http://www.phpied.com/rgb-color-parser-in-javascript/">RGBColor script</a> (which translates from one color notation to another) and added ARGB support.</p>
<p>For information about the rgba color space, read the <a href="http://www.css3.info">CSS3.info&#8217;s</a> <a href="http://www.css3.info/introduction-opacity-rgba/">brief introduction to Opacity and RGBA</a>.</p>
<h2>Future Roadmap</h2>
<p>There is quite a bit more I would like to see cssSandpaper support, including:</p>
<ol>
<li><strong><a href="http://css-tricks.com/rgba-browser-support/">solid rgba() background colors</a>:</strong> this should be quite easy to support, given it is a special case of an rgba() gradient (i.e. a transition from a color to the same color).</li>
<li><strong><a href="http://vision-media.ca/resources/css/css-transformations-animations-and-transitions-examples">CSS3 transitions</a>:</strong> performing animations with only a bit of CSS would be sweet for those who are afraid of <a href="http://en.wikipedia.org/wiki/Recursion">recursive programming</a> with the JavaScript <a href="http://www.w3schools.com/js/js_timing.asp"><code>setTimeout()</code></a> function.</li>
<li><strong><a href="https://developer.mozilla.org/en/CSS/-moz-transform-origin">transform-origin</a>:</strong> modify the origin for transformations of an element (this would allow, for example, developers to produce an animation of planets rotating around the sun, which would be the transform-origin point for all the planets).</li>
<li><strong><a href="http://www.the-art-of-web.com/css/border-radius/">border-radius</a>: </strong> Firefox and Safari have slightly diffferent methods of rounding corners of a block-level element, and it would be useful to simplify this.  It would also be nice to have support for it in any version of IE (there are<a href="http://fetchak.com/ie-css3/"> a few excellent solutions involving VML</a> are out there today, but it would be great if there was one that supported all the features that CSS3 supports, including different radii for each corner, as well as co-existance with the other features of cssSandpaper, which may not be an easy thing to do).</li>
<li><strong>CSS3 gradients with multiple color-stops:</strong> cssSandpaper uses <a href="http://msdn.microsoft.com/en-us/library/ms532997%28VS.85%29.aspx">IE&#8217;s Gradient filter</a>, which only supports two color-stops.  It would be great to do some JavaScript magic to emulate multiple color-stops.</li>
<li><strong>radial gradients in IE: </strong>although IE&#8217;s Gradient filter doesn&#8217;t support it, VML does (I found <a href="http://sample.revulo.com/VML/radial-gradient-clip.html">a really cool example of an VML</a> radial gradient, but it can only be viewed in IE).</li>
<li><strong>better support for other units of measure besides px: </strong>I personally love to use em units in my design work, and want cssSandpaper to support this and other units, like cm, in, and percentages.</li>
</ol>
<p>If <strong>anyone </strong>would like to help me implement these features or any others in cssSandpaper, please contact me either in the discussion below or at the email address at the top of this page (I can <strong>really</strong> use any assistance anyone can give). Even if you don&#8217;t know JavaScript, any comments you have on this library would be most welcome. Let&#8217;s prevent older browsers from keeping us from using CSS3 and doing some really cool design work.</p>
<p><a class="exampleLink" href="http://www.useragentman.com/blog/csssandpaper-a-css3-javascript-library/">Get the latest version of cssSandpaper from the official documentation page</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.useragentman.com/blog/2010/05/06/csssandpaper-now-supports-transform-translate-and-rgba-gradients/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Cross-Browser Animated CSS Transforms &#8212; Even in IE.</title>
		<link>http://www.useragentman.com/blog/2010/04/05/cross-browser-animated-css-transforms-even-in-ie/</link>
		<comments>http://www.useragentman.com/blog/2010/04/05/cross-browser-animated-css-transforms-even-in-ie/#comments</comments>
		<pubDate>Tue, 06 Apr 2010 03:14:09 +0000</pubDate>
		<dc:creator>zoltan</dc:creator>
				<category><![CDATA[CSS]]></category>
		<category><![CDATA[CSS3]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[animation]]></category>
		<category><![CDATA[setTimeout]]></category>

		<guid isPermaLink="false">http://www.useragentman.com/blog/?p=923</guid>
		<description><![CDATA[This is a follow-up article to my original CSS3 Transform article where I extend cssSandpaper to support scripting.  Now you can animate Css3 Transforms (as well as gradients, opacities and box-shadows) in all browsers, including IE, without a lot of issues.  Includes lots of neat examples.]]></description>
			<content:encoded><![CDATA[<div id="attachment_996" class="wp-caption alignright" style="width: 267px"><a href="/tests/cssSandpaper/clock.html"><img class="size-full wp-image-996 " title="Clock Example" src="http://www.useragentman.com/blog/wp-content/uploads/2010/04/clock.png" alt="" width="257" height="252" /></a><p class="wp-caption-text">Cross-Browser Animated CSS Clock Example based on an original WebKit example by Toby Pitman. Click image above to view.</p></div>
<p>In <a href="../2010/03/09/cross-browser-css-transforms-even-in-ie/">my  first article about CSS3</a>, I introduced a new script, <a href="http://www.useragentman.com/blog/csssandpaper-a-css3-javascript-library">cssSandpaper</a>, which allows developers to sidestep the myriad of vendor specific properties and just use one property to implement properties like <code>transform </code>for all browsers, including IE.</p>
<p>After posting the article, I saw many examples of CSS3 Transform using animations.  Toby Pitman&#8217;s <a href="http://css-tricks.com/css3-clock/">Old School Clock</a> is an excellent example of how one can use a Webkit&#8217;s <code>-webkit-transform</code> to make a small but excellent demo.  I wanted to prove that it would be easy to take this example and make it work in all modern browsers, so I did.  It took 15 minutes (okay, I had to change cssSandpaper to offer scripting support, but that didn&#8217;t take too long either).  I then coded a few more examples to show that some really neat things can be done using a small bit of JavaScript.   Here are links to these examples:</p>
<ul>
<li><a href="http://www.useragentman.com/tests/cssSandpaper/clock.html">The Cross-Browser CSS Clock</a> based on code by Toby Pitman</li>
<li><a href="http://www.useragentman.com/tests/cssSandpaper/countdown.html">A remixed movie leader countdown</a> (actually, it uses the <a href="http://en.wikipedia.org/wiki/File:RCA_Indian_Head_test_pattern.JPG">RCA Indian Head Test Pattern</a>, but whatever &#8230;)</li>
<li><a href="http://www.useragentman.com/tests/cssSandpaper/gradient.html">A psychedelic animated gradient</a></li>
</ul>
<h2>How To Do CSS3 Animated Effects?</h2>
<p><strong>If this was the perfect world</strong>, all a developer would need to do is use a DOM element&#8217;s <code>style</code> property and manipulate the appropriate camel-case CSS property.  For example, if a developer wanted to script the rotation of a node, he or she would code the following:</p>
<blockquote class="code">
<pre>    node.style.transform = "rotate(25deg)";</pre>
</blockquote>
<p>However, this is not the perfect world — after all, I am not making millions writing this blog and <strong>the current flavours of IE in use today don&#8217;t support many of the CSS3 properties</strong>.  However, using cssSandpaper, one could use the following method to do the same thing:</p>
<blockquote class="code">
<pre>    cssSandpaper.setTransform(node, "rotate(25deg)");</pre>
</blockquote>
<p>Developers can use cssSandpaper to manipulate <code>opacity</code>, <code>box-shadow</code>,  and <code>background</code> gradients, using <code>cssSandpaper.setOpacity()</code>, <code>cssSandpaper.setBoxShadow()</code> and <code>cssSandpaper.setGradient()</code> respectively.  All these functions take two parameters: a DOM node, and the appropriate CSS property values as described in the <a href="http://www.useragentman.com/downloads/cssSandpaper.1.0-beta2.zip">cssSandpaper documentation</a>.  For example:</p>
<blockquote class="code">
<pre>    cssSandpaper.setTransform(node, "rotate(25deg) scale(2, 2) skew(20deg, 20deg)");
    cssSandpaper.setOpacity(node, 0.3);
    cssSandpaper.setBoxShadow(node, "10px 10px 5px #ffcccc");
    cssSandpaper.setGradient(node, "-sand-gradient(linear, center top, center bottom, from(#0000ff), to(#ff0000));")</pre>
</blockquote>
<p>For more information, please read <a href="/blog/csssandpaper-a-css3-javascript-library">the official documentation of cssSandpaper</a>.</p>
<h2>Caveats</h2>
<ul>
<li>When animating using <code>transform</code>&#8216;s <code>scale()</code> function, make sure you avoid scaling objects too much larger than their original size.  This is due to the fact that IE scales objects as if they were images, and scaling-up an object will make the result quite pixelated.</li>
<li>The <code>-sand-gradient()</code> function does not use Firefox&#8217;s native <code>-moz-gradient</code> function to produce CSS3 gradients, but a solution using Canvas which works in all Canvas enabled versions of Firefox.  I will update cssSandpaper to use <code>-moz-gradient</code> in a future release (if anyone wants to help me get this working, please contact me via the email address at the top of this page, or by leaving a comment below).</li>
</ul>
<p><a class="exampleLink" href="http://www.useragentman.com/blog/csssandpaper-a-css3-javascript-library/">Go to the cssSandpaper documentation to download the latest version.</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.useragentman.com/blog/2010/04/05/cross-browser-animated-css-transforms-even-in-ie/feed/</wfw:commentRss>
		<slash:comments>11</slash:comments>
		</item>
		<item>
		<title>Cross Browser CSS Transforms &#8211; even in IE</title>
		<link>http://www.useragentman.com/blog/2010/03/09/cross-browser-css-transforms-even-in-ie/</link>
		<comments>http://www.useragentman.com/blog/2010/03/09/cross-browser-css-transforms-even-in-ie/#comments</comments>
		<pubDate>Tue, 09 Mar 2010 05:00:05 +0000</pubDate>
		<dc:creator>zoltan</dc:creator>
				<category><![CDATA[CSS]]></category>
		<category><![CDATA[CSS3]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[cross-browser]]></category>
		<category><![CDATA[css sandpaper]]></category>
		<category><![CDATA[css transform]]></category>
		<category><![CDATA[css3 transform]]></category>
		<category><![CDATA[cssSandpaper]]></category>
		<category><![CDATA[ie filters]]></category>
		<category><![CDATA[transform]]></category>

		<guid isPermaLink="false">http://www.useragentman.com/blog/?p=896</guid>
		<description><![CDATA[The CSS transform  property allows developers to rotate, scale, and skew blocks of HTML via CSS.  There are variants that work natively on all major browsers ... except for IE.  I created a new library, cssSandpaper, that implements CSS3 transforms (as well as gradients and box-shadows) in IE.  It also allows developers to use one transform declaration, instead of three vendor-specific ones for Opera, Firefox and WebKit browsers.  ]]></description>
			<content:encoded><![CDATA[<div class="importantNotes">
<h3>Update (April 6, 2010):</h3>
<p>Please see the <a href="/blog/2010/04/05/cross-browser-animated-css-transforms-even-in-ie/">follow up article on Cross Browser CSS3 Animation</a> which explores scripting CSS3 properties.</p>
</div>
<div class="mceTemp">
<dl id="attachment_822" class="wp-caption alignright" style="width: 267px;">
<dt class="wp-caption-dt"><a href="http://www.useragentman.com/tests/cssSandpaper/cube3.html"><img class="size-full wp-image-822 " title="CSS3 Cube Layout Example" src="http://www.useragentman.com/blog/wp-content/uploads/2010/03/cubeExampleScreenshot.png" alt="" width="257" height="208" /></a></dt>
<dd class="wp-caption-dd">
<div>An example of a page using the CSS Transform property and cssSandpaper.  Visible in most major browsers, including Internet Explorer.  Layout based on work done by Paul Hayes in his article <a href="&lt;/dd"></a></div>
</dd>
</dl>
</div>
<p><a href="&lt;/dd">The </a><a href="http://www.w3.org/TR/css3-2d-transforms/">CSS <code>transform</code> property</a> allows developers to rotate, scale, and skew blocks of HTML via CSS.  Although you can do the same thing with images in Photoshop or The GIMP, using CSS transforms allows developers to do the same thing with any HTML markup and allows users to select the text within the transformed object.</p>
<p>When I first saw sites using <code>transform</code>, I looked at the underlying code and tried to produce pages using <code>transform</code> in all browsers.  Although Firefox, Opera and Webkit based browser support it via <a href="http://reference.sitepoint.com/css/vendorspecific">vendor-specific prefixes</a> (using <code>-moz-transform</code>, <code>-o-transform</code> and <code>-webkit-transform</code> respectively) Internet Explorer doesn&#8217;t support it at all.  I didn&#8217;t like that, so I took out my JavaScript whip, beat Explorer into submission and made it do my bidding (but not without getting a few mental bruises of my own).</p>
<p>Before I start talking about the details of my solution, let&#8217;s take a look at a few examples of it in action.  The following code has been tested with Firefox 3.5, Safari 4,  Chrome 4, Internet Explorer 6 and higher.</p>
<ol>
<li><a href="/tests/cssSandpaper/rotateTest.html">Rotations Example</a></li>
<li><a href="/tests/cssSandpaper/skewTest.html">Skew Example</a></li>
<li><a href="/tests/cssSandpaper/cube3.html">Cube Example, using rotates and skews</a></li>
<li><a href="/tests/cssSandpaper/example.html">Another example that looks like a CSS3 version of a Geocities page.</a></li>
</ol>
<p>(The examples  also work on my copy of Opera 10.5, although I have seen it fail on other installations &#8211; I will update this post when I find  out why).</p>
<h2>cssSandpaper to the Rescue</h2>
<p>I saw the design potential of using CSS transforms and was frustrated at Explorer&#8217;s lack of support.  I originally tried a non-JavaScript solution which involved creating CSS rules that combine <code>transform</code> with an IE technology that does something similar: the <a href="http://msdn.microsoft.com/en-us/library/ms533014%28VS.85%29.aspx"><code>DXImageTransform.Microsoft.Matrix</code> CSS <code>filter</code></a>.</p>
<p>I then, to steal a phrase from Russel Peters, started to Hurt Real Bad:</p>
<ul>
<li>the syntax of <code>transform</code> is very obvious:<br />
<blockquote class="code">
<pre>#myObject {
   transform: rotate(40deg) scale(2.0);
}</pre>
</blockquote>
<p>but the IE filter code is quite intimidating:</p>
<blockquote class="code">
<pre>#myObject {
   filter: progid:DXImageTransform.Microsoft.Matrix(sizingMethod='auto expand',
        M11=1.5320888862379554, M12=-1.2855752193730787,
        M21=1.2855752193730796, M22=1.5320888862379558);
}</pre>
</blockquote>
<p>The scary numbers that the <code>DXImageTransform.Microsoft.Matrix</code> filter uses requires knowledge of matrix and vector mathematics.  Even though there is a <a href="http://en.wikipedia.org/wiki/Transformation_matrix">great Wikipedia article on the subject</a>, even the mathematically gifted wouldn&#8217;t want to do the calculations to do a simple rotate in CSS (I would like to note here that even though I have a university degree in Mathematics, I hate doing arithmetic inside my head.  If you don&#8217;t believe me, watch  me figure out a tip at a restaurant sometime.  I&#8217;m not kidding).</li>
<li>although it is possible to have a list of transformations using <code>transform</code>, the <code>DXImageTransform.Microsoft.Matrix</code> filter only allows one transform matrix. In order to implement multiple transforms using one filter,  a designer would have to <a href="http://en.wikipedia.org/wiki/Matrix_multiplication">convert all the transforms into matrices and multiply them together</a>.  Again, as ugly as I am when I first wake up in the morning.</li>
<li>when rotating, skewing, or doing any other transformations on objects using the <code>transform</code> property, the center of the object remains fixed.  However, the <code>Matrix</code> filter doesn&#8217;t keep the centre of the transformed object fixed, as seen by the illustration below:<br />
<table class="screenshots">
<thead>
<tr>
<th>Rotate using CSS transform</th>
<th>Rotate using IE Filter</th>
</tr>
</thead>
<tbody>
<tr>
<td><a href="http://www.useragentman.com/blog/wp-content/uploads/2010/03/rotateFirefox.png"></a><a href="http://www.useragentman.com/blog/wp-content/uploads/2010/03/rotateFirefox.png"><img class="alignnone size-full wp-image-872" title="Screenshot of object rotated using CSS transform property." src="http://www.useragentman.com/blog/wp-content/uploads/2010/03/rotateFirefox.png" alt="" width="241" height="240" /></a></td>
<td><a href="http://www.useragentman.com/blog/wp-content/uploads/2010/03/rotateIE.png"><img class="alignnone size-full wp-image-873" title="example of object rotated with IE filter." src="http://www.useragentman.com/blog/wp-content/uploads/2010/03/rotateIE.png" alt="" width="241" height="240" /></a></td>
</tr>
</tbody>
</table>
</li>
</ul>
<p><a class="exampleLink" href="/tests/cssSandpaper/failedRotateExample.html">See the above example in action (compare IE with the other browsers to see the difference for yourself)</a></p>
<p>I was about to give up on my endeavor until I read Weston Ruter&#8217;s clever <a href="http://weston.ruter.net/projects/css-gradients-via-canvas/">CSS Gradients in Canvas</a> article, which implements gradients in older versions Firefox and Opera.  What I really liked was how he used the CSS from a web page to place the canvas gradients in the page (as opposed to using CSS classes to indicate where the gradients should go).   I then thought it would be a great idea to do the same with CSS transforms &#8211; why not have  JavaScript find out which objects are transformed by reading the style sheets containing the transform rules, and if the browser is Internet Explorer, apply the <code>Matrix</code> filter, while translating the image so that the center is maintained.  How hard could it be &#8230;&#8230;.. right?</p>
<p>After a few obsessive months of coding, coffee drinking and Asprin popping (as well angrily asking myself on several occasions why the &amp;@$! I would wanted to do this in the first place), I created <code>cssSandpaper.js</code>, a library that implements <code>transform</code> (and some other CSS3 properties) as consistently as possible in all browsers.  It uses many ideas from Ruter&#8217;s gradient script, as well as <code>sylvester.js</code>, <a href="www.jcoglan.com/">James Coglan&#8217;s</a> brilliant <a href="http://sylvester.jcoglan.com/">matrix and vector math library</a>.</p>
<p>The code is currently in a beta stage, but I think that it&#8217;s in good enough shape that developers can start to play around with these really cool effects today and have it work in almost any browser.</p>
<h2>Browser Differences</h2>
<p>After I finished cssSandpaper and played around with transforms, I found some slight differences in the way browsers handled them:</p>
<ul>
<li>If you transform an object with scrollbars, it is possible to scroll the object in most browsers in most cases.  However, in the cube page I mocked up, I noticed the following behaviour:
<ul>
<li>the left facing side of the cube isn&#8217;t easily accessible in IE.</li>
<li>the scrollbars don&#8217;t appear at all in Opera.</li>
<li>the scrollbars are not quite clear in Chrome for Windows, although one can still scroll the sides if you can guess where they are.</li>
<li>Firefox 3.5 for Mac puts the scrollbars in strange places (this has been fixed 3.6).</li>
</ul>
</li>
<li>In Internet Explorer, the text is selectable but sometimes it takes a few tries to figure out how to do it (try selecting text with IE in my cube mockup and you&#8217;ll see what I mean).</li>
<li>When selecting text within Firefox, the text tends to jump around ever so slightly for some odd reason (this is quite a subtle effect that it is probably not noticeable in most instances).</li>
<li>It <strong>looks</strong> like Internet Explorer takes the block of HTML, converts it to an image and then does the transform (I&#8217;m not sure .. I&#8217;m guessing).  As a result, the text in certain situations will look a little blurry, especially if scaling is involved.</li>
</ul>
<p>As to be expected, the rendering of the transformed text in the other browsers slightly differs:</p>
<table class="screenshots">
<thead>
<tr>
<th></th>
<th>Explorer 6.x+</th>
<th>Firefox 3.5+</th>
<th>Safari 4.0+</th>
<th>Chrome 3.0+</th>
<th>Opera 10.0+</th>
</tr>
</thead>
<tbody>
<tr>
<th>Windows</th>
<td><a href="http://www.useragentman.com/blog/wp-content/uploads/2010/03/cubeExplorerWindows.png"><img class="alignnone size-full wp-image-840" title="cubeExplorerWindows" src="http://www.useragentman.com/blog/wp-content/uploads/2010/03/cubeExplorerWindows.png" alt="" /></a></td>
<td><a href="http://www.useragentman.com/blog/wp-content/uploads/2010/03/cubeFirefoxWindows.png"><img class="alignnone size-full wp-image-840" src="http://www.useragentman.com/blog/wp-content/uploads/2010/03/cubeFirefoxWindows.png" alt="" /></a></td>
<td><a href="http://www.useragentman.com/blog/wp-content/uploads/2010/03/cubeSafariWindows.png"><img class="alignnone size-full wp-image-840" src="http://www.useragentman.com/blog/wp-content/uploads/2010/03/cubeSafariWindows.png" alt="" /></a></td>
<td><a href="http://www.useragentman.com/blog/wp-content/uploads/2010/03/cubeChromeWindows.png"><img class="alignnone size-full wp-image-840" title="cubeExplorerWindows" src="http://www.useragentman.com/blog/wp-content/uploads/2010/03/cubeChromeWindows.png" alt="" /></a></td>
<td><a href="http://www.useragentman.com/blog/wp-content/uploads/2010/03/cubeOperaWindows.png"><img class="alignnone size-full wp-image-840" src="http://www.useragentman.com/blog/wp-content/uploads/2010/03/cubeOperaWindows.png" alt="" /></a></td>
</tr>
<tr>
<th>Mac OS X</th>
<td style="text-align: center;">Not Applicable</td>
<td><a href="http://www.useragentman.com/blog/wp-content/uploads/2010/03/cubeFirefoxMac.png"><img class="alignnone size-full wp-image-840" src="http://www.useragentman.com/blog/wp-content/uploads/2010/03/cubeFirefoxMac.png" alt="" /></a></td>
<td><a href="http://www.useragentman.com/blog/wp-content/uploads/2010/03/cubeSafariMac.png"><img class="alignnone size-full wp-image-840" src="http://www.useragentman.com/blog/wp-content/uploads/2010/03/cubeSafariMac.png" alt="" /></a></td>
<td><a href="http://www.useragentman.com/blog/wp-content/uploads/2010/03/cubeChromeMac.png"><img class="alignnone size-full wp-image-840" src="http://www.useragentman.com/blog/wp-content/uploads/2010/03/cubeChromeMac.png" alt="" /></a></td>
<td>Doesn&#8217;t support transforms yet (as of version 10.50)</td>
</tr>
<tr>
<th>Linux</th>
<td style="text-align: center;">Not Applicable</td>
<td><a href="http://www.useragentman.com/blog/wp-content/uploads/2010/03/cubeFirefoxUbuntu.png"><img class="alignnone size-full wp-image-840" src="http://www.useragentman.com/blog/wp-content/uploads/2010/03/cubeFirefoxUbuntu.png" alt="" /></a></td>
<td style="text-align: center;">Not Applicable</td>
<td><a href="http://www.useragentman.com/blog/wp-content/uploads/2010/03/cubeChromeUbuntu.png"><img class="alignnone size-full wp-image-840" src="http://www.useragentman.com/blog/wp-content/uploads/2010/03/cubeChromeUbuntu.png" alt="" /></a></td>
<td>Doesn&#8217;t support transforms yet (as of version 10.50)</td>
</tr>
</tbody>
</table>
<h2>Using cssSandpaper</h2>
<p>After <a href="/downloads/cssSandpaper.1.0-beta1.zip">downloading the archive</a> you must put the following tags into the head of your document <strong>after all of your style sheet declarations</strong> (this is to ensure the JavaScripts will run after the style sheets are loaded):</p>
<blockquote class="code">
<pre>&lt;script type="text/javascript" src="path/to/js/cssQuery-p.js"&gt;&lt;/script&gt;
&lt;script type="text/javascript" src="path/to/js/jcoglan.com/sylvester.js"&gt;&lt;/script&gt;
&lt;script type="text/javascript" src="path/to/js/cssSandpaper.js"&gt;&lt;/script&gt;</pre>
</blockquote>
<p>You can then use the transform property in your web pages.  Note that since the specification for the transform property is finalized by the W3C, I decided to use the  <a href="http://reference.sitepoint.com/css/vendorspecific">vendor-specific prefix</a> <code>-sand-</code> before each CSS3 property it supports.</p>
<p>The following is a description of how to use <code>transform</code>, as well as two other CSS3 properties that cssSandpaper supports, <code>box-shadow</code> and <code>gradient</code>.</p>
<p><a class="exampleLink" href="http://www.useragentman.com/blog/csssandpaper-a-css3-javascript-library/">Go to the cssSandpaper documentation to download the latest version.</a></p>
<h2>-sand-transform</h2>
<p><!-- Begin steps --></p>
<div class="steps">
<h3 class="first">Description</h3>
<p>Transforms allow developers to rotate, scale, and skew blocks of HTML via CSS.</p>
<h3>Syntax</h3>
<blockquote class="code">
<pre>#container {
   -sand-transform:  <em>&lt;function-list&gt;</em>;
}</pre>
</blockquote>
<p>where <em>&lt;function-list&gt;</em> can be a space separated list of the following functions:</p>
<table class="screenshot">
<thead>
<tr>
<th>Function</th>
<th>Purpose</th>
</tr>
</thead>
<tbody>
<tr>
<td><code>rotate(angle)</code></td>
<td>Rotates HTML elements.  <code>angle</code> can be in degrees (e.g. rotate(30deg)) or radians <code>rotate(1.3rad)</code></td>
</tr>
<tr>
<td><code>scale(sx[, sy])</code></td>
<td>Scales HTML elements.  <code>sx</code> and <code>sy</code> are numbers, where <code>1</code> represents the original size, <code>2</code> represents twice the size, etc.  Note that if <code>sy</code> isn&#8217;t specified, it is assumed to be equal to <code>sx</code>.  Similar functions are <code>scaleX(sx)</code> and <code>scaleY(sy)</code>.</td>
</tr>
<tr>
<td><code>skew(ax[, ay])</code></td>
<td>Skews the object around the x and y axes by the specified angles in degrees or radians.  If <code>ay</code> isn&#8217;t provided, it is assumed to be <code>0deg</code>.</td>
</tr>
<tr>
<td><code>matrix(a, c, b, d, tx, ty)</code></td>
<td>Applies a 2D transformation matrix comprised of the specified six values.  If you aren&#8217;t familiar with linear algebra and matrix arithmetic, this function will be hard to understand.  For further information, you may want to read <a href="http://en.wikipedia.org/wiki/Transformation_matrix">Wikipedia&#8217;s Transformation Matrix</a> article, although if you are mathematically challenged, you may run away from your computer screaming.</p>
<p>If you are familiar with matrix multiplication, note that c and b are reversed.  This follows the way Firefox has implemented this method (i <strong>believe</strong> WebKit based browsers reverse these numbers).</td>
</tr>
</tbody>
</table>
<p><strong>Note:</strong> non-Explorer browsers support the <code>translate</code> function as well.  I have not been able to find a way to make this work IE at this time, due to a technical  issue with the IE&#8217;s <code>Matrix</code> filter property (for those who want details, I use <code>sizingMethod='auto expand'</code> when using the <code>Matrix</code> filter which doesn&#8217;t support translations, and using sizingMethod=&#8217;crop&#8217; is not suitable to emulate the <code>transform</code> property).  For this reason, the <code>tx</code> and <code>ty</code> values of the <code>matrix()</code> function are not supported as well.</p>
<h3>Examples</h3>
<ul>
<li><a href="/tests/cssSandpaper/rotateTest.html">Rotations</a></li>
<li><a href="/tests/cssSandpaper/skewTest.html">Skews</a></li>
</ul>
</div>
<h2>-sand-box-shadow</h2>
<div class="steps">
<h3 class="first">Description</h3>
<p><code>box-shadow</code> allows us to take a block level element and put a shadow underneath.  There can optionally set a blur factor to give a warmer effect to the shadow.</p>
<h3>Syntax</h3>
<blockquote class="code">
<pre>#container {
   -sand-box-shadow: <em>&lt;horizontal-offset&gt;</em> <em>&lt;vertical-offset&gt;</em> <em>&lt;blur-radius&gt;</em> <em>
</em>;
}</pre>
</blockquote>
<p>Note that in IE, the <code>blur-radius</code> is not supported, due to a lack of support in IE&#8217;s <a href="http://msdn.microsoft.com/en-us/library/ms533014%28VS.85%29.aspx"><code>DropShadow</code> filter</a>.</p>
<h3>Examples</h3>
<ul>
<li><a href="/tests/cssSandpaper/boxShadowTest.html">Box-Shadow Example</a></li>
</ul>
</div>
<h2>-sand-gradient</h2>
<div class="steps">
<h3 class="first">Description</h3>
<p>Gradients are gradual blends of color, and can be <code>linear</code> or <code>radial</code>:</p>
<h3>Syntax</h3>
<blockquote class="code">
<pre>#container {
   background-image: -sand-gradient(<em>&lt;type&gt;, &lt;start-point&gt;, &lt;end-point&gt;, &lt;color-stop1&gt;, &lt;color-stop2&gt;, ..., &lt;color-stopN&gt;</em>)
}</pre>
</blockquote>
<p>The type can be <code>linear</code> and <code>gradient</code>, although Internet Explorer doesn&#8217;t support radial gradients at this time, due to limitations in IE&#8217;s <a href="http://msdn.microsoft.com/en-us/library/ms532997%28VS.85%29.aspx"><code>gradient</code> filter</a>.  Internet Explorer also only supports simple linear gradients (i.e. only  horizontal and vertical and only two colours).</p>
<p>A more detailed explanation can be found in the <a href="http://dev.w3.org/csswg/css3-images/#linear-gradients">section regarding gradients</a> of the <a href="http://dev.w3.org/csswg/css3-images/">W3C&#8217;s working draft of CSS Image Values Module Level 3</a></p>
<h3>Examples</h3>
<ul>
<li><a href="/tests/cssSandpaper/gradientTest.html">Gradients Example Page</a></li>
</ul>
</div>
<h2>Other Known Issues</h2>
<ul>
<li>Currently, when a users changes the font-size in IE, the layout gets a little messed up.  This will be fixed in a future release.</li>
<li>Scripting in IE cannot be done via the usual <code>obj.style.transform</code>.  There is, however, an alternative way of scripting the cssSandpaper supported properties which will be outlined in a future blog post (I don&#8217;t mean to keep anyone in suspense &#8230; it&#8217;s just that I feel like I&#8217;ve spent too much time on this post and I want to get at least this part out the door :-) ).</li>
<li>Opera 10.5 sort of works.  It works on my copy, but not on another copy I&#8217;ve seen.  I don&#8217;t know why yet, but I will find out soon and post and update when I do.</li>
<li>Weston Ruter&#8217;s gradient script has more features.  I will incorporate them in a future release.</li>
</ul>
<h2>Future Work</h2>
<p>cssSandpaper will eventually be about more than just transforms &#8211; I would like to be able to support other advanced CSS topics (maybe even animations, if it is possible). It probably has a few bugs in it &#8211; I would really appreciate anyone letting  me know if you find any (my e-mail address is at the top left part of the page).</p>
<p>If anyone would like to get contribute code or do testing, I would love the help too.  :-)</p>
<h2>Acknowledgments</h2>
<p>I would like to thank Weston Ruter for his <a href="http://weston.ruter.net/projects/css-gradients-via-canvas/">CSS Gradients in Canvas</a> script, since I stole many of his ideas with his permission (I will be implementing more of the advanced features of his script in a future release of CSS3Sandbox).  I would also like to thank  <a href="www.jcoglan.com/">James Coglan</a> for building  <a href="http://sylvester.jcoglan.com/">Sylvester</a> so I didn&#8217;t have to.</p>
<h2>Downloads</h2>
<p><a class="exampleLink" href="http://www.useragentman.com/blog/csssandpaper-a-css3-javascript-library/">Go to the cssSandpaper documentation to download the latest version.</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.useragentman.com/blog/2010/03/09/cross-browser-css-transforms-even-in-ie/feed/</wfw:commentRss>
		<slash:comments>71</slash:comments>
		</item>
		<item>
		<title>Introducing: The Type Rendering Project</title>
		<link>http://www.useragentman.com/blog/2009/11/30/introducing-the-type-rendering-project/</link>
		<comments>http://www.useragentman.com/blog/2009/11/30/introducing-the-type-rendering-project/#comments</comments>
		<pubDate>Mon, 30 Nov 2009 17:05:58 +0000</pubDate>
		<dc:creator>zoltan</dc:creator>
				<category><![CDATA[@font-face]]></category>
		<category><![CDATA[CSS]]></category>
		<category><![CDATA[CSS3]]></category>
		<category><![CDATA[Fonts]]></category>

		<guid isPermaLink="false">http://www.useragentman.com/blog/?p=412</guid>
		<description><![CDATA[Join a project that will allow developers to <strong>find ways to make web type look better <em>right now</em></strong>.]]></description>
			<content:encoded><![CDATA[<div id="attachment_416" class="wp-caption alignleft" style="width: 260px"><img class="size-medium wp-image-416 " title="Type Rendering Project Logo" src="http://www.useragentman.com/blog/wp-content/uploads/2009/11/r-300x300.png" alt="Type Rendering Project Logo" width="250" height="250" /><p class="wp-caption-text"><span></span></p></div>
<p>Although this blog wasn&#8217;t meant to be a forum on <em>just</em> CSS font-embedding, it is a topic of great interest to me and has been the inspiration of a few articles here (my article on <a href="/blog/2009/11/29/how-to-detect-font-smoothing-using-javascript/">JavaScript font-smoothing detection</a> being the latest).  As a result, I have been very fortunate in meeting a lot of interesting and like-minded individuals who want to make the web a better place for typography.</p>
<p>One of the more vocal ones, <a href="http://paulirish.com/">Paul Irish</a>, asked me to join a project that will allow developers to <strong>find ways to make web type look better <em>right now</em></strong>.  Other members include web font blogger Tim Brown of <a href="http://www.nicewebtype.com/">Nice Web Type</a> and Ethan Dunham of the excellent font resource <a href="http://fontsquirrel.com/">Font Squirrel</a>.  The result is <a href="http://typerendering.com/">The Type Rendering Project</a> &#8211; we’ve set some goals and would like to open up dialogue with you to fulfill them. We&#8217;re asking for your participation, and lots of expert advice, to fulfill these intentions. If you are interested and are a Twitter user, <a href="http://twitter.com/typerendering">follow us</a> &#8211;  and more importantly, <a href="http://twitter.com/home?status=I+care+about+how+type+looks+on+the+web,+so+I+follow+%40typerendering.">tell folks you care about how type looks on websites</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.useragentman.com/blog/2009/11/30/introducing-the-type-rendering-project/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>How to Detect Font-Smoothing Using JavaScript</title>
		<link>http://www.useragentman.com/blog/2009/11/29/how-to-detect-font-smoothing-using-javascript/</link>
		<comments>http://www.useragentman.com/blog/2009/11/29/how-to-detect-font-smoothing-using-javascript/#comments</comments>
		<pubDate>Sun, 29 Nov 2009 19:41:50 +0000</pubDate>
		<dc:creator>zoltan</dc:creator>
				<category><![CDATA[@font-face]]></category>
		<category><![CDATA[CSS]]></category>
		<category><![CDATA[CSS3]]></category>
		<category><![CDATA[ClearType]]></category>
		<category><![CDATA[Fonts]]></category>
		<category><![CDATA[JavaScript]]></category>

		<guid isPermaLink="false">http://www.useragentman.com/blog/?p=338</guid>
		<description><![CDATA[Not all @font-face fonts look good when font-smoothing is turned off.  Now you can detect whether a font smoothing technology is being used and serve alternative fonts for those users.]]></description>
			<content:encoded><![CDATA[<div class="importantNotes">
<h3>Interested in making web typography look better?</h3>
<p><a href="/blog/2009/11/30/introducing-the-type-rendering-project/">Read about The Type Rendering Project!</a></p>
</div>
<div id="attachment_404" class="wp-caption alignleft" style="width: 260px"><img class="size-full wp-image-404 " title="fontSmoothingLeader" src="http://www.useragentman.com/blog/wp-content/uploads/2009/11/fontSmoothingLeader.png" alt="Image a remix of Wikimedia Images http://bit.ly/5LufSS and http://bit.ly/6Jjlbd" width="250" height="250" /><p class="wp-caption-text">
<div>Examples of font-smoothing and subpixel-rendering as performed by technologies like ClearType.  Image Credit: Wikimedia Commons users <a href='http://commons.wikimedia.org/wiki/File:Antialias-vrs-Cromapixel.png'>Pandries</a> and <a href='http://commons.wikimedia.org/wiki/File:ClearType02.png'>Kalan</a></div>
<p></p></div>
<p>In <a title="&quot;More @font-face in Depth&quot; article" href="http://www.useragentman.com/blog/2009/10/09/more-font-face-fun/">an earlier article</a>, I mentioned that <a title=" Boing Boing’s Redesign Uncovers the Dark Side of Web Fonts" href="http://www.webmonkey.com/blog/Boing_Boing_s_Redesign_Uncovers_the_Dark_Side_of_Web_Fonts">Boing-Boing had a few issues when they wanted to use @font-face embedding inside their website</a>.  In short, the problem was that some fonts look bad on computer monitors without font-smoothing enabled in the operating system.  This brought on a lot of discussion as to whether there was a way to detect whether or not font-smoothing was being used using JavaScript.  I initially thought there wasn&#8217;t a way, but after seeing a promising but incomplete method of detecting font-smoothing, I spent a few days devising a way to do it.</p>
<h2>Internet Explorer Does Something Easily (For a Change)</h2>
<p><a href="http://paulirish.com/">Paul Irish</a> mentioned to me and a few other colleagues that he came across <a href="http://silkworth.net/browser_os/cleartype.html">a page using an Active X control that detects font-smoothing in IE</a>.  I was so hopeful &#8230; until I realized that this only works on browsers that have come into contact with <a href="http://www.microsoft.com/typography/cleartype/tuner/tune.aspx">Microsoft&#8217;s online Cleartype Tuner</a>, which I visited with my copy of IE months before.  If a users had never visited this page, the script would fail.</p>
<p>I was more disappointed because Googling &#8220;javascript cleartype&#8221; did not point to anything useful.  However, searching for &#8220;javascript font-smoothing&#8221; pointed me to an article that told me about Internet Explorer&#8217;s <a href="http://www.java2s.com/Code/JavaScript/Window-Browser/ScreenfontSmoothingEnabled.htm">screen.fontSmoothingEnabled</a> property.  This gives us what we need &#8230; but only in Internet Explorer.  How on Earth can we detect font-smoothing in other browsers, and in non-Windows operating systems?</p>
<h2>Canvas to the Rescue</h2>
<div id="attachment_390" class="wp-caption aligncenter" style="width: 420px"><img class="size-full wp-image-390" title="aliasAndAntiAliasComparison" src="http://www.useragentman.com/blog/wp-content/uploads/2009/11/aliasAndAntiAliasComparison.png" alt="aliasAndAntiAliasComparison" width="410" height="244" /><p class="wp-caption-text">Letter &#39;O&#39; , Arial font, 32 px, rendered both without (left) and with (right) font-smoothing</p></div>
<p style="text-align: center;">
<p>I then thought about about the screenshots I made for <a href="/blog/2009/09/20/font-face-in-depth/">the @font-face in Depth article</a>.  Can a browser render a black glyph and detect if there is some sort of non-black pixel colouring around the its edges of the glyphs? <strong> A human can tell the difference</strong>:  if there are some non-black pixels around the edge of the glyph, it must be using font-smoothing.</p>
<p>But <strong>web browsers can do this too!</strong> Just have the browser draw a letter in black inside a <code>canvas</code> tag, and then have it sift through the canvas&#8217; pixels to see if there are any that are <strong>not</strong> pure black or pure white (more accurately, have the browser check the alpha channel to see if there are any semi-transparent pixels, which have a value that is not 0 or 255).    If there are no semi-transparent pixels, then the algorithm assumes that no font-smoothing is being used. I wrote an JavaScript routine that does this &#8211; it starts from co-ordinate (8,1) and scans left to right, to the bottom of the canvas (any point on near the top on the left-hand side of the canvas would have done as well).</p>
<p>The result is a JavaScript object, <code>TypeHelpers</code>, which implements this routine in one method, <code>hasSmoothing()</code>:</p>
<blockquote class="code">
<pre>var TypeHelpers = new function(){

   // I use me instead of this.  For reasons why, please read:
   // http://w3future.com/html/stories/callbacks.xml
   var me = this;

   me.hasSmoothing = function(){

      // IE has screen.fontSmoothingEnabled - sweet!
      if (typeof(screen.fontSmoothingEnabled) != "undefined") {
         return screen.fontSmoothingEnabled;
      } else {

         try {

            // Create a 35x35 Canvas block.
            var canvasNode = document.createElement('canvas');
            canvasNode.width = "35";
            canvasNode.height = "35"

            // We must put this node into the body, otherwise
            // Safari Windows does not report correctly.
            canvasNode.style.display = 'none';
            document.body.appendChild(canvasNode);
            var ctx = canvasNode.getContext('2d');

            // draw a black letter 'O', 32px Arial.
            ctx.textBaseline = "top";
            ctx.font = "32px Arial";
            ctx.fillStyle = "black";
            ctx.strokeStyle = "black";

            ctx.fillText("O", 0, 0);

            // start at (8,1) and search the canvas from left to right,
            // top to bottom to see if we can find a non-black pixel.  If
            // so we return true.
            for (var j = 8; j &lt;= 32; j++) {
               for (var i = 1; i &lt;= 32; i++) {

                  var imageData = ctx.getImageData(i, j, 1, 1).data;
                  var alpha = imageData[3];

                  if (alpha != 255 &amp;&amp; alpha != 0) {
                     return true; // font-smoothing must be on.
                  }
               }

            }

            // didn't find any non-black pixels - return false.
            return false;
         }
         catch (ex) {
            // Something went wrong (for example, Opera cannot use the
            // canvas fillText() method.  Return null (unknown).
            return null;
         }
      }
   }

   me.insertClasses = function(){
      var result = me.hasSmoothing();
      var htmlNode = document.getElementsByTagName('html')[0];
      if (result == true) {
         htmlNode.className += " hasFontSmoothing-true";
      } else if (result == false) {
            htmlNode.className += " hasFontSmoothing-false";
      } else { // result == null
            htmlNode.className += " hasFontSmoothing-unknown";
      }
   }

}

// if EventHelpers.js is included, insert the hasFontSmoothing CSS classes
if (window.EventHelpers) {
   EventHelpers.addPageLoadEvent('TypeHelpers.insertClasses')
}</pre>
</blockquote>
<p>Note the object also has an <code>insertClasses()</code> method.  This method, when run, adds a class to the <code>html</code> tag:</p>
<ul>
<li><code>hasFontSmoothing-true</code> if font-smoothing is being used</li>
<li><code>hasFontSmoothing-false</code> if it is not</li>
<li><code>hasFontSmoothing-unknown</code> if the user agent is unable to tell</li>
</ul>
<p>This makes it easy for developers who don&#8217;t want to mess with JavaScript code and just want  to use CSS.</p>
<p>Also note the <code>EventHelpers.addPageLoadEvent()</code> call at the end of the code.  This method (which is part of <code>EventHelpers.js</code>, included with the archive below) <a href="http://dean.edwards.name/weblog/2005/09/busted/">implements Dean Edwards&#8217; window.onload alternative which doesn&#8217;t wait for all the objects in the page to be loaded</a>.   I use this implementation to execute <code>TypeHelpers.insertClasses()</code> when the page loads so any font-detection CSS rules will work right away.  Please feel free to change this code to use the equivalent function call in Dojo, Prototype, jQuery, or whatever JavaScript code framework you prefer.</p>
<h2>Example #1: JavaScript Font Smoothing Detection</h2>
<p>Enough of theory &#8230; let&#8217;s look at it in practice!  To show how to detect font-smoothing with JavaScript, I created a page that, when the page is loaded, checks to see if it can tell if font-smoothing has been implemented and tells the user.  Here is the code that does this check:</p>
<blockquote class="code">
<pre>function displayInfo() {

   var message;
   var isFontSmoothingOn = TypeHelpers.hasSmoothing();
   if (isFontSmoothingOn == true) {
      message = "This browser is using a font-smoothing technology";
   } else if (isFontSmoothingOn == false) {
      message = "This browser isn't using a font-smoothing technology"
   } else {
      message = "We could not detect if font-smoothing is being used."
   }
   document.getElementById('detectInfo').innerHTML = message;

}

window.onload = displayInfo;</pre>
</blockquote>
<p><a class="exampleLink" href="/tests/fontSmoothing/">See the above code in action</a></p>
<h2>Example #2: CSS Font Smoothing Detection</h2>
<p>As implied earlier, this library can help CSS use different fonts if the browser is using a font-smoothing technology.   For example, using the following CSS will allow a browser to use the Droid Sans embedded font only if it using font-smoothing — otherwise, it will use Arial:</p>
<blockquote class="code">
<pre>@font-face {
	font-family: "Droid Sans";
	src: url("/shared/fonts/DroidSans/DroidSans.eot");
	src: local("Droid Sans"),
	     local("Droid Sans"),
	     url("/shared/fonts/DroidSans/DroidSans.ttf") format("truetype");
}

body {
	font-family: "Arial", "Helvetica", sans-serif;
}

html.hasFontSmoothing-true body {
	font-family: "Droid Sans", "Arial", "Helvetica", sans-serif;
}</pre>
</blockquote>
<p>We can also serve special content to users depending on the way fonts are rendered on their browser.  We first create content for all three scenerios (browser uses font-smoothing, browser doesn&#8217;t use font-smoothing, and the &#8220;we cannot detect&#8221; case) and wrap the content inside &lt;code&gt;div&lt;/code&gt; tags using appropriate CSS classes:</p>
<blockquote class="code">
<pre>&lt;div class="fontSmoothingMessage initiallyHidden"&gt;
    &lt;p&gt;You browser &lt;strong&gt;is&lt;/strong&gt; rendering this page
    with font-smoothing. Because of that, we will attempt
    to serve up the Droid Sans font to render this page,
    because we think it looks cool. If you are using a
    browser (such as Google Chrome) that cannot render
    downloaded True Type fonts by default, then the page
    will be rendered using Arial instead.&lt;/p&gt;
&lt;/div&gt;

&lt;div class="noFontSmoothingMessage initiallyHidden"&gt;
    Your browser &lt;strong&gt;is not&lt;/strong&gt; rendering this
    page with font-smoothing. It is for that reason we have
    decided to use the plain old Arial font to render this
    page, because it is hinted for use for displays that
    don't employ a font-smoothing technology.
&lt;/div&gt;

&lt;div class="unknownFontSmoothingMessage initiallyHidden"&gt;
    &lt;strong&gt;We are not sure&lt;/strong&gt; if your browser is
    rendering this page with a font-smoothing technology.
    It is for that reason we have decided to use the plain
    old Arial font to render this page, because it is
    hinted for use for displays that don't employ a
    font-smoothing technology.
&lt;/div&gt;</pre>
</blockquote>
<p>Note all the <code>div</code> tags are members of the class <code>initiallyHidden</code>.  This class will be used to hide all font-smoothing related content until the script kicks in.</p>
<p>However, all this will not work unless we use the following CSS code:</p>
<blockquote class="code">
<pre>.initiallyHidden {
	display: none;
}

html.hasFontSmoothing-true .fontSmoothingMessage,
html.hasFontSmoothing-false .noFontSmoothingMessage,
html.hasFontSmoothing-unknown .unknownFontSmoothingMessage {
	display: block;
}</pre>
</blockquote>
<p>Of course, this whole soltution relies on whether JavaScript being turned on in the user&#8217;s browser.  This should be kept in mind when implementing this solution.</p>
<p><a class="exampleLink" href="/tests/fontSmoothing/cssExample.html">See the above code in action</a></p>
<h2>Download</h2>
<p>All code used in this article can be downloaded below (<strong>Note:</strong> version 1.0 was missing the Droid Sans Fonts which have now been put into the archive.  Thanks to John Faulds for pointing this out).</p>
<p><a class="exampleLink" href="/downloads/TypeHelpers.zip">TypeHelpers.js v.1.0a and sample code.</a></p>
<p>With the help of <a href="http://www.nicewebtype.com">Tim Brown</a>, it was determined that the code detected font-smoothing correctly in the following browsers:</p>
<ul>
<li>Internet Explorer 6 and up on Windows XP and higher.</li>
<li>Firefox 3.5 and higher on Windows XP and higher, Mac OS X 10.4 and higher, and Ubuntu Linux 9.10 (and probably lower)</li>
<li>Chrome 3.0 on Windows XP and higher</li>
<li>Safari 4.0.3 on Windows XP and higher and Mac OS X 10.4 and higher</li>
</ul>
<p>This script <strong>cannot detect font-smoothing in any version of Opera</strong> (at the time of this writing, this includes all versions up to 10.10), since it cannot write text inside the canvas element in a way we can poll the pixels afterwards.  If anyone can find a way of making it work with Opera, please write a comment below — I&#8217;d love to be able to support this browser.</p>
<h2>Testing Caveats</h2>
<p>Testing font-smoothing in most Windows web browsers is easy since it can be turned off inside the Display control panel. However, when using Safari for Windows, it is necessary to navigate inside Safari&#8217;s <strong>Appearance</strong> preferences and set the <strong>Font-smoothing</strong> option to <strong>Windows Standard</strong>.  This is because  by default, Safari uses it&#8217;s own built-in font-rendering engine which doesn&#8217;t seem to render aliased fonts.  In Mac OS X, it seems anti-aliasing only works for fonts below a certain size, so aliased fonts don&#8217;t seem to be an issue with that operating system.  In Ubuntu Linux I have yet to find a way of shutting of font-smoothing.  If anyone knows a way, please let me know.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.useragentman.com/blog/2009/11/29/how-to-detect-font-smoothing-using-javascript/feed/</wfw:commentRss>
		<slash:comments>25</slash:comments>
		</item>
		<item>
		<title>More @font-face fun</title>
		<link>http://www.useragentman.com/blog/2009/10/09/more-font-face-fun/</link>
		<comments>http://www.useragentman.com/blog/2009/10/09/more-font-face-fun/#comments</comments>
		<pubDate>Sat, 10 Oct 2009 00:53:36 +0000</pubDate>
		<dc:creator>zoltan</dc:creator>
				<category><![CDATA[CSS]]></category>
		<category><![CDATA[Fonts]]></category>
		<category><![CDATA[SVG]]></category>
		<category><![CDATA[@font-face. opera. chrome]]></category>
		<category><![CDATA[anti-aliasing]]></category>
		<category><![CDATA[cleartype]]></category>
		<category><![CDATA[svg fonts]]></category>

		<guid isPermaLink="false">http://www.useragentman.com/blog/?p=237</guid>
		<description><![CDATA[My first post @font-face in Depth got a huge amount of unexpected attention.  Thank you all for your comments and criticism.  Since the post, there have been quite a bit of information I have read about and thought I should share. SVG fonts for Opera and Chrome Jonathan Snook wrote a great article regarding Opera [...]]]></description>
			<content:encoded><![CDATA[<p>My first post <a href="/blog/2009/09/20/font-face-in-depth/">@font-face in Depth</a> got a <strong>huge</strong> amount of unexpected attention.  Thank you all for your comments and criticism.  Since the post, there have been quite a bit of information I have read about and thought I should share.</p>
<h2>SVG fonts for Opera and Chrome</h2>
<p><a href="http://www.snook.ca/">Jonathan Snook</a> wrote a great article <a href="http://www.snook.ca/archives/html_and_css/becoming-a-font-embedding-master">regarding Opera 10 and Google Chrome supporting SVG fonts</a> (Chrome can load them without any pesky command line switch!).   He mentions how to convert TrueType fonts to this format using <a href="http://xml.apache.org/batik/">Batik</a>, and that by removing all the <code>&lt;hkren&gt;</code> elements inside the file&#8217;s XML can make the font smaller than the original TrueType file.  Too bad the browsers don&#8217;t support this format.</p>
<h2>Opera&#8217;s buggy implementation</h2>
<p>I noticed after I launched my first @font-face article that Opera was not loading the fonts on my blog when users visited more than one page.  It turns out that Opera 10&#8242;s implementation of @font-face is a little buggy.  When you feed it a page with an embedded font, you may not see the embedded fonts properly, like in the screenshot below:</p>
<div id="attachment_238" class="wp-caption aligncenter" style="width: 310px"><a href="http://www.useragentman.com/blog/wp-content/uploads/2009/10/screenshotOpera.png"><img class="size-medium wp-image-238 " title="screenshotOpera" src="http://www.useragentman.com/blog/wp-content/uploads/2009/10/screenshotOpera-300x215.png" alt="screenshotOpera" width="300" height="215" /></a><p class="wp-caption-text">Opera sometimes doesn&#39;t load the font at all, and removes other styling (click to enlarge).</p></div>
<p><a href="http://readableweb.com/opera-admits-font-face-bugs-in-opera-10/">According to Opera</a>, this happens when &#8220;specifying different weights and styles for a single font-family name&#8221;.  Håkon Wium Lie, the company&#8217;s Chief Technology Officer , <a href="http://devfiles.myopera.com/articles/751/webfonts-workaround.html">has described a work-around</a> until the company can fix the problem properly.</p>
<p>I am debating whether to implement the work around on my site, or to wait for Opera to fix the problem and patch all Opera 10 installations via auto-update (I&#8217;m leaning towards the latter).</p>
<h2>Performance Issues With Font Embedding</h2>
<p>One of the comments on the last article came from <a href="http://stevesouders.com/">Steve Souders</a> who had <a href="/blog/2009/09/20/font-face-in-depth/#comment-37">performance concerns with font-embedding</a>.  Paul Irish came back with information showing the <a href="http://paulirish.com/2009/fighting-the-font-face-fout/">when browsers load the fonts, and what they do before the font is loaded</a>.  He also shows how to use JavaScript to make the loading behaviour consistent between browsers.</p>
<p>The performance issues with font-embedding is something I am going to be keeping an eye on, especially when it comes to font-services, which the OpenType site believes will be <a href="http://opentype.info/blog/2009/07/29/why-webfont-services-are-the-future-of-fonts-on-the-web/">the future of web font distribution for commercial fonts</a>.  Although it doesn&#8217;t seem like much of an issue currently (my blog <em>seems</em> to load quickly), I don&#8217;t want to do anything that would slow down my web applications &#8211; especially if I am using them just to make the text look a little prettier.</p>
<h2>Windows XP Rendering Issues</h2>
<p>Boing-Boing had a bad experience with font-embedding.  Turns out <a href="http://www.webmonkey.com/blog/Boing_Boing_s_Redesign_Uncovers_the_Dark_Side_of_Web_Fonts">they got a lot of complaints when they decided to incorporate it into their website</a>.  The reason: some users didn&#8217;t have ClearType turned on, and the font Boing-Boing chose looked &#8220;like ass&#8221; without ClearType.</p>
<p>Below are screenshots of this site without ClearType on:</p>
<div id="attachment_250" class="wp-caption aligncenter" style="width: 449px"><img class="size-full wp-image-250" title="ieNoAA" src="http://www.useragentman.com/blog/wp-content/uploads/2009/10/ieNoAA.png" alt="ieNoAA" width="439" height="336" /><p class="wp-caption-text">Internet Explorer without ClearType</p></div>
<p style="text-align: center;">
<div id="attachment_251" class="wp-caption aligncenter" style="width: 465px"><img class="size-full wp-image-251" title="firefoxNoAA" src="http://www.useragentman.com/blog/wp-content/uploads/2009/10/firefoxNoAA.png" alt="Firefox without ClearType" width="455" height="327" /><p class="wp-caption-text">Firefox without ClearType</p></div>
<p>The article stated that there are quite a few XP computers which apparently don&#8217;t have ClearType on by default. I wonder if there are any stats to back that up. If so, then developers embed fonts into pages should test that scenario as well.  It does look different.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.useragentman.com/blog/2009/10/09/more-font-face-fun/feed/</wfw:commentRss>
		<slash:comments>9</slash:comments>
		</item>
		<item>
		<title>@font-face in Depth</title>
		<link>http://www.useragentman.com/blog/2009/09/20/font-face-in-depth/</link>
		<comments>http://www.useragentman.com/blog/2009/09/20/font-face-in-depth/#comments</comments>
		<pubDate>Sun, 20 Sep 2009 04:04:13 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[CSS]]></category>
		<category><![CDATA[@font-face]]></category>
		<category><![CDATA[embedded font]]></category>
		<category><![CDATA[eot]]></category>
		<category><![CDATA[font]]></category>
		<category><![CDATA[font-embedding]]></category>
		<category><![CDATA[fontforge]]></category>
		<category><![CDATA[otf]]></category>
		<category><![CDATA[ttf]]></category>
		<category><![CDATA[ttf2eot]]></category>

		<guid isPermaLink="false">http://www.useragentman.com/blog/?p=13</guid>
		<description><![CDATA[The fact that we can now choose any font to embed inside our web pages and applications using @font-face is something to celebrate, and removes a long existing set of handcuffs placed on web designers.  This article covers how to use @font-face in all browsers, as well as some interesting little-known technical details.]]></description>
			<content:encoded><![CDATA[<div class="importantNotes">
<h3>Update (Oct 13, 2009):</h3>
<p>Please see the <a href="/blog/2009/10/09/more-font-face-fun/">follow up article</a> which explores SVG fonts,<br />
	Opera 10 issues, and more</p>
</div>
<div id="attachment_28" class="wp-caption alignleft" style="width: 250px"><a title="image Flickr page" href="http://www.flickr.com/photos/martab/1757415036/" target="_blank"><img class="size-full wp-image-28      " title="image Flickr page" src="http://www.useragentman.com/blog/wp-content/uploads/2009/09/fontintro2.jpg" alt="" width="240" height="180" /></a><p class="wp-caption-text">Photo by marta.b (Source: Flickr)</p></div>
<p>When I told one of my co-workers how excited I was that almost all the major browser vendors now supported font-embedding in their products, I got the same reaction from her that Bert from Sesame Street got when he told Ernie how <a title="Link to classic Sesame Street clip &quot;Bert's Bottlecaps&quot; via YouTube" href="http://www.youtube.com/watch?v=2Ls2vNfQxyc">exciting he thought bottle cap collecting was</a> (yes, gratuitous ridicule was involved).  Now, while I can somewhat understand her disinterest (she is not a front-end web developer, but a Java programmer), I defend my enthusiasm by stating here that fonts are a really important part of web design, and the fact that we can now theoretically choose <strong>any</strong> font to embed inside our web pages and applications is something to celebrate.</p>
<p>Sure, workarounds such as <a title="Mike Davidson article &quot;sIFR 2.0: Rich Accessible Typography for the Masses&quot;" href="http://www.mikeindustries.com/blog/sifr/">sIFR</a> and <a title="Cufón homepage" href="http://cufon.shoqolate.com/generate/">Cufón</a> have existed for quite a while now, but they have drawbacks (they both are best used only for headings, and Cufón  doesn&#8217;t do well at allowing copying and pasting its content).  If you really want to embed fonts in your webpages, I believe that @font-face is the safest, future-proof option.</p>
<h2>Why care about fonts?</h2>
<p>My Java developer colleague may not understand why fonts are important, but she is a regular reader of  <a href="http://www.newyorker.com/">The New Yorker</a>.  Even though its website doesn&#8217;t use @font-face embedding (the standard New Yorker font is embedded in site using images), the developers of the site obviously know why fonts are important:</p>
<ul>
<li><strong>Fonts identify who the content belongs to.</strong> Just take a look at the content and without looking at the URL bar, you know exactly where you are &#8211; that font is highly integrated into that organization&#8217;s identity and branding.  Replace the New Yorker font with Arial and it&#8217;ll look like any old website.</li>
<li><strong>Fonts given an emotional association to the content. </strong>The content has an air of authority because of the font it uses (after all, it looks like it was written by The New Yorker).  It is the reason why phishing sites use the fonts, colours and logos of the banks they are posing as.</li>
<li><strong>Fonts can help (or hinder) the legability of content.</strong> Scanning the content, you can tell where the article titles are, where the navigation is, and what section each of the articles belong to.  This is all because of the way fonts are used on the site.</li>
</ul>
<div id="attachment_60" class="wp-caption aligncenter" style="width: 460px"><img class="size-full wp-image-60  " title="newYorkerScreenshot" src="http://www.useragentman.com/blog/wp-content/uploads/2009/09/newYorkerScreenshot1.jpg" alt="newYorkerScreenshot" width="450" height="206" /><p class="wp-caption-text">The New Yorker&#39;s website is a great example of how to use fonts effectively on the web.</p></div>
<p style="text-align: left;">The New Yorker uses images in order to display fonts.  Using font embedding effectively would save on bandwidth, since one font download could be used for all the headings on the page.</p>
<h2>Font formats supported</h2>
<p>Firefox 3.5+ , Safari 3.1+, Opera 10+ and Internet Explorer 4.0+ all support @font-face embedding (Google&#8217;s Chrome 3.0 beta does as well, but <a href="http://opentype.info/blog/2009/04/16/google-chrome-with-webfonts-support/">users will have to start it up in a special mode to make it work</a> in the short term).  They all support the use of <a title="Wikipedia TrueType entry" href="http://en.wikipedia.org/wiki/TrueType">TrueType</a> (.ttf) and <a title="Wikipedia OpenType entry" href="http://en.wikipedia.org/wiki/OpenType">OpenType</a> (.otf) fonts &#8230; <strong>except</strong> for Internet Explorer which only supports the proprietary <a title="Wikipedia Embedded OpenType entry" href="http://en.wikipedia.org/wiki/Embedded_Open_Type">Embedded Open Type</a> (.eot) format.  There aren&#8217;t as many fonts available in this format and the tool Microsoft gives out that converts .ttf fonts to .eot, called <a href="http://www.microsoft.com/typography/WEFT.mspx">WEFT</a>, is quite frustrating to use, and even crashes on Vista.  There is, however, an excellent command-line tool, <a href="http://code.google.com/p/ttf2eot/">ttf2eot</a>, that does a much better job. Both WEFT and ttf2eot only convert TrueType to Embedded Open Type &#8211; if you want to convert an <em>OpenType</em> font to Embedded Open Type, it is necessary to use <a href="http://fontforge.sourceforge.net/">FontForge</a>, a free font-editing tool, to convert the font to TrueType first, and then use ttf2eot to convert the resultant font to .eot format.  Although this method is roundabout and results in loss of detail due to the way the font formats store information, it is the only way I know to do this conversion.</p>
<p>For more detail on how to perform .eot font conversions using ttf2eot and FontForge, check out Edward O&#8217;Connor&#8217;s great article <cite><a href="http://edward.oconnor.cx/2009/07/how-to-create-eot-files-without-microsoft-weft">How to create EOT files without Microsoft WEFT</a></cite></p>
<h2>Where to get fonts you can legally use in web pages.</h2>
<p>Although it is now technically possible to embed almost any font inside a web page, it is important to remember that if you use a commercial font for this purpose, you must have a license to do this.  Since putting a font on a website will allow anyone on the Web to download it, a lot of font foundries currently do not allow their fonts to be used for web page embedding. It is for this reason that the browser vendors have been reluctant to implement this technology into their products until recently (font embedding is part of the CSS2 specification, which has been around since 1998).</p>
<p>Luckily, there are a few sites that are sprouting up that have done the initial work for you by listing fonts that allow font embedding on web pages legally.  The ones that I have visited include:</p>
<ul>
<li><a href="http://www.fontsquirrel.com">Font Squirrel&#8217;s</a> <a href="http://www.fontsquirrel.com/fontface">@font-face page</a>, which include @font-face kits that include an .eot as well as an .otf or .ttf font.  Each kit also includes <strong></strong>a stylesheet which can be linked directly into your document so you can use the font with minimal coding.</li>
<li><a href="http://www.smashingmagazine.com/">Smashing Magazine&#8217;s</a> has an excellent article, <cite><a href="http://www.smashingmagazine.com/2007/11/08/40-excellent-freefonts-for-professional-design/">40+ Excellent Freefonts For Professional Design</a></cite></li>
<li><a href="http://kernest.com/">Kernest.com</a> hosts a whole slew of fonts that can be used for font-embedding.</li>
<li><a href="http://webfonts.info">Webfonts.info</a> has a page dedicated to <a href="http://webfonts.info/wiki/index.php?title=Fonts_available_for_%40font-face_embedding">fonts available for @font-face embedding</a>.</li>
</ul>
<p>Note that whenever you use <strong>any</strong> font, make sure you check the license that comes with it.  Some fonts, even if their license allows for font embedding, may not allow you to use it for commercial purposes.  Others may, but only for a fee.  To be safe, check the documentation that comes with the font.</p>
<h2>How to insert a font into your webpage</h2>
<p>Once you have a font that you&#8217;d like to insert into your webpage, you first have to declare it inside your CSS stylesheet:</p>
<blockquote class="code">
<pre>@font-face {
	font-family: 'Droid Sans';

        /* for IE */
	src: url('/shared/fonts/DroidSans/DroidSans.eot');

        /*
         * for non-IE: first see if the font exists locally on the browser's
         * computer.  If so, use that copy of the font.  Otherwise, load it
         * from the server
         */
	src: local('Droid Sans'),
	     url('/shared/fonts/DroidSans/DroidSans.ttf') format('truetype');
}</pre>
</blockquote>
<p>It is very important to note here that <strong>order counts</strong>.  There are variations of this CSS syntax, but I have settled on this syntax for the reasons described in Paul Irish&#8217;s article <a href="http://paulirish.com/2009/bulletproof-font-face-implementation-syntax/">Bulletproof @font-face syntax</a>.</p>
<p>After you declare the font, you can use it inside any of your selectors as you would use any other font:</p>
<blockquote class="code">
<pre>body {
     font-family: "Droid Sans", "Arial", "Helvetica", sans-serif;
}</pre>
</blockquote>
<p>Note that it is still necessary to use alternative fonts &#8211; this is so that user agents that don&#8217;t support @font-face will still show an appropriate, similar looking font.</p>
<p>Some font families are contained in not just one file.  For example, Droid Sand  has a bold variant that is stored in a separate TrueType file (the .eot file used by Internet Explorer in the above example contains both the regular and bold variants).  The bold variant of Droid Sans can be declared correctly in your stylesheet like this:</p>
<blockquote class="code">
<pre>@font-face {
	font-family: 'Droid Sans';
	src: url('/shared/fonts/DroidSans/DroidSans.eot');
	src: local('Droid Sans'),
	     local('Droid Sans'),
	     url('/shared/fonts/DroidSans/DroidSans.ttf') format('truetype');
}

/* Bold declaration only for non-IE browsers */
@font-face {
	font-family: 'Droid Sans';
	src: local('Droid Sans Bold'),
	     local('Droid Sans-Bold'),
	     url('/shared/fonts/DroidSans/DroidSans-Bold.ttf') format('truetype');
	font-weight: bold;
}</pre>
</blockquote>
<h2>Browser Differences</h2>
<p>To effectively illustrate how differently browsers render embedded fonts, let&#8217;s compare them side by side:</p>
<table border="0">
<thead>
<tr>
<th></th>
<th>Safari 3</p>
<p>Windows</th>
<th>Firefox 3.5</p>
<p>Windows</th>
<th>Explorer 6</p>
<p>Windows</th>
</tr>
</thead>
<tbody>
<tr>
<th>Title of this blog post</th>
<td><img title="Screenshot Detail 1 (Safari)" src="http://www.useragentman.com/blog/wp-content/uploads/2009/09/safari-font-11.png" alt="Screenshot Detail 1 (Safari)" /></td>
<td><img title="Screenshot Detail 1 (Firefox)" src="http://www.useragentman.com/blog/wp-content/uploads/2009/09/firefox-font-12.png" alt="Screenshot Detail 1 (Firefox)" /></td>
<td><img title="Screenshot Detail 1 (IE)" src="http://www.useragentman.com/blog/wp-content/uploads/2009/09/ie-font-11.png" alt="Screenshot Detail 1 (IE)" /></td>
</tr>
<tr>
<th>Close-up of the title of this blog post</th>
<td><img title="Screenshot Closeup 1a (Safari)" src="http://www.useragentman.com/blog/wp-content/uploads/2009/09/safari-font-1-detail2.png" alt="Screenshot Closeup 1a (Safari)" /></td>
<td><img title="Screenshot Closeup 1a (Firefox)" src="http://www.useragentman.com/blog/wp-content/uploads/2009/09/firefox-font-1-detail.png" alt="Screenshot Closeup 1a (Firefox)" /></td>
<td><img title="Screenshot Closeup 1a (IE)" src="http://www.useragentman.com/blog/wp-content/uploads/2009/09/ie-font-1-detail.png" alt="Screenshot Closeup 1a (IE)" /></td>
</tr>
<tr>
<th>Subheading from this blog post</th>
<td><img title="Screenshot Detail 2 (Safari)" src="http://www.useragentman.com/blog/wp-content/uploads/2009/09/safari-font-1-detail1.png" alt="Screenshot Detail 2 (Safari)" /></td>
<td><img title="Screenshot Detail 2 (Firefox)" src="http://www.useragentman.com/blog/wp-content/uploads/2009/09/firefox-font-2.png" alt="Screenshot Detail 2 (Firefox)" /></td>
<td><img title="Screenshot Detail 2 (IE)" src="http://www.useragentman.com/blog/wp-content/uploads/2009/09/ie-font-2.png" alt="Screenshot Detail 2 (IE)" /></td>
</tr>
<tr>
<th>Close-up of subheading of this blog post</th>
<td><img title="Screenshot Closeup 2 (Safari)" src="http://www.useragentman.com/blog/wp-content/uploads/2009/09/safari-font-2-detail.png" alt="Screenshot Closeup 2 (Safari)" /></td>
<td><img title="Screenshot Closeup 2 (Firefox)" src="http://www.useragentman.com/blog/wp-content/uploads/2009/09/firefox-font-2-detail.png" alt="Screenshot Closeup 2 (Firefox)" /></td>
<td><img title="Screenshot Closeup 2 (IE)" src="http://www.useragentman.com/blog/wp-content/uploads/2009/09/ie-font-2-detail.png" alt="Screenshot Closeup 2 (IE)" /></td>
</tr>
<tr>
<th>Close-up of main text of this blog post</th>
<td><img title="Screenshot Closeup 3 (Safari)" src="http://www.useragentman.com/blog/wp-content/uploads/2009/09/safari-3-detail.png" alt="Screenshot Closeup 3 (Safari)" /></td>
<td><img title="Screenshot Closeup 3 (Firefox)" src="http://www.useragentman.com/blog/wp-content/uploads/2009/09/firefox-3-detail.png" alt="Screenshot Closeup 3 (Firefox)" /></td>
<td><img title="Screenshot Closeup 3 (IE)" src="http://www.useragentman.com/blog/wp-content/uploads/2009/09/ie-3-detail.png" alt="Screenshot Closeup 3 (IE)" /></td>
</tr>
</tbody>
</table>
<p>As you may see, there are some slight differences in the way that fonts are rendered in the various browsers:</p>
<ul>
<li><strong>Fonts displayed on Safari for Windows (and all browsers on the Mac) in my opinion look the nicest</strong>: they are nicely smoothed out and the text is quite easily read at smaller sizes.  This is due to the way Apple products render fonts.  <a title="Joel Spolsky article &quot;Font smoothing, anti-aliasing, and sub-pixel rendering&quot;" href="http://www.joelonsoftware.com/items/2007/06/12.html">Apple has always differed in philosophy with Microsoft on how to render fonts</a>, and this difference is quite obvious when testing pages under the Windows operating system.</li>
<li><strong>I have found that Internet Explorer does a poor job when displaying fonts converted from OpenType</strong> — the glyphs tend to be more jagged and some elements tend to be rendered thinner than they should be. GrauBlau Web, the font used in the headings of  this page, is a converted OpenType font, and you can see this effect in the close-ups above. It does not occur in the main body of the text because Droid Sans is a TrueType font.  There seems to be two reasons for this:
<ol>
<li>There was information loss when converting GrauBlau Web from OpenType to TrueType.</li>
<li>In Explorer, <a href="http://en.wikipedia.org/wiki/ClearType">ClearType</a> renders this font using subpixel anti-aliasing quite more severely than Safari, while other Windows browsers use vanilla anti-aliasing to get the job done.  A <a href="http://www.luxography.com/">friend of mine</a> pointed this out, and he said that it&#8217;s because ClearType renders TrueType (and, it seems, Embedded Open Type) fonts with subpixel anti-aliasing, but OpenType for some reason isn&#8217;t.  I don&#8217;t know the reasons why, but in this example it looks like it is true &#8211; if you look at all of the Explorer closeups,you&#8217;ll see the coloured pixels on the edge of all the glyphs, while in Firefox only Droid Sans (which is a TrueType font) has them.</li>
</ol>
</li>
<li><strong>Firefox only allows you to embed fonts that come from the same server</strong>, unless some special headers appear in the HTTP server response.  Also, unlike the other browsers, <strong>Firefox users first see the page without the embedded font <em>until</em> the font is loaded completely</strong>, at which time the fonts suddenly appear on the page. While it can be argued that this is a good thing (the user can read the content immediately without having to rely on the special font), this may not what a web designer would consider desirable.</li>
</ul>
<p>There are a few more differences, which are better outlined at the <a href="http://webfonts.info/wiki/index.php?title=Main_Page">Web Fonts</a> Wiki, which seems to keep its information up-to-date.</p>
<h2>How to choose which fonts to use</h2>
<p>Not having gone to design school, I cannot claim to be an authority on this subject, but I think it is safe to say that when choosing the fonts that you are going to use, it should be in sync with the mood and message you want readers to experience.  Chuck Green&#8217;s article, <a href="http://www.ideabook.com/tutorials/typography/type_palettes.html">Type palettes</a>, goes into more depth on this subject, and Michael Martin of Smashing Magazine has a great article about <a href="http://www.smashingmagazine.com/2009/08/20/typographic-design-survey-best-practices-from-the-best-blogs/">Typographic Design Patterns and Best Practices</a>.  Although both of these articles don&#8217;t go into font-embedding specifically, they are still relevant reading.</p>
<p>It took me a long time to decide on the fonts I have used here on my blog. When designing the layout, I was going for two things:</p>
<ol>
<li>I wanted the body text to be easy to read</li>
<li>I wanted the feeling of the whole blog to be friendly and not look like an academic paper</li>
</ol>
<p>In the end I decided to use:</p>
<ol>
<li><a href="http://www.fonts.info/info/press/free-fonts-for-font-face-embedding.htm">GrauBlau Web</a> for the headers because it is narrow (which allows more text on a line), easy on the eyes, and since it is used on headers, the user notices it right away and thinks &#8220;Hey, this looks different!&#8221;.</li>
<li><a href="http://www.droidfonts.com/info/droid-sans-fonts/">Droid Sans</a> for the main text because it was designed to be easy to read even on low resolutions and, in combination with GrauBlau Web, gave the site a nice, pleasant and friendly look.</li>
<li><a href="http://www.droidfonts.com/info/droid-sans-mono-fonts/">Droid Sans Mono</a> for the code listings since it was designed to compliment Droid Sans, and, I believe, looks a jillion times better than Courier (which everyone up to now has been forced to use due to a lack of nice looking monospace fonts installed on most computers today).</li>
</ol>
<p>Again, I didn&#8217;t go to design school, but I think for the type of site this is (a blog about web technology), it suits its purpose rather well.   I <strong>was</strong> tempted to choose more ornate fonts, but I didn&#8217;t think they suited the content.</p>
<h2>Optimizing Font Files</h2>
<p>Sometimes you may find that the font files are a little larger than you&#8217;d like.  If you open up the font file in <a href="http://fontforge.sourceforge.net/">FontForge</a>, you may find that a lot of the glyphs inside are not necessary for what you are doing (for example, if your pages are all in English, you might not want all the accented characters that the font contains).  Use FontForge to delete the glyphs you don&#8217;t need and regenerate the font &#8211; you&#8217;ll see that you save quite a few kilobytes, especially in a Unicode font with lots of Japanese and Russian characters included.</p>
<p>Again, because of licensing issues, please ensure that the font license allows you to edit the font in this manner.</p>
<h2>Conclusion</h2>
<p>What is written here is just the tip of the iceberg.  In the next few years, I expect to be reading several articles on the best way to use embedded fonts, from technical, usability, internationalization and design perspectives. Unfortunately, I also expect to see a lot of MySpace pages looking uglier than ever.  Imagine! Now, teenagers around the world can have animated backgrounds, Black Eyed Peas songs playing on load, <strong>and</strong> have that <a href="http://www.fontsquirrel.com/fonts/junkos-typewriter">crazy typewriter font</a> on the main copy.</p>
<p>My God, what have we done &#8230;.</p>
<div id="_mcePaste" style="overflow: hidden; position: absolute; left: -10000px; top: 1368px; width: 1px; height: 1px;">http://webfonts.info/wiki/index.php?title=Fonts_available_for_%40font-face_embedding</div>
]]></content:encoded>
			<wfw:commentRss>http://www.useragentman.com/blog/2009/09/20/font-face-in-depth/feed/</wfw:commentRss>
		<slash:comments>64</slash:comments>
		</item>
	</channel>
</rss>
