<?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>Ryan Morr &#187; CSS</title>
	<atom:link href="http://ryanmorr.com/archives/category/css/feed" rel="self" type="application/rss+xml" />
	<link>http://ryanmorr.com</link>
	<description>JavaScript, CSS, and Web Apps</description>
	<lastBuildDate>Fri, 27 Aug 2010 13:28:41 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.1</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Leveraging the Most out of CSS Classes</title>
		<link>http://ryanmorr.com/archives/leveraging-the-most-out-of-css-classes</link>
		<comments>http://ryanmorr.com/archives/leveraging-the-most-out-of-css-classes#comments</comments>
		<pubDate>Fri, 27 Aug 2010 03:06:44 +0000</pubDate>
		<dc:creator>Ryan Morr</dc:creator>
				<category><![CDATA[Articles]]></category>
		<category><![CDATA[CSS]]></category>
		<category><![CDATA[UI]]></category>
		<category><![CDATA[XHTML]]></category>
		<category><![CDATA[DOM]]></category>

		<guid isPermaLink="false">http://ryanmorr.com/?p=1807</guid>
		<description><![CDATA[To harness or exploit the most out of CSS classes is to take advantage of what truly is the unsung hero of advanced RIA development as it relates to the presentation layer. When we observe our markup, we find that various portions of the DOM may require styling influences on the initial page load as [...]]]></description>
			<content:encoded><![CDATA[<p>To harness or exploit the most out of CSS classes is to take advantage of what truly is the unsung hero of advanced RIA development as it relates to the presentation layer. When we observe our markup, we find that various portions of the DOM may require styling influences on the initial page load as well as interaction events or cues to adjust its structure, positioning, visibility, state, and skin. For this classes are the clear suitor, as it brings flexibility, modularity, inheritance, and a dynamic and unobtrusive nature to the table.<span id="more-1807"></span></p>
<h3>Benefits</h3>
<p>One benefit in particular is in a nodes ability to support multiple class declarations at a time, which in a sense facilitates a multiple inheritance of sorts. This gives us the unique ability within CSS to separate an element&#8217;s various states and differing characteristics to create a more modular approach in our stylesheets. By doing so, we can create classes that are in no way specific to any one element but merely represents a characteristic or group of characteristics that any node can adopt provided the class name.</p>
<p>Inheritance can also play a key role in the design process, by letting the cascade flow through the DOM we can leverage the styles of ancestor nodes and avoid a rigid implementation with redundant style declarations. If we employ this technique, we can easily alter state of descendant nodes by adding or removing a class to or from that of an ancestor node while avoiding the need to traverse the DOM to manipulate each descendant node individually in our script.</p>
<p>As far as client-side scripting is concerned, classes are the preferred window to CSS from JavaScript. By hiding styling characteristics behind class names, JavaScript can merely alter a node&#8217;s <em>className</em> attribute to activate or deactivate a specific state of that node. In this case, JavaScript is not concerned with what styling influences are taking place on page load or even after event triggered changes in state. This allows  us to quickly consult our CSS to make minor styling changes rather than JavaScript.</p>
<h3>Abstract Classes</h3>
<p>Abstract classes are those that do not necessarily reflect a state or any specific node but common characteristics shared between nodes of varying purpose and context. Take for instance the following:</p>

<div class="wp_syntax"><div class="code"><pre class="css" style="font-family:monospace;"><span style="color: #6666ff;">.widget</span> <span style="color: #00AA00;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">font-family</span><span style="color: #00AA00;">:</span> Trebuchet MS<span style="color: #00AA00;">,</span> Tahoma<span style="color: #00AA00;">,</span> Verdana<span style="color: #00AA00;">,</span> Arial<span style="color: #00AA00;">,</span> <span style="color: #993333;">sans-serif</span><span style="color: #00AA00;">;</span>
    <span style="color: #000000; font-weight: bold;">font-size</span><span style="color: #00AA00;">:</span> <span style="color: #933;">1.1em</span><span style="color: #00AA00;">;</span>
<span style="color: #00AA00;">&#125;</span>
&nbsp;
<span style="color: #6666ff;">.overlay</span> <span style="color: #00AA00;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">position</span><span style="color: #00AA00;">:</span> <span style="color: #993333;">absolute</span><span style="color: #00AA00;">;</span>
    <span style="color: #000000; font-weight: bold;">top</span><span style="color: #00AA00;">:</span> <span style="color: #cc66cc;">0</span><span style="color: #00AA00;">;</span>
    <span style="color: #000000; font-weight: bold;">left</span><span style="color: #00AA00;">:</span> <span style="color: #cc66cc;">0</span><span style="color: #00AA00;">;</span>
    <span style="color: #000000; font-weight: bold;">width</span><span style="color: #00AA00;">:</span> <span style="color: #933;"><span style="color: #cc66cc;">100</span>%</span><span style="color: #00AA00;">;</span>
    <span style="color: #000000; font-weight: bold;">height</span><span style="color: #00AA00;">:</span> <span style="color: #933;"><span style="color: #cc66cc;">100</span>%</span><span style="color: #00AA00;">;</span>
<span style="color: #00AA00;">&#125;</span>
&nbsp;
<span style="color: #6666ff;">.content</span> <span style="color: #00AA00;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">display</span><span style="color: #00AA00;">:</span> <span style="color: #993333;">block</span><span style="color: #00AA00;">;</span>
    -khtml-box-sizing<span style="color: #00AA00;">:</span> content-box<span style="color: #00AA00;">;</span>
    -webkit-box-sizing<span style="color: #00AA00;">:</span> content-box<span style="color: #00AA00;">;</span>
    -moz-box-sizing<span style="color: #00AA00;">:</span> content-box<span style="color: #00AA00;">;</span>
    -ms-box-sizing<span style="color: #00AA00;">:</span> content-box<span style="color: #00AA00;">;</span>
    -o-box-sizing<span style="color: #00AA00;">:</span> content-box<span style="color: #00AA00;">;</span>
    box-sizing<span style="color: #00AA00;">:</span> content-box<span style="color: #00AA00;">;</span>
    zoom<span style="color: #00AA00;">:</span> <span style="color: #cc66cc;">1</span><span style="color: #00AA00;">;</span>
<span style="color: #00AA00;">&#125;</span></pre></div></div>

<p>As you can see these examples are of a very generic nature both in name and styling influences as they represent a very high level abstraction. Typically classes such as these should always be combined with others as part of a node&#8217;s class declaration to round out its structure, positioning, state, and skin.</p>
<p>The advantage of abstract classes do not come solely in their ability to apply styles to a node in a modular fashion, but also in the relationships they create. Think of it from the perspective of object-oriented programming, provided the overlay class, a node will inherit the role of an overlay. For example:</p>

<div class="wp_syntax"><div class="code"><pre class="html4strict" style="font-family:monospace;"><span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">div</span> <span style="color: #000066;">class</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;widget&quot;</span>&gt;</span>
    <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">div</span> <span style="color: #000066;">class</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;overlay widget&quot;</span>&gt;</span>
        <span style="color: #808080; font-style: italic;">&lt;!-- widget markup --&gt;</span>
    <span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">div</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">div</span>&gt;</span></pre></div></div>

<p>In this case, the parent node <em>is a</em> widget and <em>has a</em> widget and <em>has a</em> overlay while the child node <em>is a</em> widget and <em>is a</em> overlay. Although the child node is both an overlay and a widget, it is important to recognize that this node can be referenced by either statement together or independently of the other, like so:</p>

<div class="wp_syntax"><div class="code"><pre class="css" style="font-family:monospace;"><span style="color: #6666ff;">.widget</span> <span style="color: #6666ff;">.overlay</span> <span style="color: #00AA00;">&#123;</span>
    <span style="color: #808080; font-style: italic;">/* target overlays that belong to widgets */</span>
<span style="color: #00AA00;">&#125;</span>
&nbsp;
<span style="color: #6666ff;">.widget</span> <span style="color: #6666ff;">.widget</span> <span style="color: #00AA00;">&#123;</span>
    <span style="color: #808080; font-style: italic;">/* target widgets that belong to widgets */</span>
<span style="color: #00AA00;">&#125;</span>
&nbsp;
<span style="color: #6666ff;">.widget</span> <span style="color: #6666ff;">.widget</span><span style="color: #6666ff;">.overlay</span> <span style="color: #00AA00;">&#123;</span>
    <span style="color: #808080; font-style: italic;">/* target overlays that are also widgets that belong to widgets */</span>
<span style="color: #00AA00;">&#125;</span></pre></div></div>

<p>By grouping nodes together in this manner we can target nodes based on their role, state, structure, positioning, document position, or relativity to other nodes. From here we let inheritance fill in the gaps; as we move further down the DOM hierarchy the more precise and relevant our styles become to the specific nodes to which they are applied. Width and height, for example, should typically be reserved for styling in a low level context, while font (family, size, and color) is best served at a high level. Not to mention that this grouping makes it very easy for JavaScript to quickly reference or query for various nodes in various contexts for fast and easy manipulation.</p>
<h3>Character Classes</h3>
<p>Similar to abstract classes, character classes work on a lower level context and are designed to fulfil one purpose relating to a characteristic or group of characteristics. They help to achieve both visual and cross-browser consistency across an application in the purpose that they serve. For example, the following are some character classes of varying purposes:</p>

<div class="wp_syntax"><div class="code"><pre class="css" style="font-family:monospace;"><span style="color: #6666ff;">.hidden</span> <span style="color: #00AA00;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">display</span><span style="color: #00AA00;">:</span> <span style="color: #993333;">none</span><span style="color: #00AA00;">;</span>
<span style="color: #00AA00;">&#125;</span>
&nbsp;
<span style="color: #6666ff;">.float-left</span> <span style="color: #00AA00;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">float</span><span style="color: #00AA00;">:</span> <span style="color: #000000; font-weight: bold;">left</span><span style="color: #00AA00;">;</span>
    <span style="color: #000000; font-weight: bold;">display</span><span style="color: #00AA00;">:</span> <span style="color: #993333;">inline</span><span style="color: #00AA00;">;</span>
<span style="color: #00AA00;">&#125;</span>
&nbsp;
<span style="color: #6666ff;">.center</span> <span style="color: #00AA00;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">margin-left</span><span style="color: #00AA00;">:</span> <span style="color: #993333;">auto</span><span style="color: #00AA00;">;</span>
    <span style="color: #000000; font-weight: bold;">margin-right</span><span style="color: #00AA00;">:</span> <span style="color: #993333;">auto</span><span style="color: #00AA00;">;</span>
<span style="color: #00AA00;">&#125;</span>
&nbsp;
<span style="color: #6666ff;">.text-overflow</span> <span style="color: #00AA00;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">width</span><span style="color: #00AA00;">:</span> <span style="color: #933;"><span style="color: #cc66cc;">100</span>%</span><span style="color: #00AA00;">;</span>
    text-<span style="color: #000000; font-weight: bold;">overflow</span><span style="color: #00AA00;">:</span> ellipsis<span style="color: #00AA00;">;</span>
    -o-text-<span style="color: #000000; font-weight: bold;">overflow</span><span style="color: #00AA00;">:</span> ellipsis<span style="color: #00AA00;">;</span>
    <span style="color: #000000; font-weight: bold;">white-space</span><span style="color: #00AA00;">:</span> <span style="color: #993333;">nowrap</span><span style="color: #00AA00;">;</span>
    <span style="color: #000000; font-weight: bold;">overflow</span><span style="color: #00AA00;">:</span> <span style="color: #993333;">hidden</span><span style="color: #00AA00;">;</span>
<span style="color: #00AA00;">&#125;</span></pre></div></div>

<p>Typically character classes do not need to be overridden in a lower level context as they should be mutually exclusive from your structure and skin. You&#8217;ll also want to avoid depending on character classes to target descendants as part of a selector statement. Doing so will expand the scope of the class beyond its original purpose, this can snow ball into further dependencies that result in a loss of modularity and flexibility. </p>
<p>Exploiting character classes to the fullest means easy adoption of shared markup conventions and allow for ease in code integration. By putting together a small component library, you will find that they will not only provide reusable classes throughout an entire application but also amongst projects.</p>
<h3>Themed Classes</h3>
<p>As you might have guessed, themed classes have everything to do with your skin, this means fonts, colors, background images, formatting, icons, and states. Take for instance the following:</p>

<div class="wp_syntax"><div class="code"><pre class="css" style="font-family:monospace;"><span style="color: #6666ff;">.loading</span> <span style="color: #00AA00;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">cursor</span><span style="color: #00AA00;">:</span> <span style="color: #993333;">wait</span><span style="color: #00AA00;">;</span>
<span style="color: #00AA00;">&#125;</span>
&nbsp;
<span style="color: #6666ff;">.active</span> <span style="color: #00AA00;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">border</span><span style="color: #00AA00;">:</span> <span style="color: #933;">1px</span> <span style="color: #993333;">solid</span> <span style="color: #cc00cc;">#327E04</span><span style="color: #00AA00;">;</span>
    <span style="color: #000000; font-weight: bold;">background</span><span style="color: #00AA00;">:</span> <span style="color: #993333;">transparent</span> <span style="color: #993333;">url</span><span style="color: #00AA00;">&#40;</span><span style="color: #ff0000; font-style: italic;">active-state.png</span><span style="color: #00AA00;">&#41;</span> <span style="color: #933;"><span style="color: #cc66cc;">50</span>%</span> <span style="color: #933;"><span style="color: #cc66cc;">50</span>%</span> <span style="color: #993333;">repeat-x</span><span style="color: #00AA00;">;</span>
    <span style="color: #000000; font-weight: bold;">font-weight</span><span style="color: #00AA00;">:</span> <span style="color: #993333;">normal</span><span style="color: #00AA00;">;</span>
    <span style="color: #000000; font-weight: bold;">color</span><span style="color: #00AA00;">:</span> <span style="color: #cc00cc;">#ffffff</span><span style="color: #00AA00;">;</span>
    <span style="color: #000000; font-weight: bold;">outline</span><span style="color: #00AA00;">:</span> <span style="color: #993333;">none</span><span style="color: #00AA00;">;</span>
<span style="color: #00AA00;">&#125;</span>
&nbsp;
<span style="color: #6666ff;">.disabled</span> <span style="color: #00AA00;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">cursor</span><span style="color: #00AA00;">:</span> not-allowed<span style="color: #00AA00;">;</span>
    -ms-filter<span style="color: #00AA00;">:</span> <span style="color: #ff0000;">&quot;progid:DXImageTransform.Microsoft.Alpha(Opacity=60)&quot;</span><span style="color: #00AA00;">;</span>
    filter<span style="color: #00AA00;">:</span> alpha<span style="color: #00AA00;">&#40;</span>opacity<span style="color: #00AA00;">=</span><span style="color: #cc66cc;">60</span><span style="color: #00AA00;">&#41;</span><span style="color: #00AA00;">;</span>
    opacity<span style="color: #00AA00;">:</span> <span style="color: #cc66cc;">0.6</span><span style="color: #00AA00;">;</span>
    -moz-user-input<span style="color: #00AA00;">:</span> disabled<span style="color: #00AA00;">;</span>
    -moz-user-focus<span style="color: #00AA00;">:</span> ignore<span style="color: #00AA00;">;</span>
    -moz-user-modifty<span style="color: #00AA00;">:</span> read-only<span style="color: #00AA00;">;</span>
    -khtml-user-modify<span style="color: #00AA00;">:</span> read-only<span style="color: #00AA00;">;</span>
    -webkit-user-modify<span style="color: #00AA00;">:</span> read-only<span style="color: #00AA00;">;</span>
<span style="color: #00AA00;">&#125;</span></pre></div></div>

<p>Classes such as these are most useful for when theming various user interface widgets. We can use a group of semantic presentation classes to indicate the state of a widget such as default, hover, active, and disabled. As the user interacts with the page, we dynamically apply the appropriate class to the widget to represent its current state. This level of class consistency makes it easy to ensure that all nodes with a similar role or state will look the same across all widgets.</p>
<p>Like abstract classes, themed classes also create relationships, however these relationships are more targeted towards a node’s state as opposed to its role. This makes it simple to target nodes based on their current state, for instance active nodes:</p>

<div class="wp_syntax"><div class="code"><pre class="css" style="font-family:monospace;"><span style="color: #6666ff;">.overlay</span><span style="color: #6666ff;">.active</span> <span style="color: #6666ff;">.header</span> <span style="color: #00AA00;">&#123;</span>
    <span style="color: #808080; font-style: italic;">/* target the header of active overlays */</span>
<span style="color: #00AA00;">&#125;</span>
<span style="color: #6666ff;">.overlay</span><span style="color: #6666ff;">.active</span> <span style="color: #6666ff;">.body</span> <span style="color: #00AA00;">&#123;</span>
    <span style="color: #808080; font-style: italic;">/* target the body of active overlays */</span>
<span style="color: #00AA00;">&#125;</span></pre></div></div>

<p>Here we can add additional rules to automatically style the descendants of a node based on its state without directly referencing the descendants in CSS via additional class names or in JavaScript via DOM traversal. </p>
<p>Progressive enhancement also falls under the cloud of themed classes as it can be utilized to bring greater aesthetics to your skin. It differs in the sense that a class will be responsible for providing a single detail (rounded corners, drop shadow, gradient backgrounds, etc.) to the theme in a cross-browser and consistent manner:</p>

<div class="wp_syntax"><div class="code"><pre class="css" style="font-family:monospace;"><span style="color: #6666ff;">.shadow</span> <span style="color: #00AA00;">&#123;</span>
    -khtml-box-shadow<span style="color: #00AA00;">:</span> <span style="color: #933;">0px</span> <span style="color: #933;">0px</span> <span style="color: #933;">10px</span> rgba<span style="color: #00AA00;">&#40;</span><span style="color: #cc66cc;">0</span><span style="color: #00AA00;">,</span> <span style="color: #cc66cc;">0</span><span style="color: #00AA00;">,</span> <span style="color: #cc66cc;">0</span><span style="color: #00AA00;">,</span> <span style="color: #cc66cc;">0.5</span><span style="color: #00AA00;">&#41;</span><span style="color: #00AA00;">;</span>
    -webkit-box-shadow<span style="color: #00AA00;">:</span> <span style="color: #933;">0px</span> <span style="color: #933;">0px</span> <span style="color: #933;">10px</span> rgba<span style="color: #00AA00;">&#40;</span><span style="color: #cc66cc;">0</span><span style="color: #00AA00;">,</span> <span style="color: #cc66cc;">0</span><span style="color: #00AA00;">,</span> <span style="color: #cc66cc;">0</span><span style="color: #00AA00;">,</span> <span style="color: #cc66cc;">0.5</span><span style="color: #00AA00;">&#41;</span><span style="color: #00AA00;">;</span>
    -moz-box-shadow<span style="color: #00AA00;">:</span> <span style="color: #933;">0px</span> <span style="color: #933;">0px</span> <span style="color: #933;">10px</span> rgba<span style="color: #00AA00;">&#40;</span><span style="color: #cc66cc;">0</span><span style="color: #00AA00;">,</span> <span style="color: #cc66cc;">0</span><span style="color: #00AA00;">,</span> <span style="color: #cc66cc;">0</span><span style="color: #00AA00;">,</span> <span style="color: #cc66cc;">0.5</span><span style="color: #00AA00;">&#41;</span><span style="color: #00AA00;">;</span>
    box-shadow<span style="color: #00AA00;">:</span> <span style="color: #933;">0px</span> <span style="color: #933;">0px</span> <span style="color: #933;">10px</span> rgba<span style="color: #00AA00;">&#40;</span><span style="color: #cc66cc;">0</span><span style="color: #00AA00;">,</span> <span style="color: #cc66cc;">0</span><span style="color: #00AA00;">,</span> <span style="color: #cc66cc;">0</span><span style="color: #00AA00;">,</span> <span style="color: #cc66cc;">0.5</span><span style="color: #00AA00;">&#41;</span><span style="color: #00AA00;">;</span>
<span style="color: #00AA00;">&#125;</span>
&nbsp;
<span style="color: #6666ff;">.corners</span> <span style="color: #00AA00;">&#123;</span>
    -khtml-border-radius<span style="color: #00AA00;">:</span> <span style="color: #933;">5px</span><span style="color: #00AA00;">;</span>
    -webkit-border-radius<span style="color: #00AA00;">:</span> <span style="color: #933;">5px</span><span style="color: #00AA00;">;</span>
    -moz-border-radius<span style="color: #00AA00;">:</span> <span style="color: #933;">5px</span><span style="color: #00AA00;">;</span>
    border-radius<span style="color: #00AA00;">:</span> <span style="color: #933;">5px</span><span style="color: #00AA00;">;</span>
<span style="color: #00AA00;">&#125;</span></pre></div></div>

<p>By grouping vendor specific styles together (-webkit-, -moz-, -o-, -ms-) nodes will adopt the behaviour in supported browsers and degrade gracefully in unsupported browsers. This helps to keep code not only backwards compatible but also future proof:</p>
<p>If properly implemented we can achieve visual consistency across an application and allow components to be themeable in a rather simplied and modular fashion.</p>
<h3>Summary</h3>
<p>When we combine all these techniques, we find utter simplicity in forming both the structure and skin of a node, while minimizing the amount of node specific style declarations in our stylesheet.  If done right, you will find your markup will greatly improve as a result and it will be as easy to write as the following:</p>

<div class="wp_syntax"><div class="code"><pre class="html4strict" style="font-family:monospace;"><span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">div</span> <span style="color: #000066;">class</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;widget overlay corners shadow active&quot;</span>&gt;</span>
    <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">div</span> <span style="color: #000066;">class</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;header content&quot;</span>&gt;</span>
        <span style="color: #808080; font-style: italic;">&lt;!-- header markup --&gt;</span>
    <span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">div</span>&gt;</span>
    <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">div</span> <span style="color: #000066;">class</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;body content&quot;</span>&gt;</span>
        <span style="color: #808080; font-style: italic;">&lt;!-- body markup --&gt;</span>
    <span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">div</span>&gt;</span>
    <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">div</span> <span style="color: #000066;">class</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;footer content&quot;</span>&gt;</span>
        <span style="color: #808080; font-style: italic;">&lt;!-- footer markup --&gt;</span>
    <span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">div</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">div</span>&gt;</span></pre></div></div>

<p>These techniques help to serve as an advantage not only to your CSS but also your markup and JavaScript for a truly unobtrusive implementation. To learn more techniques or to read further on a few I&#8217;ve discussed here, please refer to Nicole Sullivan&#8217;s <a href="http://oocss.org/" target="_blank">OOCSS</a> (Object-Oriented CSS) project, it is truly a great read and highly recommended.</p>
]]></content:encoded>
			<wfw:commentRss>http://ryanmorr.com/archives/leveraging-the-most-out-of-css-classes/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>FX 2.0: The Full-Featured Animation Framework</title>
		<link>http://ryanmorr.com/archives/fx-2-0-the-full-featured-animation-framework</link>
		<comments>http://ryanmorr.com/archives/fx-2-0-the-full-featured-animation-framework#comments</comments>
		<pubDate>Sat, 04 Jul 2009 04:44:39 +0000</pubDate>
		<dc:creator>Ryan Morr</dc:creator>
				<category><![CDATA[CSS]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[Projects]]></category>
		<category><![CDATA[DOM]]></category>
		<category><![CDATA[FX]]></category>

		<guid isPermaLink="false">http://ryanmorr.com/?p=1443</guid>
		<description><![CDATA[It has by far been the most popular thing to come out of this blog since its inception, and I am very proud to officially release version 2, and this time I&#8217;ve included all the bells and whistles you&#8217;ve been asking for! The framework now boasts support for an impressive amount of transitions, CSS multi-unit [...]]]></description>
			<content:encoded><![CDATA[<p>It has by far been the most popular thing to come out of this blog since its inception, and I am very proud to officially release version 2, and this time I&#8217;ve included all the bells and whistles you&#8217;ve been asking for! The framework now boasts support for an impressive amount of transitions, CSS multi-unit support (em, %, ex, and all the rest), and many more optimizations for increased performance to get those smooth animations we all want.<span id="more-1443"></span></p>
<h3>How to Use?</h3>
<p>Not much has changed in the way of syntax from the <a href="http://ryanmorr.com/archives/fx-lightweight-and-standalone-animation">previous version</a>. The first argument accepts either the id of the element you wish to animate or the element itself. The second argument specifies the attributes of the animation, by defining the style and an ending value (&#8220;to&#8221;) and optionally a starting value (&#8220;from&#8221;) which otherwise defaults to the elements currently computed style:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> fx <span style="color: #000000;">=</span> <span style="color: #003366; font-weight: bold;">new</span> FX<span style="color: #000000;">&#40;</span>el<span style="color: #000000;">,</span> <span style="color: #000000;">&#123;</span>
    <span style="color: #3366CC;">'font-size'</span><span style="color: #000000;">:</span> <span style="color: #000000;">&#123;</span>to<span style="color: #000000;">:</span> <span style="color: #CC0000;">14</span><span style="color: #000000;">&#125;</span><span style="color: #000000;">,</span>
    <span style="color: #3366CC;">'background-color'</span><span style="color: #000000;">:</span> <span style="color: #000000;">&#123;</span>from<span style="color: #000000;">:</span> <span style="color: #3366CC;">'FF0000'</span><span style="color: #000000;">,</span> to<span style="color: #000000;">:</span> <span style="color: #3366CC;">'0000FF'</span><span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">;</span></pre></div></div>

<p>Additionally, you can optionally specify a duration (defaults to 0.7 seconds) as the third argument, the transition (brand new) as the fourth argument, and finally a callback method and the context you wish to execute the callback in as the fifth and sixth parameters:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> fx <span style="color: #000000;">=</span> <span style="color: #003366; font-weight: bold;">new</span> FX<span style="color: #000000;">&#40;</span><span style="color: #3366CC;">'element'</span><span style="color: #000000;">,</span> <span style="color: #000000;">&#123;</span>
    top<span style="color: #000000;">:</span> <span style="color: #000000;">&#123;</span>to<span style="color: #000000;">:</span> <span style="color: #CC0000;">200</span><span style="color: #000000;">&#125;</span><span style="color: #000000;">,</span>
    left<span style="color: #000000;">:</span> <span style="color: #000000;">&#123;</span>to<span style="color: #000000;">:</span> <span style="color: #CC0000;">200</span><span style="color: #000000;">&#125;</span><span style="color: #000000;">,</span>
    width<span style="color: #000000;">:</span> <span style="color: #000000;">&#123;</span>from<span style="color: #000000;">:</span> <span style="color: #CC0000;">100</span><span style="color: #000000;">,</span> to<span style="color: #000000;">:</span> <span style="color: #CC0000;">200</span><span style="color: #000000;">&#125;</span><span style="color: #000000;">,</span>
    height<span style="color: #000000;">:</span> <span style="color: #000000;">&#123;</span>from<span style="color: #000000;">:</span> <span style="color: #CC0000;">100</span><span style="color: #000000;">,</span> to<span style="color: #000000;">:</span> <span style="color: #CC0000;">200</span><span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span><span style="color: #000000;">,</span> <span style="color: #CC0000;">1.3</span><span style="color: #000000;">,</span> <span style="color: #3366CC;">'easeOut'</span><span style="color: #000000;">,</span> callback<span style="color: #000000;">,</span> <span style="color: #003366; font-weight: bold;">this</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">;</span></pre></div></div>

<p>The only method you need to concern yourself with is the start method, which starts the animation and requires no arguments:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> fx <span style="color: #000000;">=</span> <span style="color: #003366; font-weight: bold;">new</span> FX<span style="color: #000000;">&#40;</span><span style="color: #3366CC;">'element'</span><span style="color: #000000;">,</span> <span style="color: #000000;">&#123;</span>height<span style="color: #000000;">:</span> <span style="color: #CC0000;">400</span><span style="color: #000000;">&#125;</span><span style="color: #000000;">&#125;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">;</span>
fx.<span style="color: #006600;">start</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">;</span></pre></div></div>

<p>That&#8217;s it! In just a couple of lines, you have the ability to animate any element any which way you want! What more could you ask for? I&#8217;d actually like to know!</p>
<h3>CSS Multi-Unit Support</h3>
<p>Brand new to the framework, FX now supports multiple CSS units for all scalable styles such as width and height. By defining an optional &#8220;units&#8221; property for each attribute you can specify which unit to apply to that specific style:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> fx <span style="color: #000000;">=</span> <span style="color: #003366; font-weight: bold;">new</span> FX<span style="color: #000000;">&#40;</span><span style="color: #3366CC;">'box'</span><span style="color: #000000;">,</span> <span style="color: #000000;">&#123;</span>
    width<span style="color: #000000;">:</span> <span style="color: #000000;">&#123;</span>to<span style="color: #000000;">:</span> <span style="color: #CC0000;">20</span><span style="color: #000000;">,</span> units<span style="color: #000000;">:</span> <span style="color: #3366CC;">'em'</span><span style="color: #000000;">&#125;</span><span style="color: #000000;">,</span>
    height<span style="color: #000000;">:</span> <span style="color: #000000;">&#123;</span>to<span style="color: #000000;">:</span> <span style="color: #CC0000;">60</span><span style="color: #000000;">,</span> units<span style="color: #000000;">:</span> <span style="color: #3366CC;">'%'</span><span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">;</span></pre></div></div>

<p>Any and all CSS units are supported, including; em, pt, %, ex, in, mm, cm, and more. If no unit is specified for the attribute, it defaults to px.</p>
<h3>Transitions</h3>
<p>A feature promised and often asked for, FX now includes a much greater support for various transitional easing methods. Included in the library are your four basic transitions; linear, easeIn, easeOut, and easeInOut which is defined as an optional fourth parameter of the constructor, defaulting to easeInOut if not provided:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> fx <span style="color: #000000;">=</span> <span style="color: #003366; font-weight: bold;">new</span> FX<span style="color: #000000;">&#40;</span><span style="color: #3366CC;">'component'</span><span style="color: #000000;">,</span> <span style="color: #000000;">&#123;</span>
    opacity<span style="color: #000000;">:</span> <span style="color: #000000;">&#123;</span>to<span style="color: #000000;">:</span> <span style="color: #CC0000;">1</span><span style="color: #000000;">&#125;</span><span style="color: #000000;">,</span>
    left<span style="color: #000000;">:</span> <span style="color: #000000;">&#123;</span>to<span style="color: #000000;">:</span> <span style="color: #CC0000;">300</span><span style="color: #000000;">,</span> units<span style="color: #000000;">:</span> <span style="color: #3366CC;">'ex'</span><span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span><span style="color: #000000;">,</span> <span style="color: #CC0000;">1</span><span style="color: #000000;">,</span> <span style="color: #3366CC;">'easeIn'</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">;</span></pre></div></div>

<p>In case that isn&#8217;t enough, you can also download the optional transitions pack, which adds an additional 30 transitions to the framework such as; quint, expo, circ, elastic, bounce, and many more!</p>
<h3>Optimizations</h3>
<p>Version two features over a dozen optimizations designed to improve performance, usability, browser support (still without a trace of browser sniffing), and overall efficiency. For you this means increased speed and a smoother animation.</p>
<h3>Demo</h3>
<p>To try an illustrative example of all the old features mixed with the new, click <a href="http://www.ryanmorr.com/tests/fx2/">here</a>. The example demonstrates multiple tweens against multiple styles using multiple units, and multiple transitions &#8211; all of which are randomly generated.</p>
<h3>What&#8217;s Next?</h3>
<p>The framework itself still has many different directions it can go, and I&#8217;m sure in time we&#8217;ll gain support for any and all features we&#8217;ve yet to attain. From here I hope to create additional functionality exclusively related to elements and element control, this includes animation queues, delays, and a wealth of helper methods for effects such as fading, sliding, highlighting, motion, morphing, and many more. If you have any suggestions as to what you would like to see in upcoming versions, as always, I welcome your input and look forward to supporting this framework for a long time to come.</p>
<h3>Download</h3>
<ul>
<li><a onclick="javascript:pageTracker._trackPageview(’/Links/http://ryanmorr.com/modules/fx-compressed.js’);" href="http://ryanmorr.com/modules/fx2/fx-compressed.js">Compressed (5kb)</a></li>
<li><a onclick="javascript:pageTracker._trackPageview(’/Links/http://ryanmorr.com/modules/fx.js’);" href="http://ryanmorr.com/modules/fx2/fx.js">Fully Documented (10kb)</a></li>
</ul>
<h3>Transitions</h3>
<ul>
<li><a href="http://ryanmorr.com/modules/fx2/transitions-compressed.js">Compressed (4kb)</a></li>
<li><a href="http://ryanmorr.com/modules/fx2/transitions.js">Uncompressed (5kb)</a></li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://ryanmorr.com/archives/fx-2-0-the-full-featured-animation-framework/feed</wfw:commentRss>
		<slash:comments>11</slash:comments>
		</item>
		<item>
		<title>Detecting Browser CSS Style Support</title>
		<link>http://ryanmorr.com/archives/detecting-browser-css-style-support</link>
		<comments>http://ryanmorr.com/archives/detecting-browser-css-style-support#comments</comments>
		<pubDate>Sun, 03 May 2009 18:42:19 +0000</pubDate>
		<dc:creator>Ryan Morr</dc:creator>
				<category><![CDATA[CSS]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[Feature Testing]]></category>

		<guid isPermaLink="false">http://ryanmorr.com/?p=1190</guid>
		<description><![CDATA[If you have ever wanted to manipulate those brand spanking new styles such as -moz-transform, -webkit-transition, or even some older ones like opacity in IE and max-width in IE6 but didn&#8217;t because you were reluctant of browser support, than you are not alone. For this article, I will be exploring some unique tactics in an [...]]]></description>
			<content:encoded><![CDATA[<p>If you have ever wanted to manipulate those brand spanking new styles such as -moz-transform, -webkit-transition, or even some older ones like opacity in IE and max-width in IE6 but didn&#8217;t because you were reluctant of browser support, than you are not alone. For this article, I will be exploring some unique tactics in an effort to solve the dilemma as it relates to JavaScript.<span id="more-1190"></span></p>
<p>Since browser detection is entirely unreliable and unnecessary, we need to find something better. Instead we will employ feature testing to ascertain the browser&#8217;s capability of computing the style we are trying to determine support for. The method here is simple, if the browser can interpret the style than it must be supported, otherwise it must not.</p>
<p>In IE we can use the little known runtimeStyle object inherit to all elements in IE, it allows us to set the cascaded style (represented in currentStyle) which will override any and all other styling influences on the element. However we won&#8217;t be using runtimeStyle to set the style, but to check the value it holds to determine whether or not the style was properly computed. By simply examining the value held for the style in the runtimeStyle object, we find that it returns undefined for unsupported styles:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">typeof</span> el.<span style="color: #006600;">runtimeStyle</span>.<span style="color: #006600;">width</span> <span style="color: #000000;">!==</span> <span style="color: #3366CC;">'undefined'</span><span style="color: #000000;">;</span> <span style="color: #009900; font-style: italic;">// true</span>
<span style="color: #003366; font-weight: bold;">typeof</span> el.<span style="color: #006600;">runtimeStyle</span>.<span style="color: #006600;">opacity</span> <span style="color: #000000;">!==</span> <span style="color: #3366CC;">'undefined'</span><span style="color: #000000;">;</span> <span style="color: #009900; font-style: italic;">// false</span></pre></div></div>

<p>As for any standards compliant browsers, we check for support in a similar fashion; using the <em>getComputedStyle</em> method we can check to see if the style is capable of being interpreted. As with runtimeStyle in IE, only unsupported styles will return a value of undefined:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">typeof</span> view.<span style="color: #006600;">getComputedStyle</span><span style="color: #000000;">&#40;</span>el<span style="color: #000000;">,</span> <span style="color: #003366; font-weight: bold;">null</span><span style="color: #000000;">&#41;</span>.<span style="color: #006600;">test</span> <span style="color: #000000;">!==</span> <span style="color: #3366CC;">'undefined'</span><span style="color: #000000;">;</span> <span style="color: #009900; font-style: italic;">// false</span>
<span style="color: #003366; font-weight: bold;">typeof</span> view.<span style="color: #006600;">getComputedStyle</span><span style="color: #000000;">&#40;</span>el<span style="color: #000000;">,</span> <span style="color: #003366; font-weight: bold;">null</span><span style="color: #000000;">&#41;</span>.<span style="color: #006600;">minWidth</span> <span style="color: #000000;">!==</span> <span style="color: #3366CC;">'undefined'</span><span style="color: #000000;">;</span> <span style="color: #009900; font-style: italic;">// true</span></pre></div></div>

<p>Thats it! When we combine these techniques we get a robust method to detect CSS style support in all the mainstream browsers. Throw in some caching for increased performance and <em>isStyleSupported</em> is born:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">isStyleSupported <span style="color: #000000;">=</span> <span style="color: #000000;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#123;</span>
    <span style="color: #003366; font-weight: bold;">var</span> cache <span style="color: #000000;">=</span> <span style="color: #000000;">&#123;</span><span style="color: #000000;">&#125;</span><span style="color: #000000;">;</span>
    <span style="color: #003366; font-weight: bold;">var</span> el <span style="color: #000000;">=</span> document.<span style="color: #006600;">createElement</span><span style="color: #000000;">&#40;</span><span style="color: #3366CC;">'div'</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">;</span>
&nbsp;
    <span style="color: #003366; font-weight: bold;">return</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #000000;">&#40;</span>style<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#123;</span>
        <span style="color: #003366; font-weight: bold;">if</span><span style="color: #000000;">&#40;</span>style <span style="color: #003366; font-weight: bold;">in</span> cache<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#123;</span>
            <span style="color: #003366; font-weight: bold;">return</span> cache<span style="color: #000000;">&#91;</span>style<span style="color: #000000;">&#93;</span><span style="color: #000000;">;</span>
        <span style="color: #000000;">&#125;</span><span style="color: #003366; font-weight: bold;">else</span><span style="color: #000000;">&#123;</span>
            <span style="color: #003366; font-weight: bold;">var</span> supported <span style="color: #000000;">=</span> <span style="color: #003366; font-weight: bold;">false</span><span style="color: #000000;">;</span>
            <span style="color: #003366; font-weight: bold;">if</span><span style="color: #000000;">&#40;</span>el.<span style="color: #006600;">runtimeStyle</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#123;</span>
                supported <span style="color: #000000;">=</span> <span style="color: #003366; font-weight: bold;">typeof</span> el.<span style="color: #006600;">runtimeStyle</span><span style="color: #000000;">&#91;</span>style<span style="color: #000000;">&#93;</span> <span style="color: #000000;">!==</span> <span style="color: #3366CC;">'undefined'</span><span style="color: #000000;">;</span>
            <span style="color: #000000;">&#125;</span><span style="color: #003366; font-weight: bold;">else</span><span style="color: #000000;">&#123;</span>
                <span style="color: #003366; font-weight: bold;">var</span> view <span style="color: #000000;">=</span> document.<span style="color: #006600;">defaultView</span><span style="color: #000000;">;</span>
                <span style="color: #003366; font-weight: bold;">if</span><span style="color: #000000;">&#40;</span>view <span style="color: #000000;">&amp;&amp;</span> view.<span style="color: #006600;">getComputedStyle</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#123;</span>
                    <span style="color: #003366; font-weight: bold;">var</span> cs <span style="color: #000000;">=</span> view.<span style="color: #006600;">getComputedStyle</span><span style="color: #000000;">&#40;</span>el<span style="color: #000000;">,</span> <span style="color: #003366; font-weight: bold;">null</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#91;</span>style<span style="color: #000000;">&#93;</span><span style="color: #000000;">;</span>
                    supported <span style="color: #000000;">=</span> <span style="color: #003366; font-weight: bold;">typeof</span> cs <span style="color: #000000;">!==</span> <span style="color: #3366CC;">'undefined'</span><span style="color: #000000;">;</span>
                <span style="color: #000000;">&#125;</span>
            <span style="color: #000000;">&#125;</span>
            <span style="color: #003366; font-weight: bold;">return</span> cache<span style="color: #000000;">&#91;</span>style<span style="color: #000000;">&#93;</span> <span style="color: #000000;">=</span> supported<span style="color: #000000;">;</span>
        <span style="color: #000000;">&#125;</span>
    <span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">;</span></pre></div></div>

<p>Perfect right? Well not so fast, we don’t have to stop there, why not extend the method to not only determine support for styles, but their assignable values? Multiple backgrounds, gradients, and the rgba specifications are just a few of the values new to CSS3 that we can leverage given a reliable test case. </p>
<p>Amazingly, IE again makes this quite easy using the technique we&#8217;ve already employed for determining style support. All we need to add is value assignment via the style object and wrap the test case in a try-catch block as IE will throw an error for unsupported style values.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">try</span><span style="color: #000000;">&#123;</span>
    el.<span style="color: #006600;">style</span>.<span style="color: #006600;">width</span> <span style="color: #000000;">=</span> <span style="color: #3366CC;">'12px'</span><span style="color: #000000;">;</span>
    supported <span style="color: #000000;">=</span> <span style="color: #003366; font-weight: bold;">typeof</span> el.<span style="color: #006600;">runtimeStyle</span>.<span style="color: #006600;">width</span> <span style="color: #000000;">!==</span> <span style="color: #3366CC;">'undefined'</span><span style="color: #000000;">;</span> <span style="color: #009900; font-style: italic;">// true</span>
&nbsp;
    el.<span style="color: #006600;">style</span>.<span style="color: #006600;">width</span> <span style="color: #000000;">=</span> <span style="color: #3366CC;">'abcd'</span><span style="color: #000000;">;</span>
    supported <span style="color: #000000;">=</span> <span style="color: #003366; font-weight: bold;">typeof</span> el.<span style="color: #006600;">runtimeStyle</span>.<span style="color: #006600;">width</span> <span style="color: #000000;">!==</span> <span style="color: #3366CC;">'undefined'</span><span style="color: #000000;">;</span> <span style="color: #009900; font-style: italic;">// false</span>
<span style="color: #000000;">&#125;</span><span style="color: #003366; font-weight: bold;">catch</span><span style="color: #000000;">&#40;</span>e<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#123;</span><span style="color: #000000;">&#125;</span><span style="color: #000000;">;</span></pre></div></div>

<p>However, if we are looking to determine support for the style values in standards compliant browsers, than <em>getComputedStyle</em> proves to be unreliable. Take for instance any number of scalable styles such as width and height, the method will always convert any units back to pixels, meaning a quick comparison would fail:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">el.<span style="color: #006600;">style</span>.<span style="color: #006600;">width</span> <span style="color: #000000;">=</span> <span style="color: #3366CC;">&quot;1em&quot;</span><span style="color: #000000;">;</span>
document.<span style="color: #006600;">defaultView</span>.<span style="color: #006600;">getComputedStyle</span><span style="color: #000000;">&#40;</span>el<span style="color: #000000;">,</span> <span style="color: #003366; font-weight: bold;">null</span><span style="color: #000000;">&#41;</span>.<span style="color: #006600;">width</span><span style="color: #000000;">;</span> <span style="color: #009900; font-style: italic;">// 16px</span></pre></div></div>

<p>In addition to that testing against unsupported style values will not always return a definitive result where as support can be easily determined. For example, setting an element&#8217;s color to an unrecognized value will always result in the return of the rgb representation of the color black:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">el.<span style="color: #006600;">style</span>.<span style="color: #006600;">color</span> <span style="color: #000000;">=</span> <span style="color: #3366CC;">&quot;test&quot;</span><span style="color: #000000;">;</span>
document.<span style="color: #006600;">defaultView</span>.<span style="color: #006600;">getComputedStyle</span><span style="color: #000000;">&#40;</span>el<span style="color: #000000;">,</span> <span style="color: #003366; font-weight: bold;">null</span><span style="color: #000000;">&#41;</span>.<span style="color: #006600;">color</span><span style="color: #000000;">;</span> <span style="color: #009900; font-style: italic;">// rgb(0,0,0)</span></pre></div></div>

<p>To quickly resolve this and avoid making some elaborate parsing method, we need to use a secondary test case. We create a new element as part of our base element&#8217;s innerHTML and add the styling we wish to test for as inline styles. When we retrieve the new element and examine its style, we find that unsupported style values will always return an empty string:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">el.<span style="color: #006600;">innerHTML</span> <span style="color: #000000;">=</span> <span style="color: #3366CC;">'&lt;div style=&quot;color:test ; width:10px&quot;&gt;&lt;/div&gt;'</span><span style="color: #000000;">;</span>
el.<span style="color: #006600;">firstChild</span>.<span style="color: #006600;">style</span>.<span style="color: #006600;">color</span> <span style="color: #000000;">!==</span> <span style="color: #3366CC;">&quot;&quot;</span><span style="color: #000000;">;</span> <span style="color: #009900; font-style: italic;">// false</span>
el.<span style="color: #006600;">firstChild</span>.<span style="color: #006600;">style</span>.<span style="color: #006600;">width</span> <span style="color: #000000;">!==</span> <span style="color: #3366CC;">&quot;&quot;</span><span style="color: #000000;">;</span> <span style="color: #009900; font-style: italic;">// true</span></pre></div></div>

<p>Using this technique means any styles supplied must be in standard hyphenated CSS format (background-color) as opposed to the previous method which must be handed camel-cased styles (backgroundColor). This means that we need to include a function to handle camel case conversions; a small hit on performance yes, but also some syntastic sugar for the method &#8211; give and take I suppose.</p>
<p>Throwing these new techniques together results in a method capable of determining support for not only styles but their supported assignable/recognizable values by supplying an optional second parameter.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">isStyleSupported <span style="color: #000000;">=</span> <span style="color: #000000;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#123;</span>
    <span style="color: #003366; font-weight: bold;">var</span> cache <span style="color: #000000;">=</span> <span style="color: #000000;">&#123;</span><span style="color: #000000;">&#125;</span><span style="color: #000000;">;</span>
    <span style="color: #003366; font-weight: bold;">var</span> el <span style="color: #000000;">=</span> document.<span style="color: #006600;">createElement</span><span style="color: #000000;">&#40;</span><span style="color: #3366CC;">'div'</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">;</span>
&nbsp;
    <span style="color: #003366; font-weight: bold;">var</span> toCamelCase <span style="color: #000000;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #000000;">&#40;</span>str<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#123;</span>
        <span style="color: #003366; font-weight: bold;">var</span> parts <span style="color: #000000;">=</span> str.<span style="color: #006600;">split</span><span style="color: #000000;">&#40;</span><span style="color: #3366CC;">'-'</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">,</span> camel <span style="color: #000000;">=</span> parts<span style="color: #000000;">&#91;</span><span style="color: #CC0000;">0</span><span style="color: #000000;">&#93;</span><span style="color: #000000;">;</span>
        <span style="color: #003366; font-weight: bold;">for</span><span style="color: #000000;">&#40;</span><span style="color: #003366; font-weight: bold;">var</span> i<span style="color: #000000;">=</span><span style="color: #CC0000;">1</span><span style="color: #000000;">,</span> len<span style="color: #000000;">=</span>parts.<span style="color: #006600;">length</span><span style="color: #000000;">;</span> i <span style="color: #000000;">&lt;</span> len<span style="color: #000000;">;</span> i<span style="color: #000000;">++</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#123;</span>
            camel <span style="color: #000000;">+=</span> parts<span style="color: #000000;">&#91;</span>i<span style="color: #000000;">&#93;</span>.<span style="color: #006600;">charAt</span><span style="color: #000000;">&#40;</span><span style="color: #CC0000;">0</span><span style="color: #000000;">&#41;</span>.<span style="color: #006600;">toUpperCase</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">+</span> parts<span style="color: #000000;">&#91;</span>i<span style="color: #000000;">&#93;</span>.<span style="color: #006600;">substring</span><span style="color: #000000;">&#40;</span><span style="color: #CC0000;">1</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">;</span>
        <span style="color: #000000;">&#125;</span>
        <span style="color: #003366; font-weight: bold;">return</span> camel<span style="color: #000000;">;</span>
    <span style="color: #000000;">&#125;</span><span style="color: #000000;">;</span>
&nbsp;
    <span style="color: #003366; font-weight: bold;">return</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #000000;">&#40;</span>style<span style="color: #000000;">,</span> value<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#123;</span>
        <span style="color: #003366; font-weight: bold;">var</span> key <span style="color: #000000;">=</span> style <span style="color: #000000;">+</span> value <span style="color: #000000;">||</span> <span style="color: #3366CC;">&quot;&quot;</span><span style="color: #000000;">;</span>
        <span style="color: #003366; font-weight: bold;">if</span><span style="color: #000000;">&#40;</span>key <span style="color: #003366; font-weight: bold;">in</span> cache<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#123;</span>
            <span style="color: #003366; font-weight: bold;">return</span> cache<span style="color: #000000;">&#91;</span>key<span style="color: #000000;">&#93;</span><span style="color: #000000;">;</span>
        <span style="color: #000000;">&#125;</span><span style="color: #003366; font-weight: bold;">else</span><span style="color: #000000;">&#123;</span>
            <span style="color: #003366; font-weight: bold;">var</span> supported <span style="color: #000000;">=</span> <span style="color: #003366; font-weight: bold;">false</span><span style="color: #000000;">;</span>
            <span style="color: #003366; font-weight: bold;">var</span> camel <span style="color: #000000;">=</span> toCamelCase<span style="color: #000000;">&#40;</span>style<span style="color: #000000;">&#41;</span><span style="color: #000000;">;</span>
            <span style="color: #003366; font-weight: bold;">if</span><span style="color: #000000;">&#40;</span>el.<span style="color: #006600;">runtimeStyle</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#123;</span>
                <span style="color: #003366; font-weight: bold;">try</span><span style="color: #000000;">&#123;</span>
                    el.<span style="color: #006600;">style</span><span style="color: #000000;">&#91;</span>camel<span style="color: #000000;">&#93;</span> <span style="color: #000000;">=</span> value <span style="color: #000000;">||</span> <span style="color: #3366CC;">&quot;&quot;</span><span style="color: #000000;">;</span>
                    supported <span style="color: #000000;">=</span> <span style="color: #003366; font-weight: bold;">typeof</span> el.<span style="color: #006600;">runtimeStyle</span><span style="color: #000000;">&#91;</span>camel<span style="color: #000000;">&#93;</span> <span style="color: #000000;">!==</span> <span style="color: #3366CC;">'undefined'</span><span style="color: #000000;">;</span>
                <span style="color: #000000;">&#125;</span><span style="color: #003366; font-weight: bold;">catch</span><span style="color: #000000;">&#40;</span>e<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#123;</span><span style="color: #000000;">&#125;</span><span style="color: #000000;">;</span>
            <span style="color: #000000;">&#125;</span><span style="color: #003366; font-weight: bold;">else</span><span style="color: #000000;">&#123;</span>
                <span style="color: #003366; font-weight: bold;">var</span> view <span style="color: #000000;">=</span> document.<span style="color: #006600;">defaultView</span><span style="color: #000000;">;</span>
                <span style="color: #003366; font-weight: bold;">if</span><span style="color: #000000;">&#40;</span>view <span style="color: #000000;">&amp;&amp;</span> view.<span style="color: #006600;">getComputedStyle</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#123;</span>
                    <span style="color: #003366; font-weight: bold;">var</span> cs <span style="color: #000000;">=</span> view.<span style="color: #006600;">getComputedStyle</span><span style="color: #000000;">&#40;</span>el<span style="color: #000000;">,</span> <span style="color: #003366; font-weight: bold;">null</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#91;</span>camel<span style="color: #000000;">&#93;</span><span style="color: #000000;">;</span>
                    supported <span style="color: #000000;">=</span> <span style="color: #003366; font-weight: bold;">typeof</span> cs <span style="color: #000000;">!==</span> <span style="color: #3366CC;">'undefined'</span><span style="color: #000000;">;</span>
                    <span style="color: #003366; font-weight: bold;">if</span><span style="color: #000000;">&#40;</span>value<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#123;</span>
                        el.<span style="color: #006600;">innerHTML</span> <span style="color: #000000;">=</span> <span style="color: #3366CC;">'&lt;div style=&quot;'</span><span style="color: #000000;">+</span>style<span style="color: #000000;">+</span><span style="color: #3366CC;">':'</span><span style="color: #000000;">+</span>value<span style="color: #000000;">+</span><span style="color: #3366CC;">'&quot;&gt;&lt;/div&gt;'</span><span style="color: #000000;">;</span>
                        supported <span style="color: #000000;">=</span> supported <span style="color: #000000;">&amp;&amp;</span> el.<span style="color: #006600;">firstChild</span>.<span style="color: #006600;">style</span><span style="color: #000000;">&#91;</span>camel<span style="color: #000000;">&#93;</span> <span style="color: #000000;">!==</span> <span style="color: #3366CC;">&quot;&quot;</span><span style="color: #000000;">;</span>
                    <span style="color: #000000;">&#125;</span>
                <span style="color: #000000;">&#125;</span>
            <span style="color: #000000;">&#125;</span>
            <span style="color: #003366; font-weight: bold;">return</span> cache<span style="color: #000000;">&#91;</span>key<span style="color: #000000;">&#93;</span> <span style="color: #000000;">=</span> supported<span style="color: #000000;">;</span>
        <span style="color: #000000;">&#125;</span>
    <span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">;</span></pre></div></div>

<p>So how reliable are the methods? Well in all my testing in all the browsers I could get my hands on &#8211; I encountered just one false positive, that being fixed positioning in IE6, something I am still struggling to explain. To test the reliability yourself, I have put together a simple <a href="http://ryanmorr.com/tests/isstylesupported/index.html" target="_self">test page</a> which includes various styles and their corresponding results when tested against <em>isStyleSupported</em>. In addition to that, I have included two text inputs so you can try and test any styles you wish.</p>
<p>Hopefully we&#8217;ll see some of the developers of the mainstream libraries take notice and employ these or similar techniques in future versions of their respective frameworks and finally rid themselves of the unreliable browser detection so many of them still utilize today in cases such as this.</p>
]]></content:encoded>
			<wfw:commentRss>http://ryanmorr.com/archives/detecting-browser-css-style-support/feed</wfw:commentRss>
		<slash:comments>9</slash:comments>
		</item>
		<item>
		<title>FX: Lightweight and Standalone Animation</title>
		<link>http://ryanmorr.com/archives/fx-lightweight-and-standalone-animation</link>
		<comments>http://ryanmorr.com/archives/fx-lightweight-and-standalone-animation#comments</comments>
		<pubDate>Fri, 23 Jan 2009 01:57:25 +0000</pubDate>
		<dc:creator>Ryan Morr</dc:creator>
				<category><![CDATA[CSS]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[Projects]]></category>
		<category><![CDATA[DOM]]></category>
		<category><![CDATA[FX]]></category>

		<guid isPermaLink="false">http://ryanmorr.com/?p=281</guid>
		<description><![CDATA[Its one of those unnecessary things, but it sure does look pretty, I&#8217;m of course talking about animation. Flash or Javascript based, I find it adds that little some extra for the end-user&#8217;s experience; allowing them to fully understand the actions taking place by visually seeing components in transition. Most all mainstream libraries support animation [...]]]></description>
			<content:encoded><![CDATA[<p>Its one of those unnecessary things, but it sure does look pretty, I&#8217;m of course talking about animation. Flash or Javascript based, I find it adds that little some extra for the end-user&#8217;s experience; allowing them to fully understand the actions taking place by visually seeing components in transition. Most all mainstream libraries support animation in one way or another, and they are all quite impressive, despite the fact the core of each library essentially works the same. <span id="more-281"></span></p>
<p>Here, I present to you my version appropriately named FX. The motive here was to create a simple script for your basic animations and of course to be library independent, with emphasis on lightweight. Where I have strayed away from the other solutions found in various frameworks is that I&#8217;ve stripped out most of the extra features, electing instead for the core functionality. The syntax is similar to that which you would find in YUI, I find it simple yet covers all the bases pretty well. I drew inspiration for the programming behind it from Mootools due to its simplicity as a whole. To use it is pretty simple; just tell it what to do, give it a duration, optional callback, and then let it fly.</p>
<h3>Features</h3>
<ul>
<li>Perform tween on almost all CSS properties</li>
<li>Supports both scroll and color animations</li>
<li>Allows for both to and from values for each attribute</li>
<li>Supports transitional easing</li>
<li>Optionally assign a callback and context to execute the function in upon animation completion</li>
<li>Supports IE6+, FF, Opera, Safari, and Chrome</li>
<li>No browser detection</li>
</ul>
<h3>Example</h3>
<p>The syntax is pretty straigth forward, as I mentioned before it looks similar to what you would find in YUI. The following example demonstrates just some of the attributes supported for animation. For a more impressive sampling of FX, see this <a href="http://www.ryanmorr.com/tests/fx/" target="_self">example</a>.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> fx <span style="color: #000000;">=</span> <span style="color: #003366; font-weight: bold;">new</span> FX<span style="color: #000000;">&#40;</span><span style="color: #3366CC;">'element'</span><span style="color: #000000;">,</span> <span style="color: #000000;">&#123;</span>
    <span style="color: #3366CC;">'top'</span><span style="color: #000000;">:</span> <span style="color: #000000;">&#123;</span>to<span style="color: #000000;">:</span> <span style="color: #CC0000;">500</span><span style="color: #000000;">&#125;</span><span style="color: #000000;">,</span>
    <span style="color: #3366CC;">'left'</span><span style="color: #000000;">:</span> <span style="color: #000000;">&#123;</span>to<span style="color: #000000;">:</span> <span style="color: #CC0000;">300</span><span style="color: #000000;">&#125;</span><span style="color: #000000;">,</span>
    <span style="color: #3366CC;">'width'</span><span style="color: #000000;">:</span> <span style="color: #000000;">&#123;</span>from<span style="color: #000000;">:</span> <span style="color: #CC0000;">100</span><span style="color: #000000;">,</span> to<span style="color: #000000;">:</span> <span style="color: #CC0000;">300</span><span style="color: #000000;">&#125;</span><span style="color: #000000;">,</span>
    <span style="color: #3366CC;">'height'</span><span style="color: #000000;">:</span> <span style="color: #000000;">&#123;</span>from<span style="color: #000000;">:</span> <span style="color: #CC0000;">100</span><span style="color: #000000;">,</span> to<span style="color: #000000;">:</span> <span style="color: #CC0000;">200</span><span style="color: #000000;">&#125;</span><span style="color: #000000;">,</span>
    <span style="color: #3366CC;">'scrollLeft'</span><span style="color: #000000;">:</span> <span style="color: #000000;">&#123;</span>from<span style="color: #000000;">:</span> <span style="color: #CC0000;">0</span><span style="color: #000000;">,</span> to<span style="color: #000000;">:</span> <span style="color: #CC0000;">50</span><span style="color: #000000;">&#125;</span><span style="color: #000000;">,</span>
    <span style="color: #3366CC;">'background-color'</span><span style="color: #000000;">:</span> <span style="color: #000000;">&#123;</span>from<span style="color: #000000;">:</span> <span style="color: #3366CC;">'#FF0000'</span><span style="color: #000000;">,</span> to<span style="color: #000000;">:</span> <span style="color: #3366CC;">'#0000FF'</span><span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span><span style="color: #000000;">,</span> <span style="color: #CC0000;">1.5</span><span style="color: #000000;">,</span> callback<span style="color: #000000;">,</span> context<span style="color: #000000;">&#41;</span><span style="color: #000000;">;</span>
&nbsp;
fx.<span style="color: #006600;">start</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">;</span></pre></div></div>

<p>At the moment, only pixels are supported as the default units for all animations, and there is only one type of transitional easing available. At some later point in time I will be updating to add support for both of these features, so check back soon!</p>
<h3>Download</h3>
<ul>
<li><a href="http://ryanmorr.com/modules/fx/fx-compressed.js">Compressed (4kb)</a></li>
<li><a onclick="javascript:pageTracker._trackPageview(’/Links/http://ryanmorr.com/modules/fx.js’);" href="http://ryanmorr.com/modules/fx/fx.js">Fully Documented (8kb)</a></li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://ryanmorr.com/archives/fx-lightweight-and-standalone-animation/feed</wfw:commentRss>
		<slash:comments>15</slash:comments>
		</item>
	</channel>
</rss>
