<?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>Manic Mouse &#187; Code</title>
	<atom:link href="http://manicmouse.com/category/code/feed/" rel="self" type="application/rss+xml" />
	<link>http://manicmouse.com</link>
	<description>By Ben Callahan, on making websites.</description>
	<lastBuildDate>Wed, 08 Dec 2010 20:27:41 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	
		<item>
		<title>Image-Free Apple Style Pagination with CSS3</title>
		<link>http://manicmouse.com/image-free-apple-style-pagination-with-css3/</link>
		<comments>http://manicmouse.com/image-free-apple-style-pagination-with-css3/#comments</comments>
		<pubDate>Mon, 09 Nov 2009 06:16:11 +0000</pubDate>
		<dc:creator>Ben</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[CSS]]></category>
		<category><![CDATA[HTML]]></category>
		<category><![CDATA[Semantics]]></category>
		<category><![CDATA[User Interface]]></category>
		<category><![CDATA[Web]]></category>

		<guid isPermaLink="false">http://manicmouse.com/?p=16</guid>
		<description><![CDATA[A step-by-step explanation on how to create "Apple Style" pagination using only semantic mark-up and CSS3.]]></description>
			<content:encoded><![CDATA[<h3>Explaining My Inspiration</h3>
<p><img class="alignleft size-thumbnail wp-image-144" title="Beautiful Pagination" src="http://manicmouse.com/wp-content/uploads/2009/11/apple-style-pagination1-185x185.png" alt="Beautiful Pagination" width="185" height="185" />I remember the first time Apple introduced a very simple pagination UI element, back in an early version of the iTunes Store. I loved this element. It was clean, simple, easy to understand. Everything about it just seemed to work. Unfortunately, the latest version of the store no longer sports this elegant form of pagination. It&#8217;s been replaced with an anorexic scroll bar which means I can&#8217;t show you a screen capture. However, if you used iTunes a year or two ago, you&#8217;ll remember what I&#8217;m talking about. It&#8217;s the small row of dots with one &#8220;selected&#8221; to show you where in a set of pages you are. Apple used this with a simple right-to-left animation to add to the illusion that you were sliding through their content.</p>
<p>For some strange reason, this simple user experience made a mark on me. Honestly, I&#8217;m not even sure if Apple really introduced this form of pagination. This is just the first time I remember seeing it.</p>
<p>So, flash forward to a few weeks ago when one of the <a title="Andy Rossi" href="http://iamrocco.net">FORGE UI/UX Designers (Andy Rossi)</a> hands me a PSD of a design for a web-app we&#8217;re working on. To my delight, Andy has recreated this style of pagination – with his own twist, of course. My job is to turn this beautiful little UI element into something that&#8217;s usable. And, if you know me, that means finding a robust solution, something that can be expanded easily and will degrade gracefully.</p>
<p>The rest of this article walks through my process in creating this interface element with the semantic and forward-thinking approach that makes <a title="FORGE - where ideas take shape" href="http://forgeideas.com">FORGE</a> unique in our market.</p>
<h3>Project Paramaters</h3>
<p>I had a few requirements in getting started, that I&#8217;ll share with you first:</p>
<ul>
<li>semantic mark-up</li>
<li>cross-browser/operating system compatible</li>
<li>image free</li>
</ul>
<p>The first two seem simple enough, but the last (image free) may seem a little odd. This stems from my recent interest in incorporating CSS3 into our production work at FORGE. We&#8217;ve been experimenting (<a title="Handcrafted CSS by Dan Cedarholm with Ethan Marcotte" href="http://handcraftedcss.com/">like many others</a>) with using some of the browser vendor specific CSS properties and allowing for a graceful degradation in &#8220;lesser&#8221; browsers. I was curious if this could be done simply with <code>background-color</code> and <code>border-radius</code>, so I set out to do it.</p>
<h3>Writing the Mark-Up</h3>
<p>I decided early on to use an ordered list as the primary mark-up element. We&#8217;re talking about a UI component that represents where you are in an ordered set of pages, so this makes semantic sense. Here&#8217;s what I came up with:</p>
<pre class="brush: xml;">
&lt;ol class=&quot;pagination&quot;&gt;
	&lt;li class=&quot;on&quot;&gt;&lt;a title=&quot;Page 1&quot; href=&quot;page1.html&quot;&gt;Page 1&lt;/a&gt;&lt;/li&gt;
	&lt;li&gt;&lt;a title=&quot;Page 2&quot; href=&quot;page2.html&quot;&gt;Page 2&lt;/a&gt;&lt;/li&gt;
	&lt;li&gt;&lt;a title=&quot;Page 3&quot; href=&quot;page3.html&quot;&gt;Page 3&lt;/a&gt;&lt;/li&gt;
	&lt;li&gt;&lt;a title=&quot;Page 4&quot; href=&quot;page4.html&quot;&gt;Page 4&lt;/a&gt;&lt;/li&gt;
&lt;/ol&gt;
</pre>
<p>Quite simply, you can see we have an ordered list with a class of &#8220;pagination&#8221;. Each list item inside our ordered list contains an anchor tag pointing to the page it represents. The class of &#8220;on&#8221; is added to the list item containing a link to the current page. This should look very familiar as most of us are handling our standard page navigation with mark-up very similar to this.</p>
<div id="attachment_26" class="wp-caption alignleft" style="width: 110px"><img class="size-full wp-image-26 " title="Screen Capture of Basic Mark-Up" src="http://manicmouse.com/wp-content/uploads/2009/11/ol-no-style.png" alt="Ordered List with No CSS" width="100" height="100" /><p class="wp-caption-text">Ordered List with No CSS</p></div>
<p>This mark-up renders pretty much the same in every browser our there. Here&#8217;s a screen capture I pulled from FireFox 3.5 on my Mac Book Pro. Kinda boring, but it gets better.</p>
<h3>Adding Some Style</h3>
<p>Now the fun begins. Let&#8217;s apply some simple styles to get this looking more like our elegant Apple pagination. First, we&#8217;ll remove the numbers on the left side of the list with a simple <code>list-style:none;</code>. Then we&#8217;ll float the entire list to the left and throw a 2 pixel border on it. Additionally, let&#8217;s remove all the padding from our ordered list as browsers typically will stick some padding on the left side.</p>
<p>Next we&#8217;ll make some adjustments to the list items and anchor tags. Both of these elements will need floated to the left to get them to flow horizontally instead of vertically and to get the ordered list&#8217;s border to show up in the right spot. Then we can set a fixed width and height for the anchor tags (I picked 6 pixels, but you can adapt these styles to suit your taste) and use the old <code>text-indent: -9999px;</code> trick to hide the anchor text. Finally we&#8217;ll need a little breathing room for our anchors, so let&#8217;s put some margin on the right side of each and give them a background color that matches the border of the ordered list.</p>
<p>The CSS should look something like this so far:</p>
<pre class="brush: css;">
.pagination {
  list-style: none;
  float: left;
  border: 2px solid #4b4b4b;
}

.pagination li {
  float: left;
}

.pagination li a {
  float: left;
  width: 6px;
  height: 6px;
  margin-right: 3px;
  text-indent: -9999px;
  background-color: #4b4b4b;
}
</pre>
<div id="attachment_34" class="wp-caption alignright" style="width: 210px"><img class="size-full wp-image-34" title="Ordered list with basic css" src="http://manicmouse.com/wp-content/uploads/2009/11/ol-basic-style.png" alt="Basic CSS applied to our ordered list (FireFox 3.5 on OS X on the left and IE7 on WinXP on the right)" width="200" height="100" /><p class="wp-caption-text">Basic CSS applied to our ordered list (FireFox 3.5 on OS X on the left and IE7 on WinXP on the right)</p></div>
<p>This screen capture shows how things are looking in FireFox 3.5 on a Mac (left) and IE7 on WinXP (right). You can see we&#8217;re starting to get somewhere. Not quite complete, but certainly moving in the right direction. You&#8217;ll notice that the small squares are click-able links, these are actually the anchor tags. I&#8217;m sure you have also noticed that the &#8220;squares&#8221; aren&#8217;t quite square in IE. We can fix this by adding a <code>line-height</code> of 6 pixels to the anchors. You may also have noticed that the spacing isn&#8217;t quite right. We&#8217;ve set a 3px margin on the right of each anchor, which actually puts 6px of space between the last anchor tag and the ordered list&#8217;s border. If we remove the 3px padding from the right of the ordered list itself, we&#8217;ll have this problem solved.</p>
<h3>Making Circles from Rounded Corners</h3>
<p>CSS3 provides a beautiful little property called <code>border-radius</code>. This allows us to create rounded corners with CSS alone. Unfortunately, most browsers don&#8217;t support the official CSS3 property name yet. Luckily they do have vendor specific prefixes which allow us to get to some of this functionality now. Let&#8217;s set the <code>-moz-border-radius</code>, <code>-webkit-border-radius</code> and the <code>border-radius</code> properties of the anchors to 3 pixels. Since the anchor&#8217;s are 6 pixels high and 6 pixels wide, a 3 pixel <code>border-radius</code> will make a perfect circle. Let&#8217;s also set the same properties of the ordered list itself to 8 pixels. The calculation here is a little more complex. We have 6 pixels of height (from each anchor) plus 3 pixels of padding above and below each anchor (which puts us at 12 pixels) plus 2 pixels in the thickness of the top border and the bottom border. This gives us a total height of 16 pixels. Cut it in half and you get the 8 that we need.</p>
<p><em>Note: we will include all three of these properties in our CSS. If the browser doesn&#8217;t recognize a given property, it will just ignore it. Putting the actual <code>border-radius</code> property in is just a little future-proofing, for when the browsers actually support this without their vendor prefix.</em></p>
<p>Here&#8217;s the modified CSS:</p>
<pre class="brush: css;">
.pagination {
  list-style: none;
  float: left;
  padding: 3px 0 3px 3px;
  border: 2px solid #4b4b4b;
  -moz-border-radius: 8px;
  -webkit-border-radius: 8px;
  border-radius: 8px;
}

.pagination li {
  float: left;
}

.pagination li a {
  float: left;
  width: 6px;
  height: 6px;
  line-height: 6px;
  margin-right: 3px;
  text-indent: -9999px;
  background-color: #4b4b4b;
  -moz-border-radius: 3px;
  -webkit-border-radius: 3px;
  border-radius: 3px;
}
</pre>
<div id="attachment_37" class="wp-caption alignleft" style="width: 210px"><img class="size-full wp-image-37" title="Ordered list with basic style" src="http://manicmouse.com/wp-content/uploads/2009/11/ol-basic-style-2.png" alt="Basic CSS with a few modifications (FireFox 3.5 on OS X on the left and IE 7 on XP on the right)." width="200" height="100" /><p class="wp-caption-text">Basic CSS with a few modifications (FireFox 3.5 on OS X on the left and IE 7 on XP on the right).</p></div>
<p>You can see from the screen capture that the FireFox version (on the left) is looking great. Unfortunately, IE doesn&#8217;t support the <code>border-radius</code> property in any way. So, we&#8217;re out of luck getting the pagination to<a title="Do website need to look exactly the same in every browser?" href="http://dowebsitesneedtolookexactlythesameineverybrowser.com/"> look exactly the same in IE</a>. I actually have no problem with this. The squares, while not what we were going for originally, are still functional. They also provide a better user experience than a numbered list of links&#8230; The real question is, are you OK with some visitors seeing circles and some squares?</p>
<div id="attachment_40" class="wp-caption alignleft" style="width: 415px"><img class="size-full wp-image-40 " title="Ordered list with most of the CSS applied." src="http://manicmouse.com/wp-content/uploads/2009/11/ol-all-white1.png" alt="The ordered list with most of the CSS applied. Various browser results shown." width="405" height="100" /><p class="wp-caption-text">The ordered list with most of the CSS applied. Various browsers tested.</p></div>
<p>Now, the only thing that&#8217;s left is to add a little color modification to the &#8220;current&#8221; page. While we&#8217;re at it, we&#8217;ll specify a background color on the body to make this thing look great. <span style="text-decoration: line-through;">Additionally, I noticed that on FireFox, if you click on one of the circles, you see an outlined box around a portion of the ordered list. This can be removed with a simple style applied to the anchors: </span><code><span style="text-decoration: line-through;">outline: none;</span></code><span style="text-decoration: line-through;">.</span></p>
<p><em><span style="color: #008000;">Correction: adding <code>outline: none;</code> removes key accessibility functionality from this system. Please don&#8217;t do this. I&#8217;m working on an alternative <code>:active</code> style which will solve this problem! Thanks for your patience.</span></em></p>
<p>Here&#8217;s the final CSS:</p>
<pre class="brush: css;">
body {
  background-color: #2b2b2b;
}

.pagination {
  list-style: none;
  float: left;
  padding: 3px 0 3px 3px;
  border: 2px solid #4b4b4b;
  -moz-border-radius: 8px;
  -webkit-border-radius: 8px;
  border-radius: 8px;
}

.pagination li {
  float: left;
}

.pagination li a {
  float: left;
  width: 6px;
  height: 6px;
  line-height: 6px;
  margin-right: 3px;
  text-indent: -9999px;
  background-color: #4b4b4b;
  -moz-border-radius: 3px;
  -webkit-border-radius: 3px;
  border-radius: 3px;
}

.pagination li.on a {
     background-color: #1f84e3;
}
</pre>
<div id="attachment_41" class="wp-caption alignleft" style="width: 415px"><img class="size-full wp-image-41" title="Final ordered list with all CSS applied" src="http://manicmouse.com/wp-content/uploads/2009/11/ol-all-grey.png" alt="The final ordered list with all of our CSS applied. Various browsers tested." width="405" height="100" /><p class="wp-caption-text">The final ordered list with all of our CSS applied. Various browsers tested.</p></div>
<p>And this screen capture shows how it looks in various browsers with all of our styles applied. You can see that it&#8217;s not exactly consistent, but it&#8217;s darn close, and still functional everywhere. The fact that we&#8217;re not using any images here is what&#8217;s most fascinating to me. It&#8217;s proof that we really do need to start evaluating how we do things everyday. Given this task six months ago, I would have inevitably started in PhotoShop chopping up some circles. Today, things are different. It&#8217;s a great time to be web developer.</p>
<p>Another great thing about this code is how expandable it is. Need to fit a few more pages in? Just add list items and it works beautifully. No heading back to your image editor to re-export that border image. Color changes are a breeze also. It&#8217;s ALL taken care of in the CSS file – it really doesn&#8217;t get any easier than this.</p>
<h3>What&#8217;s Next?</h3>
<p>After completing this much, I applied some JavaScript to animate a fade out and fade in of the current anchor during the page transition. This is actually quite simple, but I&#8217;ll save that for another day.</p>
<p>I also had a thought that we could do all of this using the &#8220;em&#8221; measurement instead of pixels. I did a quick test and it was quite entertaining to watch the entire pagination list grow and shrink as I changed my browser&#8217;s default font size. I don&#8217;t know that this is something people would expect, or need, but it is kind of fun.</p>
<p>I would also like to state, that I have NOT done a complete browser/OS test run of this. Only the browsers you see listed above were tested, so I&#8217;m sure there are a few other issues that could be addressed. If you happen across an instance where this doesn&#8217;t work, feel free to post a comment and we&#8217;ll get it figured out.</p>
<p>The example files are available for your viewing (or downloading) pleasure at the following links. Feel free to use and enjoy (and show a little link-love).</p>
<p><a title="Pixel-Based" href="http://manicmouse.com/wp-content/demo/apple-pagination/pixels.html">Pixel-Based Image-Free Apple Style Pagination</a></p>
<p><a title="Em-Based" href="http://manicmouse.com/wp-content/demo/apple-pagination/ems.html">Em-Based Image-Free Apple Style Pagination</a></p>
<p><a href="http://manicmouse.com/wp-content/uploads/2009/11/apple-pagination.zip">Download Apple Style Pagination Examples</a></p>
]]></content:encoded>
			<wfw:commentRss>http://manicmouse.com/image-free-apple-style-pagination-with-css3/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>

