<?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>Van SEO Design &#187; CSS</title>
	<atom:link href="http://www.vanseodesign.com/category/css/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.vanseodesign.com</link>
	<description>Helping you build search engine friendly websites</description>
	<lastBuildDate>Thu, 09 Feb 2012 15:26:44 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<xhtml:meta xmlns:xhtml="http://www.w3.org/1999/xhtml" name="robots" content="noindex" />
		<item>
		<title>Animation With CSS: It&#8217;s Easier Than You Think</title>
		<link>http://www.vanseodesign.com/css/animation/</link>
		<comments>http://www.vanseodesign.com/css/animation/#comments</comments>
		<pubDate>Mon, 14 Nov 2011 13:30:44 +0000</pubDate>
		<dc:creator>Steven Bradley</dc:creator>
				<category><![CDATA[CSS]]></category>

		<guid isPermaLink="false">http://www.vanseodesign.com/?p=3539</guid>
		<description><![CDATA[When we combine movements (transforms) of inanimate objects over different timeframes (transitions) we get animation. CSS3 expands on what&#8217;s possible with transforms and transitions with the aptly named animations module and I&#8217;d like to spend some time today looking through that module.

I&#8217;ve created a simple animation demo to go along with this post. By no [...]]]></description>
			<content:encoded><![CDATA[<p>When we combine movements (<a href="http://www.vanseodesign.com/css/transforms/">transforms</a>) of inanimate objects over different timeframes (<a href="http://www.vanseodesign.com/css/transitions/">transitions</a>) we get animation. CSS3 expands on what&#8217;s possible with transforms and transitions with the aptly named <a href="http://www.w3.org/TR/css3-animations/">animations module</a> and I&#8217;d like to spend some time today looking through that module.<br />
<span id="more-3539"></span><br />
I&#8217;ve created a simple <a href="http://www.vanseodesign.com/blog/demo/animation/">animation demo</a> to go along with this post. By no means is this award winning animation. In fact it&#8217;s pretty cheesy, but hopefully it illustrates how to work with animation.</p>
<p><a href="http://caniuse.com/#search=css3%20animation">Browser support for animation</a> is limited to Webkit browsers like Safari and Chrome as well as Firefox. Animation is coming to IE10 and as of now there&#8217;s unknown support in Opera. For now you also need to use vendor prefixes, though I won&#8217;t be showing the prefixes in this post.</p>
<p><a href="http://xilverkit.deviantart.com/art/Storyboard-Markers-56098572"><img src="http://www.vanseodesign.com/blog/wp-content/uploads/2011/11/animation-sketch.jpg" alt="Keyframe sketches of a parrot for an animation" width="465" height="313" /></a></p>
<h2>Keyframes</h2>
<p>The first thing we need to understand with animation is the idea of keyframes. If you&#8217;ve ever worked with Flash or similar you should know what keyframes are.</p>
<p>With transitions we had a starting point and ending point. What we didn&#8217;t have was anything in between. Keyframes give us the in between. They give us finer control over the intermediate stages between beginning and ending.</p>
<p>Essentially they define a <a href="http://www.vanseodesign.com/web-design/dominance/">series of start and end points</a>, with all intermediate keyframes being the ending point for one transition and the starting point for the next. This allows for the variance of durations and timing functions through the entire animation sequence.</p>
<h3>@keyframes Rule</h3>
<p>In css3 keyframes are specified using an @rule, followed by a user defined name for the animation. Inside are lists of property: value pairs set at different % over the animation. Each % is a new keyframe.</p>
<pre class="css">
@keyframes <span class="cssString">&#039;my-animation&#039;</span> {
  0% {
    <span class="cssProperty">left</span><span class="cssRest">:</span><span class="cssValue"> 0</span><span class="cssRest">;</span>
  <span class="cssMedia">}</span>
  50% {
    <span class="cssProperty">left</span><span class="cssRest">:</span><span class="cssValue"> 250px</span><span class="cssRest">;</span>
  <span class="cssMedia">}</span>
  100% {
    <span class="cssProperty">left</span><span class="cssRest">:</span><span class="cssValue"> 500px</span><span class="cssRest">;</span>
  <span class="cssMedia">}</span>
<span class="cssMedia">}</span>
</pre>
<p>Above are 3 keyframes under the name my-animation. These frames occur at 0%, 50%, and 100% of the animation. Each simply moves an element by setting it&#8217;s left property to a new value.</p>
<p>It&#8217;s not the most complex animation, but it illustrates the basics of defining @keyframes.</p>
<p>Every <a href="http://developer.apple.com/library/safari/#documentation/InternetWeb/Conceptual/SafariVisualEffectsProgGuide/Animations/Animations.html">keyframe animation</a> must have a rule for 0% (from) and 100% (to) or else it&#8217;s invalid. The &#8220;from&#8221; and &#8220;to&#8221; keywords can be used instead of the % values. Any properties inside the individual keyframes that can&#8217;t be animated are ignored.</p>
<p>The keyframes can be specified in any order, though the sequence will proceed from 0% to 100% as expected. All selectors and values are first determined and sorted in increasing order of time.</p>
<p>You&#8217;re not limited to defining one @keyframes rule. Multiple @keyframes can be defined and called in a single document. However @keyframes don&#8217;t <a href="http://www.vanseodesign.com/css/css-specificity-inheritance-cascaade/">cascade</a> so an animation will never derive keyframes from more than one @keyframes rule.</p>
<p><a href="http://www.w3.org/TR/css3-animations/#keyframes-"><img src="http://www.vanseodesign.com/blog/wp-content/uploads/2011/11/animation.png" alt="Animation states specified by keyframes"  width="465" height="504" /></a></p>
<h2>Animation Behavior</h2>
<p>Animations are what happens between keyframes and they&#8217;re similar to transitions in that they change css properties over time with the following key differences.</p>
<ul>
<li><strong>Transitions</strong> trigger implicitly when a property changes values. The property change causes the transition.</li>
<li><strong>Animations</strong> are explicitly executed when animation properties are applied. The animation causes the property change.</li>
</ul>
<p>Because of the latter explicit values for any properties being animated are required.</p>
<p>During an animation the computed values of properties are controlled by the animation. These computed values override other values that may have been set. Before and after animations those other values will apply.</p>
<h2>Animation Properties</h2>
<p>The @keyframes rule above defines the change that will happen over the animation.</p>
<p>Most of the properties below are applied to the element(s) that will undergo that animation. They determine which animation to use and how it will be applied.</p>
<p>There are 7 animation properties and some should look familiar if you&#8217;ve spent any time with <a href="http://www.vanseodesign.com/css/transitions/">transitions</a>.</p>
<ul>
<li>animation-name</li>
<li>animation-duration</li>
<li>animation-timing-function</li>
<li>animation-iteration-count</li>
<li>animation-direction</li>
<li>animation-play-state</li>
<li>animation-delay</li>
</ul>
<p>And of course there will be a shorthand animation property combining all of the above.</p>
<h3>animation-name property</h3>
<p>The animation-name property defines a list of animations that are applied. It&#8217;s used to select which @keyframes will apply to an element.</p>
<pre class="css">
<span class="cssSelector">div {</span>
  <span class="cssProperty">animation-name</span><span class="cssRest">:</span><span class="cssValue"> my-animation</span><span class="cssRest">;</span>
<span class="cssSelector">}</span>
</pre>
<p>The above sets the @keyframes my-animation as the one to use to animate the div.</p>
<h3>animation-duration property</h3>
<p>Like transition-duration, the animation-duration property defines the length of time over which the entire animation will run. Naturally it takes a time as its value and its default value is 0 or an instant change.</p>
<pre class="css">
<span class="cssSelector">div {</span>
  <span class="cssProperty">animation-duration</span><span class="cssRest">:</span><span class="cssValue"> 5s</span><span class="cssRest">;</span>
<span class="cssSelector">}</span>
</pre>
<p><a href="http://www.flickr.com/photos/kojotomoto/2598342665/"><img src="http://www.vanseodesign.com/blog/wp-content/uploads/2011/08/strucclock-3.jpg" alt="'Haouse under the star' clock" width="465" height="349" /></a></p>
<h3>animation-timing-function property</h3>
<p>The animation-timing-function property describes how the animation progresses and it works the same as transition-timing-function with the same values</p>
<ul>
<li>ease</li>
<li>linear</li>
<li>ease-in</li>
<li>ease-out</li>
<li>ease-in-out</li>
<li>cubic-bezier</li>
</ul>
<p>I&#8217;ll refer you to my <a href="http://www.vanseodesign.com/css/transitions/">post on transitions</a> for specifics of how each of the above works.</p>
<p>If the timing function is applied to the element it sets the timing over the entire animation, however the timing function can also be set on individual keyframes, which would only affect the timing from that keyframe to the next.</p>
<pre class="css">
@keyframes <span class="cssString">&#039;my-animation&#039;</span> {
  0% {
    <span class="cssProperty">left</span><span class="cssRest">:</span><span class="cssValue"> 0</span><span class="cssRest">;</span>
    <span class="cssProperty">animation-timing-function</span><span class="cssRest">:</span><span class="cssValue"> ease-in</span><span class="cssRest">;</span>
  <span class="cssMedia">}</span>
  50% {
    <span class="cssProperty">left</span><span class="cssRest">:</span><span class="cssValue"> 250px</span><span class="cssRest">;</span>
    <span class="cssProperty">animation-timing-function</span><span class="cssRest">:</span><span class="cssValue"> ease-in-out</span><span class="cssRest">;</span>
  <span class="cssMedia">}</span>
  100% {
    <span class="cssProperty">left</span><span class="cssRest">:</span><span class="cssValue"> 500px</span><span class="cssRest">;</span>
    <span class="cssProperty">animation-timing-function</span><span class="cssRest">:</span><span class="cssValue"> linear</span><span class="cssRest">;</span>
  <span class="cssMedia">}</span>
<span class="cssMedia">}</span>
</pre>
<h3>animation-iteration-count property</h3>
<p>Transitions run once when some event occurs. <a href="http://css3.bradshawenterprises.com/animations/">Animations</a> can be set to run multiple times. The animation-iteration-count property defines how many times to run the animation sequence.</p>
<p>Its value is a number with the default being 1. A value of infinite means to run the animation forever in an endless loop and a negative value becomes 0.</p>
<p>A non-integer number can be used and will lead to animation ending part way through a cycle. For example setting animation-iteration-count to 2.5 will run the animation 2 &frac12; times, finishing right in the middle of a cycle.</p>
<pre class="css">
<span class="cssSelector">div {</span>
  animation-iteration-count: 9;
<span class="cssSelector">}</span>
</pre>
<p>The above will run the animation 9 times before stopping.</p>
<p><a href="http://www.flickr.com/photos/batega/1376939615/"><img src="http://www.vanseodesign.com/blog/wp-content/uploads/2011/11/direction-art.jpg" alt="An arrow shaped sign with the word 'Art' displayed and pointing left" width="465" height="310" /></a></p>
<h3>animation-direction property</h3>
<p>The animation-direction property defines whether or not the animation should run in reverse during alternate cycles. It takes 2 values normal (default) or alternate.</p>
<pre class="css">
<span class="cssSelector">div {</span>
  <span class="cssProperty">animation-direction</span><span class="cssRest">:</span><span class="cssValue"> alternate</span><span class="cssRest">;</span>
<span class="cssSelector">}</span>
</pre>
<p>The above would set odd numbered iterations to proceed in normal direction and even numbered iterations to proceed in reverse or the alternate direction</p>
<p>Alternate also reverses the timing-functions so ease-in played in reverse becomes ease-out during the alternate direction.</p>
<h3>animation-play-state property</h3>
<p>You can define if the animation is running or paused (the 2 values) with the animation-play-state property.</p>
<pre class="css">
<span class="cssSelector">div {</span>
  <span class="cssProperty">animation-play-state</span><span class="cssRest">:</span><span class="cssValue"> paused</span><span class="cssRest">;</span>
<span class="cssSelector">}</span>
</pre>
<p>A paused animation continues to display the current value of animation until resumed where the resumed animation will then start at that current display.</p>
<h3>animation-delay property</h3>
<p>As you&#8217;d expect the animation-delay property defines when the animation will start. It works exactly the same as transition-delay.</p>
<pre class="css">
<span class="cssSelector">div {</span>
  <span class="cssProperty">animation-delay</span><span class="cssRest">:</span><span class="cssValue"> 0.5s</span><span class="cssRest">;</span>
<span class="cssSelector">}</span>
</pre>
<p>The above delays the start of the animation for &frac12; second.</p>
<h3>animation shorthand</h3>
<p>Like usual you don&#8217;t need to specific all the above properties separately, but can use the shorthand. The shorthand takes the following order.</p>
<p>[&lt;animation-name&gt; | &lt;animation-duration&gt; | &lt;animation-timing-function&gt; | &lt;animation-delay&gt; | &lt;animation-iteration-count&gt; | &lt;animation-direction&gt;] [, [&lt;animation-name&gt; | &lt;animation-duration&gt; | &lt;animation-timing-function&gt; | &lt;animation-delay&gt; | &lt;animation-iteration-count&gt; | &lt;animation-direction&gt;] ]</p>
<p>Multiple animations are separated by a comma.</p>
<p><a href="http://www.flickr.com/photos/dafnecholet/5374200948/"><img src="http://www.vanseodesign.com/blog/wp-content/uploads/2011/11/calendar.jpg" alt="Calendar" width="465" height="310" /></a></p>
<h2>Animation Events</h2>
<p>You can use Javascript to control some aspects of an animation such as whether or not the animation is running or paused. There are 3 animation related events sent to the DOM.</p>
<ul>
<li><strong>animationstart</strong> &mdash; occurs at the start of the animation</li>
<li><strong>animation end</strong> &mdash; occurs when the animation finishes</li>
<li><strong>animation iteration</strong> &mdash; occurs at the end of each iteration of an animation for which animation-iteration-count is greater than one</li>
</ul>
<p>Each of the above events has 3 significant properties.</p>
<ul>
<li>event.target</li>
<li>event.animationName</li>
<li>event.elapsedTime</li>
</ul>
<p>The event handler can determine the current iteration through a counting mechanism.</p>
<p>With the above in mind you can do things like pause and run an animation with a <a href="http://www.vanseodesign.com/css/css-navigation-buttons/">button</a> click.</p>
<h2>Additional Resources</h2>
<p>Unlike my <a href="http://www.vanseodesign.com/blog/demo/animation/">simple animation demo</a> there are some really good examples of using css animations on web pages. Here are a couple of collections of examples. There&#8217;s a lot of repeat between them.</p>
<ul>
<li><a href="http://webdesignerwall.com/trends/47-amazing-css3-animation-demos">47 Amazing CSS3 Animation Demos</a></li>
<li><a href="http://www.1stwebdesigner.com/css/50-awesome-css3-animations/">50 Awesome Animations made with CSS3</a></li>
</ul>
<p>Here&#8217;s a really good post on <a href="http://coding.smashingmagazine.com/2011/09/14/the-guide-to-css-animation-principles-and-examples/">making your animations look more realistic</a>. It covers some basic principals of animation and shows how to apply them using the css described throughout this post.</p>
<p>Johannes Tonollo has recently put up a site called <a href="http://www.ui-transitions.com/#home">Meaningful Transitions</a>, which seeks to apply principles to animation in user interface design.</p>
<p><a href="http://xilverkit.deviantart.com/art/Storyboard-Markers-56098572"><img src="http://www.vanseodesign.com/blog/wp-content/uploads/2011/11/storyboard-8.jpg" alt="Storyboard for 2 people in a restaurant" width="465" height="517" /></a></p>
<h2>Summary</h2>
<p>CSS animations aren&#8217;t quite everywhere online yet. Browser support is getting closer, but still needs more support, and let&#8217;s face it not every page needs to be or should filled with moving parts.</p>
<p>Like transitions though, animations should not be applied to critical <a href="http://www.vanseodesign.com/web-design/design-elements/">design elements</a> and so while browser support may not be completely there you can still use animations to communicate non-critical information or just to have some fun.</p>
<p>Where appropriate they can be a nice addition to a web page. While the properties and keyframes above can be combined to created some complex animations, I hope you can see that it&#8217;s not to hard to use them for some simple animations.</p>
<p>Have you experimented with css animations or used them in practice? If so where have you found they work best? How have you used them?</p>
<img src="http://www.vanseodesign.com/blog/?ak_action=api_record_view&id=3539&type=feed" alt="" />]]></content:encoded>
			<wfw:commentRss>http://www.vanseodesign.com/css/animation/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>CSS3 Transforms: Adding 2D and 3D Effects To Web Pages</title>
		<link>http://www.vanseodesign.com/css/transforms/</link>
		<comments>http://www.vanseodesign.com/css/transforms/#comments</comments>
		<pubDate>Thu, 10 Nov 2011 13:30:17 +0000</pubDate>
		<dc:creator>Steven Bradley</dc:creator>
				<category><![CDATA[CSS]]></category>

		<guid isPermaLink="false">http://www.vanseodesign.com/?p=3525</guid>
		<description><![CDATA[Transitions are a way to smooth css changes to an element so they&#8217;re more natural. However, they don&#8217;t define the changes themselves. Today I want to look at a set of more dynamic changes, namely transforms.


CSS transforms come in two types.

2D Transforms allow elements rendered by CSS to be transformed in two-dimensional space.
3D Transforms extends [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://www.vanseodesign.com/css/transitions/">Transitions are a way to smooth css changes</a> to an element so they&#8217;re more natural. However, they don&#8217;t define the changes themselves. Today I want to look at a set of more dynamic changes, namely transforms.<br />
<span id="more-3525"></span><br />
<a href="http://www.flickr.com/photos/suzanneandsimon/305244453/"><img src="http://www.vanseodesign.com/blog/wp-content/uploads/2011/11/transformer.jpg" alt="Transformer vehicle" width="465" height="304" /></a></p>
<p>CSS transforms come in two types.</p>
<ul>
<li><a href="http://www.w3.org/TR/css3-2d-transforms/">2D Transforms</a> allow elements rendered by CSS to be transformed in two-dimensional space.</li>
<li><a href="http://www.w3.org/TR/css3-3d-transforms/">3D Transforms</a> extends CSS Transforms to allow elements rendered by CSS to be transformed in three-dimensional space.</li>
</ul>
<p>2D transforms have pretty good <a href="http://caniuse.com/#search=transform">browser support</a>, by which I mean they work in the current versions of all major browsers.</p>
<p>They won&#8217;t work in IE8 and below, however you&#8217;re probably used to that, and like transitions we probably shouldn&#8217;t be using transforms for critical elements.</p>
<p>I&#8217;ve set up a <a href="http://www.vanseodesign.com/blog/demo/transforms/">demo illustrating different 2D transforms</a>.</p>
<p>The situation with 3D transforms is different. These will work in Safari and Chrome (as well as mobile Safari and mobile Android), but not much else. IE10 will support them, though with Firefox and Opera it&#8217;s anyone&#8217;s guess.</p>
<p>Like transitions we&#8217;ll need to use vendor prefixes to get things working, though in this post I&#8217;ll show properties without the prefixes.</p>
<p><a href="http://www.w3.org/TR/css3-2d-transforms/#transform-values"><img src="http://www.vanseodesign.com/blog/wp-content/uploads/2011/11/transform.png" alt="A square that's been rotated 45 degrees, scaled up 1.5 times and moved 80px to the right and down" width="465" height="320" /></a></p>
<h2>2D CSS Transforms</h2>
<p>If you think of a computer monitor as a flat <a href="http://www.vanseodesign.com/web-design/form-surface-volume/">2-dimensional plane</a> then 2D transforms are a way to manipulate elements on that 2-dimensional plane.</p>
<p>There are only 2 properties we have to deal with, though the magic really happens with the values we give to the first property.</p>
<ul>
<li>transform property</li>
<li>transform-origin property</li>
<li>transformation functions (values of the transform property)</li>
</ul>
<p>As we&#8217;ll see in a moment it&#8217;s the last of these, transform functions where the fun begins.</p>
<h3>transform</h3>
<p>The values you can give to the <a href="https://developer.mozilla.org/en/CSS/transform">transform</a> property are none or one or more transform functions. The idea is to set which transform you&#8217;ll be using.</p>
<pre class="css">
<span class="cssSelector">div {</span>
  <span class="cssProperty">transform</span><span class="cssRest">:</span><span class="cssValue"> scale(1.5)</span><span class="cssRest">;</span>
<span class="cssSelector">}</span>
</pre>
<p>The above will scale divs 1.5 times their initial value.</p>
<p>Transforms apply to both <a href="http://www.vanseodesign.com/css/display-property/">block level and inline elements</a>. When we use relative measurements like % they will refer to the element&#8217;s box and not its container.</p>
<h3>transform-origin</h3>
<p>As the name implies the transform-origin property sets the origin of the transformation. The origin becomes a fixed point in the before and after transformation states.</p>
<pre class="css">
<span class="cssSelector">div {</span>
  <span class="cssProperty">transform-origin</span><span class="cssRest">:</span><span class="cssValue"> 0, 0</span><span class="cssRest">;</span>
<span class="cssSelector">}</span>
</pre>
<p>The above sets the origin to 0,0 and this point will be fixed throughout the transformation.</p>
<p>Values allowed are [percentage | length | left | center | right] [percentage | length | top | center | bottom]</p>
<p>The 1st value is the horizontal point and the 2nd is the vertical point unless one of the keywords used. If either horizontal or vertical value isn&#8217;t specified it defaults to center.</p>
<p><a href="http://www.w3.org/TR/css3-2d-transforms/#transform-functions"><img src="http://www.vanseodesign.com/blog/wp-content/uploads/2011/11/matrix-multiply.png" alt="diagram showing matrix multiplication for transformation functions" width="465" height="100" /></a></p>
<h2>Transformation Functions</h2>
<p>This is the magic. Transformation functions define how the element is changed. With one exception you should be able to figure out what each does from the name alone.</p>
<h3>Matrix</h3>
<p>Let&#8217;s start with the exception, since it&#8217;s the general case for all that follows.</p>
<p>The idea here is that all <a href="http://www.w3.org/TR/SVG/coords.html#TransformMatrixDefined">transformations can be represented by a 3&times;3 matrix</a>. In the 2D transforms we&#8217;re only using the top 2 rows in the matrix for a total of 6 values.</p>
<p>The original coordinate system (pre-transform) &equals; this matrix &#038;times the new coordinate system (post-transform).</p>
<p>In essence the matrix transform function allows you to create a custom transform, while the remaining functions are all specific values of this matrix.</p>
<pre class="css">
<span class="cssSelector">div {</span>
 matrix(&lt;number&gt;, &lt;number&gt;, &lt;number&gt;, &lt;number&gt;, &lt;number&gt;, &lt;number&gt;);
<span class="cssSelector">}</span>
</pre>
<p>You&#8217;ll likely use the functions that follow  in practice, but should you want to use matrix you would specific each of the 6 values.</p>
<p><a href="http://www.w3.org/TR/css3-2d-transforms/#transform-functions"><img src="http://www.vanseodesign.com/blog/wp-content/uploads/2011/11/translation.png" alt="A  squre undergoing a translation transform" width="465" height="237" /></a></p>
<h3>Translation</h3>
<p>There are 3 translation transforms, which allow you to move an element from one place to another.</p>
<ul>
<li>translate(&lt;translation-value&gt;[, &lt;translation-value&gt;])</li>
<li>translateX(&lt;translation-value&gt;)</li>
<li>translateY(&lt;translation-value&gt;)</li>
</ul>
<pre class="css">
<span class="cssSelector">div {</span>
  <span class="cssProperty">transform</span><span class="cssRest">:</span><span class="cssValue"> translate(10px, 30%)</span><span class="cssRest">;</span>
 <span class="cssSelector">}</span>
</pre>
<p>translation-values can be either a length or a % and each of the 3 functions above does just what you think it will do.</p>
<p>All 3 <a href="http://www.vanseodesign.com/web-design/does-your-design-flow/">move the element</a> from one location to another by an amount equal to the values set in the function. The first in both x and y coordinates and the latter 2 in one or the other.</p>
<p>When translate is used the first value is the x-coordinate and the second the y-coordinate.</p>
<h3>Scale</h3>
<p>There are also 3 scaling functions, which I&#8217;m sure you can guess make the element larger or smaller</p>
<ul>
<li>scale(&lt;number&gt;[, &lt;number&gt;])</li>
<li>scaleX(&lt;number&gt;)</li>
<li>scaleY(&lt;number&gt;)</li>
</ul>
<pre class="css">
<span class="cssSelector">div {</span>
  <span class="cssProperty">transform</span><span class="cssRest">:</span><span class="cssValue"> scale(2.0)</span><span class="cssRest">;</span>
<span class="cssSelector">}</span>
</pre>
<p><a href="http://www.vanseodesign.com/web-design/size-scale-proportion/">Scale</a> takes a unitless number. Numbers larger than 1.0 scale up and numbers less than 1.0 scale down. Like translate we can scale in both the x and y directions or in one or the other.</p>
<p>As with translate the first value is the x-coordinate and the second the y-coordinate.</p>
<h3>Rotate</h3>
<p>Rotate only has the single function since by definition rotation acts around a fixed point (transform-origin) instead of x and y coordinates.</p>
<ul>
<li>rotate(&lt;angle&gt;)</li>
</ul>
<pre class="css">
<span class="cssSelector">div {</span>
  <span class="cssProperty">transform</span><span class="cssRest">:</span><span class="cssValue"> rotate(45deg)</span><span class="cssRest">;</span>
<span class="cssSelector">}</span>
</pre>
<p>Positive angles rotate clockwise and negative values rotate counter-clockwise</p>
<h3>Skew</h3>
<p>I&#8217;ll give you one guess what skew does. It works similarly to the functions above and comes in the generic and x and y flavors.</p>
<ul>
<li>skew(&lt;angle&gt; [, &lt;angle&gt;])</li>
<li>skewX(&lt;angle&gt;)</li>
<li>skewY(&lt;angle&gt;)</li>
</ul>
<p>With skew you define the angle of skew in either or both directions</p>
<pre class="css">
<span class="cssSelector">div {</span>
  <span class="cssProperty">transform</span><span class="cssRest">:</span><span class="cssValue"> skew(5deg, 20deg)</span><span class="cssRest">;</span>
<span class="cssSelector">}</span>
</pre>
<p><a href="http://www.flickr.com/photos/mccheek/3849489764/"><img src="http://www.vanseodesign.com/blog/wp-content/uploads/2011/11/caterpillar.jpg" alt="A caterpillar eating a leaf" width="465" height="309" /></a></p>
<h2>Multiple Transforms</h2>
<p>You aren&#8217;t limited to a single transform, but can use several as a space separated list.</p>
<pre class="css">
<span class="cssSelector">div {</span>
  <span class="cssProperty">transform</span><span class="cssRest">:</span><span class="cssValue"> translate(10px 30%) scale(2.0) rotate(45deg) skew(5deg, 20deg)</span><span class="cssRest">;</span>
<span class="cssSelector">}</span>
</pre>
<p>When multiple transforms are set the net result is the same as if each transform was applied separately in the order in which they were applied.</p>
<p><a href="http://www.flickr.com/photos/zigazou76/5535071771/"><img src="http://www.vanseodesign.com/blog/wp-content/uploads/2011/11/3d-glasses.jpg" alt="3D glasses" width="465" height="309" /></a></p>
<h2>3D CSS Transforms</h2>
<p>If you&#8217;ve understood how <a href="http://www.opera.com/docs/specs/presto25/css/transforms/">2D transforms</a> work you shouldn&#8217;t have any trouble understanding 3D transforms either as they simply extend their 2D counterparts.</p>
<p>They use the transform property the same way by specifying one of the transform functions. They also set a transform-origin with the one difference being the z-coordinate added to the x and y coordinates.</p>
<pre class="css">
<span class="cssSelector">div {</span>
  <span class="cssProperty">transform-origin</span><span class="cssRest">:</span><span class="cssValue"> 0, 0, 0</span><span class="cssRest">;</span>
<span class="cssSelector">}</span>
</pre>
<p>There are a few new properties added as well, which I&#8217;ll cover briefly since 3D transforms probably aren&#8217;t ready for use yet beyond experimentation.</p>
<ul>
<li><strong>transform-style</strong> &mdash; defines how nested elements are rendered in 3d space. There are 2 values; flat and preserve-3d. Flat means that children of the element are rendered flattened onto the 2d plane of the element and preserve-3d prevents this flattening.</li>
<li><strong>perspective</strong> &mdash; applies the same transform as the perspective transform-function (which we&#8217;ll get to momentary) except that it applies to children of the element. Values are none or a number.</li>
<li><strong>perspective-origin</strong> &mdash; establishes the origin for the perspective set above. Values are  [percentage | length | left | center | right] [percentage | length | top | center | bottom].</li>
<li><strong>backface-visibility</strong> &mdash; determines if the backside of the transformed element is visible. Values are either visible or hidden.</li>
</ul>
<p><a href="http://www.flickr.com/photos/16339684@N00/2637683898/"><img src="http://www.vanseodesign.com/blog/wp-content/uploads/2011/11/cocoon.jpg" alt="A cocoon seen through a magnifying glass" width="465" height="349" /></a></p>
<h2>3D Transformation Functions</h2>
<p>The <a href="http://developer.apple.com/library/safari/#documentation/InternetWeb/Conceptual/SafariVisualEffectsProgGuide/Transforms/Transforms.html">3D transform functions build on the 2D functions</a> in a rather predictable way, mostly by adding one or 2 new functions to each group that shouldn&#8217;t require explanation.</p>
<p>To matrix is added matrix3d, which now takes into account that bottom row of the matrix.</p>
<p>matrix3d(&lt;number&gt;, &lt;number&gt;, &lt;number&gt;, &lt;number&gt;, &lt;number&gt;, &lt;number&gt;, &lt;number&gt;, &lt;number&gt;, &lt;number&gt;;)</p>
<p>Again the remaining functions are specific values of matrix or matrix3d</p>
<p>Translate adds:</p>
<ul>
<li>translate3d(&lt;translation-value&gt;, &lt;translation-value&gt;, &lt;translation-value&gt;)</li>
<li>translateZ(&lt;translation-value&gt;)</li>
</ul>
<p>Scale adds:</p>
<ul>
<li>scale3d(&lt;number&gt;, &lt;number&gt;, &lt;number&gt;)</li>
<li>scaleZ(&lt;number&gt;)</li>
</ul>
<p>Rotate adds:</p>
<ul>
<li>rotate3d(&lt;number>, &lt;number>, &lt;number>, &lt;angle&gt;)</li>
<li>rotateX(&lt;angle&gt;)</li>
<li>rotateY(&lt;angle&gt;)</li>
<li>rotateZ(&lt;angle&gt;)</li>
</ul>
<p>Because we now have another dimension we can specify around which axis the element will rotate.</p>
<p>Skew doesn&#8217;t add anything new, but we do have one new function, perspective.</p>
<ul>
<li>perspective(&lt;number&gt;)</li>
</ul>
<p>Perspective sets a perspective projection matrix, which maps a viewing cube onto a pyramid whose base is infinitely far away and whose peak represents the viewer&#8217;s position.</p>
<p>The number set in the function specifics the depth and represents the distance of the z=0 plane from the viewer.</p>
<p>Huh? That&#8217;s how I felt reading what perspective does. Perhaps this will be clearer.</p>
<p>Lower values lead to a flatter pyramid and higher values a more pronounced pyramid. The lower the number the more foreshortening in the perspective.</p>
<p>Again these 3D transforms are still not supported well and so for now are more for you to experiment with than use in an actual project. However there are some amazing <a href="http://www.netmagazine.com/node/1525?">examples of 3d transforms</a>. You&#8217;ll want to use a Webkit browser to see them. </p>
<p><a href="http://www.flickr.com/photos/snowpeak/6041857571/"><img src="http://www.vanseodesign.com/blog/wp-content/uploads/2011/11/butterfly.jpg" alt="Orange butterfly on a  leaf" width="465" height="348" /></a></p>
<h2>Summary</h2>
<p>Like transitions, transforms can be a nice way to add something to <a href="http://www.vanseodesign.com/web-design/meaningful-design-aesthetics/">the experience of your design</a>.  Transforms don&#8217;t require a css change in order to happen, though they can certainly happen with changes in the state of an element.</p>
<p>Used sparingly and subtly transforms can work to great effect, but be careful about getting carried away with them.</p>
<p>Few if any want to see elements on your page moving around or changing shape all the time. However adding a glow effect when mousing over an element or <a href="http://www.sitepoint.com/css3-transform-background-image/">setting a background image at an angle</a> can be a nice surprise for your audience.</p>
<p>Because browser support is not 100% there yet, you do need to make sure the transformations you set aren&#8217;t critical to the design, but for non-critical things go right ahead and use them.</p>
<img src="http://www.vanseodesign.com/blog/?ak_action=api_record_view&id=3525&type=feed" alt="" />]]></content:encoded>
			<wfw:commentRss>http://www.vanseodesign.com/css/transforms/feed/</wfw:commentRss>
		<slash:comments>8</slash:comments>
		</item>
		<item>
		<title>CSS Transitions: A Simple Way To Delight Your Visitors</title>
		<link>http://www.vanseodesign.com/css/transitions/</link>
		<comments>http://www.vanseodesign.com/css/transitions/#comments</comments>
		<pubDate>Mon, 07 Nov 2011 12:30:44 +0000</pubDate>
		<dc:creator>Steven Bradley</dc:creator>
				<category><![CDATA[CSS]]></category>

		<guid isPermaLink="false">http://www.vanseodesign.com/?p=3520</guid>
		<description><![CDATA[We all want our designs to be functional and usable. We want visitors to easily complete tasks and find what they want. We&#8217;d also like to delight them and make their experience enjoyable and memorable.

CSS3 transitions are an easy way to begin creating that delightful experience.
I&#8217;ve built a demo of a navigation bar with and [...]]]></description>
			<content:encoded><![CDATA[<p>We all want our designs to be <a href="http://www.vanseodesign.com/web-design/minimize-errors-part-i/">functional and usable</a>. We want visitors to easily complete tasks and find what they want. We&#8217;d also like to delight them and make their experience enjoyable and memorable.<br />
<span id="more-3520"></span><br />
<a href="http://www.w3.org/TR/css3-transitions/">CSS3 transitions</a> are an easy way to begin creating that delightful experience.</p>
<p>I&#8217;ve built a <a href="http://www.vanseodesign.com/blog/demo/transitions/">demo of a navigation bar</a> with and without transitions applied. Mouse over the menu items to see the difference transitions can make.</p>
<p>With every post about css3 something needs to be said about browser support. There&#8217;s a section below on support, but for the most part it&#8217;s pretty good as long as you remember to use vendor prefixes and aren&#8217;t using IE.</p>
<p>Without IE support you might think that transitions aren&#8217;t ready for prime time just yet, but this is one area where you can safely use css3 even if it won&#8217;t work across all browsers.</p>
<p>In his book <a href="http://www.abookapart.com/products/css3-for-web-designers">CSS3 For Web Designers</a>, Dan Cederholm points out that we can divide the visual experience on websites into two categories:</p>
<ul>
<li><strong>critical</strong> &mdash; branding, usability, accessibility, layout</li>
<li><strong>non-critical</strong> &mdash; interaction, visual rewards, feedback, movement</li>
</ul>
<p>You shouldn&#8217;t use the latest and greatest technology for the critical items, but they&#8217;re fine to use for the non-critical and transitions play more into the non-critical.</p>
<p>Remove transitions and visitors will still get a fully functional and usable design. They&#8217;ll simply be missing a little extra delight.</p>
<p><a href="http://www.flickr.com/photos/mararie/2904598732/sizes/l/"><img src="http://www.vanseodesign.com/blog/wp-content/uploads/2011/11/astronomical-clock.jpg" alt="Astronomical clock" width="465" height="349" /></a></p>
<h2>How to Use CSS3 Transitions</h2>
<p>The W3C offers the following clear and simple definition of transitions.</p>
<blockquote><p>
CSS transitions allow property changes in css values to occur smoothly over a specified duration.
</p></blockquote>
<p><a href-"http://css3.bradshawenterprises.com/">Transitions</a> are about having changes happen over some duration instead of instantly. In their simplest form they make these changes less jarring and in more complex forms they allow us to create some interesting animations</p>
<p>There are 4 transition properties we can use:</p>
<ul>
<li>transition-property</li>
<li>transition-duration</li>
<li>transition-timing-function</li>
<li>transition-delay</li>
</ul>
<p>Each is quite simple to understand and put to use.</p>
<p><a href=""><img src="http://www.vanseodesign.com/blog/wp-content/uploads/2011/11/transition.png" alt="Transition of left and background-color" width="465" height="122" /></a></p>
<h3>transition-property</h3>
<p>The transition-property defines what properties the transition will apply to. It accepts values of none (the default), all, or a single css property or comma separated list of properties.</p>
<pre class="css">
<span class="cssSelector">nav a {</span>
  <span class="cssProperty">transition-property</span><span class="cssRest">:</span><span class="cssValue"> background-color</span><span class="cssRest">;</span>
<span class="cssSelector">}</span>
</pre>
<p>The above says that any time the <a href="http://www.vanseodesign.com/css/background-properties/">background-color</a> of a link inside the nav element changes a transition should be used.</p>
<p>Transitions can be applied to any element  as well as the :before and :after pseudo classes.</p>
<h3>transition-duration</h3>
<p>A time frame is the key component of a transition so it should be no surprise there&#8217;s a property to define this time frame. The transition-duration property takes a time as a value, usually expressed in seconds (s)</p>
<pre class="css">
<span class="cssSelector">nav a {</span>
  <span class="cssProperty">transition-duration</span><span class="cssRest">:</span><span class="cssValue"> 0.5s</span><span class="cssRest">;</span>
<span class="cssSelector">}</span>
</pre>
<p>The above tells us the transition we&#8217;re applying to <a href="http://www.vanseodesign.com/css/css-navigation-buttons/">nav links</a> should occur over a duration of half a second from start to end.</p>
<p>The default value is 0, which is the same immediate change we get without using transitions. Any value greater than 0 slows the transition from the immediate.</p>
<p><a href="http://www.w3.org/TR/css3-transitions/#transition-duration"><img src="http://www.vanseodesign.com/blog/wp-content/uploads/2011/11/transition-timing-function.png" alt="S-curve showing the transition timing function" width="465" height="375" /></a></p>
<h3>transition-timing-function</h3>
<p>The <a href="http://www.the-art-of-web.com/css/timing-function/">timing function</a> describes how the intermediate values during the transition are calculated and allows for the transition to change speed over its duration.</p>
<p>The effects of the timing function are commonly called easing functions.</p>
<p>It&#8217;s probably the most complicated part of transitions, though it&#8217;s still just as easy to work with. It&#8217;ll take me longer to explain than for you to start using.</p>
<p>The timing function is specified using a cubic bezier curve, which you can see in the image above. There are 4 points on the curve (P0, P1, P2, and P3). Point P0 is always located at coordinates (0,0) and point P3 is always at (1,1). Points P1 and P2 are what the timing function changes</p>
<p>The transition-timing-function has 6 values:</p>
<ul>
<li><strong>ease (default)</strong> &mdash; equivalent to cubic-bezier(0.25, 0.1, 0.25, 1.0)</li>
<li><strong>linear</strong> &mdash; equivalent to cubic-bezier(0.0, 0.0, 1.0, 1.0)</li>
<li><strong>ease-in</strong> &mdash; equivalent to cubic-bezier(0.42, 0, 1.0, 1.0)</li>
<li><strong>ease-out</strong> &mdash; equivalent to cubic-bezier(0, 0, 0.58, 1.0)</li>
<li><strong>ease-in-out</strong> &mdash; equivalent to cubic-bezier(0.42, 0, 0.58, 1.0) </li>
<li><strong>cubic-bezier</strong> &mdash; user defined</li>
</ul>
<p>With the cubic-bezier value you set 4 numbers between 0 and 1, which correspond to the x and y values (x1, y1, x2, y2) of points P1 and P2 of the curve.</p>
<pre class="css">
<span class="cssSelector">nav a {</span>
  <span class="cssProperty">transition-timing-function</span><span class="cssRest">:</span><span class="cssValue"> linear</span><span class="cssRest">;</span>
  <span class="cssProperty">transition-timing-function</span><span class="cssRest">:</span><span class="cssValue"> bezier-curve(0.2, 0.4, 0.8, 0.7)</span><span class="cssRest">;</span>
<span class="cssSelector">}</span>
</pre>
<p>For shorter durations the values won&#8217;t show much difference and ease or linear will probably suffice. The longer the duration, the greater the difference in the values.</p>
<p>You can <a href="http://en.wikipedia.org/wiki/B%C3%A9zier_curve#Cubic_B.C3.A9zier_curves">read up on the math of bezier curves</a>  or you can just <a href="http://matthewlein.com/ceaser/">play around</a> to see how they work. The latter will be a bit easier.</p>
<p><a href="http://www.flickr.com/photos/michaelduxbury/5824469025/"><img src="http://www.vanseodesign.com/blog/wp-content/uploads/2011/11/delay.jpg" alt="Airport display showing flight delays" width="465" height="349" /></a></p>
<h3>transition-delay</h3>
<p>As you can probably guess the transition-delay property defines when the transition will start. As with duration it accepts a time as it&#8217;s value.</p>
<p>The default is 0, which means the transition will begin instantly, though it will still end as defined by the transition-duration.</p>
<pre class="css">
<span class="cssSelector">nav a {</span>
  <span class="cssProperty">transition-delay</span><span class="cssRest">:</span><span class="cssValue"> 0.2s</span><span class="cssRest">;</span>
<span class="cssSelector">}</span>
</pre>
<p>The above delays the start of the transition for 2 &frasl; 10 of a second.</p>
<p>A negative value can be used and means the transition will start immediately, but will begin part way through the transition as if it had started before the action occurred.</p>
<h3>transition shorthand</h3>
<p>As with most things css there&#8217;s a transition shorthand for the 4 properties above.</p>
<p>nav a {<br />
  transition-property | transition-duration | transition-timing-function | transition-delay, [transition-property | transition-duration | transition-timing-function | transition-delay];<br />
}</p>
<pre class="css">
<span class="cssSelector">nav a {</span>
  <span class="cssProperty">transition</span><span class="cssRest">:</span><span class="cssValue"> background 0.5s ease 0.2s</span><span class="cssRest">;</span>
<span class="cssSelector">}</span>
</pre>
<p>The <a href="http://www.vanseodesign.com/web-design/design-basics-alignment/">order is important</a>. The first time value will be applied to duration and second will be applied to delay.</p>
<p><a href="http://www.flickr.com/photos/orangeacid/420493902/"><img src="http://www.vanseodesign.com/blog/wp-content/uploads/2011/11/shift.jpg" alt="Closeup of an arrow pointing up and the word shift" width="465" height="309" /></a></p>
<h2>Starting and Reversing a Transition</h2>
<p>In general when a transition starts it must complete according to the transition properties set even if those properties are changed by another action. However at times it doesn&#8217;t make sense to do this.</p>
<p>A common case is mousing over an element that starts a transition and then quickly mousing out. The rule above says the :hover transition has to complete before transitioning back to it&#8217;s initial value, however this doesn&#8217;t match expected behavior.</p>
<p>Expected behavior is that on mouse out the original transition stops and immediately moves in reverse. This is what the spec calls for.</p>
<p>You can <a href="http://www.w3.org/TR/css3-transitions/#reversing">read the technical explanation</a> about how this is accomplished, but the gist is that whatever part of the transition has happened up to the point where the mouse-out occurred now happens in reverse.</p>
<p>You don&#8217;t have to do anything to make this happen either. It&#8217;s all automatic.</p>
<h2>Properties that can be Transitioned</h2>
<p>Quite a few properties can be transitioned from backgrounds and borders to positioning and z-index and all things in between. There&#8217;s a <a href="http://www.w3.org/TR/css3-transitions/#properties-from-css-">complete list in the spec</a>.</p>
<p>Suffice it to say you shouldn&#8217;t find yourself lacking for different properties you can transition.</p>
<p><a href="http://www.w3.org/TR/css3-transitions/#properties-from-css-"><img src="http://www.vanseodesign.com/blog/wp-content/uploads/2011/11/transition-properties.png" alt="Table of transition properties, values, and html elements they can be applied to" width="465" height="124" /></a></p>
<h2>Transition Events</h2>
<p>What triggers a transition? Anything that changes. While most examples will show transitions on hover there are <a href="http://www.impressivewebs.com/css3-transitions-without-hover/">more events you can apply them to</a>.</p>
<p>If an element&#8217;s style can be triggered to change in any way that change can be transitioned. Some examples:</p>
<ul>
<li>:hover</li>
<li>:focus</li>
<li>:active</li>
<li>:checked</li>
<li>:disabled</li>
<li>media queries</li>
</ul>
<p>If you haven&#8217;t seen it yet check the latest incarnation of <a href="http://css-tricks.com/">CSS Tricks</a> for an example of the last. As you resize your browser the search box will move to a new location with a transition applied.</p>
<p>One thing you might have wondered while reading this post is why the transition is set on the default state instead of the changed state. Wouldn&#8217;t it make sense to define the transition on the hover for example instead of the default link?</p>
<p>The multiple states for which a transition can occur is why the transition property is set on the default state of the element. Imagine having to set a new set of transition properties on each possible triggering state. Much easier to set it once.</p>
<h2>Browser Support</h2>
<p>I mentioned at the start that browser support is good and bad in the usual suspects. Webkit introduced transition and so they work in both Safari and Chrome. Firefox has supported them since version 4.0 and Opera support began in version 10.5.</p>
<p>Transitions are coming to Internet Explorer in version 10, though we know that means it&#8217;ll be awhile before all IE users can see our wonderful transitions.</p>
<p>Again that&#8217;s ok since the transition shouldn&#8217;t be a critical element in your design. An IE user can see the same instant change they would see if transitions didn&#8217;t exist at all.</p>
<ul>
<li><a href="http://caniuse.com/css-transitions">Can I Use CSS Transitions</a></li>
<li><a href="http://www.opera.com/docs/specs/presto23/css/transitions/">CSS3 Transitions support in Opera</a></li>
<li><a href="https://developer.mozilla.org/en/CSS/CSS_transitions">CSS transitions: Mozilaa</a></li>
</ul>
<p>Do keep in mind that vendor prefixes are still needed for all transition properties in all browsers, including IE10.</p>
<pre class="css">
<span class="cssSelector">nav a {</span>
  <span class="cssProperty">-webkit-transition-property</span><span class="cssRest">:</span><span class="cssValue"> all</span><span class="cssRest">;</span>
  <span class="cssProperty">-moz-transition-property</span><span class="cssRest">:</span><span class="cssValue"> all</span><span class="cssRest">;</span>
  <span class="cssProperty">-o-transition-property</span><span class="cssRest">:</span><span class="cssValue"> all</span><span class="cssRest">;</span>
  <span class="cssProperty">-ms-transition-property</span><span class="cssRest">:</span><span class="cssValue"> all</span><span class="cssRest">;</span>
  <span class="cssProperty">transition-property</span><span class="cssRest">:</span><span class="cssValue"> all</span><span class="cssRest">;</span>
<span class="cssSelector">}</span>
</pre>
<p><img src="http://www.vanseodesign.com/blog/wp-content/uploads/2011/08/strucbrowser-logos.jpg" alt="Collage of browser logos" width="465" height="211" /></p>
<h2>Summary</h2>
<p>CSS transitions are simple to work with and they&#8217;re a really nice effect to make changes on your site <a href="http://www.vanseodesign.com/web-design/wabi-sabi-web-design/">happen more naturally</a>. Without them changes are instant, but that&#8217;s generally not how things work in real life.</p>
<p>Transitions are easy to set up. In fact most of what you&#8217;ll be doing is experimenting with different time settings to see if a change looks better occurring over a half second or a full second or some other time frame. You can get rather creative with a few small changes.</p>
<p>You need to use vendor prefixes for now, but browser support is good everywhere outside of IE and support is coming to IE. Since transitions will be non-critical to your design you can safely use them now even if IE users won&#8217;t see them yet.</p>
<p>Have you been using transitions in your work yet? Any tips to share for how and where to apply them?</p>
<p>Next week I want to look at another set of properties we can use for non-critical elements in our designs, namely 2D and 3D transforms, which will allow us to move, scale, rotate, and skew design elements.</p>
<img src="http://www.vanseodesign.com/blog/?ak_action=api_record_view&id=3520&type=feed" alt="" />]]></content:encoded>
			<wfw:commentRss>http://www.vanseodesign.com/css/transitions/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>How To Add Images To Your CSS Borders</title>
		<link>http://www.vanseodesign.com/css/border-images/</link>
		<comments>http://www.vanseodesign.com/css/border-images/#comments</comments>
		<pubDate>Thu, 03 Nov 2011 12:30:22 +0000</pubDate>
		<dc:creator>Steven Bradley</dc:creator>
				<category><![CDATA[CSS]]></category>

		<guid isPermaLink="false">http://www.vanseodesign.com/?p=3495</guid>
		<description><![CDATA[The last few posts I&#8217;ve been working through the background and borders module for css3. So far we&#8217;ve looked at css backgrounds, simple borders, and rounded corners.
Today I want to walk through the border-image property and close with a couple of miscellaneous properties around the boxes we&#8217;ve been talking about.

Browser support: At the moment browser [...]]]></description>
			<content:encoded><![CDATA[<p>The last few posts I&#8217;ve been working through the background and borders module for css3. So far we&#8217;ve looked at <a href="http://www.vanseodesign.com/css/background-properties/">css backgrounds</a>, simple <a href="">borders, and rounded corners</a>.</p>
<p>Today I want to walk through the border-image property and close with a couple of miscellaneous properties around the boxes we&#8217;ve been talking about.<br />
<span id="more-3495"></span><br />
<strong>Browser support:</strong> At the moment <a href="http://caniuse.com/border-image">browser support for css border images</a> is limited. The latest version of Chrome should support everything here. Safari, Firefox, and Opera should support border images only when the shorthand is used. There is currently no support in IE.</p>
<p><a href="http://www.w3.org/TR/css3-background/#border-images"><img src="http://www.vanseodesign.com/blog/wp-content/uploads/2011/10/border-image.png" alt="A border image and the resulting border it creates" width="465" height="233" /></a></p>
<h2>Border Images</h2>
<p>CSS3 introduces the ability to use an image as the border around an element in place of one of the border-styles.</p>
<p>It also provides properties for controlling to a greater degree how these images will display as the border. The <a href="http://www.w3.org/TR/css3-background/#border-images">border-image properties</a> we can use are:</p>
<ul>
<li>border-image-source</li>
<li>border-image-slice</li>
<li>border-image-width</li>
<li>border-image-outset</li>
<li>border-image-repeat</li>
</ul>
<p>Let&#8217;s talk about each of these in more detail and then take a look at the border-image drawing process, which will hopefully tie everything together.</p>
<h3>border-image-source</h3>
<p>This is the easiest of the properties to understand and perhaps the only one you&#8217;ll ever set. As you&#8217;d expect it&#8217;s used for the path of the image you want to use as the border.</p>
<pre class="css">
<span class="cssSelector">div {</span>
  <span class="cssProperty">border-image-source</span><span class="cssRest">:</span><span class="cssValue"> url(<span class="cssString">"path-to-image"</span>)</span><span class="cssRest">;</span>
<span class="cssSelector">}</span>
</pre>
<p>If a value of none is used or if the image can&#8217;t be found then border-styles will be applied instead, suggesting that when using <a href="http://www.sitepoint.com/css3-border-images/">border-image</a> you should still provide some default border-styles to fall back to.</p>
<p>This seems pretty simple, but it may surprise you to look at the image above and realize the image on the left becomes the border for the right.</p>
<p>That might not seem obvious, but will hopefully become clearer with the remaining properties. Keep the image above in mind when reading through the rest of this post.</p>
<p><a href="http://www.w3.org/TR/css3-background/#border-images"><img src="http://www.vanseodesign.com/blog/wp-content/uploads/2011/10/border-image-slice.png" alt="Diagram showing an image sliced into 9 regions" width="465" height="400" /></a></p>
<h3>border-image-slice</h3>
<p>What&#8217;s happening with the <a href="http://www.sitepoint.com/css3-border-image/">border-image</a> above is that it and the area it will be painted on are first sliced into 9 different regions (like a tic-tac-toe board) and then those slices are set around the border of the element.</p>
<p>The border-image-slice property sets how the image itself is divided.</p>
<p>The 9 slices of the image will be 4 corners, 4 edges, and a middle. Again think tic-tac-toe or better, just look at the image above. The middle slice is discarded. It&#8217;s treated as fully transparent unless you use the fill keyword.</p>
<p>The border-image-slice property allows you to set the offsets to slice the image into these 9 regions.</p>
<p>Basically it lets you move the lines of the tic-tac-toe board up and down or left and right. The values are the usual number or &#37; with numbers representing px for a raster image and vector coordinates for a vector image.</p>
<pre class="css">
<span class="cssSelector">div {</span>
  <span class="cssProperty">border-image-slice</span><span class="cssRest">:</span><span class="cssValue"> 20 40 20 40</span><span class="cssRest">;</span>
<span class="cssSelector">}</span>
</pre>
<p>You can add the optional fill value at the end. Assuming a raster image, the above would draw the lines for slicing 20px from the top and bottom and 40px from the left and right.</p>
<p>Negative values are not allowed and values greater then the image size are reduced to 100&#37;. If the 4th value is absent it&#8217;s the same as the 2nd. If the 3rd and/or 2nd is absent it&#8217;s the same as the 1st value.</p>
<p>The regions may overlap, but if the sums for left and right are greater than the image&#8217;s width, the top, bottom, and middle are considered empty or transparent.</p>
<p><a href="http://www.w3.org/TR/css3-background/#border-image-slice"><img src="http://www.vanseodesign.com/blog/wp-content/uploads/2011/10/slice.png" alt="A rectangle divided into 9 unequal slices" width="465" height="354" /></a></p>
<h3>border-image-width</h3>
<p>The border-image-width property also sets offsets. These are used to divide the painting area into 9 parts that will contain the 9 regions of the image sliced above.</p>
<p>The 4 values set represent the inward distances from the sides of the image area.</p>
<pre class="css">
<span class="cssSelector">div {</span>
  <span class="cssProperty">border-image-width</span><span class="cssRest">:</span><span class="cssValue"> 20px 10% 20 10%</span><span class="cssRest">;</span>
<span class="cssSelector">}</span>
</pre>
<p>Values can be a length, number, or &#37; with the default being auto.</p>
<p>As above if the 4th value is absent it&#8217;s the same as the 2nd. If the 3rd and/or 2nd is absent it&#8217;s the same as the 1st value.</p>
<p>Also as above negative values are not allowed and if there&#8217;s overlap the numbers are reduced until there is no longer an overlap.</p>
<p>The auto value uses a width that is intrinsic to the width or height of the image slice.</p>
<h3>border-image-outset</h3>
<p>The border-image-outset specifies the amount by which the border area extends outside the border box</p>
<p>It&#8217;s values can be a length or number and the same 4th being absent, etc applies. Once again no negative numbers can be used.</p>
<p>Even if the outset is set to increase the apparent size of the border box, that expanded area doesn&#8217;t trigger things like scrolling or mouse events. The area is there for the border-image, but nothing else.</p>
<h3>border-image-repeat</h3>
<p>The border-image-repeat property sets how the <a href="http://css-tricks.com/6883-understanding-border-image/">border images</a> for the sides and middle are sized and scaled and has the following values and definitions</p>
<ul>
<li><strong>stretch</strong> &mdash; the image is stretched to fill the area (default)</li>
<li><strong>repeat</strong> &mdash; the image is tiled to fill the area</li>
<li><strong>round</strong> &mdash; the image is tiled to fill the area with a whole number of images and then rescaled to fill the area remaining</li>
<li><strong>space</strong> &mdash; the image is tiled to fill the area with a whole number of images and then space is evenly distributed around the images</li>
</ul>
<p>Two values should be given, the first for the horizontal and the second for the vertical. If the second isn&#8217;t given it&#8217;s considered the same as the first.</p>
<p><a href="http://www.w3.org/TR/css3-background/#border-images"><img src="http://www.vanseodesign.com/blog/wp-content/uploads/2011/10/border-image-properties.png" alt="Diagram illustrating the different border image properties" width="455" height="550" /></a></p>
<h2>border-image Drawing Process</h2>
<p>The above properties aren&#8217;t difficult to understand, but reading through them you may still be thinking, huh? How exactly did that border-image we started with end up looking like the border in the image on the right?</p>
<p>That&#8217;s how I felt too reading through the spec. Hopefully the <a href="http://www.w3.org/TR/css3-background/#border-image-process">drawing process</a> below helps clarify things a bit.</p>
<p>After the border image (source) is sliced into 9 parts, the resulting nine images are scaled, <a href="http://www.vanseodesign.com/css/css-positioning/">positioned</a> and tiled into their corresponding regions in 4 steps.</p>
<ol>
<li><strong>Scale to border-image-width</strong> &mdash; For the top and bottom images the height is set according to the image-slice and then the width is <a href="http://www.vanseodesign.com/web-design/size-scale-proportion/">scaled</a>. For the left and right images the width is set and then the height scaled. The corners are scaled to match both the horizontal and the vertical. The  middle image is finally scaled to match the top and left images.</li>
<li><strong>Scale to border-image-repeat</strong> &mdash; The top middle and bottom  images are the ones that repeat and they are further scaled according to the rules of the value set &mdash; stretch, repeat, round, or space.</li>
<li><strong>Position the first tile</strong> &mdash; If repeat is used, the images are centered and then scaled &mdash; top, middle, bottom and then left, middle, right. Otherwise the images are placed along the top or left edge</li>
<li><strong>Tile and draw </strong> &mdash;  The images are lastly tiled to fill their respective areas. All images are drawn at the same <a href="http://www.vanseodesign.com/css/css-stack-z-index/">stacking level</a> as normal borders, which is immediately in front of the background. If space was set for background-repeat, partial tiles are discarded and extra space is distributed before, after, and between tiles.</li>
</ol>
<p>The basic idea again is the border image area and the border image are each divided into 9 parts &mdash; 4 corners, 4 edges, and a middle.  The size of these parts are determined by the border-image-slice and border-image-width and border-image-outset properties.</p>
<p>The 9 image parts are then placed into the 9 regions of the border image area according to one of several border-image-repeat algorithms.</p>
<p>Using jQuery Nora Brown set up a nice <a href="http://www.norabrowndesign.com/css-experiments/border-image-anim.html">demo to show how border-images work</a>. Play around with the demo if the above isn&#8217;t making complete sense to get a better feeling for how it all works and ties together.</p>
<h2>border-image Shorthand</h2>
<p>Of course all of the above properties can be shorthanded as follows:</p>
<p>&lt;border-image-source&gt; | &lt;border-image-slice&gt; [ / &lt;border-image-width&gt;? [ / &lt;border-image-outset&gt; ]? ]? | &lt;border-image-repeat&gt;</p>
<pre class="css">
<span class="cssSelector">div {</span>
  <span class="cssProperty">border-image</span><span class="cssRest">:</span><span class="cssValue"> url(<span class="cssString">"path-to-image"</span>) 20 40 20 40 / 10 20 10 20 / 10 space</span><span class="cssRest">;</span>
<span class="cssSelector">}</span>
</pre>
<p>Anything omitted is set to its default.</p>
<p>You&#8217;re more likely not to set the outset and keep the slice and width the same so:</p>
<pre class="css">
<span class="cssSelector">div {</span>
  <span class="cssProperty">border-image</span><span class="cssRest">:</span><span class="cssValue"> url(<span class="cssString">"path-to-image"</span>) 20% repeat</span><span class="cssRest">;</span>
<span class="cssSelector">}</span>
</pre>
<p>If you&#8217;re going to play with border-images you&#8217;ll have to use the shorthand. <a href="http://caniuse.com/border-image">No browser currently supports the individual properties</a>. The usual players should support the shorthand though. All except IE, which is only introducing support in IE10.</p>
<p><a href="http://www.w3.org/TR/css3-background/#the-box-decoration-break"><img src="http://www.vanseodesign.com/blog/wp-content/uploads/2011/10/box-break.png" alt="Diagram showing a sliced box on the left and a cloned box on the right as it breaks across pages" width="465" height="363" /></a></p>
<h2>Miscellaneous Properties</h2>
<p>There are two more properties in regards to boxes that are covered under the <a href="http://www.vanseodesign.com/css/css-background-property/">css backgrounds</a> and borders module.</p>
<ul>
<li>box-decoration-break</li>
<li>box-shadow</li>
</ul>
<h3>box-decoration-break</h3>
<p>The box-decoration-break property sets how to treat boxes that are broken by a page break, a column break, or a line break for inline elements.</p>
<p>It&#8217;s used to determine whether to treat the box as broken pieces or continuous and whether each box is wrapped individually with padding and border. It also defines how the background-positioning area is set</p>
<p>The following 2 values can be used:</p>
<ul>
<li><strong>clone</strong> &mdash; each box is individually wrapped with padding, border, border-radius, border-image, and box-shadow. Clone treats the boxes as two individual boxes.</li>
<li><strong>slice</strong> &mdash; no individual wrapping of padding and border. The box is considered to be a single box that&#8217;s been sliced in two. Padding and border are applied as though the box wasn&#8217;t split.</li>
</ul>
<p>Slice is the default value and probably better indicates that the box is the same from page to page, etc.</p>
<pre class="css">
<span class="cssSelector">p {</span>
  <span class="cssProperty">box-decoration-break</span><span class="cssRest">:</span><span class="cssValue"> clone</span><span class="cssRest">;</span>
<span class="cssSelector">}</span>
</pre>
<p>As far as I could find, there&#8217;s not much browser support yet, though <a href="http://www.opera.com/docs/specs/presto26/css/backgroundsborders/#misc">Opera seems to be supporting box-decoration-break</a>.</p>
<p><a href="http://www.w3.org/TR/css3-background/#box-shadow"><img src="http://www.vanseodesign.com/blog/wp-content/uploads/2011/10/box-shadow.png" alt="Diagram showing the different box shadow properties and the resulting shadow" width="465" height="543" /></a></p>
<h3>box-shadow</h3>
<p>As you would guess <a href="http://www.css3.info/preview/box-shadow/">box-shadow</a> adds a shadow to the box, something we previously had to use images to do.</p>
<p>It&#8217;s values are a  comma-separated list of shadows, each specified by 2&ndash;4 length values, an optional color, and an optional &#8216;inset&#8217; keyword.</p>
<p>&lt;shadow&gt; &equals; inset? &#038;&#038; [ &lt;length&gt;{2,4} &#038;&#038; &lt;color&gt;? ]</p>
<p>These values represent the horizontal offset, vertical offset, blur radius, spread distance, color, and the optional inset.</p>
<pre class="css">
<span class="cssSelector">.shadow {</span>
  box-shadow: 5px 5px 10px #999;
<span class="cssSelector">}</span>
</pre>
<p>Inset changes the shadow from a drop shadow to an outer shadow and can be <a href="http://trentwalton.com/2010/11/22/css-box-shadowinset/">used to create some interesting effects</a>. A drop shadow moves an element above the page, while an outer shadow makes an element recede into page.</p>
<p>With spread positive values expand the shadow and negative values contract the shadow. With blur the larger the value the greater the blur.</p>
<p>The shadow shape matches the shape creates by the border-radius, but a border-image has no effect on box shadow.</p>
<p>As is usually the case when talking browser support, the box-shadow property is <a href="http://dev.opera.com/articles/view/cross-browser-box-shadows/">supported in modern browsers including IE9</a>, but won&#8217;t work in IE8 and below.</p>
<p><a href="http://www.flickr.com/photos/neoliminal/2487507985/"><img src="http://www.vanseodesign.com/blog/wp-content/uploads/2011/10/picture-frame-2.jpg" alt="Rusty picture frame around a demolished building" width="465" height="450" /></a></p>
<h2>Summary</h2>
<p>Being able to use an image for an element&#8217;s border is something you probably agree is nice to be able to do. If you&#8217;ve been following along with this post you&#8217;ll have noted browser supported isn&#8217;t quite what it could be.</p>
<p>However outside of IE8 and below the shorthand should work so you can play with border-images now. And since the fallback is the ordinary border-styles you&#8217;d have to use anyway you really should be fine using border-images in any browser, especially as a border image shouldn&#8217;t be conveying any essential information.</p>
<p>How border images are drawn may be a little confusing at first, but hopefully this post has helped clear up any confusion you might have had. Hopefully it hasn&#8217;t created more confusion.</p>
<p>As I&#8217;ve been asking in some of the other css3 posts, have you used border-image in your work and if so how have you found it?</p>
<img src="http://www.vanseodesign.com/blog/?ak_action=api_record_view&id=3495&type=feed" alt="" />]]></content:encoded>
			<wfw:commentRss>http://www.vanseodesign.com/css/border-images/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>CSS Borders: The Basics And Rounded Corners</title>
		<link>http://www.vanseodesign.com/css/borders-rounded-corners/</link>
		<comments>http://www.vanseodesign.com/css/borders-rounded-corners/#comments</comments>
		<pubDate>Mon, 31 Oct 2011 12:30:56 +0000</pubDate>
		<dc:creator>Steven Bradley</dc:creator>
				<category><![CDATA[CSS]]></category>

		<guid isPermaLink="false">http://www.vanseodesign.com/?p=3489</guid>
		<description><![CDATA[Last week I walked through the css background property. The other half of the same W3C spec covers borders and so today I want to begin a walk though of css borders.

Borders are something I&#8217;m sure you use on most projects. Like backgrounds you&#8217;re probably familiar with css borders and yet may not be aware [...]]]></description>
			<content:encoded><![CDATA[<p>Last week I walked through the <a href="http://www.vanseodesign.com/css/background-properties/">css background property</a>. The other half of the same W3C spec covers borders and so today I want to begin a walk though of <a href="http://www.w3.org/TR/css3-background/#borders">css borders</a>.<br />
<span id="more-3489"></span><br />
Borders are something I&#8217;m sure you use on most projects. Like backgrounds you&#8217;re probably familiar with css borders and yet may not be aware of some of all the properties and their values.</p>
<p>In this post I&#8217;ll look at the basic css border and then move into rounded corners with border-radius. Next time I&#8217;ll take an in-depth look at working with border-images.</p>
<p><a href="http://www.flickr.com/photos/macinate/3417246829/"><img src="http://www.vanseodesign.com/blog/wp-content/uploads/2011/10/fence-wooden.jpg" alt="Closeup of a wooden fence with another wooden fence blurred in the background" width="465" height="309" /></a></p>
<h2>CSS Border</h2>
<p>As I said above I&#8217;m sure you&#8217;re familiar with using css borders. I&#8217;m also going to guess there are a few finer points with them that you may not know.</p>
<p>You may get tired of me <a href="http://www.vanseodesign.com/web-design/rearranging-boxes/">talking about boxes</a> all the time, but let me refer to the box model once again. Borders are the area between padding and margin. Margins are outside of borders and paddings are inside, but you already knew that.</p>
<p>Borders apply to all html elements except tables when the table&#8217;s border-collapse property has been set to collapse.</p>
<h3>border-color</h3>
<p>As the name implies the border-color sets a color for the border. You can apply any color using an acceptable color name, hexadecimal notation, rgb, or rgba notation.</p>
<p>border-color is actually a shorthand for 4 properties, border-top-color, border-right-color, border-bottom-color, border-left-color.</p>
<p>Most of the time you&#8217;re going to set the same color and use the shorthand, but you can achieve some interesting <a href="http://www.vanseodesign.com/css/creating-shapes-with-css-borders/">shapes and effects</a> by varying the color along with some other properties.</p>
<p><a href="http://www.w3.org/TR/css3-background/#the-border-style"><img src="http://www.vanseodesign.com/blog/wp-content/uploads/2011/10/border-styles.png" alt="How different border styles might look in a browser" width="465" height="266" /></a></p>
<h3>border-style</h3>
<p>You probably use the same 2 or 3 border-styles all the time. Solid most often and perhaps dotted and dashed at times. There are several other values you can use, though.</p>
<p>The border style can be set to none, hidden, dotted, dashed, solid, double, groove, ridge, inset, outset</p>
<p><a href="http://www.vanseodesign.com/css/cross-browser-css/">Different browsers</a> will represent these styles somewhat differently. A border-style of ridge won&#8217;t look exactly the same in Safari or Chrome as it does in Firefox or IE, but the general characteristics will be the same.</p>
<p>The image above shows what each of these styles should look like approximately, but play with them in different browsers to see how they actually appear.</p>
<p>Like border-color, border-style is also shorthand. You can set each side with border-top-style, etc.</p>
<h3>border-width</h3>
<p>border-width is, of course, about sizing the width of the border. Allowable values are.</p>
<ul>
<li>length (px, em)</li>
<li>thin</li>
<li>medium</li>
<li>thick</li>
<li>inherit</li>
</ul>
<p>Generally you&#8217;ll set the length since thin, medium, and thick are determined by the browser. They will remain consistent in a document, but otherwise their size is out of your control.</p>
<p>And as with the other two properties above border-width is shorthand for border-top-width, etc.</p>
<h3>Border Shorthand</h3>
<p>Speaking of shorthand, we can use one shorthand of all 3 of the properties above.</p>
<p>{code type=css}<br />
div {<br />
  border-width border-style color;<br />
}</p>
<p>div {<br />
  border: 1px solid red;<br />
}<br />
{code}</p>
<p>You don&#8217;t need to use all three properties in the shorthand. I often find I want to add top and bottom or left and right borders, but not the other.	</p>
<p>{code type=css}<br />
div {<br />
  border: solid red;<br />
  border-width: 1px 0;<br />
}<br />
{code}</p>
<p><a href="http://www.w3.org/TR/css3-background/#the-border-radius"><img src="http://www.vanseodesign.com/blog/wp-content/uploads/2011/10/rounded-corners.png" alt="Diagram showing rounded corners created by different horizontal and vertical border-radius values" width="465" height="300" /></a></p>
<h2>Rounded Corners</h2>
<p>In css every element is a rectangular box. There&#8217;s no way around it. Curves are nice though, and for years developers had to use images to show curves on web pages.</p>
<p>The border-radius property changed that.</p>
<h3>border-radius</h3>
<p>At its simplest you set a <a href="http://www.css3.info/preview/rounded-border/">border-radius</a> by giving it a single value as either a length or &#37;</p>
<p>{code type=css}<br />
div {<br />
  border-radius: 10px;<br />
}<br />
{code}</p>
<p>The 10px represents the radius of a circle at each corner and the corner is drawn as an arc of this circle instead of as a 90 degree corner.</p>
<p>The above is shorthand for:</p>
<p>{code type=css}<br />
div {<br />
  border-top-left-radius: 10px;<br />
  border-top-right-radius: 10px;<br />
  border-bottom-right-radius: 10px;<br />
  border-bottom-left-radius: 10px;<br />
{code}</p>
<p>There&#8217;s more though. Each corner can have both a horizontal and vertical radius. You show these two values with a slash. The values before the slash are the horizontal values and those after the slash are vertical.</p>
<p>{code type=css}<br />
div {<br />
  border-top-left-radius: 10px / 2%;<br />
}<br />
{code}</p>
<p>Putting things back together in the shorthand you get</p>
<p>{code type=css}<br />
div {<br />
  border-radius: 10px 5px 15px 10px / 5px 10px 15px 20px;<br />
}<br />
{code}</p>
<p>The corners follow a clockwise top-left, top-right, bottom-right, bottom-left order. If the bottom-left value is omitted it&#8217;s the same as the top-right and if the bottom-right is omitted it&#8217;s the same as the top-left. The opposite corner in both cases.</p>
<p>The border-radius property has pretty <a href="http://caniuse.com/border-radius">good support in modern browsers</a>, though it won&#8217;t work in IE8 or below. Until recently you needed to use vendor specific prefixes, but that&#8217;s no longer the case in the latest browsers including IE9.</p>
<p>For IE8 and below I find jQuery offers a nice solution that works similarly to the css. You can <a href="http://jquery.malsup.com/corner/">see it demonstrated here</a>. The demo links to the jQuery plugin it relies on.</p>
<p><a href="http://www.w3.org/TR/css3-background/#corner-shaping"><img src="http://www.vanseodesign.com/blog/wp-content/uploads/2011/10/corner-shaping.png" alt="Diagram showing corner shaping: Different width borders and borders thicker than the radius" width="465" height="201" /></a></p>
<h3>Corner Shaping</h3>
<p>The resulting shape of the corner can vary based on both the border radius and the border width.</p>
<p>It&#8217;s easiest to see in the image above, which is the effect of a a rounded corner connecting two borders of unequal thickness (left) and the effect of a rounded corner on two borders that are thicker than the radius of the corner (right).</p>
<p>This happens because the radius of the padding edge (inner border) is the radius of the outer border minus the corresponding border thickness, which can result in a negative or positive value as well as a value of 0.</p>
<h3>Corner Clipping</h3>
<p>Last week I talked about the background-clipping property. The <a href="http://www.vanseodesign.com/css/css-background-property/">background</a>, though not a border-image (which we&#8217;ll get to next week) are clipped to the curve of the border-radius.</p>
<p>Other things such as the overflow property are also clipped to the curve and mouse events don&#8217;t occur outside the curve.</p>
<p>The element is still a rectangular shape, but many things can&#8217;t happen in the small area between the curve and 90 degree corner that&#8217;s no longer seen.</p>
<p><a href="http://www.w3.org/TR/css3-background/#corner-transitions"><img src="http://www.vanseodesign.com/blog/wp-content/uploads/2011/10/transition-region.png" alt="Diagram showing the transitional region over a rounded corner" width="465" height="138" /></a></p>
<h3>Color and Style Transitions</h3>
<p>When the borders meeting at a corner have different properties set (color, radius, etc) a transition has to occur.</p>
<p>Again this is probably easiest to see in the image above, which shows several different rounded corners and the area over which a transition will exist.</p>
<p>I won&#8217;t attempt to explain the math involved because I have no idea what it is.</p>
<p>The important thing to know is that this transition exists and that we can probably have some fun playing around with different values and see what happens.</p>
<h3>Overlapping Curves</h3>
<p>Corner curves can&#8217;t overlap. If the radii of two adjacent corners exceeds the size of the border box, browsers will proportionally reduce the values until they no longer overlap.</p>
<p>The values may be further reduced if they interfere with things like scrollbars as well.</p>
<p>Again I&#8217;ll spare you the complicated algorithm, though you&#8217;re welcome to <a href="http://www.w3.org/TR/css3-background/#corner-overlap">check for yourself</a> if you&#8217;re interested in the math.</p>
<h3>The Effect of border-radius on Tables</h3>
<p>The border-radius properties do apply to <a href="http://www.vanseodesign.com/css/tables/">tables or inline-tables</a>, though potentially may not if the border-collapse of the table is set to collapse.</p>
<p>The effect of border-radius on inline elements is undefined. There may be a definition in the future, but for now your guess is as good as anyone&#8217;s as to what will happen.</p>
<p>Perhaps it&#8217;s best not to use the border-radius properties on tables at the moment.</p>
<p><a href="http://www.flickr.com/photos/sbsin/5779996965/"><img src="http://www.vanseodesign.com/blog/wp-content/uploads/2011/10/fence-closeup.jpg" alt="Closeup of an iron fence with cobweb" width="465" height="309" /></a></p>
<h2>Summary</h2>
<p>Much of the above was probably familiar to you. We&#8217;ve all been using borders for years and for the last few I&#8217;m sure the border-radius has entered the work of many of you.</p>
<p>While most of what&#8217;s included in this post seems simple on the surface there&#8217;s quite a bit you can do with borders if you experiment, especially if you play around with the height and width of the content box.</p>
<p>You can create some interesting <a href="http://css-tricks.com/examples/ShapesOfCSS/">shapes with css</a> borders and with the addition of some pseudo elements you can create some rather complex shapes</p>
<p>Hopefully I&#8217;ve been able to introduce you to a few new things or perhaps clear up something that hasn&#8217;t been obvious. Play around with css borders and see what you can do.</p>
<img src="http://www.vanseodesign.com/blog/?ak_action=api_record_view&id=3489&type=feed" alt="" />]]></content:encoded>
			<wfw:commentRss>http://www.vanseodesign.com/css/borders-rounded-corners/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>CSS Background: There&#8217;s More To Know Than You Think</title>
		<link>http://www.vanseodesign.com/css/background-properties/</link>
		<comments>http://www.vanseodesign.com/css/background-properties/#comments</comments>
		<pubDate>Mon, 24 Oct 2011 12:30:00 +0000</pubDate>
		<dc:creator>Steven Bradley</dc:creator>
				<category><![CDATA[CSS]]></category>

		<guid isPermaLink="false">http://www.vanseodesign.com/?p=3464</guid>
		<description><![CDATA[You likely use css backgrounds in every site you build. You give an element a background color and tell another element to let a background image repeat. How much do you know about all the other background properties?

Did you know you can control where in the background an image is painted or that you can [...]]]></description>
			<content:encoded><![CDATA[<p>You likely use <a href="http://www.vanseodesign.com/css/css-background-property/">css backgrounds</a> in every site you build. You give an element a background color and tell another element to let a background image repeat. How much do you know about all the other background properties?<br />
<span id="more-3464"></span><br />
Did you know you can control where in the background an image is painted or that you can even specify which part of an element is the background?</p>
<p>I&#8217;d like to run through the different <a href="http://www.w3.org/TR/css3-background/">background properties</a> css3 offers us. Some of what follows I&#8217;m sure will be familiar to you. Some will probably be new. Hopefully this post will give you a greater understanding of a property you use all the time.</p>
<p><strong>Note:</strong> As mentioned in the comments below support for some of what&#8217;s in this post is less than I originally thought.</p>
<p>The space and round values don&#8217;t appear to be working (outside of Opera) for the background-repeat property and issues have been reported with background-position. My apologies for not being aware of these limitations earlier.</p>
<p><a href="http://www.flickr.com/photos/zooboing/5394918666/"><img src="http://www.vanseodesign.com/blog/wp-content/uploads/2011/10/background-4.jpg" alt="Background of colored stripes in a sun ray pattern" width="465" height="362" /></a></p>
<h2>CSS Background</h2>
<p>The first thing to understand is what does css consider as background and we&#8217;ll turn to the css box model with its content, padding, border, and margin for an answer.</p>
<p>Everything except margins is considered background, though we&#8217;ll see in a bit that css3 allows us to alter this background painting area to some degree,</p>
<p>Backgrounds can be transparent (the default), filled with color, or filled with images. Different properties determine which is used and in the case of images further properties are used to determine how the images are sized, positioned, and more.</p>
<p>In practice you&#8217;ll often use the background shorthand to set all these properties, but let&#8217;s look at them individually. I&#8217;ll come back to the shorthand later in this post.</p>
<h3>background-color</h3>
<p>I&#8217;m sure you&#8217;ve set a css background color before. For those who&#8217;ve never used it here&#8217;s one way you&#8217;d set a background color on paragraphs.</p>
<pre class="css">
<span class="cssSelector">p {</span><span class="cssProperty">background-color</span><span class="cssRest">:</span><span class="cssValue"> red</span><span class="cssRest">;</span><span class="cssSelector">}</span>
</pre>
<p>You can also use hexadecimal or rgb notation instead.</p>
<pre class="css">
<span class="cssSelector">p {</span><span class="cssProperty">background-color</span><span class="cssRest">:</span><span class="cssValue"> #f00</span><span class="cssRest">;</span><span class="cssSelector">}</span>
<span class="cssSelector">p {</span><span class="cssProperty">background-color</span><span class="cssRest">:</span><span class="cssValue"> #ff0000</span><span class="cssRest">;</span><span class="cssSelector">}</span>
<span class="cssSelector">p {</span><span class="cssProperty">background-color</span><span class="cssRest">:</span><span class="cssValue"> rgb(255, 0, 0</span><span class="cssRest">;</span>)<span class="cssSelector">}</span>
</pre>
<p>All 3 lines of css above are equivalent and set a red background on the paragraph.</p>
<p>One last way to specify a <a href="http://www.w3.org/TR/css3-color/#rgba-color">background-color is rgba</a>. The &#8220;a&#8221; stands for alpha transparency and is set as a fraction between 0 and 1, with 0 being fully transparent and 1 being fully opaque.</p>
<pre class="css">
<span class="cssSelector">p {</span><span class="cssProperty">background-color</span><span class="cssRest">:</span><span class="cssValue"> rgba(255, 0, 0, 0.5)</span><span class="cssRest">;</span><span class="cssSelector">}</span>
</pre>
<p>The above sets the background to the same red as before, though at 50&#37; opacity. <a href="http://css-tricks.com/2151-rgba-browser-support/">Browser support for rgba</a> isn&#8217;t as good as rgb, but the fallback is generally to present the color as completely opaque or with an &#8220;a&#8221; of 1.0.</p>
<p>The one thing you might not be familiar with is that the background-color will be clipped according to the background-clip property.</p>
<p>We&#8217;ll get to this property shortly, but for now know it&#8217;s a way to prevent color from appearing behind the border of your element or even behind the element&#8217;s padding.</p>
<p><a href="http://www.flickr.com/photos/brenda-starr/5084485739/"><img src="http://www.vanseodesign.com/blog/wp-content/uploads/2011/10/background-2.jpg" alt="Background image of a path through the forest" width="465" height="322" /></a></p>
<h3>background-image</h3>
<p>I&#8217;m sure you&#8217;ve also set a background-image before. What&#8217;s nice is that in css3 we can add multiple images like so:</p>
<pre class="css">
<span class="cssSelector">body {</span>background-image: url(<span class="cssString">"image1"</span>), url(<span class="cssString">"image2"</span>), url(<span class="cssString">"image3"</span>)<span class="cssSelector">}</span>
</pre>
<p>The above adds 3 background images to the body of a document.</p>
<p>Each image creates a new background <a href="http://www.vanseodesign.com/css/css-stack-z-index/">layer</a> so the images sit one on top of another. A value of none, an image with 0 width and height, or an image that fails to download still creates a new layer, though one that doesn&#8217;t show an image.</p>
<p>The first image in the list is seen as being closest to the viewer and each successive image is one layer further into the background. If a background color is also specified, it&#8217;s painted below all images and is furthest from the viewer.</p>
<p>There are two important guidelines you should follow when setting background-images.</p>
<ul>
<li>Specify a background-color along with background-image(s) to preserve contrast for text if the images(s) don&#8217;t display.</li>
<li>Don&#8217;t use background-images to <a href="http://www.vanseodesign.com/web-design/web-design-harmony-concept-conveyance-and-theme/">convey important information</a> as they may not display in all browsers.</li>
</ul>
<p>Background images will be sized, positioned, and tiled according properties we&#8217;ll cover momentarily.</p>
<p>With multiple images the values of these other properties will also be a comma separated list and the first value will correspond to the first image, etc.</p>
<p>If there are excess values at the end of these other properties, they&#8217;ll be ignored and if there are not enough the given values are repeated until there are enough to match the images.</p>
<p><a href="http://caniuse.com/#search=background">Browser support</a> for multiple background images is good in all browsers, except for Internet Explorer 8 and below.</p>
<p><a href="http://www.flickr.com/photos/brian_reynolds/2207257172/"><img src="http://www.vanseodesign.com/blog/wp-content/uploads/2011/10/pattern.jpg" alt="Repating tile pattern" width="465" height="267" /></a></p>
<h3>background-repeat</h3>
<p>Background-repeat determines how background-images are tiled and has the allowed values of no-repeat, repeat-x, repeat-y, repeat, space, and round.</p>
<ul>
<li><strong>no-repeat</strong> &mdash; the image isn&#8217;t tiled</li>
<li><strong>repeat-x</strong> &mdash; the image tiles horizontally only</li>
<li><strong>repeat-y</strong> &mdash; the image tiles vertically only</li>
<li><strong>repeat</strong> &mdash; the image tiles both horizontally and vertically</li>
<li><strong>space</strong> &mdash; the image will tile to fill the background with as many complete images as possible. The images wil then be spaced evenly with first and last images touching the edge</li>
<li><strong>round</strong> &mdash; image will be tiled to fill the background with as many complete images as possible. The images will then be scaled to fit the area exactly.</li>
</ul>
<p>You&#8217;ve no doubt used all of the first 4 values above, but how many of you were aware of the last 2. I know I wasn&#8217;t.</p>
<pre class="css">
<span class="cssSelector">body {</span>
  <span class="cssProperty">background-repeat</span><span class="cssRest">:</span><span class="cssValue"> no-repeat, repeat-y, space</span><span class="cssRest">;</span>
<span class="cssSelector">}</span>
</pre>
<p>Assuming the 3 background images from above the first wouldn&#8217;t be repeated, the second would be repeated vertically, and the last would use the space algorithm to repeat the images both horizontally and vertically.</p>
<h3>background-attachment</h3>
<p>The background-attachment property determines how the image scrolls with respect to the viewport. It has the 3 values below</p>
<ul>
<li><strong>fixed</strong> &mdash; the image doesn&#8217;t scroll. It&#8217;s fixed where it is.</li>
<li><strong>local</strong> &mdash; the image scrolls with the element&#8217;s content. It&#8217;s fixed to the content.</li>
<li><strong>scroll</strong> &mdash; the image scrolls with element.  It&#8217;s fixed to element.</li>
</ul>
<pre class="css">
<span class="cssSelector">body {</span>
  <span class="cssProperty">background-attachment</span><span class="cssRest">:</span><span class="cssValue"> fixed</span><span class="cssRest">;</span>
<span class="cssSelector">}</span>
</pre>
<p>The above keeps the background-image fixed in place with respect to the body.</p>
<p>I can&#8217;t think of a single time I&#8217;ve ever set the background-attachment to local. Most of the time I leave it as the default (scroll), but sometimes fixing the image can make for an interesting effect.</p>
<p><a href="http://www.w3.org/TR/css3-background/#the-background-position"><img src="http://www.vanseodesign.com/blog/wp-content/uploads/2011/10/background-position.png" alt="Diagram showing background position coordinates 50% from top and 75% from left" width="465" height="310" /></a></p>
<h3>background-position</h3>
<p>By default background images are painted at the top-left corner of an element, but they don&#8217;t have to be. You can change where they&#8217;re positioned through the background-position property.</p>
<p>The background-position can be set horizontally, vertically, or both. Horizontally you can use the values center, left, and right. Vertically you can use the values center, top, and bottom. In both directions you can set a &#37; or length.</p>
<pre class="css">
<span class="cssSelector">body {</span>
  <span class="cssProperty">background-position</span><span class="cssRest">:</span><span class="cssValue"> right 20px</span><span class="cssRest">;</span>
<span class="cssSelector">}</span>
</pre>
<p>The above <a href="http://www.vanseodesign.com/css/css-positioning/">positions</a> the image 20px down along the right edge of the element.</p>
<p>When 2 values are set the first is assumed to be the horizontal and the second vertical, unless the keywords say otherwise. When only one is set the second is assumed to be center.</p>
<p>&#37; and length represent an offset from the top-left corner, however if 3 or 4 values are given then &#37; or length is offset from keyword</p>
<pre class="css">
<span class="cssSelector">body {</span>
  <span class="cssProperty">background-position</span><span class="cssRest">:</span><span class="cssValue"> bottom 10px right 20%</span><span class="cssRest">;</span>
<span class="cssSelector">}</span>
</pre>
<p>The above paints the image 10px from bottom and 20&#37; from right.</p>
<p><a href="http://xjordanx.deviantart.com/art/Background-17006349"><img src="http://www.vanseodesign.com/blog/wp-content/uploads/2011/10/background-6.jpg" alt="Abstract background image" width="465" height="349" /></a></p>
<h2>CSS3 Background Properties</h2>
<p>Outside of the ability to use multiple background images all of the above have been available for awhile. Below are some new background properties specific to css3.</p>
<p>They <a href="http://caniuse.com/#search=background-image">work in most browsers</a>, however they won&#8217;t work in IE8 or below.</p>
<h3>background-clip</h3>
<p>Earlier in this post I mentioned that the background was everything in the element&#8217;s box except for the margins. This and the next property can alter that.</p>
<p>The background-clip property determines where a background is clipped, which determines the background painting area. It has the following values</p>
<ul>
<li><strong>border-box</strong> &mdash; painted within (clipped to) border</li>
<li><strong>padding-box</strong> &mdash; painted within (clipped to) padding</li>
<li><strong>content-box</strong> &mdash; painted within (clipped to) content</li>
</ul>
<pre class="css">
<span class="cssSelector">body {</span>
  <span class="cssProperty">background-clip</span><span class="cssRest">:</span><span class="cssValue"> content-box</span><span class="cssRest">;</span>
<span class="cssSelector">}</span>
</pre>
<p>The above clips the image or color so it only <a href="http://www.vanseodesign.com/css/display-property/">displays</a> behind the content part of the element&#8217;s box. The color or image will not show behind the element&#8217;s padding or border.</p>
<p>This is another property I can&#8217;t claim to have used, but having learned of it I can think of times when I would want to.</p>
<p><a href="http://www.flickr.com/photos/bitchbuzz/4008573590/"><img src="http://www.vanseodesign.com/blog/wp-content/uploads/2011/10/origin.jpg" alt="Sign for Origin: the London Craft Fair" width="465" height="349" /></a></p>
<h3>background-origin</h3>
<p>Similar is the background-origin property, which sets the origin for the background-position property. It&#8217;s values are the same as background-clip.</p>
<ul>
<li><strong>border-box</strong> &mdash; positioned relative to border box</li>
<li><strong>padding-box</strong> &mdash; positioned relative to padding box</li>
<li><strong>content-box</strong> &mdash; positioned relative to content box</li>
</ul>
<pre class="css">
<span class="cssSelector">body {</span>
  <span class="cssProperty">background-origin</span><span class="cssRest">:</span><span class="cssValue"> padding-box</span><span class="cssRest">;</span>
<span class="cssSelector">}</span>
</pre>
<p>Above the image or color would start inside the body border and behind any padding on the body.</p>
<p>Note that if the background-attachment is fixed then background-position has no effect.</p>
<h3>background-size</h3>
<p>The last property we can set on backgrounds is background-size. While images will be whatever size they are by default (the auto value) you can control the image size in several ways.</p>
<p>The obvious values are the usual &#37; and length.</p>
<pre class="css">
<span class="cssSelector">body {</span>
  <span class="cssProperty">background-size</span><span class="cssRest">:</span><span class="cssValue"> 300px 40%</span><span class="cssRest">;</span>
<span class="cssSelector">}</span>
</pre>
<p>The first value is the width and the second the height. Negative values are not permitted.</p>
<p>If only one value is given, the second is assumed to be auto. When one value or both are set to auto the image size is resolved using the images aspect ratio or failing that 100&#37;.</p>
<p>There are 2 other values that control how the image might be sized.</p>
<ul>
<li><strong>contain</strong> &mdash;  <a href="http://www.vanseodesign.com/web-design/size-scale-proportion/">scales</a> the image while preserving its aspect ratio to the largest size that both height and width can still fit within the background area</li>
<li><strong>cover</strong> &mdash;  scales the image while preserving its aspect ratio to the smallest size that both height and width can still fit within the background area</li>
</ul>
<p>If neither width or height is specified then the image is sized based on the contain method above.</p>
<p><a href="http://springlight.deviantart.com/art/Music-is-106389262"><img src="http://www.vanseodesign.com/blog/wp-content/uploads/2011/10/musical-shorthand.jpg" alt="musical notation as shorthand" width="465" height="291" /></a></p>
<h2>Background Property Shorthand</h2>
<p>While you might set the above background properties individually, most of the time you&#8217;ll likely use the shorthand. In the past I&#8217;ve always placed the color first, the image second, and then any other properties after without thinking much to their order.</p>
<p>It&#8217;s always seemed to work, but it&#8217;s incorrect. The W3C specifies the following order.</p>
<ul>
<li>&lt;bg-layer&gt; &equals; &lt;bg-image&gt; || &lt;bg-position&gt; [ / &lt;bg-size&gt; ]? || &lt;repeat-style&gt; || &lt;attachment&gt; || &lt;box&gt;{1,2}</li>
<li>&lt;final-bg-layer&gt; &equals; &lt;bg-image&gt; || &lt;bg-position&gt; [ / &lt;bg-size&gt; ]? || &lt;repeat-style&gt; || &lt;attachment&gt; || &lt;box&gt;{1,2} || &lt;background-color&gt;</li>
</ul>
<pre class="css">
background-image background-position / background-size background-repeat background-attachment background-origin background-clip color;
</pre>
<p>Color can only be used on the final background layer. A single background image is of course the final background layer.</p>
<pre class="css">
<span class="cssSelector">body {</span><span class="cssProperty">background</span><span class="cssRest">:</span><span class="cssValue"> url(<span class="cssString">"image.png"</span>) top left / 95% 95% no-repeat scroll padding-box content-box #333</span><span class="cssRest">;</span><span class="cssSelector">}</span>
</pre>
<p>The above is equivalent to:</p>
<pre class="css">
<span class="cssSelector">body {</span>
  <span class="cssProperty">background-image</span><span class="cssRest">:</span><span class="cssValue"> url(<span class="cssString">"image.png"</span>)</span><span class="cssRest">;</span>
  <span class="cssProperty">background-position</span><span class="cssRest">:</span><span class="cssValue"> top left</span><span class="cssRest">;</span>
  <span class="cssProperty">background-size</span><span class="cssRest">:</span><span class="cssValue"> 95% 95%</span><span class="cssRest">;</span>
  <span class="cssProperty">background-repeat</span><span class="cssRest">:</span><span class="cssValue"> no-repeat</span><span class="cssRest">;</span>
  <span class="cssProperty">background-attachment</span><span class="cssRest">:</span><span class="cssValue"> scroll</span><span class="cssRest">;</span>
  <span class="cssProperty">background-origin</span><span class="cssRest">:</span><span class="cssValue"> padding-box</span><span class="cssRest">;</span>
  <span class="cssProperty">background-clip</span><span class="cssRest">:</span><span class="cssValue"> content-box</span><span class="cssRest">;</span>
  <span class="cssProperty">background-color</span><span class="cssRest">:</span><span class="cssValue"> #333<span class="cssSelector">}</span></span><span class="cssRest">;</span>
<span class="cssMedia">}</span>
</pre>
<p>You don&#8217;t have to <a href="http://www.vanseodesign.com/css/css-specificity-inheritance-cascaade/">specify a value</a> for every property in the shorthand. To use the shorthand on multiple images you would separate each set of values by a comma, remembering only to use a color on the last set of values.</p>
<p><a href="http://www.flickr.com/photos/andrearusky/2240595375/"><img src="http://www.vanseodesign.com/blog/wp-content/uploads/2011/10/background-3.jpg" alt="Abstract background with a vintage feel" width="465" height="279" /></a></p>
<h2>Summary</h2>
<p>CSS backgrounds are something you no doubt use on every site. However you may not have been familiar with each of the different background properties available or some of the values for the properties you do know.</p>
<p>While much of the time you&#8217;ll happily let defaults reign with most of these properties, a few give you some nice control over how and where your background images display.</p>
<p>Browsers support is generally good for multiple images and the three new properties, origin, clip, and size, but do know that IE8 and below won&#8217;t support them.</p>
<p>That may or may not be an issue depending on which browsers you need to support and how you&#8217;re using these properties.</p>
<p>Hopefully this post has given you a greater understanding of a property you likely know well. Have you used any of the new properties or worked with multiple background images yet? If so how have you used them and how have they worked for you?</p>
<img src="http://www.vanseodesign.com/blog/?ak_action=api_record_view&id=3464&type=feed" alt="" />]]></content:encoded>
			<wfw:commentRss>http://www.vanseodesign.com/css/background-properties/feed/</wfw:commentRss>
		<slash:comments>25</slash:comments>
		</item>
		<item>
		<title>Are CSS Tables Better Than HTML Tables?</title>
		<link>http://www.vanseodesign.com/css/tables/</link>
		<comments>http://www.vanseodesign.com/css/tables/#comments</comments>
		<pubDate>Thu, 13 Oct 2011 12:30:29 +0000</pubDate>
		<dc:creator>Steven Bradley</dc:creator>
				<category><![CDATA[CSS]]></category>

		<guid isPermaLink="false">http://www.vanseodesign.com/?p=3428</guid>
		<description><![CDATA[Mention css and tables in the same sentence and controversy is sure to follow. Web designers like myself have been telling you not to use html tables for layouts and now here we have a way to create tables with css alone.

What&#8217;s the difference between html tables and css tables? Should we use css tables? [...]]]></description>
			<content:encoded><![CDATA[<p>Mention css and tables in the same sentence and controversy is sure to follow. Web designers like myself have been telling you <a href="http://www.vanseodesign.com/css/css-divs-vs-tables/">not to use html tables for layouts</a> and now here we have a way to create tables with css alone.<br />
<span id="more-3428"></span><br />
What&#8217;s the difference between html tables and css tables? Should we use css tables? If so how?</p>
<p>Once again I want to thank Pedro for emailing me the idea to talk about css tables. I hope I cover what you&#8217;re interested in knowing.</p>
<p>Let&#8217;s get to the how of css tables first and then tackle the question of whether or not you&#8217;d want to use them in practice.</p>
<p><a href="http://www.flickr.com/photos/59810064@N07/5646039032/"><img src="http://www.vanseodesign.com/blog/wp-content/uploads/2011/10/css-table.jpg" alt="Glass top coffee table with the letters css on the table legs" width="465" height="235" /></a></p>
<h2>How to Create CSS Tables</h2>
<p>The <a href="http://www.w3.org/TR/CSS2/tables.html">css table model</a> is based on the html4 table model and has pretty good <a href="http://caniuse.com/css-table">browser support</a>. In both table models the table structure parallels the visual display of the table itself.</p>
<p>Rows are primary. The row is specified explicitly and columns are derived from how the rows and cells are set up.</p>
<p>I&#8217;m sure you&#8217;ve worked with html tables before and if you have you shouldn&#8217;t have any problem creating css tables.</p>
<p>Each html table element has an equivalent css display value. The only real difference is that there&#8217;s no distinction between td and th with the css variety.</p>
<p>Below are the html table elements and their corresponding css display value.</p>
<pre class="css">
<span class="cssSelector">table     {</span> display: table <span class="cssSelector">}</span>
<span class="cssSelector">tr        {</span> display: table-row <span class="cssSelector">}</span>
<span class="cssSelector">thead     {</span> display: table-header-group <span class="cssSelector">}</span>
<span class="cssSelector">tbody     {</span> display: table-row-group <span class="cssSelector">}</span>
<span class="cssSelector">tfoot     {</span> display: table-footer-group <span class="cssSelector">}</span>
<span class="cssSelector">col       {</span> display: table-column <span class="cssSelector">}</span>
<span class="cssSelector">colgroup  {</span> display: table-column-group <span class="cssSelector">}</span>
<span class="cssSelector">td, th    {</span> display: table-cell <span class="cssSelector">}</span>
<span class="cssSelector">caption   {</span> display: table-caption <span class="cssSelector">}</span>
</pre>
<p>Captions can be positioned above or below the table with the caption-side property</p>
<pre class="css">
<span class="cssSelector">#caption {</span>caption-side: top<span class="cssSelector">}</span>
<span class="cssSelector">#caption {</span>caption-side: bottom<span class="cssSelector">}</span>
</pre>
<p>Looking at the above it shouldn&#8217;t be too hard to figure out how to set up a css table. Here&#8217;s a simple example.</p>
<pre class="html">
<span class="htmlOtherTag">&lt;div id=<span class="htmlAttributeValue">&quot;table&quot;</span>&gt;</span>
  <span class="htmlOtherTag">&lt;div class=<span class="htmlAttributeValue">&quot;row&quot;</span>&gt;</span>
    <span class="htmlOtherTag">&lt;span class=<span class="htmlAttributeValue">&quot;cell&quot;</span>&gt;</span><span class="htmlOtherTag">&lt;/span&gt;</span>
    <span class="htmlOtherTag">&lt;span class=<span class="htmlAttributeValue">&quot;cell&quot;</span>&gt;</span><span class="htmlOtherTag">&lt;/span&gt;</span>
     <span class="htmlOtherTag">&lt;span class=<span class="htmlAttributeValue">&quot;cell&quot;</span>&gt;</span><span class="htmlOtherTag">&lt;/span&gt;</span>
  <span class="htmlOtherTag">&lt;/div&gt;</span>
  <span class="htmlOtherTag">&lt;div class=<span class="htmlAttributeValue">&quot;row&quot;</span>&gt;</span>
    <span class="htmlOtherTag">&lt;span class=<span class="htmlAttributeValue">&quot;cell&quot;</span>&gt;</span><span class="htmlOtherTag">&lt;/span&gt;</span>
    <span class="htmlOtherTag">&lt;span class=<span class="htmlAttributeValue">&quot;cell&quot;</span>&gt;</span><span class="htmlOtherTag">&lt;/span&gt;</span>
     <span class="htmlOtherTag">&lt;span class=<span class="htmlAttributeValue">&quot;cell&quot;</span>&gt;</span><span class="htmlOtherTag">&lt;/span&gt;</span>
  <span class="htmlOtherTag">&lt;/div&gt;</span>
  <span class="htmlOtherTag">&lt;div class=<span class="htmlAttributeValue">&quot;row&quot;</span>&gt;</span>
    <span class="htmlOtherTag">&lt;span class=<span class="htmlAttributeValue">&quot;cell&quot;</span>&gt;</span><span class="htmlOtherTag">&lt;/span&gt;</span>
    <span class="htmlOtherTag">&lt;span class=<span class="htmlAttributeValue">&quot;cell&quot;</span>&gt;</span><span class="htmlOtherTag">&lt;/span&gt;</span>
     <span class="htmlOtherTag">&lt;span class=<span class="htmlAttributeValue">&quot;cell&quot;</span>&gt;</span><span class="htmlOtherTag">&lt;/span&gt;</span>
  <span class="htmlOtherTag">&lt;/div&gt;</span>
<span class="htmlOtherTag">&lt;/div&gt;</span>
</pre>
<pre class="css">
<span class="cssSelector">#table {</span><span class="cssProperty">display</span><span class="cssRest">:</span><span class="cssValue"> table</span><span class="cssRest">;</span><span class="cssSelector">}</span>
<span class="cssSelector">.row {</span><span class="cssProperty">display</span><span class="cssRest">:</span><span class="cssValue"> table-row</span><span class="cssRest">;</span><span class="cssSelector">}</span>
<span class="cssSelector">.cell {</span><span class="cssProperty">display</span><span class="cssRest">:</span><span class="cssValue"> table-cell</span><span class="cssRest">;</span><span class="cssSelector">}</span>
</pre>
<p>If you look only at the html above you can easily see the basic table structure except that I&#8217;ve used div and span with ids and classes instead of table, tr, and td.</p>
<p>A relatively small amount of css then presents the divs and spans as the familiar table, table row, and table cell.</p>
<p>In addition to the above the css table model includes an inline-table value, which defines a new table the same as display: table, but does so according to the inline formatting context.</p>
<p><a href="http://www.flickr.com/photos/1967chevrolet/4272868759/"><img src="http://www.vanseodesign.com/blog/wp-content/uploads/2010/08/columns.jpg" alt="Columns: Temple of a Thousand Warriors" width="465" height="294" /></a></p>
<h3>Columns and Column-Groups</h3>
<p>While tables cells are always descendants of table rows it makes sense to set some properties on columns. The css table model allows for the following on columns and column-groups</p>
<ul>
<li><strong>border</strong> &mdash; The usual properties as long as border-collapse has been set to collapse on the table element</li>
<li><strong>background</strong> &mdash; The usual properties as long as both row and cell have background set to transparent</li>
<li><strong>width</strong> &mdash; Sets a max-width on the column</li>
<li><strong>visibility</strong> &mdash; If set to collapse (the only available value) then <a href="http://www.w3.org/Style/Examples/007/folding.en.html">column cells don&#8217;t display</a>, cells spanning into other columns are clipped, and width of the table is adjusted</li>
</ul>
<h3>CSS Table Stacking Context</h3>
<p>Different table elements have different <a href="http://www.vanseodesign.com/css/css-stack-z-index/">stacking contexts</a> for the purpose of adding backgrounds to these different layers.</p>
<p>These layers can be seen in the image below.</p>
<ol>
<li>table &#8211; lowest layer</li>
<li>column group</li>
<li>columns</li>
<li>row group</li>
<li>rows</li>
<li>cells &#8211; highest layer</li>
</ol>
<p>The background of any layer will only be seen if all the layers above it have backgrounds set to transparent.</p>
<p>This can be a nice way to show an empty cell is truly empty by having its background be transparent and letting the background of the row, column, or table show through.</p>
<p><a href="http://www.w3.org/TR/CSS2/tables.html"><img src="http://www.vanseodesign.com/blog/wp-content/uploads/2011/10/tbl-layers.png" alt="Table layers stacking context" width="465" height="362" /></a></p>
<h3>Table Layout Algorithm</h3>
<p>The width of css tables can be <a href="http://www.w3.org/TR/CSS2/tables.html#width-layout">calculated using one of two algorithms</a>. This can be controlled through the table-layout property and the 2 values below.</p>
<ul>
<li><strong>fixed</strong> &mdash; The width of the layout is not determined by its content, but rather by the widths set on the table, cells, borders, and/or cell spacing</li>
<li><strong>auto</strong> &mdash; The width of table is set by width of columns and borders</li>
</ul>
<p>The important thing to consider is that a fixed table-layout is a one-pass calculation and very quick. On the other hand auto requires the same multiple passes of html tables. It&#8217;s also the default value.</p>
<p>If you explicitly set a width on the table then the fixed table layout algorithm will be used.</p>
<p>By default the cell height will be the minimum necessary to display the contents of the cell, but you can also explicitly set heights. All cells in a row will be the height of the cell with the maximum minimum necessary height.</p>
<p>The vertical-align property of each table cell determines the cell&#8217;s alignment within the row</p>
<ul>
<li>baseline</li>
<li>top</li>
<li>bottom</li>
<li>middle</li>
<li>sub, super, text-top, text-bottom, &lt;length&gt;, &lt;percentage&gt;</li>
</ul>
<p>The last group of values do not apply to cells, but the text within the cells. The cell is aligned at the baseline instead.</p>
<h3>CSS Table Borders</h3>
<p>There are 3 interesting properties for table borders</p>
<ul>
<li><strong>border-collapse</strong> &mdash; values can be collapse, separate, or inherit</li>
<li><strong>border-spacing</strong> &mdash; values can be &lt;length&gt; (horizontal), &lt;length&gt; (vertical), or inherit.  border-spacing is the distance between cell borders.</li>
<li><strong>empty-cells</strong> &mdash; values can be show, hide, or inherit. If cells are empty or set to visibility: hidden content doesn&#8217;t show by default. Setting empty-cells: show on the table will cause backgrounds and borders to display for the empty cell.</li>
</ul>
<p><a href="http://www.w3.org/TR/CSS2/tables.html"><img src="http://www.vanseodesign.com/blog/wp-content/uploads/2011/10/tbl-spacing.png" alt="Diagram showing table spacing, borders, and width" width="465" height="320" /></a></p>
<h2>Should You Use CSS Tables?</h2>
<p>Are css tables better than html tables? If so what advantages do they have and if not why should we use them at all? Good questions that I don&#8217;t have great answers for.</p>
<p>I can say I&#8217;ve never used a css table in practice and have no intention of using them any time soon. If a page calls for tabular content it strikes me than an html table is called for and I think we have and will have <a href="http://www.vanseodesign.com/css/elastic-layout-code/">better techniques for site layout</a> than css tables.</p>
<p>I took a look at an older post I wrote on <a href="http://www.vanseodesign.com/css/css-divs-vs-tables/">css vs tables</a> to remind myself of the cons for using html tables for layout over a combination of divs and css.</p>
<ul>
<li><strong>Extra code</strong> &mdash; html tables require more structural code than divs, but css tables use just as much. If anything css tables use more since  ids and classes will likely be added. If html tables use too much code then css tables do too.</li>
<li><strong>Rigid structure</strong> &mdash; html <a href="http://meyerweb.com/eric/thoughts/2009/02/17/wanted-layout-system/">tables are source dependent</a>. The order you structure the cells is the same order in which they&#8217;ll display. The same is essentially true of css tables as well.</li>
<li><strong>Browser rendering</strong> &mdash; browsers require multiple passes at the structure of html tables. They should only require one pass to display a css table if the table-layout is set to fixed. They&#8217;ll require the same multiple passes if set to auto.</li>
</ul>
<p>Considering the above css tables aren&#8217;t offering enough benefit over html tables to use them for layout.</p>
<p>We could reach and suggest that since the css can be easily changed a css table is less rigid than an html table, but in practice I think they&#8217;re going to be just as rigid.</p>
<p>CSS tables do have the <a href="http://www.digital-web.com/articles/everything_you_know_about_CSS_Is_wrong/">advantage of being more semantically correct</a> as we can choose html elements that better describe our content.</p>
<p>Overall it&#8217;s hard for me to see much improvement in css tables over html tables, some perhaps, but not enough to justify to myself using them.</p>
<p>I think some of the other css layout modules on the horizon will be better and even our current practice of <a href="http://www.vanseodesign.com/css/understanding-css-floats/">using floats</a> and <a href="http://www.vanseodesign.com/css/css-positioning/">positioning for layout</a> are still a better option.</p>
<p>At the same time I can&#8217;t say I&#8217;ve worked much with css tables. This post is the most time I&#8217;ve spend with them since they&#8217;ve been introduced and I&#8217;m open to someone else&#8217;s reasons for why we should use them.</p>
<p>I have a hunch they&#8217;ll remain in use to solve some specific problems like <a href="http://www.vanseodesign.com/css/vertical-centering/">vertically centering content</a> or cleverly <a href="http://adactio.com/journal/4780/">switching the display order</a> of different elements in a responsive design.</p>
<p>They may also prove to be a good <a href="http://forabeautifulweb.com/blog/about/are_css_tables_ready_for_prime_time/">pattern for navigation</a>. I don&#8217;t see them being a viable solution to the problem of layouts though.</p>
<p><a href="http://www.flickr.com/photos/alexlomas/6073276846/"><img src="http://www.vanseodesign.com/blog/wp-content/uploads/2011/10/periodic-table.jpg" alt="Infography showing periodic table of OS admin" width="465" height="329" /></a></p>
<h2>Summary</h2>
<p>CSS tables are pretty simple to understand and use. It&#8217;s mostly a matter of remembering the corresponding css display property values for the basic html table elements.</p>
<p>table becomes display: table. td becomes display: table-cell, etc.</p>
<p>There are some nice features of css tables like the ability to collapse one or more columns through the visibility: collapse property and they can be used as solutions to some specific problems.</p>
<p>However they don&#8217;t provide enough advantage for me over html tables when it comes to layout. Their advantages seem minor and a bit of a reach. Ultimately I think we have better layout solutions than both html and css tables.</p>
<p>Again though I&#8217;m open to being convinced otherwise.</p>
<p>Have you used css tables in practice? If so how? Have you used them for site layout? To present tabular data? To solve a specific problem?</p>
<img src="http://www.vanseodesign.com/blog/?ak_action=api_record_view&id=3428&type=feed" alt="" />]]></content:encoded>
			<wfw:commentRss>http://www.vanseodesign.com/css/tables/feed/</wfw:commentRss>
		<slash:comments>32</slash:comments>
		</item>
		<item>
		<title>The CSS Display Property: A Reintroduction To A Familiar Friend</title>
		<link>http://www.vanseodesign.com/css/display-property/</link>
		<comments>http://www.vanseodesign.com/css/display-property/#comments</comments>
		<pubDate>Thu, 06 Oct 2011 12:30:54 +0000</pubDate>
		<dc:creator>Steven Bradley</dc:creator>
				<category><![CDATA[CSS]]></category>

		<guid isPermaLink="false">http://www.vanseodesign.com/?p=3406</guid>
		<description><![CDATA[A few weeks ago I received an email from Pedro Reis asking if I would write a post about the css display property. I thought it would make for an interesting topic as the display property sits at the heart of a lot of what we do in css layouts.

Pedro was specifically interested in the [...]]]></description>
			<content:encoded><![CDATA[<p>A few weeks ago I received an email from Pedro Reis asking if I would write a post about the <a href="http://www.w3.org/TR/css3-box/#the-lsquo">css display property</a>. I thought it would make for an interesting topic as the display property sits at the heart of a lot of what we do in <a href="http://www.vanseodesign.com/css/elastic-layout-code/">css layouts</a>.<br />
<span id="more-3406"></span><br />
Pedro was specifically interested in the table values for display, though, since they deserve a full post I&#8217;ll save the css table talk until next week.</p>
<p>For now I want to look at all the other values for display. There&#8217;s more than you might realize.</p>
<p><a href="http://xaliaz.deviantart.com/art/Put-Yer-Heart-on-Display-112592860"><img src="http://www.vanseodesign.com/blog/wp-content/uploads/2011/09/css-display-2.jpg" alt="CSS displayed inside an old black and white tv" width="465" height="336" /></a></p>
<h2>The CSS Display Property</h2>
<p>You probably don&#8217;t set the <a href="http://www.quirksmode.org/css/display.html">display property</a> all that often and yet you use it all the time.</p>
<p>All elements have a default display and most of the time that default is exactly what you want. In fact when you choose to use a div, it&#8217;s mainly because of it&#8217;s default display value of block.</p>
<p>Let&#8217;s take a look at some of the values display can take. Again I&#8217;ll leave the table values until next week.</p>
<ul>
<li>block</li>
<li>inline</li>
<li>inline-block</li>
<li>list-item</li>
<li>none</li>
<li>inherit</li>
</ul>
<p>There are a few other values, which we&#8217;ll get to later in this post, but above are the basics.</p>
<p>I&#8217;m sure they&#8217;re all familiar to you and yet they deserve a little more explorations, particularly in the differences between block and inline boxes.</p>
<p><a href="http://www.w3.org/TR/CSS2/box.html"><img src="http://www.vanseodesign.com/blog/wp-content/uploads/2011/09/box-model-21.png" alt="CSS box model" width="465" height="340" /></a></p>
<h3>Block and Inline Formatting Contexts</h3>
<p><strong>Block level elements</strong> are laid out according to the box model, where each block has a width and height, as well as vertical and horizontal padding, border, and margin. <a href="http://www.w3.org/TR/CSS2/visuren.html#block-formatting">Blocks are displayed vertically</a> one after the other, with the distance between them depending on the margins set.</p>
<p>Vertical margins collapse. If one box has a margin-bottom of 30px and the other has a margin-top of 20px, there isn&#8217;t 50px of space between them. The margins collapse to the larger value so there would only be 30px of space between the blocks.</p>
<p><strong>Inline elements</strong> are <a href="http://www.w3.org/TR/CSS2/visuren.html#inline-formatting">displayed horizontally</a> and don&#8217;t follow the box model. Horizontally their padding and margin is respected, but not so vertically.  The heights of inline boxes are set according to the rules of <a href="http://www.w3.org/TR/CSS2/visudet.html#line-height">line-height calculations</a>. For the most part that will mean the height of the containing block.</p>
<p>The are 2 key differences between block and inline boxes.</p>
<ol>
<li>Block elements are laid out vertically while inline elements are laid out horizontally</li>
<li>Block elements form a new box according to the <a href="http://www.instantshift.com/2009/11/16/css-box-model-the-foundation-for-improving-your-css/">box model</a>, while inline elements don&#8217;t</li>
</ol>
<p><strong>Inline-block elements</strong> are a combination of the two. They act like inline boxes on the outside, being laid out horizontally, but they&#8217;re block level boxes on the inside. They do form a new box and have vertical paddings and margins.</p>
<p><strong>List-items</strong> behave like block level elements with the addition of an extra box (the marker box) used to place a marker. Ordered and unordered lists are one containing block level box with several block and market box combinations inside.</p>
<p>When the <a href="http://www.vanseodesign.com/css/visibility-vs-display/">value of display is set to none</a>, no box of any kind is created. The element has effectively been removed from the document flow and other elements behave as though it doesn&#8217;t exist.</p>
<p>Inherit naturally means to use the same value as set on the parent element.</p>
<p>I realize these are simple concepts, but they do sit at the foundation of so many other things where css layouts are concerned.</p>
<p>For example floating a block level element changes it from being laid out vertically to horizontally. <a href="http://www.vanseodesign.com/css/css-positioning/">Positioning</a> the same box has other elements treating it as though its display was set to none, while the box treats itself as the same block level element it&#8217;s always been.</p>
<p><a href="http://www.flickr.com/photos/adactio/154271679/"><img src="http://www.vanseodesign.com/blog/wp-content/uploads/2011/09/cssmastery.jpg" alt="CSS Mastery book cover on shelf with other css books"  width="465" height="349" /></a></p>
<h2>Other Display Values</h2>
<p>I mentioned above there are more values for display. Some are seldom used and others while not used much now, likely will in the future.</p>
<ul>
<li>run-in</li>
<li>flexbox</li>
<li>grid</li>
<li>templates (display: abc) &mdash; there&#8217;s no value templates, rather the template module allows for a variety of different values</li>
<li>ruby</li>
</ul>
<p>I&#8217;ve <a href="http://www.vanseodesign.com/css/flexbox/">covered flexbox previously</a> and will point you there for details. I&#8217;d like to cover both grids and templates in future posts so I&#8217;ll save more details for those posts. I hope you don&#8217;t mind.</p>
<p>Today I&#8217;ll briefly touch on run-in and ruby.</p>
<h3>Run-in Boxes</h3>
<p>The <a href="http://www.w3.org/TR/css3-box/#run-in-boxes">W3C gives the following rules for run-in boxes</a>.</p>
<ul>
<li>If the run-in box contains a block box, the run-in box becomes a block box.</li>
<li>If a sibling block box (that does not float and is not absolutely positioned) follows the run-in box, the run-in box becomes the first inline box of the block box. A run-in cannot run in to a block that already starts with a run-in or that itself is a run-in.</li>
<li>Otherwise, the run-in box becomes a block box.</li>
</ul>
<p>Chris Coyier does a better job of <a href="http://css-tricks.com/596-run-in/">explaining why you&#8217;d actually use display: run-in</a>.</p>
<p>It&#8217;s essentially a way to get page headings to sit inline with the text that follows them. <a href="http://www.vanseodesign.com/css/understanding-css-floats/">Floating</a> the headings or setting them to display: inline won&#8217;t work.</p>
<p>Browser support is growing for run-in, but it&#8217;s still not what you&#8217;d call great.</p>
<p><a href="http://www.w3.org/TR/css3-ruby/"><img src="http://www.vanseodesign.com/blog/wp-content/uploads/2011/09/display-ruby.png" alt="A css ruby box showing ruby text and ruby base" width="465" height="190" /></a></p>
<h3>The CSS Ruby Model</h3>
<p><a href="http://www.w3.org/TR/css3-ruby/">Ruby</a> is the commonly used name for a run of text that appears in the immediate vicinity of another run of text. One of these runs of text is considered the base and the other the text. The base serves as an annotation and pronunciation guide for the text.</p>
<p>If after reading the above you said &#8220;huh?&#8221; to yourself then you did what I did when reading about ruby in the spec.</p>
<p>It mainly applies to East Asian languages and East Asian typography and I won&#8217;t pretend to know much about either.</p>
<p>An element set to display as ruby has it&#8217;s own ruby box model and there are actually several ruby display properties.</p>
<ul>
<li>ruby</li>
<li>ruby-base</li>
<li>ruby-text</li>
<li>ruby-base-container</li>
<li>ruby-text-container</li>
</ul>
<p>I&#8217;m going to guess most of you won&#8217;t encounter display: ruby beyond this post and instead of trying to present more details I&#8217;ll just point you again to the <a href="http://www.w3.org/TR/css3-ruby/">W3C ruby spec</a> if you&#8217;re interested in knowing more.</p>
<p><a href="http://djeric.deviantart.com/art/free-psd-apple-display-123035073"><img src="http://www.vanseodesign.com/blog/wp-content/uploads/2011/09/css-display.jpg" alt="CSS displayed inside a computer monitor" width="465" height="322" /></a></p>
<h2>Summary</h2>
<p>Every element behaves according to one of the <a href="http://www.w3.org/TR/CSS2/visuren.html#display-prop">display property</a> values. Most elements will either display as either block or inline. List items naturally default to list-item. Odds are you don&#8217;t set the display property all that often and generally fine with the defaults.</p>
<p>I realize this post is probably rather basic for many of you. Hopefully it helped clear up something about the difference between block and inline boxes or introduced you to a new display value you&#8217;d like to explore.</p>
<p>Next week I&#8217;ll take a look at the display values we didn&#8217;t talk about today, namely those values that are associated with the css table module.</p>
<img src="http://www.vanseodesign.com/blog/?ak_action=api_record_view&id=3406&type=feed" alt="" />]]></content:encoded>
			<wfw:commentRss>http://www.vanseodesign.com/css/display-property/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>CSS Selectors: Should You Optimize Them To Perform Better?</title>
		<link>http://www.vanseodesign.com/css/css-selector-performance/</link>
		<comments>http://www.vanseodesign.com/css/css-selector-performance/#comments</comments>
		<pubDate>Mon, 03 Oct 2011 12:30:09 +0000</pubDate>
		<dc:creator>Steven Bradley</dc:creator>
				<category><![CDATA[CSS]]></category>

		<guid isPermaLink="false">http://www.vanseodesign.com/?p=3399</guid>
		<description><![CDATA[For the last few weeks I&#8217;ve been looking at different css selector patterns. One thing I haven&#8217;t mention is the efficiency of the selectors, something Chad commented about on the very first post in the series.

I had originally intended on adding a link or two about selector efficiency in one of the posts, but completely [...]]]></description>
			<content:encoded><![CDATA[<p>For the last few weeks I&#8217;ve been looking at different css selector patterns. One thing I haven&#8217;t mention is the efficiency of the selectors, something <a href="http://www.vanseodesign.com/css/attribute-selectors/#comment-139161">Chad commented</a> about on the very first post in the series.<br />
<span id="more-3399"></span><br />
I had originally intended on adding a link or two about selector efficiency in one of the posts, but completely forgot. Instead I&#8217;ll offer this post, along with those links I had originally intended on including.</p>
<p>Here are the 3 css selector posts in case you missed any.</p>
<ul>
<li><a href="http://www.vanseodesign.com/css/attribute-selectors/">Do You Use These 7 Attribute Selectors In Your CSS?</a></li>
<li><a href="http://www.vanseodesign.com/css/combinators-pseudo-classes/">How To Use CSS Combinators and Simple Pseudo Class Selectors</a></li>
<li><a href="http://www.vanseodesign.com/css/pseudo-elements-classes/">How To Use Structural Pseudo Classes and Pseudo Element Selectors</a></li>
</ul>
<p>One thing I should say at the outset is that for most of us what follows probably isn&#8217;t going to significantly affect anything on our sites. I&#8217;ll talk more about this later in the post, but please know you aren&#8217;t likely to see large savings in performance from optimizing your selectors.</p>
<p><a href="http://www.flickr.com/photos/werkman/5586120601/"><img src="http://www.vanseodesign.com/blog/wp-content/uploads/2011/09/reading.jpg" alt="Man reading a book while standing neck deep in water" width="465" height="310" /></a></p>
<h2>How Do Browsers Read Selectors?</h2>
<p>This entire discussion is based on how browsers read selectors in order to identify which html gets styled and the key is that <a href="http://stackoverflow.com/questions/5797014/css-selectors-parsed-right-to-left-why">browsers will read your selector from right to left</a>.</p>
<pre class="css">
div.nav &gt; ul li a[title]
</pre>
<p>A browser seeing the above selector will first try to match a[title] in the html, and then proceed to the left matching li, ul, and finally div.nav.</p>
<p>This last part of the selector (in this case a[title]) is called the &#8220;key selector&#8221; and it&#8217;s ultimately what will determine how efficient your selector will be.</p>
<p>The sooner browsers can filter out a mismatch, the less they have to check and, the more efficient the selector. David Hyatt writing about <a href="https://developer.mozilla.org/en/Writing_Efficient_CSS">selector performance</a> for Mozilla tells us:</p>
<blockquote><p>
This is the key to dramatically increasing performance. The fewer rules required to check for a given element, the faster style resolution will be.
</p></blockquote>
<p>Since a selector that fails is <a href="http://www.vanseodesign.com/wordpress/efficient-web-design/">more efficient</a> than if the same selector matches we want to optimize key selectors to fail. The more specific the key selector, the quicker the browser find mismatches.</p>
<p>So which type of selector is most and least efficient?</p>
<p><a href="http://www.flickr.com/photos/44313045@N08/6101415124/"><img src="http://www.vanseodesign.com/blog/wp-content/uploads/2011/09/performance.jpg" alt="Sustainability graphic on Performance.gov, earth with 2 arrows circling" width="465" height="280" /></a></p>
<h2>Selector Efficiency</h2>
<p>Below is the <a href="http://csswizardry.com/2011/09/writing-efficient-css-selectors/">order of efficiency for selectors</a>. IDs are the most efficient and pseudo classes and pseudo elements are the least efficient.</p>
<ol>
<li>id (#myid)</li>
<li>class (.myclass)</li>
<li>tag (div, h1, p)</li>
<li>adjacent sibling (h1 + p)</li>
<li>child (ul > li)</li>
<li>descendent (li a)</li>
<li>universal (*)</li>
<li>attribute (a[rel="external"])</li>
<li>pseudo-class and pseudo element (a:hover, li:first)</li>
</ol>
<p>Looking at the above list we&#8217;d prefer our key selector to be an id or class than a child or descendent selector. CSS3 selectors like pseudo-classes and attributes are very useful, but also the most inefficient.</p>
<pre class="css">
div #myid
</pre>
<p>will be more efficient than</p>
<pre class="css">
#myid div
</pre>
<p>because the key selector of the first is more efficient than the key selector of the second.</p>
<p>Adding tags in front of ids and classes also slows thing down and makes the selector less efficient so</p>
<pre class="css">
#myid
.myclass
</pre>
<p>are more efficient than</p>
<pre class="css">
p#myid
p.myclass
</pre>
<p><a href="http://www.yourdigitalspace.com/2011/04/60-beautiful-firefox-wallpapers-for-your-desktop/"><img src="http://www.vanseodesign.com/blog/wp-content/uploads/2011/09/mozilla-firefox-logo.jpg" alt="Mozilla Firefox logo superimposed over image of a space nebula" width="465" height="291" /></a></p>
<h3>Guidelines from Mozilla</h3>
<p>In David&#8217;s article he offered some <a href="http://code.google.com/speed/page-speed/docs/rendering.html">guidelines for writing efficient css selectors</a>, which I&#8217;ll present below.</p>
<ul>
<li>Avoid Universal Rules</li>
<li>Don’t qualify ID Rules with tag names or classes</li>
<li>Don’t qualify Class Rules with tag names</li>
<li>Use the most specific category possible</li>
<li>Avoid the descendant selector</li>
<li>Tag Category rules should never contain a child selector</li>
<li>Question all usages of the child selector</li>
<li>Rely on inheritance</li>
<li>Use scoped stylesheets</li>
</ul>
<p>You can <a href="https://developer.mozilla.org/en/Writing_Efficient_CSS">read his original article</a> for more details. It&#8217;s a quick read. It is also over 10 years old. Not that its age invalidates any of the information. Browsers still match selectors from right to left after all.</p>
<p>However a few details like saying descendant selectors are the least efficient should be updated, as there now exist new and less efficient selectors that didn&#8217;t exist when the article was written.</p>
<p><a href="http://www.flickr.com/photos/hiddenloop/4541195635/"><img src="http://www.vanseodesign.com/blog/wp-content/uploads/2011/09/question-mark-4.jpg" alt="Line drawing of a robot with a question mark in a thought bubble" width="465" height="365" /></a></p>
<h2>Does it Matter?</h2>
<p>This is all well and good, but does any of it matter? Should you rewrite existing stylesheets to make your selectors more efficient? If ids are the most efficient selector should you start adding ids to every html element in your document?</p>
<p>Probably not. For one there&#8217;s a balance between <a href="http://www.vanseodesign.com/web-design/semantic-html/">writing semantic code</a> and writing code for improved performance. There are advantages to both semantics and performance and we shouldn&#8217;t choose one entirely over the other.</p>
<p>Second selector efficiency and inefficiency probably isn&#8217;t going to impact most sites all that much.</p>
<p>We&#8217;re not talking a difference of several seconds from most to least efficient. We&#8217;re talking about milliseconds in the worst case. For the majority of sites you aren&#8217;t going to notice a difference and the effort likely outweighs the savings.</p>
<p>That said a large site that generates a lot of traffic probably will see a difference and it might very well be worth the effort to optimize selectors.</p>
<p>It also makes sense to understand how this works and be more efficient in general For example knowing that</p>
<pre class="css">
#myid
</pre>
<p>is more efficient than</p>
<pre class="css">
p#myid
</pre>
<p>why would you add the tag in front?</p>
<p>I wouldn&#8217;t make my html less <a href="http://www.vanseodesign.com/web-design/html5-semantic-elements/">semantic</a> or clutter it with extra code to save a few milliseconds, but I would change</p>
<pre class="css">
ul#nav li a
</pre>
<p>to</p>
<pre class="css">
#nav a
</pre>
<p>as a matter of practice, since the second is faster and requires no change to the html.</p>
<p>Are you going to notice the difference on most of the sites you develop? Again not likely, but still why wouldn&#8217;t you choose to be more efficient where you can?</p>
<p>I wouldn&#8217;t take this information and start recoding live sites to make them more efficient, but why not take a few minutes to learn which selector patterns perform better and use the more efficient patterns when you have the choice to use several.</p>
<p><a href="http://www.flickr.com/photos/sidelong/246816211/"><img src="http://www.vanseodesign.com/blog/wp-content/uploads/2011/09/test.jpg" alt="Boxes with the word test across them" width="465" height="372" /></a></p>
<h3>Testing Your Selectors</h3>
<p>Steve Souders created a page where you can test different selectors to compare their efficiency. I&#8217;ve played with it a little ad the results agree with what I&#8217;ve posted above.</p>
<ul>
<li><a href="http://stevesouders.com/efws/css-selectors/csscreate.php">CSS Test Creator </a></li>
</ul>
<p>I don&#8217;t know how accurate the test results are, but it&#8217;s worth playing around to get a feel for how quickly some selectors are in comparison to others.</p>
<p>Jon Sykes ran some tests of his own a few years ago mainly to look at the effect of descendent selectors on performance.</p>
<ul>
<li><a href="http://blog.archive.jpsykes.com/151/testing-css-performance/">Testing CSS Performance</a></li>
<li><a href="http://blog.archive.jpsykes.com/152/testing-css-performance-pt-2/">Testing CSS Performance (pt 2)</a></li>
<li><a href="http://blog.archive.jpsykes.com/153/more-css-performance-testing-pt-3/">More CSS Performance Testing (pt 3)</a></li>
</ul>
<p>His conclusion at the end of the series:</p>
<blockquote><p>
be aware that descender and child selectors can affect performance, but don’t worry about them so much as to not use them.
</p></blockquote>
<p>After seeing the tests above Steve Souders <a href="http://www.stevesouders.com/blog/2009/03/10/performance-impact-of-css-selectors/">ran his own test</a> coming to the conclusion that the gains from optimizing selectors are probably not worth the cost for most sites.</p>
<p>In a later post he received some feedback about web pages where inefficient selectors did make a noticeable difference and ultimately recommends <a href="http://www.stevesouders.com/blog/2009/06/18/simplifying-css-selectors/">optimizing the key selector</a> of css selectors.</p>
<p><a href="http://www.flickr.com/photos/adactio/2318808494/"><img src="http://www.vanseodesign.com/blog/wp-content/uploads/2011/09/css-selector-api1.jpg" alt="Presentation slide talking about the css selector api" width="465" height="349" /></a></p>
<h2>Summary</h2>
<p>There&#8217;s definitely a performance hit in using some selector patterns over others, but how significant is that performance hit? The answer will depend on the specific site and selectors.</p>
<p>For most sites <a href="http://css-tricks.com/6386-efficiently-rendering-css/">it probably won&#8217;t make much of a difference</a>. You shouldn&#8217;t be concerned that the selectors you used developing your last site now need to be revisited and optimized.</p>
<p>However you may want to consider selector efficiency when developing your next site. At the very least it makes sense to understand which patterns are more and less efficient.</p>
<p>Every little bit helps so why not be more efficient where you can be and if you happen to be working on that large trafficked site those little bits can add up to significant savings.</p>
<p>Have you spent any time optimizing your css selectors? Have you noticed any difference in page load times and was it worth the effort?</p>
<img src="http://www.vanseodesign.com/blog/?ak_action=api_record_view&id=3399&type=feed" alt="" />]]></content:encoded>
			<wfw:commentRss>http://www.vanseodesign.com/css/css-selector-performance/feed/</wfw:commentRss>
		<slash:comments>15</slash:comments>
		</item>
		<item>
		<title>How To Use Structural Pseudo Classes and Pseudo Element Selectors</title>
		<link>http://www.vanseodesign.com/css/pseudo-elements-classes/</link>
		<comments>http://www.vanseodesign.com/css/pseudo-elements-classes/#comments</comments>
		<pubDate>Mon, 26 Sep 2011 12:30:38 +0000</pubDate>
		<dc:creator>Steven Bradley</dc:creator>
				<category><![CDATA[CSS]]></category>

		<guid isPermaLink="false">http://www.vanseodesign.com/?p=3328</guid>
		<description><![CDATA[The last couple of weeks we&#8217;ve been looking at different types of css selectors. First we looked at the simple and attribute selectors and then we looked at combinators and some pseudo class selectors.

This week we&#8217;ll finish up with structural pseudo-classes and pseudo elements. I think you&#8217;ll find both have some practical applications for styling [...]]]></description>
			<content:encoded><![CDATA[<p>The last couple of weeks we&#8217;ve been looking at different types of css selectors. First we looked at the <a href="http://www.vanseodesign.com/css/attribute-selectors/">simple and attribute selectors</a> and then we looked at <a href="http://www.vanseodesign.com/css/combinators-pseudo-classes/">combinators and some pseudo class selectors</a>.<br />
<span id="more-3328"></span><br />
This week we&#8217;ll finish up with structural pseudo-classes and pseudo elements. I think you&#8217;ll find both have some practical applications for styling web pages.</p>
<p><strong>Note:</strong> Once again a reminder that the numbers in parenthesis represent <a href="http://www.w3.org/TR/css3-selectors/#selectors">which level css the selector was introduced</a> in.</p>
<p><a href="http://sebastienthuan.deviantart.com/art/Structure-67560099"><img src="http://www.vanseodesign.com/blog/wp-content/uploads/2011/09/structure.jpg" alt="Structure, Saint Jean Cap Ferrat, France" width="465" height="465" /></a></p>
<h2>Structural Pseudo-Classes</h2>
<p>Structural pseudo-classes were introduced as a way to target <a href="http://www.vanseodesign.com/web-design/html5-semantic-elements/">html elements</a> based on information in the document tree that aren&#8217;t easily represented by simple selectors or combinators. Most were introduced in css3.</p>
<p>You may already be using a few of these. If you aren&#8217;t you likely find yourself wanting to target the elements they select. Without them we generally have to add a class or id to the element we&#8217;re targeting.</p>
<p><strong>E:root</strong> (3) &mdash; matches an element E that&#8217;s the root of the document. In html this is always the html element.</p>
<pre class="css">
<span class="cssSelector">:root {</span><span class="cssProperty">background</span><span class="cssRest">:</span><span class="cssValue"> blue</span><span class="cssRest">;</span><span class="cssSelector">}</span>
<span class="cssSelector">html {</span><span class="cssProperty">background</span><span class="cssRest">:</span><span class="cssValue"> blue</span><span class="cssRest">;</span><span class="cssSelector">}</span>
</pre>
<p>The lines of css above are functionally equivalent. Perhaps not the most useful structural pseudo-class, but the easiest to explain.</p>
<p><strong>E:nth-child(n)</strong> (3) &mdash; matches an element E that&#8217;s the nth child of its parent.</p>
<p>Assuming we have a <a href="http://www.vanseodesign.com/css/css-divs-vs-tables/">table</a> with a large number of rows.</p>
<pre class="css">
<span class="cssProperty">tr</span><span class="cssRest">:</span><span class="cssValue">nth-child(4) {background: #ccc</span><span class="cssRest">;</span>}
</pre>
<p>The css above says to find the 4th row and give it a background color that&#8217;s a light gray.</p>
<p>A common use of the <a href="http://reference.sitepoint.com/css/understandingnthchildexpressions">nth-child pseudo-class</a> is coloring alternate rows of a table.</p>
<pre class="css">
<span class="cssProperty">tr</span><span class="cssRest">:</span><span class="cssValue">nth-child(2n+1) {background: #ccc</span><span class="cssRest">;</span>}
<span class="cssProperty">tr</span><span class="cssRest">:</span><span class="cssValue">nth-child(2n) {background: #eee</span><span class="cssRest">;</span>}
</pre>
<p>2n+1 represents odd numbered rows and 2n represents even numbered rows. Special odd and even values can also be used.</p>
<pre class="css">
<span class="cssProperty">tr</span><span class="cssRest">:</span><span class="cssValue">nth-child(odd) {background: #ccc</span><span class="cssRest">;</span>}
<span class="cssProperty">tr</span><span class="cssRest">:</span><span class="cssValue">nth-child(even) {background: #eee</span><span class="cssRest">;</span>}
</pre>
<p>You aren&#8217;t limited to 2 though. Any number can be used.</p>
<pre class="css">
<span class="cssProperty">tr</span><span class="cssRest">:</span><span class="cssValue">nth-child(10n+1) {background: #ccc</span><span class="cssRest">;</span>}
</pre>
<p>Here the 11th, 21st, 31st, etc rows would be given a light gray background.</p>
<p><strong>E:nth-last-child(n)</strong> (3) &mdash; matches an element E that&#8217;s the nth child of its parent, counting from the last child.</p>
<p>nth-last child works similarly to nth-child, except that the count starts from the last element in the list as opposed to the first. Again assuming a table with a large number of rows.</p>
<pre class="css">
<span class="cssProperty">tr</span><span class="cssRest">:</span><span class="cssValue">nth-last-child(1) {background: #ccc</span><span class="cssRest">;</span>}
</pre>
<p>Here the last row is the one to get the <a href="http://www.vanseodesign.com/css/css-background-property/">background color</a>.</p>
<pre class="css">
<span class="cssProperty">tr</span><span class="cssRest">:</span><span class="cssValue">nth-last-child(4) {background: #ccc</span><span class="cssRest">;</span>}
</pre>
<p>Here the 4th last row gets the background color.</p>
<p>All the same values can be used</p>
<pre class="css">
<span class="cssProperty">tr</span><span class="cssRest">:</span><span class="cssValue">nth-last-child(2n+1) {background: #ccc</span><span class="cssRest">;</span>}
<span class="cssProperty">tr</span><span class="cssRest">:</span><span class="cssValue">nth-last-child(2n) {background: #eee</span><span class="cssRest">;</span>}
</pre>
<pre class="css">
<span class="cssProperty">tr</span><span class="cssRest">:</span><span class="cssValue">nth-last-child(odd) {background: #ccc</span><span class="cssRest">;</span>}
<span class="cssProperty">tr</span><span class="cssRest">:</span><span class="cssValue">nth-last-child(even) {background: #eee</span><span class="cssRest">;</span>}
</pre>
<p>Both of the above would result in all odd and even rows getting different background colors. While you probably wouldn&#8217;t use nth-last-child for these, you could.</p>
<p>More typical use of nth-last-child is to target a specific or few specific items at the end of the list.</p>
<p><a href="http://gwendo0.deviantart.com/art/Numbers-PNG-x22-244112685"><img src="http://www.vanseodesign.com/blog/wp-content/uploads/2011/09/numbers-4.jpg" alt="Numbers" width="465" height="314" /></a></p>
<h3>:nth-of-type and :nth-last-of-type</h3>
<p>The rest of the structural <a href="http://css-tricks.com/5762-pseudo-class-selectors/">pseudo-classes</a> are plays on the above two so if you understand how the above two work all you&#8217;ll need to understand the rest is how they differ.</p>
<p><strong>E:nth-of-type(n)</strong> (3) &mdash; matches an element E that&#8217;s the nth sibling of its type</p>
<p><strong>E:nth-last-of-type(n)</strong> (3) &mdash; matches an element E that&#8217;s the nth sibling of its type, counting from the last one </p>
<p>nth-of-type() and nth-last-of-type() work the same way nth-child() and nth-last-child() work, the difference being that what we&#8217;re targeting has to be a specific type of element as opposed to any child element.</p>
<p>You&#8217;d use the of-type classes when the parent of the element you&#8217;re targeting has different types of elements as children.</p>
<pre class="html">
<span class="htmlOtherTag">&lt;div&gt;</span>
 <span class="htmlOtherTag">&lt;p&gt;</span><span class="htmlOtherTag">&lt;/p&gt;</span>
<span class="htmlOtherTag">&lt; img src=<span class="htmlAttributeValue">&quot;&quot;</span> alt=<span class="htmlAttributeValue">&quot;&quot;</span> / &gt;</span>
 <span class="htmlOtherTag">&lt;p&gt;</span><span class="htmlOtherTag">&lt;/p&gt;</span>
  <span class="htmlOtherTag">&lt;ul&gt;</span>
    <span class="htmlOtherTag">&lt;li&gt;</span><span class="htmlOtherTag">&lt;/li&gt;</span>
    <span class="htmlOtherTag">&lt;li&gt;</span><span class="htmlOtherTag">&lt;/li&gt;</span>
  <span class="htmlOtherTag">&lt;/ul&gt;</span>
 <span class="htmlOtherTag">&lt;p&gt;</span><span class="htmlOtherTag">&lt;/p&gt;</span>
<span class="htmlOtherTag">&lt;/div&gt;</span>
</pre>
<pre class="css">
<span class="cssProperty">p</span><span class="cssRest">:</span><span class="cssValue">nth-of-type(2) {font-size: 1.2em</span><span class="cssRest">;</span>}
<span class="cssProperty">p</span><span class="cssRest">:</span><span class="cssValue">nth-last-of-type(1) {font-size: 1.2em</span><span class="cssRest">;</span>}
</pre>
<p>I&#8217;m sure you can figure out which paragraphs are targeted by the css above Again we&#8217;re using the of-type pseudo-classes because the elements inside the div are of mixed type.</p>
<h3>:first-child and :last-child</h3>
<p>These two pseudo-classes are shortcuts for nth-child(1) and nth-last-child(1). Like the two nth classes these require all the children to be of the same type.</p>
<p><strong>E:first-child</strong> (2) &mdash; matches an element E that&#8217;s the first child of its parent</p>
<p><strong>E:last-child</strong> (3) &mdash; matches an element E that&#8217;s the last child of its parent</p>
<p>A common use is in <a href="http://www.vanseodesign.com/css/simple-navigation-bar-with-css-and-xhtml/">navigation bars</a> when you want to style either the first, last or both menu items in the list differently than those in the middle of the bar.</p>
<p>Often I&#8217;ll add a border between menu items in navigation bars by setting either a left or right border. However that necessitates either adding a border to one end or removing it from the other. li:last-child and li:first-child come in handy for this.</p>
<h3>:first-of-type and :last-of-type</h3>
<p>These two elements are the functional equivalent of nth-of-type(1) and nth-last-of-type(1). They work the same as first-child and last-child except that they only target an element of a specific type.</p>
<p><strong>E:first-of-type</strong> (3) &mdash; matches  an element E that&#8217;s first sibling of its type.</p>
<p><strong>E:last-of-type</strong> (3) &mdash; matches  an element E that&#8217;s last sibling of its type.</p>
<p>Styling the first of last paragraph inside a div, article, or section might be a common use for the first and last of-type classes.</p>
<h3>only-child and only-of-type</h3>
<p>These next two elements are again similar except that they only target elements with a single child.</p>
<p><strong>E:only-child</strong> (3) &mdash; matches an element E that&#8217;s the only child of its parent.</p>
<p><strong>E:only-of-type</strong> (3) &mdash; matches an element E that&#8217;s the only sibling of its type.</p>
<p>With only-child you&#8217;d have only the one element inside the parent. With only-of-type you could have multiple children, but only the one type of element you&#8217;re targeting.</p>
<h3>:empty</h3>
<p><strong>E:empty</strong> (3) &mdash; matches an element E that has no children (including text nodes)</p>
<p>:empty is a way to target empty tags. For example it wouldn&#8217;t match</p>
<pre class="html">
<span class="htmlOtherTag">&lt;p&gt;</span>Some text<span class="htmlOtherTag">&lt;/p&gt;</span>
</pre>
<p>It would match</p>
<pre class="html">
<span class="htmlOtherTag">&lt;p&gt;</span><span class="htmlOtherTag">&lt;/p&gt;</span>
</pre>
<p>Ideally you won&#8217;t leave any empty tags in your html so I&#8217;m not sure how useful this one is. Perhaps as a way to find and remove any empty tags you&#8217;ve accidentally left behind.</p>
<p><iframe width="465" height="262" src="http://www.youtube.com/embed/Konn2NMmSXw" frameborder="0" allowfullscreen></iframe></p>
<h2>Pseudo Elements</h2>
<p>There are some things we want to style in practice, but have no way at all to select based on information in the document tree.</p>
<p>Normally to select these items we&#8217;d have to add an element like a span and also give it some kind of class. <a href="http://css-tricks.com/9516-pseudo-element-roundup/">Pseudo elements</a> are a way to target these items.</p>
<p>We have 4 pseudo elements we can target</p>
<h3>::first-line pseudo element</h3>
<p><strong>E::first-line</strong> (1) &mdash; matches the first formatted line of an element E.</p>
<pre class="css">
<span class="cssProperty">p</span><span class="cssRest">:</span><span class="cssValue">first-line {text-transform: uppercase</span><span class="cssRest">;</span>}
</pre>
<p>The above changes the first line in all paragraphs to uppercase. Of course the <a href="http://www.vanseodesign.com/web-design/legible-readable-typography/">first line of text</a> can be somewhat dynamic if you&#8217;ve created a flexible layout and the browser width is varied.</p>
<h3>::first-letter pseudo element</h3>
<p><strong>E::first-letter</strong> (1) &mdash; matches the first formatted letter of an element E.</p>
<p>Similar to first-line,  first-letter styles only the first letter differently. An obvious use is to create drop caps.</p>
<pre class="html">
<span class="htmlOtherTag">&lt;p class=<span class="htmlAttributeValue">&quot;intro&quot;</span>&gt;</span>This is the first paragraph<span class="htmlOtherTag">&lt;/p&gt;</span>
</pre>
<pre class="css">
p.<span class="cssProperty">intro</span><span class="cssRest">:</span><span class="cssValue">:first-letter {font-size:4em</span><span class="cssRest">;</span> <span class="cssProperty">font-weight</span><span class="cssRest">:</span><span class="cssValue">bold</span><span class="cssRest">;</span> <span class="cssProperty">color</span><span class="cssRest">:</span><span class="cssValue"> #f00</span><span class="cssRest">;</span> <span class="cssProperty">float</span><span class="cssRest">:</span><span class="cssValue">left</span><span class="cssRest">;</span><span class="cssMedia">}</span>
</pre>
<h3>::before and ::after pseudo elements</h3>
<p><strong>E::before</strong> (2) &mdash; matches generated content before an element E.</p>
<p><strong>E::after</strong> (2) &mdash; matches generated content after an element E.</p>
<p>The ::before and ::after pseudo elements are ways to add content before or after an element. Common uses are to add a quote image before and after blockquotes or to use your own special bullet in front of a list.</p>
<pre class="html">
<span class="htmlOtherTag">&lt;blockquote&gt;</span><span class="htmlOtherTag">&lt;/blockquote&gt;</span>
</pre>
<pre class="css">
<span class="cssProperty">blockquote</span><span class="cssRest">:</span><span class="cssValue">:before {content: <span class="cssString">"&#038;lsquo</span><span class="cssRest">;</span>"</span><span class="cssMedia">}</span>
<span class="cssProperty">blockquote</span><span class="cssRest">:</span><span class="cssValue">:after {content: <span class="cssString">"&#038;rsquo</span><span class="cssRest">;</span>"</span><span class="cssMedia">}</span>
</pre>
<p>The above adds a single left quotation mark before the blockquote and a single right quotation mark after it. </p>
<p>While there will be times when you don&#8217;t specifically want to add any content you still need to include the content property with ::before and ::after. Leaving it blank is allowed, but it must be included.</p>
<pre class="css">
<span class="cssProperty">blockquote</span><span class="cssRest">:</span><span class="cssValue">:before {content: <span class="cssString">""</span></span><span class="cssRest">;</span>}
</pre>
<p>You can do quite a lot with these two pseudo elements as seen in the video above or this demo for <a href="http://nicolasgallagher.com/css-pseudo-element-solar-system/">building a css solar system</a>.</p>
<h3>Double or Single Colon?</h3>
<p>One note about all 4 <a href="http://www.suburban-glory.com/blog?page=127">pseudo elements</a> is their use of a double colon out front.</p>
<p>This is technically the correct syntax however browsers that accept pseudo elements also allow a single colon and the double colon doesn&#8217;t work in all browsers.</p>
<p>For practical purposes you&#8217;re better off using the single colon for now so:</p>
<ul>
<li>:first-letter</li>
<li>:last-letter</li>
<li>:before</li>
<li>:after</li>
</ul>
<h2>Browser Support</h2>
<p>Generally browser support for pseudo elements is better than the structural pseudo classes, though it varies some. The elements will work as far back as IE8 and with first-letter and first-line support goes back to IE5.5. Yep, you read that right.</p>
<p>Most of the structural pseudo classes don&#8217;t start working until IE9, though support for first-child goes back to IE6.</p>
<ul>
<li><a href="http://www.quirksmode.org/css/contents.html">CSS contents and browser compatibility</a></li>
<li><a href="http://dev.l-c-n.com/CSS3-selectors/browser-support.php#pE">pseudo elements support</a></li>
<li><a href="http://tools.css3.info/selectors-test/test.html">CSS3 Selectors Test</a></li>
</ul>
<p><a href="http://www.flickr.com/photos/geirarne/110995239/"><a href="</a>&#8220;><img src="http://www.vanseodesign.com/blog/wp-content/uploads/2011/09/css.png" alt="CSS written in blue on a black background" width="465" height="349" /></a></p>
<h2>Summary</h2>
<p>Like the other selectors we&#8217;ve covered you may have already used some of these in practice, while others may be completely new to you.</p>
<p>You&#8217;ll find more support for the pseudo elements than the structural pseudo classes, but you can likely find practical uses for most everything mentioned here.</p>
<p>I hope this walk through of <a href="http://www.456bereastreet.com/archive/200601/css_3_selectors_explained/">css selectors</a> has been useful and brought to light a few selectors you may not use or be familiar with. I know that&#8217;s been the case for me and hopefully these posts will serve as a reminder for both of our development practice.</p>
<img src="http://www.vanseodesign.com/blog/?ak_action=api_record_view&id=3328&type=feed" alt="" />]]></content:encoded>
			<wfw:commentRss>http://www.vanseodesign.com/css/pseudo-elements-classes/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
	</channel>
</rss>

