<?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>Think Vitamin &#187; Brian Suda</title>
	<atom:link href="http://thinkvitamin.com/author/brian-suda/feed/" rel="self" type="application/rss+xml" />
	<link>http://thinkvitamin.com</link>
	<description>The Web Practitioner&#039;s Blog</description>
	<lastBuildDate>Thu, 09 Feb 2012 16:41:31 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	
		<item>
		<title>Getting Started With (and Gotchas of) CSS Media Queries</title>
		<link>http://thinkvitamin.com/design/getting-started-and-gotchas-of-css-media-queries/</link>
		<comments>http://thinkvitamin.com/design/getting-started-and-gotchas-of-css-media-queries/#comments</comments>
		<pubDate>Fri, 27 Aug 2010 14:00:32 +0000</pubDate>
		<dc:creator>Brian Suda</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[CSS3]]></category>
		<category><![CDATA[Design]]></category>

		<guid isPermaLink="false">http://thinkvitamin.com/?p=7120</guid>
		<description><![CDATA[For all the nay sayers that think the iPad is just a big iPhone, you couldn&#8217;t be further from the truth. Now, I won&#8217;t go as far as to say it is &#8220;Magical&#8221;, but with several million sold in the first month, it isn&#8217;t a fad. With it&#8217;s wider screen and larger keyboard, there are [...]]]></description>
			<content:encoded><![CDATA[<p>For all the nay sayers that think the iPad is just a big iPhone, you couldn&#8217;t be further from the truth. Now, I won&#8217;t go as far as to say it is &#8220;Magical&#8221;, but with several million sold in the first month, it isn&#8217;t a fad. </p>
<p>With it&#8217;s wider screen and larger keyboard, there are several aspects you need to rethink as you are designing you web applications for this device. In this article I am going to walk you through how to adapt your web app to look and feel more like a native application that makes use of CSS media queries to change the layout when the device is rotated between horizontal and portrait modes.<span id="more-7120"></span> </p>
<p>The beauty of this progressive enhancement methodology is that you don&#8217;t need multiple versions of your code for desktop or mobile devices.</p>
<p><img class="alignnone" src="http://thinkvitamin.com/wp-content/uploads/2010/08/mail_rotator_l_20100225.jpg" alt="" width="368" height="277" /></p>
<p>If we look at a common design pattern for applications, we tend to see some sort of left-hand navigation with a large main area to display the content. This is the way the iPad&#8217;s mail.app works. </p>
<p>The left-hand navigation is a list of email messages, which you can then select to see the full message in the main window. It also isn&#8217;t chance that this left-hand navigation is the same width as the iPhone screen to re-use many of the same widgets and design elements. </p>
<p>When the iPad is rotated into portrait mode, the screen width becomes too skinny to display a two column layout.</p>
<p><img class="alignnone" src="http://thinkvitamin.com/wp-content/uploads/2010/08/mail_rotator_p_20100225.jpg" alt="" width="278" height="370" /></p>
<p>Therefore, the left-hand menu is hidden away as a button which toggles the menu as an overlay. I am going to walk you through how to do all of this using CSS media queries. It&#8217;s much easier than you would expect.</p>
<h3>Setting up the HTML</h3>
<p>Firstly, we need to set-up a template for our content will flow into. We&#8217;ll start with the standard two column layout and progressively enhance it to handle different screen widths.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #339933;">&lt;</span>div id<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;nav&quot;</span> <span style="color: #000000; font-weight: bold;">class</span><span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;saveSpace&quot;</span><span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;</span>ul<span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;</span>li<span style="color: #339933;">&gt;</span>Navigation Here<span style="color: #339933;">&lt;/</span>li<span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;/</span>ul<span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;/</span>div<span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;</span>div id<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;main&quot;</span><span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;</span>h1<span style="color: #339933;">&gt;</span>Content Here<span style="color: #339933;">&lt;/</span>h1<span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;/</span>div<span style="color: #339933;">&gt;</span></pre></td></tr></table></div>

<h3>Setting up the Basic CSS</h3>
<p>With a bit of CSS we position these two items and give them specific widths and padding.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
</pre></td><td class="code"><pre class="css" style="font-family:monospace;"><span style="color: #cc00cc;">#nav</span> <span style="color: #00AA00;">&#123;</span>
<span style="color: #000000; font-weight: bold;">width</span><span style="color: #00AA00;">:</span> <span style="color: #933;">200px</span><span style="color: #00AA00;">;</span>
<span style="color: #000000; font-weight: bold;">height</span><span style="color: #00AA00;">:</span> <span style="color: #933;">100%</span><span style="color: #00AA00;">;</span>
<span style="color: #000000; font-weight: bold;">border-right</span><span style="color: #00AA00;">:</span> <span style="color: #933;">1px</span> <span style="color: #993333;">solid</span> <span style="color: #cc00cc;">#ccc</span><span style="color: #00AA00;">;</span>
<span style="color: #000000; font-weight: bold;">position</span><span style="color: #00AA00;">:</span> <span style="color: #993333;">fixed</span><span style="color: #00AA00;">;</span> <span style="color: #000000; font-weight: bold;">top</span><span style="color: #00AA00;">:</span> <span style="color: #cc66cc;">0</span><span style="color: #00AA00;">;</span>
<span style="color: #000000; font-weight: bold;">left</span><span style="color: #00AA00;">:</span> <span style="color: #cc66cc;">0</span><span style="color: #00AA00;">;</span>
<span style="color: #000000; font-weight: bold;">z-index</span><span style="color: #00AA00;">:</span> <span style="color: #cc66cc;">1000</span><span style="color: #00AA00;">;</span>
<span style="color: #00AA00;">&#125;</span>
&nbsp;
<span style="color: #cc00cc;">#main</span> <span style="color: #00AA00;">&#123;</span>
<span style="color: #000000; font-weight: bold;">margin-left</span><span style="color: #00AA00;">:</span> <span style="color: #933;">200px</span><span style="color: #00AA00;">;</span>
<span style="color: #000000; font-weight: bold;">padding</span><span style="color: #00AA00;">:</span> <span style="color: #cc66cc;">0</span><span style="color: #00AA00;">;</span>
<span style="color: #000000; font-weight: bold;">height</span><span style="color: #00AA00;">:</span> <span style="color: #933;">100%</span><span style="color: #00AA00;">;</span>
<span style="color: #00AA00;">&#125;</span>
&nbsp;
<span style="color: #6666ff;">.saveSpace</span> <span style="color: #00AA00;">&#123;</span>
<span style="color: #000000; font-weight: bold;">display</span><span style="color: #00AA00;">:</span> <span style="color: #993333;">block</span><span style="color: #00AA00;">;</span>
<span style="color: #00AA00;">&#125;</span></pre></td></tr></table></div>

<p>This mimics a two column layout, but we only want it to display like this when the iPad is in portrait mode. Using CSS media queries, this is very simple. </p>
<p>There are a few key words, such as &#8220;max-width&#8221;, &#8220;min-width&#8221; to create rules based on width, but there are also rules for orientation and dpi. For more information, Ethan Marcotte wrote an excellent article on A List Apart called <a href="http://www.alistapart.com/articles/responsive-web-design/">Responsive Web Design</a>. </p>
<p>For our example, we are going to put the threshold at 800 pixels. Anything larger than 800px gets the two column view and anything smaller will see only the content area with a hidden menu, just like mail.app.</p>
<h3>Adding in Media Queries</h3>
<p>Our previous CSS has the basic layout for the two column mode, now we need to add in some additional CSS which over-rides some of the previous styles during certain situations of size. At the bottom of your CSS file, you can added the following:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
</pre></td><td class="code"><pre class="css" style="font-family:monospace;"><span style="color: #a1a100;">@media all and (max-width: 800px) {</span>
<span style="color: #cc00cc;">#nav</span> <span style="color: #00AA00;">&#123;</span>
<span style="color: #000000; font-weight: bold;">border</span><span style="color: #00AA00;">:</span> <span style="color: #933;">5px</span> <span style="color: #993333;">solid</span> <span style="color: #cc00cc;">#131e31</span><span style="color: #00AA00;">;</span>
<span style="color: #000000; font-weight: bold;">position</span><span style="color: #00AA00;">:</span> <span style="color: #993333;">fixed</span><span style="color: #00AA00;">;</span>
<span style="color: #000000; font-weight: bold;">top</span><span style="color: #00AA00;">:</span> <span style="color: #933;">75px</span><span style="color: #00AA00;">;</span>
<span style="color: #000000; font-weight: bold;">left</span><span style="color: #00AA00;">:</span> <span style="color: #933;">10px</span><span style="color: #00AA00;">;</span>
<span style="color: #000000; font-weight: bold;">height</span><span style="color: #00AA00;">:</span> <span style="color: #993333;">auto</span><span style="color: #00AA00;">;</span>
<span style="color: #00AA00;">&#125;</span>
<span style="color: #cc00cc;">#main</span> <span style="color: #00AA00;">&#123;</span>
<span style="color: #000000; font-weight: bold;">margin-left</span><span style="color: #00AA00;">:</span> <span style="color: #933;">0px</span><span style="color: #00AA00;">;</span>
<span style="color: #00AA00;">&#125;</span>
<span style="color: #6666ff;">.saveSpace</span> <span style="color: #00AA00;">&#123;</span>
<span style="color: #000000; font-weight: bold;">display</span><span style="color: #00AA00;">:</span> <span style="color: #993333;">none</span><span style="color: #00AA00;">;</span>
<span style="color: #00AA00;">&#125;</span>
<span style="color: #00AA00;">&#125;</span></pre></td></tr></table></div>

<p>It is important that this is at the bottom so that it takes precedence over the other rules declared above. This saves us from having to wrap everything above in a CSS query to explicitly say only apply this when min-width is 800.</p>
<p>With the<tt>#main</tt>, we have removed the left padding and therefore it no longer appears as a two column design. The <tt>#nav</tt> has been moved slightly away from the edge to make it appear more like a floating menu and finally, we are adding a style for <tt>.saveSpace</tt> of <tt>display: none</tt>. This will initially hide the menu until the JavaScript adds or removed that class to view the contents.</p>
<p>If you put this into your HTML and resize your browser above and below 800 pixels you will see that the design shifts from a one column to a two column design and back. This is a great tool for websites, not just web apps attempting to emulate native design. </p>
<p>Several websites have use this technique to make 3-4 column layouts at wide screen width and adjusting the number of columns depending if the screen is skinnier. <a href="http://hicksdesign.co.uk/">Hicksdesign</a>  is an excellent example of reformatting the layout based on design.</p>
<h3>Adding in the jQuery</h3>
<p>In this example, the next thing we need to layer on is the ability to show and hide the left-hand menu. We can do this with a button that toggles the menu through a tiny line of jQuery. To do this we need to change our HTML inside <tt>#main</tt> to account for a menu button.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #339933;">&lt;</span>div id<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;banner&quot;</span><span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;</span>a id<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;menuButton&quot;</span> <span style="color: #000000; font-weight: bold;">class</span><span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;button&quot;</span> href<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;#&quot;</span><span style="color: #339933;">&gt;</span>Menu<span style="color: #339933;">&lt;/</span>a<span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;</span>h1<span style="color: #339933;">&gt;</span>Menu Title<span style="color: #339933;">&lt;/</span>h1<span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;/</span>div<span style="color: #339933;">&gt;</span></pre></td></tr></table></div>

<p>This gives is the app banner and a menu button, but we need to add the corresponding CSS to hide some of this when it is not needed.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
</pre></td><td class="code"><pre class="css" style="font-family:monospace;"><span style="color: #cc00cc;">#banner</span> <span style="color: #00AA00;">&#123;</span>
<span style="color: #000000; font-weight: bold;">border-bottom</span><span style="color: #00AA00;">:</span><span style="color: #933;">1px</span> <span style="color: #993333;">solid</span> <span style="color: #cc00cc;">#797f90</span><span style="color: #00AA00;">;</span>
<span style="color: #000000; font-weight: bold;">border-top</span><span style="color: #00AA00;">:</span><span style="color: #933;">1px</span> <span style="color: #993333;">solid</span> <span style="color: #cc00cc;">#fff</span><span style="color: #00AA00;">;</span>
<span style="color: #000000; font-weight: bold;">height</span><span style="color: #00AA00;">:</span><span style="color: #933;">42px</span><span style="color: #00AA00;">;</span> <span style="color: #000000; font-weight: bold;">text-align</span><span style="color: #00AA00;">:</span> <span style="color: #993333;">center</span><span style="color: #00AA00;">;</span>
<span style="color: #00AA00;">&#125;</span>
&nbsp;
<span style="color: #cc00cc;">#banner</span> h1 <span style="color: #00AA00;">&#123;</span>
<span style="color: #000000; font-weight: bold;">margin</span><span style="color: #00AA00;">:</span> <span style="color: #cc66cc;">0</span> <span style="color: #993333;">auto</span><span style="color: #00AA00;">;</span>
<span style="color: #000000; font-weight: bold;">padding</span><span style="color: #00AA00;">:</span><span style="color: #cc66cc;">0</span><span style="color: #00AA00;">;</span>
<span style="color: #000000; font-weight: bold;">color</span><span style="color: #00AA00;">:</span><span style="color: #cc00cc;">#000</span><span style="color: #00AA00;">;</span>
<span style="color: #000000; font-weight: bold;">font-size</span><span style="color: #00AA00;">:</span><span style="color: #933;">20px</span><span style="color: #00AA00;">;</span>
<span style="color: #000000; font-weight: bold;">line-height</span><span style="color: #00AA00;">:</span> <span style="color: #933;">20px</span><span style="color: #00AA00;">;</span>
<span style="color: #000000; font-weight: bold;">padding-top</span><span style="color: #00AA00;">:</span> <span style="color: #933;">8px</span><span style="color: #00AA00;">;</span>
<span style="color: #000000; font-weight: bold;">font-weight</span><span style="color: #00AA00;">:</span><span style="color: #993333;">bold</span><span style="color: #00AA00;">;</span>
<span style="color: #000000; font-weight: bold;">height</span><span style="color: #00AA00;">:</span><span style="color: #933;">32px</span><span style="color: #00AA00;">;</span>
<span style="color: #000000; font-weight: bold;">white-space</span><span style="color: #00AA00;">:</span><span style="color: #993333;">nowrap</span><span style="color: #00AA00;">;</span>
text-<span style="color: #000000; font-weight: bold;">overflow</span><span style="color: #00AA00;">:</span> ellipsis<span style="color: #00AA00;">;</span>
<span style="color: #000000; font-weight: bold;">overflow</span><span style="color: #00AA00;">:</span> <span style="color: #993333;">hidden</span><span style="color: #00AA00;">;</span>
<span style="color: #000000; font-weight: bold;">width</span><span style="color: #00AA00;">:</span> <span style="color: #933;">55%</span><span style="color: #00AA00;">;</span>
<span style="color: #00AA00;">&#125;</span>
&nbsp;
<span style="color: #cc00cc;">#menuButton</span> <span style="color: #00AA00;">&#123;</span>
<span style="color: #000000; font-weight: bold;">position</span><span style="color: #00AA00;">:</span> <span style="color: #993333;">absolute</span><span style="color: #00AA00;">;</span>
<span style="color: #000000; font-weight: bold;">top</span><span style="color: #00AA00;">:</span> <span style="color: #933;">8px</span><span style="color: #00AA00;">;</span>
<span style="color: #000000; font-weight: bold;">left</span><span style="color: #00AA00;">:</span> <span style="color: #933;">62px</span><span style="color: #00AA00;">;</span>
<span style="color: #000000; font-weight: bold;">display</span><span style="color: #00AA00;">:</span> <span style="color: #993333;">none</span><span style="color: #00AA00;">;</span>
<span style="color: #000000; font-weight: bold;">right</span><span style="color: #00AA00;">:</span> <span style="color: #993333;">auto</span><span style="color: #00AA00;">;</span>
<span style="color: #00AA00;">&#125;</span></pre></td></tr></table></div>

<p>Finally, inside our CSS media query we need to change the <tt>#menuButton</tt> so it is visible.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
</pre></td><td class="code"><pre class="css" style="font-family:monospace;"><span style="color: #cc00cc;">#menuButton</span> <span style="color: #00AA00;">&#123;</span>
<span style="color: #000000; font-weight: bold;">display</span><span style="color: #00AA00;">:</span> <span style="color: #993333;">inline</span><span style="color: #00AA00;">;</span>
<span style="color: #00AA00;">&#125;</span></pre></td></tr></table></div>

<p>After including jQuery,</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #339933;">&lt;</span>script src<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;http://ajax.googleapis.com/ajax/libs/jquery/1.4/jquery.min.js&quot;</span> type<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;text/javascript&quot;</span><span style="color: #339933;">&gt;&lt;/</span>script<span style="color: #339933;">&gt;</span></pre></td></tr></table></div>

<p>We can add the following to the bottom of the HTML file.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
</pre></td><td class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #339933;">&lt;</span>script type<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;text/javascript&quot;</span><span style="color: #339933;">&gt;</span> 
  $<span style="color: #009900;">&#40;</span>document<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">ready</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;#menuButton&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">toggle</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span> 
         $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;#nav&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">removeClass</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;saveSpace&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> 
      <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
         $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;#nav&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">addClass</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;saveSpace&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> 
    <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
   <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #339933;">&lt;/</span>script<span style="color: #339933;">&gt;</span></pre></td></tr></table></div>

<h3>View the Demo</h3>
<p>You can view a <a href="http://thinkvitamin.com/wp-content/uploads/2010/08/basic-demo.html">very basic working demo here</a>. If you resize your screen you can see the left hand navigation change depending on the width. Notice that the content also re-adjusts to the presence of the left-hand navigation.</p>
<p>When the page is loaded, jQuery adds some behaviour to the <tt>#menuButton</tt>. The toggle function runs the first function the first time it is clicked and the second function the second time it is clicked. </p>
<p>On the third click, it runs the first function again and so on, toggling between the two. This is an easy way to create a show/hide button for our navigation. Rather than trying to show and hide the menu by directly adding <tt>display: none</tt> or <tt>block</tt> we add or remove a class called <tt>saveSpace</tt>. </p>
<p>This isn&#8217;t immediately obvious why, but because we are using CSS media queries to change our layout and the CSS is not re-applied.  </p>
<p>If we had a hidden the <tt>#nav</tt> while in the skinnier mode and re-adjusted the width to make the screen wider than 800 pixels, the <tt>#nav</tt> would be hidden, even if we explicitly said in the CSS <tt>#nav {display: block;}</tt>. By adding and removing the class we get around this issue.</p>
<p><a href='http://thinkvitamin.com/wp-content/uploads/2010/08/demo1.html'>View Final Demo</a></p>
<h3>Issues with Media Queries</h3>
<p>Has anyone noticed the issue yet? In this example we are using two different technologies to layer behaviour into our HTML. CSS Media Queries are run based on something the user does. They resize the browser and the design changes. </p>
<p>Previously, this was done with lots of JavaScript running checking window dimension and then applying different styles accordingly. Luckily, CSS media queries relieve us and the browser from all this hard work. But in this example, we are using JavaScript to show and hide the menu at skinny screen resolutions. </p>
<p>We need a way to progressively enhance this situation for browsers with JavaScript disabled. The problem is that instead of having a 1&#215;2 matrix, JavaScript versus No-JavaScript, we now have a 2&#215;2 matrix, JavaScript and CSS media queries, JavaScript and No-CSS media queries, No-JavaScript and CSS media queries and No-JavaScript and No-CSS media queries.</p>
<p>In this example, if the JavaScript is disabled, then we need to make the <tt>#menuButton</tt> a real link that takes you to the same page, but the server would need to update the CSS when it spat out the page so the menu was toggled from the previous state. </p>
<p>This would solve the no-JavaScript with CSS media queries issue. If your browser does not support CSS media queries, then this is less of a problem because the left-hand menu would never be hidden.</p>
<h3>JavaScript to the Rescue?</h3>
<p>There have been efforts using JavaScript to bring media queries to older browsers. By including an additional JS file (<a href="http://code.google.com/p/css3-mediaqueries-js/">http://code.google.com/p/css3-mediaqueries-js/</a>) it is possible to make sure your design behaves in a similar fashion across all your customers&#8217; browsers. </p>
<p>The downside is that this requires JavaScript, but it does eliminate one of the 4 possibilities reported above, JavaScript/No-Media-Queries.</p>
<p>Mixing behaviour between two different technologies can cause more issues that need to be handled as browsers catch-up to supporting CSS Media Queries. This means more work on your part as a developer, but it produces a better experience for the customer.</p>
]]></content:encoded>
			<wfw:commentRss>http://thinkvitamin.com/design/getting-started-and-gotchas-of-css-media-queries/feed/</wfw:commentRss>
		<slash:comments>16</slash:comments>
		</item>
		<item>
		<title>Do It Yourself eBooks</title>
		<link>http://thinkvitamin.com/business/do-it-yourself-ebooks/</link>
		<comments>http://thinkvitamin.com/business/do-it-yourself-ebooks/#comments</comments>
		<pubDate>Tue, 15 Jun 2010 13:00:08 +0000</pubDate>
		<dc:creator>Brian Suda</dc:creator>
				<category><![CDATA[Business]]></category>

		<guid isPermaLink="false">http://carsonified.com/?p=6200</guid>
		<description><![CDATA[In this article, I&#8217;ll introduce you to the exciting world of eBooks, specifically the ePub format in regards to Apple&#8217;s new iBookstore, but much of the same information applies to other devices and online stores. With all this hype surrounding the iPad, Kindle and other eReaders the world of electronic publishing is now more accessible [...]]]></description>
			<content:encoded><![CDATA[<p>In this article, I&#8217;ll introduce you to the exciting world of eBooks, specifically the <a href="http://en.wikipedia.org/wiki/EPUB">ePub format</a> in regards to Apple&#8217;s new <a href="http://www.apple.com/ipad/features/ibooks.html">iBookstore</a>, but much of the same information applies to other devices and online stores.</p>
<p>With all this hype surrounding the <a href="http://www.apple.com/ipad/">iPad</a>, <a href="http://www.amazon.com/dp/B0015T963C/?tag=gocous-20&amp;hvadid=4139607947&amp;ref=pd_sl_7caym1p0w_e">Kindle</a> and other eReaders the world of electronic publishing is now more accessible than ever. With the new iOS 4.0, the iBooks application is available for the iPhone and iPod touch, further increasing the reach of your eBooks.</p>
<p>Digitimes is estimating <a href="http://www.digitimes.com/news/a20100426VL204.html">sales of eReaders in 2010 to be greater than 11 million units</a>. To put that in some sort of perspective, there was an estimated 36 million netbooks sold in 2009. While these eReaders won&#8217;t be overtaking desktops or laptops any time soon, it does represent a very niche opening in the market. The types of people who are buying eReaders are the types of people who like to read and more importantly, buy books. With 11 million+ customers it makes sense to directly target these potential customers with your content.<span id="more-6200"></span></p>
<h3>Creating Your own eBook</h3>
<p>If you don&#8217;t know anything about how to create an eBook, that&#8217;s OK, there are several applications for both Windows and the Mac which can help you to quickly get through the process and generation of ePub files from your existing content. You have no excuse not to be exploring the eBooks possibilities.</p>
<p>Under the hood, an ePub file is basically a zipped-up set of XHTML files and a few XML files to describe the table of contents and where to find the XHTML. Joe Clarke wrote an <a href="http://www.alistapart.com/articles/ebookstandards/">excellent article</a> on the topic of &#8220;HTML isn&#8217;t just for the internet&#8221;. So your years of HTML knowledge is transferable to creating ePub files.</p>
<p>If you&#8217;re not that comfortable with HTML, that&#8217;s OK, there are apps which can help. These are just a few examples of WYSIWYG editors. You can type or paste in existing text, do some quick formatting such as bold, underline, italics, headings, etc. Then save it all into the ePub format suitable for publishing.</p>
<ul>
<li><strong>Sigil</strong> &#8211; <a href="http://code.google.com/p/sigil/">http://code.google.com/p/sigil/</a></li>
<li><strong>Calibre</strong> &#8211; <a href="http://calibre-ebook.com/">http://calibre-ebook.com/</a></li>
<li><strong>eCub</strong> &#8211; <a href="http://www.juliansmart.com/ecub">http://www.juliansmart.com/ecub</a></li>
</ul>
<p>These programs are quick and easy ways for you to get started dabbling in eBook creation. Once you dig into the process you&#8217;ll find that there are some obvious limitations with the format. Different eReaders implement different aspects of the ePub format.</p>
<h3>eReaders are Different</h3>
<p>All eReaders work slightly different, some have screens that are only black and white, their screen resolutions vary and physical sizes are all over the map.</p>
<p>This means that you don&#8217;t have absolute control you do in print, infact, you probably have even less control than in HTML. With HTML, an absolutely positioned element <tt>left: 0</tt> and <tt>top: 0</tt> is definitely in the corner of the browser window. This isn&#8217;t always the case with eReaders because of the application chrome.</p>
<h3>Platforms</h3>
<p>We can look at the same book, Alice in Wonderland, available on several different eBook readers and dedicated applications.</p>
<p>The <a href="http://www.classicsapp.com/">Classics App</a> is a dedicated application with a fixed list of books available. You cannot add more eBooks into it, but as a stand-alone application has a very minimal user interface.</p>
<p style="text-align: center;"><img class="aligncenter" title="classicsapp" src="http://carsonified.com/wp-content/uploads/2010/06/classicsapp.gif" alt="" width="235" height="334" /></p>
<p>Free Books is another dedicated application which has a list of free books that you can download. The UI is pretty sparse and can only be read in portrait mode. There is a large list of books to choose from, but you can&#8217;t add any of your own.</p>
<p style="text-align: center;"><img class="aligncenter" style="border: 1px solid #999;" title="freebooks" src="http://carsonified.com/wp-content/uploads/2010/06/freebooks.gif" alt="" width="470" height="614" /></p>
<p><a href="http://www.lexcycle.com/">Stanza app</a> allows you to import your own eBooks and purchase them from many popular online stores. This is a screen shot of the iPhone version, but there is also an iPad version. As you can see, there is no UI chrome during the reading experience, you just tap on different thirds of the screen to turn the pages or activate the menu.</p>
<p style="text-align: center;"><img class="aligncenter" title="stanza" src="http://carsonified.com/wp-content/uploads/2010/06/stanza.gif" alt="" width="235" height="332" /></p>
<p>Apple&#8217;s iBooks application allows for both portrait and landscape reading. You can add your own eBooks via iTunes, but it has the faux book chrome which cannot be removed.</p>
<p style="text-align: center;"><img class="aligncenter" style="border: 1px solid #999;" title="ibooks" src="http://carsonified.com/wp-content/uploads/2010/06/ibooks.gif" alt="" width="470" height="343" /></p>
<p>Amazon&#8217;s kindle application can import some formats, but not the standard ePub format. For this screenshot, the chrome is exposed. During reading, most of the UI buttons disappear for easier reading. It is also possible to read both landscape and portrait mode.</p>
<p style="text-align: center;"><img class="aligncenter" style="border: 1px solid #999;" title="kindle" src="http://carsonified.com/wp-content/uploads/2010/06/kindle.gif" alt="" width="470" height="343" /></p>
<p>As you can see, the variety of styles, colours, fonts and designs used just to display a book is pretty varied. This opens the door to whether you should be creating a dedicated application for your content or sell it as an ePub file.</p>
<h3>Your Content as an App versus an eBook</h3>
<p>There have been several successful printed books that have been turned into eBook applications. The <a href="http://www.atomicantelope.com/alice/">Alice</a> application in Apple&#8217;s app store takes the classic tale of Alice in Wonderland and adds a layer of interactivity.</p>
<p>It has been called &#8220;a sort of pop-up book for the 21st century&#8221;, which describes it perfectly because, how many pop-up books do you own? Several of the Dr. Seuss books have also been converted into <a href="http://www.oceanhousemedia.com/products/drseuss/">eBook apps</a>. They are simply replicating the fully illustrated book, page for page in the app. No interactivity, but no eReader app chrome, just the original, classic drawings.</p>
<h3>Pros &amp; Cons</h3>
<p>Developing your eBook as an application has pros and cons. The first major downside is that you are limited to the number of platforms it can run on. If you spend many, many man-hours developing an application like Alice which takes full advantage of all the different sensors, then porting the same book to different devices loses some of its functionality. Not to mention your app can only be sold in the countries that have App stores. And on top of all that are the restrictions and issues with Apple review process.</p>
<p>The obvious up-side of an application is that you have much more control of the experience and can avoid the native eReader chrome. The ePub format is a subset of XHTML and CSS, so the possibilities, while large, are not capable of the same full experience of a native application.</p>
<p>The major advantage of an ePub format eBook is that you don&#8217;t need to learn a new programming language like Cocoa or commit to a single platform. The ePub format works on most of the major eReaders (with the exception of the Kindle). So the same .epub file can be sold across different devices via different sales channels.</p>
<h3>Reflowing Text</h3>
<p>You can also reflow ePub data. Text can easily be increased or decreased and the font changed and the eReader handles the changing layout. This is not possible with an Application by default, nor with other formats such as PDF files. The dynamic rotating and reflowing of text in portrait or landscape mode is native to eReaders.</p>
<p>Ultimately, your goal is to making money from your hard work and content, so you need to balance all the issues and select the option that will best help you achieve your goals.</p>
<h3>How to Sell It &#8211; Two Different Roads</h3>
<p>When it comes to trying to sell your creation, the app route or the book route are two very different roads. With the app route, you need to join the Apple developer program at a set yearly fee or $99USD, choose your price and collect 70% on each sale. This is the only way to get your application sold to Apple customers&#8217; devices. There is no legitimate way to sell your app independent of the Apple App Store.</p>
<p>With the eBook option, there are many more possibilities. Apple has an equivalent store for eBooks. If you are with a major publisher this is handled for you. If you aren&#8217;t, then it&#8217;s more difficult, but not impossible to get into the Apple iBookstore. Publishers such as <a href="http://lulublog.com/2010/04/03/lulu-on-the-ipad/">Lulu</a> can put your eBooks there, but they take a cut.</p>
<p>Depending on your needs and how much effort you want to put towards the project, this might be just fine. It also appears that Apple itself is starting to open-up and allow individuals with a US Tax ID to submit their books directly to the <a href="http://itunes.com/sellyourbooks">iBookstore</a>. This might make it easier for self-publishing efforts to avoid losing a percentage to publishing houses and keep more of their hard earned cash.</p>
<p>Unlike apps which are compiled for a specific platform, you can sell your eBooks via any existing channel and your customers can upload them to their eReaders.</p>
<h3>iBooks &amp; iTunes</h3>
<p>The iBooks application syncs with iTunes. Therefore, you can drop any .epub file in iTunes and it will be imported for reading into iBooks. This means that you can sell the ePub file on USB flash drive, on a CD or even as a digital download.</p>
<p>By connecting with other services such as PayPal, your customers could be buying your eBooks online directly from your own site and you can keep a larger chunk of the sale. You do lose the promotion of being in the Apple iBookstore, but the margins are higher, so it becomes a trade-off.</p>
<p>For a small, community focused book or industry report, selling it online as an .ePub file via your own website might be the best way to make the most money.</p>
<h3>Get Writing</h3>
<p>When the blogging phenomenon exploded, it allowed people to express themselves online in a way that was previously impossible. At no point in history had it been so easy for anyone to speak their mind and to publish to the masses freely without requiring permission, lots of money or a contract from a third party.</p>
<p>We are currently deep in the next extension to this, microblogging. Sites like Twitter allow quick short bursts of content to be deployed to those same masses which consumed blogs and other online content. We are now just beginning to see the next wave.</p>
<p>As the trend is moving from long blog-length content, to shorter SMS-length content, the eBook has an uphill battle for your attention, but at the same time, it has never been easier for an individual to publish quality, book-length content and get paid for it.</p>
<p>All the pieces of the puzzle are falling into place: a quality screen for viewing text, smart devices that allow downloading of additional content, online digital book stores and easy payment systems. Put all of these aspects together and the opportunities to sell eBooks has never been better.</p>
<p>At this point, you know about all the tools that are needed to create your own eBooks and sell them online. You just need a good idea and to start writing. So what are you waiting for?</p>
]]></content:encoded>
			<wfw:commentRss>http://thinkvitamin.com/business/do-it-yourself-ebooks/feed/</wfw:commentRss>
		<slash:comments>11</slash:comments>
		</item>
		<item>
		<title>4 Ways to Create Web-Based Data Visualisations</title>
		<link>http://thinkvitamin.com/design/4-ways-to-create-web-based-data-visualisations/</link>
		<comments>http://thinkvitamin.com/design/4-ways-to-create-web-based-data-visualisations/#comments</comments>
		<pubDate>Tue, 20 Apr 2010 22:19:29 +0000</pubDate>
		<dc:creator>Brian Suda</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[Design]]></category>
		<category><![CDATA[HTML5]]></category>

		<guid isPermaLink="false">http://carsonified.com/?p=5555</guid>
		<description><![CDATA[Editors Note: In this Article Brian Suda explores 4 ways of creating web bases charts and graphs. Example files for the canvas and Flot based charts are available for download. Charts and graphs are an excellent way to dig deeper, tell a story and get to the information behind the numbers. As more and more [...]]]></description>
			<content:encoded><![CDATA[<p><em><strong>Editors Note:</strong> In this Article Brian Suda explores 4 ways of creating web bases charts and graphs. Example files for the canvas and Flot based charts are available for <a href="http://carsonified.com/wp-content/uploads/2010/04/brian-suda-chart-demos.zip">download</a>.</em></p>
<p>Charts and graphs are an excellent way to dig deeper, tell a story and get to the information behind the numbers. As more and more data sets are becoming publicly available, we want to convert this dull tabular data into something more visually appealing. In this article I&#8217;ll walk you through 4 different ways to take a data set and convert it into a chart that&#8217;s suitable for display on a website.</p>
<h3>1: HTML</h3>
<p style="text-align: center;"><img class="size-full wp-image-5554  aligncenter" style="border: 1px solid #999;" src="http://carsonified.com/wp-content/uploads/2010/04/image.png" alt="" width="400" height="200" /></p>
<p>In HTML, the simplest way to display a chart would be to drop it in as an image. This picture could be generated offline in an application like Excel, SPSS, or R and exported as an image.</p>
<p>The downside of this method is that it can&#8217;t easily be updated unless you go back to the original application, adjust the data&#8217;s value, export the graphic, then re-upload it to the server. A better solution would be to create the charts as images dynamically on the website itself.</p>
<p>Besides hiring a programmer to create some custom charting software, there are a few other options available online. <a href="http://code.google.com/apis/charttools/">Google Charts</a> is probably the easiest and most widely known. You get all the benefits of the Google infrastructure, caching, image processing, geographic distribution, etc. all for free.<span id="more-5555"></span></p>
<h3>Getting Started with Google Charts</h3>
<p>To get started, Google has a pretty comprehensive site with examples and <a href="http://code.google.com/apis/chart/">instructions</a>. By passing different attributes in the URL&#8217;s query string, it&#8217;s easy for you to create a static chart with a server-side or client-side script. For most use-cases, creating an image in this way will suffice. It&#8217;s fast, simple and works across browsers with no hassle.</p>
<p>Let&#8217;s see how easy it is to create a quick pie chart of browser statistics. I had a look at Wikipedia&#8217;s Browser Usage Page and went with the W3Counter values for the top 5 major browsers. As you can see it breaks down as follows:</p>
<ol>
<li>IE: 47.9</li>
<li>Firefox: 32.3</li>
<li>Chrome: 7.4</li>
<li>Safari: 5.5</li>
<li>Opera: 2.1</li>
</ol>
<p>Next we need to put these into the query string of a URL that Google Image Charts recognize.</p>
<p><code><a href="http://chart.apis.google.com/chart?cht=p&amp;chs=400x200&amp;chtt=W3Counter%20Browser%20Stats&amp;chd=t:47.9,32.3,7.4,5.5,2.1&amp;chl=IE|Firefox|Chrome|Safari|Opera">http://chart.apis.google.com/chart?cht=p&amp;chs=400x200&amp;chtt=W3Counter%20Browser%20Stats&amp;chd=t:47.9,32.3,7.4,5.5,2.1&amp;chl=IE|Firefox|Chrome|Safari|Opera</a></code></p>
<h3>Chart Parameters</h3>
<p>There are several parameters, we&#8217;ll start from left to right. <tt>cht</tt> is the Chart Type, in this case &#8216;p&#8217; stands for pie chart. Next we have <tt>chs</tt>, which is chart size. In my example I choose 400 pixels wide by 200 pixels tall. There is a maximum size, but it is easy to tweak these numbers as needed for your own site. <tt>chtt</tt> is chart title, which displays the text across the top to let readers know what this is a pie chart of.</p>
<p>Next we move to <tt>chd</tt>, which is chart data. There are several different ways to encode the data, but for this example I am using &#8216;text&#8217;, so we start with &#8216;t:&#8217;. Then it is just a comma separated list of the values. So far, pretty easy!</p>
<p>Finally, we have <tt>chl</tt>, which is chart legend. These are separated by a vertical bar &#8216;|&#8217; and their order needs to correspond to the order of the <tt>chd</tt> values. My first data value was 47.9 which corresponds to the first <tt>chl</tt> value of Internet Explorer.</p>
<p>Now all you need to do is paste that url into your browser and you can see the result. You can even put that right into your own HTML using the <tt>&lt;img alt=&quot;&quot; /&gt;</tt> element. You don&#8217;t need to save them and upload them to your own server. It is possible to link to them off Google&#8217;s server.</p>
<h3>From Pie to Bar Chart</h3>
<p>Now, let&#8217;s see what happens when you change <tt>cht</tt> from &#8216;p&#8217; for pie chart to &#8216;bvs&#8217; to make it a vertical bar chart! You probably want to add <tt>&amp;chbh=a</tt> to the end of the URL, chbh stands for chart bar chart spacing and &#8216;a&#8217; sets it to &#8216;auto&#8217;. Voila, a barchart of the same data. As you can tell, with almost no effort or knowledge of coding, you have made two different types of charts from a boring table of raw data.</p>
<p>The major downside to using images of charts is that your results are locked-up in as an image file rather than something that can be further manipulated by others. No cutting and pasting of the raw data, issues with screen-readers, and it doesn&#8217;t easily allow for further manipulation by the reader.</p>
<h3>2: Flash</h3>
<p style="text-align: left;"><img class="size-full wp-image-5552  aligncenter" style="border: 1px solid#999;" title="flash" src="http://carsonified.com/wp-content/uploads/2010/04/flash.png" alt="" width="470" /></p>
<p>Flash and other plugins such as Java, allow you to create rich experiences in the browser. There are several free and paid applications to help you create charts and graphs which are more dynamic than an image. Again, Google offer many of their static image charts as <a href="http://code.google.com/apis/visualization/interactive_charts.html">interactive Flash</a> versions, along with several new graphs that are only available in Flash.</p>
<p>Flash allows you to create visualizations which contain additional information. For instance, when the reader hovers over a section or clicks on a data point more information can appear. Interactive graphs allow you to drill down deeper into specific parts of a graph. Highlighting just a range of data can zoom in to the graph and view a smaller slice of the data.</p>
<h3>Deeper Interactivity with Flash</h3>
<p>An excellent example of an interactive chart is <a href="http://code.google.com/apis/visualization/documentation/gallery/motionchart.html#Example">Google&#8217;s Motion chart</a>. When you press the play button time progresses and you can see the sales figures versus the expenses of different fruits for different regions. This is something that is not possible with the static image charts. The ability to easily turn off and on different fruits, to add trails over time and to look at several days with of information in a single chart.</p>
<p>Flash used to be the goto technology for creating dynamic and interactive interfaces. You can still create robust, dynamic charts and graphs using Flash, but it&#8217;s becoming more and more problematic. As the next wave of devices don&#8217;t support Flash, no one seems to really miss it. What this means is that you need to consider alternatives to Flash-based content.</p>
<p>What about a pure web standards implementations? Well, there are at least two ways to achieve this, CSS and the new CANVAS element.</p>
<h3>3: CSS</h3>
<p style="text-align: center;"><a href="http://carsonified.com/wp-content/uploads/2010/04/css.png"><img class="aligncenter size-full wp-image-5551" style="border: 1px solid#999;" title="css" src="http://carsonified.com/wp-content/uploads/2010/04/css.png" alt="" width="470" /></a></p>
<p>Many charts, like the barchart, can be thought of as just a list of values and we already have a pretty good way to mark-up lists in HTML. We can repurpose our old friends the <tt>&lt;ol&gt;</tt> and <tt>&lt;ul&gt;</tt> elements. With some CSS positioning and a bit of magic, it&#8217;s possible to make a nice barchart from a list. There are several implementations of pure CSS charts out there that can get you started.</p>
<p>Eric Meyer <a href="http://meyerweb.com/eric/css/edge/bargraph/demo.html">wrote a demo</a> several years ago, but it is still as useful today as when it was written. He also managed to convert a table into a <a href="http://meyerweb.com/eric/thoughts/2008/01/21/structured-timeline">nice timeline</a> using only CSS . This demonstrates the power and flexibility of HTML and CSS in creating charts and timelines.</p>
<p>The beauty of implementing this in CSS is that the data is directly in the HTML. Anyone changing the list values is updating this the chart for free. It&#8217;s also readable without any CSS or image capabilities, search engines can index the data, others can copy and paste out the information and screen-readers can easily access the information.</p>
<h3>CSS Code Generators and Tutorials</h3>
<p>Christian Heilmann has also been experimenting with CSS bar charts. On his website, he created a simple tool that will <a href="http://icant.co.uk/csscharts/">generate the code for you</a>. This is all done in pure CSS, so there is no Javascript libraries or other heavy dependancies in the code, which makes it faster than some of the alternatives.</p>
<p>Another great article about CSS charts and graphs is from Wilson Miner and his A List Apart article called <a href="http://www.alistapart.com/articles/accessibledatavisualization/">&#8220;Accessible Data Visualization with Web Standards</a>&#8220;. He goes ever further and demonstrates &#8220;<a href="http://en.wikipedia.org/wiki/Sparkline">sparklines</a>&#8220;, tiny nuggets of data inline with regular text. It is an excellent tutorial if you aren&#8217;t as comfortable with CSS. It walks you through the whole process really well with plenty of examples.</p>
<p>The downside to CSS charts is that they seem to be limited to bar charts and timelines, things with long lines, vertically or horizontally. Making complex charts and graphs are out of reach of the simple tools that CSS makes available.</p>
<h3>4: Canvas</h3>
<p>The newest and most exciting entry into this foray is the <tt>&lt;canvas&gt;</tt> element. It&#8217;s new in HTML5 and acts as an area in which you can draw 2D and 3D shapes. Now, you might be thinking to yourself, if canvas is some HTML5 thing isn&#8217;t that not going to be ready until 2020?</p>
<p>Well, yes and no. It is part of HTML5, but it is available in most browsers today, and the ones that don&#8217;t directly support it, can be made to support it with an extra javascript file. The <tt>&lt;canvas&gt;</tt> element can be retrofitted to earlier browsers including IE6, which makes it an excellent contender to Flash!</p>
<h3>A Quick Canvas Demo</h3>
<p>A simple example of the power of <tt>&lt;canvas&gt;</tt> is to draw two overlapping shapes with some simple opacity. To do this, we need two things. A <tt>&lt;canvas&gt;</tt> element and a few lines of javascript to draw the figures. In this example we&#8217;ll just create a canvas element that is 200 by 200 with an id of &#8216;figures&#8217;. The bit inside the canvas element will be ignored by browsers that support it, otherwise it will be interpreted as an inline element like a <tt>&lt;span&gt;</tt> for those who don&#8217;t.</p>
<p>The next major part to get the drawing working is a few lines of javascript. If you&#8217;re not comfortable with Javascript, that&#8217;s OK. These are pretty easy to understand, then we&#8217;ll show you a library which whisks all this code away for something much easier!</p>
<p><script src="http://gist.github.com/373125.js?file=gistfile1.txt"></script> First, we need to get the DOM element that corresponds to the canvas. We do this by grabbing it based on the id, &#8216;figures&#8217;. Next, we need to tell it that this canvas we&#8217;ll be drawing on should be only 2D, that the bit about <tt>var context = canvas.getContext('2d');</tt>. Then we need to create a fill style. This is the colour of the shape we are creating. It uses the same rgb syntax as CSS, so you should be familiar with that.</p>
<p>Next, the <tt>fillRect()</tt> is used to create a simple rectangle. It takes four parameters, the X,Y of where to start and a length and a width. In our case we&#8217;re starting at 30 pixels from the left and 30 pixels from the top. Then creating a box 100 pixels wide by 100 pixels tall. If you stopped there you&#8217;d have a nice greenish rectangle, but that&#8217;s not very exciting.</p>
<p>Let&#8217;s add another one to the mix to show off a bit.  Repeating the same steps, we give it a fill colour.  This time we&#8217;ll use the <tt>rgba()</tt> syntax so we can give it an opacity of 50% or 0.5 from solid. We&#8217;ll then start the rectangle at 50,50 and have it the same width. What you see should look something like this:</p>
<p><img class="aligncenter size-full wp-image-5550" style="border: 1px solid #999;" title="canvas-figures" src="http://carsonified.com/wp-content/uploads/2010/04/canvas-figures.png" alt="" width="198" height="198" /></p>
<h3>Flot Makes it Easy</h3>
<p>That&#8217;s the very basics in how to draw 2D shapes in <tt>&lt;canvas&gt;</tt>, but we&#8217;re talking about a charts and graphs replacement for Flash, not Paint! One open source library which does creates beautiful charts and graphs is called <a href="http://code.google.com/p/flot/">Flot</a>. This works on top of jQuery and generates charts that are nearly indistinguishable from Flash, yet work on the iPhone and iPad platforms.</p>
<p>You don&#8217;t need to learn anything new since it&#8217;s using the jQuery and Javascript knowledge you already have, plus there are several tutorials and examples that can help you get started.  The <tt>&lt;canvas&gt;</tt> element opens-up new doors and possibilities for web-based interactive charts and graphs. Pulling either from the local HTML or from a remote AJAX call, Flot can create beautiful charts on the fly with little or no coding needed.  Lets look a quick example:</p>
<p><img class="aligncenter size-full wp-image-5549" style="border: 1px solid #999;" title="basic-flot" src="http://carsonified.com/wp-content/uploads/2010/04/basic-flot.png" alt="" width="470" /></p>
<p>As you can see, we&#8217;ve generated a nice spiky line graph, but how much code did it take? Let&#8217;s have a look. Much like the previous example, we need some container. In this case we&#8217;ve started with a div with an id of &#8216;placeholder&#8217; and a height and width. We didn&#8217;t use the <tt>&lt;canvas&gt;</tt> element here because the Javascript will automatically swap it out if the browser supports it, otherwise it will use some magic and use the <tt>&lt;div&gt;</tt> instead.  <script src="http://gist.github.com/373140.js?file=Flot"></script></p>
<p>Then we have a two lines of Javascript code. The first sets-up all the x,y variables for the line. The second line is the flot jQuery function saying to created, or <tt>$.plot()</tt>, this chart. It takes two variables, the first is the element you want to use as the drawing canvas and the second is the data to be used. In this case the x,y coordinates we created to make a line.</p>
<p>That&#8217;s almost it, I did lie, you need to also remember to add a few lines at the top of your HTML to include jQuery, flot and a special javascript file to make this work in IE, but that&#8217;s just a cut and paste from the example.</p>
<p>This is probably the most basic chart you can create with flot, but it can get much more complex. Their <a href="http://people.iola.dk/olau/flot/examples/">examples page</a> demonstrates multiple lines, stacked bar charts, selectable regions, and much, much more.</p>
<h3>Conclusion</h3>
<p>So next time you are looking to create charts and graphs dynamically online, you should remember the pros and cons of these four techniques and venture outside of your comfort zone and learn something new.</p>
<p>We know CSS has powerful abilities and that  is backwards compatible with some help, so there is no reason you need to spend a large budget for designing a custom plugin or the slow workflow of creating static images when there are web standard solutions available.</p>
]]></content:encoded>
			<wfw:commentRss>http://thinkvitamin.com/design/4-ways-to-create-web-based-data-visualisations/feed/</wfw:commentRss>
		<slash:comments>14</slash:comments>
		</item>
		<item>
		<title>Finding your Flock in Twitter</title>
		<link>http://thinkvitamin.com/code/finding-your-flock-in-twitter/</link>
		<comments>http://thinkvitamin.com/code/finding-your-flock-in-twitter/#comments</comments>
		<pubDate>Thu, 18 Feb 2010 11:00:26 +0000</pubDate>
		<dc:creator>Brian Suda</dc:creator>
				<category><![CDATA[Code]]></category>

		<guid isPermaLink="false">http://carsonified.com/?p=4585</guid>
		<description><![CDATA[Twitter is certainly an interesting beast. It has been the poster child of success stories, saving lives, reporting tragedies, over turning governments and letting thousands of people know where you are going for lunch. There is no denying the impact it has on our scenius. I wondered if there was a way to find out [...]]]></description>
			<content:encoded><![CDATA[<p>Twitter is certainly an interesting beast. It has been the poster child of success stories, saving lives, reporting tragedies, over turning governments and letting thousands of people know where you are going for lunch. There is no denying the impact it has on our scenius.</p>
<p>I wondered if there was a way to find out a bit more about how twitter was being used within the local community and if it was even possible to take a smaller stream from the river of data Twitter produces daily. I chose as my local community Iceland.</p>
<p>I did so for several reasons, it is small-only about 300,000 people total, it has a distinct language so it is easy to find people using the only the text search, and it is where I live so any cool information I learn about, I can actually use it.</p>
<p>Any of the following steps can easily be reproduced for your local area, community or group. You&#8217;ll just need to find the key to unlocking like-minded people.</p>
<h3>RSS not API</h3>
<p>The <a href="http://apiwiki.twitter.com/">Twitter API</a> has plenty of great features, but to accomplish all of this I am using the <a href="http://search.twitter.com">twitter search</a> RSS results. There are plenty of libraries in your favourite language to parse RSS, so I won&#8217;t go into depth about those nuts and bolts. The key to getting quality data is to formulate very specific searches.</p>
<p>In Icelandic, there are several characters and words unique to the language. In your niche group there is jargon which can be used to identify the people in the know, a sort of 21st century Shibboleth.</p>
<p>To try and find Icelandic Twitters, an easy first pass is to search for purely Icelandic words. This link will return an RSS feed of matching tweets.</p>
<p><a href="http://search.twitter.com/search.atom?q=það ">http://search.twitter.com/search.atom?q=það </a></p>
<p><code>&lt;entry&gt;<br />
&lt;id&gt;tag:search.twitter.com,2005:8243287523&lt;/id&gt;<br />
&lt;published&gt;2010-01-26T17:29:23Z&lt;/published&gt;<br />
&lt;link type="text/html" href="http://twitter.com/username/statuses/12345" rel="alternate"/&gt;<br />
&lt;title&gt;...&lt;/title&gt;<br />
&lt;content type="html"&gt;...&lt;/content&gt;<br />
&lt;updated&gt;2010-01-26T17:29:23Z&lt;/updated&gt;<br />
&lt;link type="image/png" href="http://a1.twimg.com/profile_images/1234/profile5_normal.png" rel="image"/&gt;<br />
&lt;twitter:geo&gt;<br />
&lt;/twitter:geo&gt;<br />
&lt;twitter:source&gt;...&lt;/twitter:source&gt;<br />
&lt;twitter:lang&gt;is&lt;/twitter:lang&gt;<br />
&lt;author&gt;<br />
&lt;name&gt;Display Name&lt;/name&gt;<br />
&lt;uri&gt;http://twitter.com/username&lt;/uri&gt;<br />
&lt;/author&gt;<br />
&lt;/entry&gt;<span id="more-4585"></span></code></p>
<p>From this, we can then extract the display names and twitter usernames and plenty of other information about the tweet.</p>
<p>If you try searching for some jargon within your field, you can begin to collect like-minded people.</p>
<p>There is also a feature in the search which allows for language detection. Using the parameter <tt>&amp;lang=is</tt> it is possible to further filter the search. For English speakers, this might be more difficult than other languages. The system isn&#8217;t perfect either, but it can be away to limit search results for terms like &#8220;schmuck&#8221; in German to jewelry and &#8220;schmuck&#8221; in English to an insult.</p>
<p>Finally, you can restrict the results to a geographically close group of friends by using the near parameter <tt>&amp;near=New+York&amp;within=15&amp;units=mi</tt>. You can also specific the distance and units (miles or kilometers). At the moment this is trying to using the user&#8217;s Location, but in the future it might also use their new <a href="http://blog.twitter.com/2009/08/location-location-location.html">geolocation API</a>. People don&#8217;t always list their location, or change it out of solidarity to a cause.</p>
<p>These systems aren&#8217;t perfect, but when put together they allow for an easy way to thin down the millions of twitter to a much smaller more manageable size.</p>
<h3>Create your own Groups</h3>
<p>This is ideal for local affinity groups or video game clans. Want to see what all &#8220;EVE Online&#8221; player who are in a specific company and live in &#8220;New York City&#8221; have to say? Well we can search twitter and start to collect matching usernames, index them and search for interesting conversations. Maybe you can make new friends locally beyond the game.</p>
<p>The downside of using search is that you will only find new people as they tweet. Search only goes back 10 days (maybe less as twitter gets larger and larger). There will also be some false positives, but if you select some good, specific search terms, then you minimize the work pruning out mistakes.</p>
<p>I am searching twitter once an hour for any new posts that meet the criteria. Once an hour is enough for me, but depending on the size and frequency of your group, you can poll more or less frequently. As I loop through the RSS results and find username not already in the database, I add them so I have a nice growing list of people. I then subscribe to their RSS and save all the tweets into another table for later analysis.</p>
<h3>Find and Subscribe</h3>
<p>Over time, I have managed to find 3900+ Icelanders on twitter. That&#8217;s over 1% of the population. Twitter does have a limit of 2000 that people you can follow before you need to maintain a ratio of following to followers. This prevent spammers from following thousands of people and wrecking the system.</p>
<p>Once you have the twitter username, it isn&#8217;t necessary to follow them via twitter&#8217;s functionality, it&#8217;s enough to subscribe directly to their RSS feed if they are public. Now it seems rediculous to subscribe to thousands of RSS feeds, that&#8217;s a bandwidth hog. So I have offloaded that work to Google Reader. Google has cached versions, so all I need to do is access a single feed in Google Reader which is a composite of all the Twitter RSS feeds I have found. This makes a single RSS fire hose of your groups tweets.</p>
<h3>Twitter Stats</h3>
<p>Since I started mining my twitter data, some interesting statistics have emerged. I looked at the last 24 hours to see how many unique usernames had posted. Roughly 10% of the twitter population in Iceland is posting on a daily basis, but how active are they? Well, we can look at how many posts there were in the last 24 hours and divide that into active users and get about 6.5 posts per active twitter account per day.</p>
<p>I also looked at a longer time span of the last seven days. This allows me to get a break down by day to see the activity. As you can see, Sundays are pretty slow, then it picks up again on Monday. Presumably, as people return to work and are back in-front of the computer they get back to twittering. You can see the current stats at <a href="http://icelanders.optional.is/">http://icelanders.optional.is/</a></p>
<p>These stats are valuable information if you are trying to spread your message via social networks. Do you send it out on a Sunday where there is less traffic and it is more likely to be seen by fewer eyeballs or on Monday when more people are online, but there is a deluge of other tweets?</p>
<p><a href="http://carsonified.com/wp-content/uploads/2010/02/dcgxb64p_262hpggvfgp_b.png"><img class="aligncenter size-full wp-image-4608" title="dcgxb64p_262hpggvfgp_b" src="http://carsonified.com/wp-content/uploads/2010/02/dcgxb64p_262hpggvfgp_b.png" alt="" width="420" height="200" /></a></p>
<p>I also broke it down further into hour-by-hour slices. As you can see, the early morning hours from midnight until around 8am are pretty quiet. Still some activity, but not the time to expect someone to reply.</p>
<p><a href="http://carsonified.com/wp-content/uploads/2010/02/dcgxb64p_264drrt7cg3_b.png"><img class="aligncenter size-full wp-image-4609" title="dcgxb64p_264drrt7cg3_b" src="http://carsonified.com/wp-content/uploads/2010/02/dcgxb64p_264drrt7cg3_b.png" alt="" width="420" height="200" /></a></p>
<p>Once you are archiving a sub-set of twitter, it becomes possible to search this smaller list. Maybe the types of complains within the group vary greatly than with the general public. Are discussions staying within the niche group or are links and retweets getting outside of the core group? You can even find out how many people within this group are following each other and determine the level of interconnectedness.</p>
<p>Recently, I went through the database and using the Twitter API, found out when each person signed-up. I could then look to see if there was a growth spike or continual steady growth. It turns out there was a spike in early 2009.</p>
<p style="text-align: center;"><em>Click image to view large version</em></p>
<p style="text-align: center;"><a href="http://carsonified.com/wp-content/uploads/2010/02/twitter-trend-en-big.png"><img class="aligncenter size-full wp-image-4610" title="dcgxb64p_265g2x3jdcj_b" src="http://carsonified.com/wp-content/uploads/2010/02/dcgxb64p_265g2x3jdcj_b.png" alt="" width="470" height="213" /></a></p>
<p>Right around the time Twitter was being touted to the masses via Oprah, the major local newspaper joined and mentioned it a few times in print. These two, along with other factors really boosted the sign-ups from a few tens of people to several hundred a month! Even now, there is a solid 100+ new Icelanders coming to Twitter each month to see what it is all about.</p>
<p>Building something similar for your group allows you watch for spikes in discussion or sign-ups, look for trending topics within your community, engage in the discussion and find new friends.</p>
]]></content:encoded>
			<wfw:commentRss>http://thinkvitamin.com/code/finding-your-flock-in-twitter/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Stay on :target</title>
		<link>http://thinkvitamin.com/design/stay-on-target/</link>
		<comments>http://thinkvitamin.com/design/stay-on-target/#comments</comments>
		<pubDate>Thu, 31 Jan 2008 09:00:34 +0000</pubDate>
		<dc:creator>Brian Suda</dc:creator>
				<category><![CDATA[Design]]></category>

		<guid isPermaLink="false">http://www.thinkvitamin.com/features/css/stay-on-target</guid>
		<description><![CDATA[In this article, I want to introduce you to a really powerful CSS3 pseudo selector called :target. Much like :hover, :target is invoked during certain interactions with the website. Specifically, when applied to a fragment identifier. On a page such as http://example.com/index.html#hello-world, the id=”hello-world” element is the target and any matching :target styles would be [...]]]></description>
			<content:encoded><![CDATA[<p>In this article, I want to introduce you to a really powerful CSS3 pseudo selector called :target. Much like :hover, :target is invoked during certain interactions with the website. Specifically, when applied to a fragment identifier. On a page such as http://example.com/index.html#hello-world, the id=”hello-world” element is the target and any matching :target styles would be applied.</p>
<p>I am going to demonstrate two examples of when and how :target can be used. Hopefully, this introduction will get you thinking about some of simple ways :target can add benefit your customers.</p>
<h3>Tabs in CSS</h3>
<p>A well structured document is one that makes sense in the ordering of sections and topics. It works well when there is no CSS and no JavaScript. Tabbed systems make use of some sort of display tab which shows the current section, and in doing so hides the other sections. We’ve all seen a tabbed system before, and probably  plenty of poor implementations as well. Sometimes when you click the tab, it calls a JavaScript function to populate the area below the tab. The downside is that when there is no JavaScript the data does not appear.</p>
<p>The ideal way to build a tab system is to have all your data in the HTML, then use JavaScript to hide the other tab panes and build the tabs themselves dynamically. That way if there is no JavaScript, it just defaults to a linear set of text blocks. This is progressive enhancement; build for the lowest common denominator and add more functionality and styling as you go — no one is left behind, not even googlebot.</p>
<p>Instead of using JavaScript to do this show/hide, it is possible to use CSS3 using the :target selector. Before you get too excited, it must be said that the :target selector is not supported in IE6 or IE7, so the practical uses for this are pretty slim. Instead this can be used for user chrome type objects which don’t effect functionality, but improve usability. You can add the :target info today, and when browsers implementations catch-up they get the bells and whistles auto-magically. It is important to <strong>not</strong> use :target for mission critical things, but there are plenty of other possibilities. As an example, I will show you how make a simple tab system based on <a href="http://daniel.glazman.free.fr/weblog/targetExample.html#general">Daniel Glazman’s example</a>.</p>
<p>First we should create the basic un-styled HTML.<br />
<code><br />
&lt;h2 id="topic1"&gt;&lt;a href="#content1"&gt;Topic 1&lt;/a&gt;&lt;/h2&gt;<br />
This is the text for topic 1. Hello World!<br />
&lt;h2 id="topic2"&gt;&lt;a href="#content2"&gt;Topic 2&lt;/a&gt;&lt;/h2&gt;<br />
Different text for topic 2. Ah, foobar.<br />
</code><br />
Without CSS or JavaScript this text is completely accessible and reads normally, it just makes the page longer. Tabs are a way to compact the information into a smaller space, so lets proceed to do this.</p>
<p>If we add some simple styles to the page, we can get the two headings to appear and act like tabs through positioning. The interesting part is the :target selector.</p>
<p><code>// set the default state first<br />
p {<br />
display: none;<br />
}</code></p>
<p><code> </code></p>
<p><code>// When the link is clicked, set the contents to display<br />
p:target {<br />
display: block;<br />
}<br />
</code></p>
<p>When the fragment (#content1) is not part of the URL, the :target selector does not match the</p>
<p>element, so it is set the default view of display: none. This is a very simple way to create a tabbed system.</p>
<h3>The Yellow Fade Technique</h3>
<p>There is a popular usability feature called the “Yellow Fade Technique”. This is used to direct the user’s attention back to a specific area subtly. For example, when you want to edit a piece of a page you may click edit which takes to a new page with form fields. When you press save you are returned to the original page where there is a brief yellow fade of the area that was just edited. It brings your eye to the area, possibly to confirm the changes look correctly in the template, or to just make people aware the change took place at all. Either way, it seems to be a welcomed usability feature that has been picked-up by several web applications.</p>
<p>Normally, this is accomplished through the use of JavaScript and page IDs or classes. The JavaScript runs an onload function looking for those classes or ID and once the parts of the page have been identified, the script runs a timer and ever few microseconds change the background color from pure yellow to white. This create a smooth, not too annoying, yellow fade effect.</p>
<p>The downside to all this is that there are several resources at work, including onload functions (which might be happening <strong>every</strong> time the page loads, not just the times when a form is submitted) and hooks into the classes and IDs and JavaScript timers. All this adds to the client download and subtracts from the speed of the page.</p>
<p>With CSS3 there is a much easier and simpler solution. Using the :target pseudo class it is possible to emulate the yellow fade without a single line of JavaScript.</p>
<p>First, you need to create a tiny 1 pixel-wide yellow animated gif. This will fade from yellow to white. The animated gif isn’t repeating, so it will fade once and then stay pure white. This also defines the length of the fade, so there is no need to for a JavaScript timer.</p>
<p>Second, we need to set this as the background image for the area which has been changed. To do this, we use the CSS3 :target pseudo selector.</p>
<p><code>#here:target {<br />
background-image: url('yellow-fade.gif');<br />
}<br />
</code></p>
<p>This will fade the element with an id=”here”, the URL would look like http://example.com/#here. It is good web architecture to use fragment identifiers for each of your headings and to use <a href="http://en.wikipedia.org/wiki/Representational_State_Transfer">RESTful URLs</a>.</p>
<p>Now, the downside to that CSS statement is that it will only fade the area with the ID.</p>
<p><code><br />
&lt;h1 id="here"&gt;Hello World&lt;/h1&gt;<br />
I am saying Hello to everyone in the World!</code><br />
Only the heading element would get the yellow fade. So how do we fix this? Well, we could change the ID to a class and create something like:<br />
<code>.here:target {<br />
background-image: url('yellow-fade.gif');<br />
}</code><br />
But then on the server, we’d have to add classes dynamically to all our elements.<br />
<code><br />
&lt;h1 class="here"&gt;Hello World&lt;/h1&gt;<br />
I am saying Hello to everyone in the World!</code><br />
We could put a wrapper around both the heading and the paragraph.<br />
<code><br />
&lt;div id="here"&gt;<br />
&lt;h1&gt;Hello World&lt;/h1&gt;<br />
I am saying Hello to everyone in the World!<br />
&lt;/div&gt;<br />
</code><br />
But that seems too much work. Instead, we can make use of another CSS combinator, the adjacent sibling selector:<br />
<code>#here:target, #here:target p {<br />
background-image: url('yellow-fade.gif');<br />
}</code><br />
This targets both the heading with the ID ‘here’ and the first sibling paragraph. So both will get the background image and the subsequent fade. You can easily change your CSS to fit your HTML design, but this is a quick and simple alternative to the Yellow Fade technique in JavaScript used on so many sites.</p>
<p>In CSS there is also a wildcard * to stand for all nodes. So you could further take the example to<br />
<code>#here:target, #here:target * {<br />
background-image: url('yellow-fade.gif');<br />
}</code><br />
This would match the first sibling of #here no matter what the type of element it might be. Firefox correctly supports this, but as of writing, Safari does not.</p>
<h3>Blurring the lines between behaviour and presentation</h3>
<p>I am obligated by the behaviorists to mention that there is a debate over presentation and behavior, where does one stop and the other start? By adding the :target selector into your CSS, you are styling the HTML based on the behavior of the browser and the users. We talk about the “holy trinity” of web design, and we have spent the last few years campaigning to divide data, presentation and behavior into their respective HTML, CSS and JavaScript camps. The :target, as well as the :hover, selectors blur that line between presentation and behavior. Depending on how comfortable or how much of a purist you are, you may or may not agree with what has been demonstrated.</p>
<p>These examples were written to better understand the CSS3 :target element and to explore those blurry grey-areas. Hopefully, a discussion will emerge with even more ideas, caveats and problems that were not originally thought of. Together we can create some best practices on when to best use CSS behavior and JavaScript presentation.</p>
<h3>Conclusion</h3>
<p>Over a year ago, Andy Budd demonstrated the yellow-fade technique with :target and I was impressed! (<a href="http://www.andybudd.com/presentations/css3/">http://www.andybudd.com/presentations/css3/</a>) In the time in between, I have seen very little attention or articles on this little known selector. As I was started working on this article, I found several examples some from as early as January 2003 that talk about uses for the :target selector. The reason for not getting the traction it deserves could be the lack of information about when and how to use it correctly or documentation on the poor CSS3 implementations in browsers.</p>
<p>CSS3 is closer than you think, many of the selectors and attributes are available in several major browsers today. They can be used with progressive enhancement to add more style to your design at little cost, when other browsers catch-up they get the benefits of your progressive code for free. The CSS3 :target selector is just one example of some of the new selectors arriving that can be taken advantage of in interesting new ways.</p>
]]></content:encoded>
			<wfw:commentRss>http://thinkvitamin.com/design/stay-on-target/feed/</wfw:commentRss>
		<slash:comments>17</slash:comments>
		</item>
	</channel>
</rss>

<!-- Dynamic page generated in 1.023 seconds. -->
<!-- Cached page generated by WP-Super-Cache on 2012-02-11 16:19:04 -->

