Thursday, March 29, 2012

The EMs have it: Proportional Media Queries FTW!

If we’re going to be proportional…


A core tenet of Responsive Web Design (RWD) is fluidity and proportion. Instead of using fixed-width layouts, we enlightened web devs and designers use percentages in our CSS. Font units aren’t pixels or points anymore, they’re percentages (typically for top-level baseline reset) or, more often, ems. And yet the vast majority of us still write width-based media queries in pixels, e.g.:
@media all and (min-width: 500px) {}
@media screen and (max-width: 800px) {}
It’s a natural thought process: for windows or screens of particular width ranges, we want to flow our content in a particular way. This naturally seems like a pixel dimension. But, once again, to bring out my big hammer, what about when we look at things from a content perspective?
Folks who design for traditional reading media—where the content really is king—don’t center design decisions around the absolute width of content-holding elements so much as around the optimal line lengths for the content they’re flowing. There are some tried-and-true numbers one can shoot for that make for the “right” number of letters (and thus words) per line for comfortable human reading.
Thus actual column width is a function of font size and ems-per-line.

Baseline expectations

You may have seen the rule of thumb (useful, admittedly!) that makes the following general equation in terms of baseline font sizes in CSS:
100% = 1 em ~= 16px ~= 14pt
This means that, at your baseline, that is, before you’ve adjust font sizes in any child elements, the default font size in the browser is usually approximately 16px and usually approximately 14pt but always 100% and 1em.
If we start from a baseline of 16px, you may well wonder what the difference (beyond academic) is between a media query like:
@media all and (min-width: 400px)
and one like this, that uses ems multiplied against the 16px baseline approximation:
@media all and (min-width: 25em)

The answer, my friend, is content

Here’s the 64-dollar question or whatever:
What happens when the baseline is not 14pt/16px?
On Ye Olde Desktop Web, this situation most often comes about (“most often” is unscientific here, but I’m willing to toss out the hypothesis, at least) from user-intiated zoom in browsers. Low-sight users might do it, I know I do it when I’m surfing the web on my Mac Mini from the couch across the room. I’m not that old but, man, it’s hard to see the contents of a Wikipedia article on a 23″ monitor from 10 feet away.
As I zoom in, that is, make the text larger in my browser, I’m no longer at a 14pt/16px baseline. 100%/1em in my world is now a different number, maybe 18pt, maybe 32px. Pixel-defined content holders no longer have comfortable amounts of words per line. Pixel-defined content holders that float might wrap awkwardly as the content in them swells.

Case study: Our nav

Our creative director and CSS wizard extraordinaire, Aileen, whipped us up a beaut of a responsive new site layout. For various screen widths, our site’s nav has a few different behaviors.
In pixels at normal zoom, the nav elements fit in a line roughly at a width of around 656px. There’s generous room to detatch and float next to the logo at around 960px.
For screens/windows narrow enough that the full set of top-level nav elements would not fit on one line, we use the menu button nav pattern:

For screens/windows narrow enough that the full set of top-level nav elements would not fit on one line, we use the menu button nav pattern.

For screens/windows in a particular range that are wide enough to fit the top-level nav elements on a single line, we do that.

For screens/windows in a particular range that are wide enough to fit the top-level nav elements on a single line, we do that.

For screens/windows wide enough to fit all of the top-level nav elements horizontally and have enough room left over for our logo, we detatch and float the nav to the top right.

For screens/windows wide enough to fit all of the top-level nav elements horizontally and have enough room left over for our logo, we detatch and float the nav to the top right.

But what happens if a user has his or her zoom set higher?

I’ll show you a little experiment. I’m using the Chrome browser, and I’m viewing our site with a window about 670 pixels wide. With a pixel-based media query, that puts me in the second category of nav experience: all of the top level items are shown horizontally, docked to the top of the content:


OK, now I’m going to use the Zoom In command twice to make my text larger.
With a pixel-based media query:

 

With an em-based media query:


Why did this happen?
The pixel-based media query @media all and (min-width:656px) still evaluates to true with the zoomed-in text and therefore creates awkwardly-wrapped nav elements.
However, the em-based media query of @media all and (min-width: 41em) scales to the larger text size. Zoomed in like this, the browser no longer satisfies that query: we have fewer than 41ems to work with. So we deliver the menu-button nav pattern and other layout and styling appropriate for the way the text fits. Content, again, is what ends up dictating what we’re doing in the end.
You could also make the text smaller and watch the same proportional adjustments occur, in the inverse.
BTW: It should be noted that, unlike window resizes, which cause media queries to be re-evaluated immediately, you’ll need to reload the current page if you zoom in or out for em-based media queries to re-apply. My hunch is that most users who zoom a lot keep their zoom set as they navigate around different pages and aren’t changing zoom settings on a site-by-site basis. I could be wrong about that, though.

The specifics of em-based media queries

The media queries on our site are all crafted based on the approximate baseline. We played with our browser window widths, adjusting them until it looked like they were at the appropriate width to create a breakpoint at normal text zoom. We noted that pixel measurement and divided it by the rough baseline of 16px to arrive at our em units. There may well be a better way to do this math, but this seems to do the trick so far.

A device-specific aside

One of the things that spurred me toward thinking about em-based media queries was my own Kindle Touch. Through no action of my own, the Kindle browser’s default text zoom is, roughly put, high. Like newer smartphones, the Kindle has a high pixel density, 167ppi. An absolute-sized font (say, 16px) on a high-density screen is correspondingly tiny. Most smartphone browsers get around this by reporting their resolution differently for purposes of the web. An iPhone 4, for example, which has a real resolution of 640×960 and 326ppi, masquerades as 320×480 in the browser as a way to get normally-designed websites to look normally-sized and not teeny tiny. For the iPhone, 1em is still approximately 16px or 14pt, and so pixel-based media queries generally behave equivalently to em-based ones (disclaimer: that’s a broad generalization).
The Kindle Touch, on the other hand, has taken a different approach. It reports its web pixel resolution as 600×800, but its default text size is considerably larger than 16px/14pt.
Our early, pixel-based media queries for our nav looked dreadful on the Kindle. Its resolution meant that it was using our tablet-range layout, but its text was enormous, causing widespread ugliness as the pixel-width elements didn’t scale to adjust. The site looks pretty decent, however, with media queries in ems:



Our site looks tolerable on a Kindle Touch. That's about as good as it's going to get on that browser.
Our site looks tolerable on a Kindle Touch. That’s about as good as it’s going to get on that browser. 
 Article by:

Thursday, February 16, 2012

How to Create a Flashy Menu Using CSS3

How to Create a Flashy Menu Using CSS3

Today I’m going to show you how to create a flashy menu using CSS3. This tutorial is for beginners and can be completed fairly quickly. Make sure you click on the link below to see a demo of the menu and get the source code.
Download the Demo & Source
Final result:

The HTML

Our HTML document contains an unordered list and each list item is a link with an anchor tag. The span contains the name of the menu item.
 <ul class="main-ul">
          <li><a href="#"><span>Home</span></a></li>
          <li><a href="#"><span>Article</span></a></li>
          <li><a href="#"><span>Blog</span></a></li>
          <li><a href="#"><span>Gallery</span></a></li>
          <li><a href="#"><span>About</span></a></li>
          <li><a href="#"><span>Contact Us</span></a></li>
          <li><a href="#"><span>Alumini</span></a></li>
          <li><a href="#"><span>Portfolio</span></a></li>
  </ul>

The CSS

I am using a pattern for our page background from SubtlePattern.
 body{
  background: #eee url(../images/white_paperboard.png) repeat top right;
 }
Now let’s position menu list items. I am using 25% width for each item, so in each row four menu items can be positioned. I’m aligning text of each list item to center.
 .main-ul li {
    float:left;
      position:relative;
       width:25%;
      text-align:center;
   }
Next let’s position each anchor tag and change text decoration to none. I am using a light gray color background. I am also adding CSS3 transition effects to these elements with a duration of one second.
 .main-ul li a {
                       display:block;
      padding-bottom:20px;
      padding-right:10px;
      padding-top:10px;
      padding-left:10px;
       text-decoration:none;
      position: relative;
       z-index: 100;
      background-color: rgba(164, 164, 164, 0.2);
       -webkit-transition: all 1s;
      -moz-transition: all 1s;
       -o-transition: all 1s;
       transition: all 1s;
    }
I am using ‘Kotta One’ font for span text, normal font size and weight of 20px and 700 respectively. I’ve made the font color for the text in its hover state white.
 .main-ul li a span{
            display:block;
            padding-top:10px;
            font-weight:700;
            font-size: 20px;
            color: rgba(120, 120, 120, 0.9);
            text-transform:uppercase;
            font-family: 'Kotta One', serif;
  }
 .main-ul li:hover span{
                    color: #fff;
 }
Here comes our main part, I have already added transition effect for the anchor tags. Now add hover effects for each anchor tag list item by changing its background color. So when someone hovers over each list menu item it will change background color to a new color. I’m also adding CSS3 transform rotate effects of 3 degrees.
 .main-ul li:nth-child(1):hover a{
                 background-color: rgba(175,54,55,0.8);
                 -moz-transform: rotate(-3deg);
                 -webkit-transform: rotate(-3deg);
                 -o-transform: rotate(-3deg);
                  transform: rotate(-3deg);
 }
Now repeat the above step for all list items with a new background color of your choice!
 .main-ul li:nth-child(2):hover a{
    background-color: rgba(199, 204, 73, 0.8);
    -moz-transform: rotate(-3deg);
    -webkit-transform: rotate(-3deg);
    -o-transform: rotate(-3deg);
    transform: rotate(-3deg);
 }
 .main-ul li:nth-child(3):hover a{
    background-color: rgba(213, 135, 11, 0.8);
    -moz-transform: rotate(3deg);
    -webkit-transform: rotate(3deg);
    -o-transform: rotate(3deg);
    transform: rotate(3deg);
 }
 .main-ul li:nth-child(4):hover a{
    background-color: rgba(51, 143, 144, 0.8);
    -moz-transform: rotate(3deg);
    -webkit-transform: rotate(3deg);
    -o-transform: rotate(3deg);
    transform: rotate(3deg);
 }
 .main-ul li:nth-child(5):hover a{
    background-color: rgba(117,18,98,0.8);
    -moz-transform: rotate(-3deg);
    -webkit-transform: rotate(-3deg);
    -o-transform: rotate(-3deg);
    transform: rotate(-3deg);
 }
 .main-ul li:nth-child(6):hover a{
    background-color: rgba(33, 136, 215, 0.8);
    -moz-transform: rotate(-3deg);
    -webkit-transform: rotate(-3deg);
    -o-transform: rotate(-3deg);
    transform: rotate(-3deg);
 }
 .main-ul li:nth-child(7):hover a{
    background-color: rgba(109, 109, 109, 0.8);
    -moz-transform: rotate(3deg);
    -webkit-transform: rotate(3deg);
    -o-transform: rotate(3deg);
    transform: rotate(3deg);
 }
 .main-ul li:nth-child(8):hover a{
    background-color: rgba(152, 120, 92, 0.8);
    -moz-transform: rotate(3deg);
    -webkit-transform: rotate(3deg);
    -o-transform: rotate(3deg);
    transform: rotate(3deg);
 }
That’s it, we have accomplished a simple flashy menu, so that when someone hovers over our menu items simultaneously it will change background color and slightly rotate. Thanks for reading!

Wednesday, February 8, 2012

A Farewell to CSS3 Gradients

Before we get started, let’s get one thing straight:
I ♥ CSS3.
I understand that we’ve never had it so good. In fact, I have no doubt that time-​​travellers visiting from 2004 would be sick with jealousy at how easily we now conjure up rounded corners, shadows, and object rotations.
Sure, the browser-​​specific prefixes (i.e. –moz, –webkit –o, etc) are an irritation, but happily the rest of the notation is sensible, consistent and reliably supported across modern browsers.
That is, with one notable exception…

CSS3 gradients: Yes, they’re actually kinda crappy.

While the idea of programmable gradients is great, using them is seriously messy. Even in perfect-​​browser-​​utopia, gradients are consciously limited to rudimentary linear or radial application. You certainly can’t combine them, or make them contour a shape or anything useful like that.
More importantly, support is complicated in most browsers and non-​​existent in some.
Here’s a snapshot of the current state of play:
BrowserRadial CSSLinear CSS
Firefox-moz-radial-gradient( 500px 25%, circle, blue 20px, #eef 40px );-moz-linear-gradient(top, #444444, #999999);)
Chrome 11, Safari 5-webkit-radial-gradient(#cde6f9, #81a8cb);-webkit-linear-gradient(top, #444444, #999999);)
Chrome-webkit-gradient( radial, 500 25%, 20, 500 25%, 40, from(blue), to(#eef) );-webkit-gradient(linear, left top, left bottom, from(#444444), to(#999999));
Opera 11No support-o-linear-gradient(top, #444444, #999999);)
Opera 10–No supportNo support
IE10 beta-ms-radial-gradient(top center, circle cover,
orange, yellow 25px, turquoise)
-ms-linear-gradient(30deg, orange, yellow 50%, turquoise);
IE9No supportNo support
IE8No support-ms-filter: "progid:DXImageTransform.Microsoft.gradient( la la la...
IE5-​​7No supportfilter: progid:DXImageTransform.Microsoft.gradient(la la la..
Wow, that looks fun, huh? In short, it’s a blunt instrument with cranky syntax and patchy support.

Ok, so what’s the alternative?

While there have been some great recent discussions on the subject of gradients — John Allsop’s recent article gives the perfect overview — most focus on using CSS3, while occasionally patching a hole here and there with dab of SVG.
Could it be we’re looking at it the wrong way?
In 2011, SVG is actually well-​​supported by all the current mainstream browsers — even on mobile. What if we started with SVG and patched around the edges? Looking back over the 15 year evolution of CSS, it’s been a slow, often tortured development. At that development rate, it’s hard to imagine CSS ever developing into a truly powerful, flexible, visual design tool.
But with SVG, arguably we have that fully-​​featured, visual language today.

Step back: Exactly what is SVG?

So, if you’re thinking ‘SVG? That’s the new Volkswagon, right?’, let’s do a quick catchup.
Scalable Vector Graphics (SVG) is an XML-​​based file format for describing two-​​dimensional vector graphics. Like Illustrator’s AI format, it builds images using maths and geometry, rather than a grid of pixels (i.e. JPEGs, PSD, etc). That gives SVG images some very web-​​relevant superpowers — chiefly, SVG scales without distortion while maintaining tiny file sizes.

How do you make SVG?

There are quite a few ways to make your own SVG.
  • Hand code: If you’re geeky enough, just open your ol’ text editor and start typing. Like most XML formats, SVG mostly makes sense when you read it.
  • Illustrator: Adobe Illustrator can export to SVG, though to be frank, the code is not exactly svelte and sexy. Maybe not the right tool here.
  • Inkscape: Inkscape is a powerful free, open source editor that uses SVG as it’s native file format. It rightly has lots of fans, but might be overkill for producing simple web-​​ready graphics.
  • Raven: Aviary’s Raven is an online vector application that includes a ‘save to SVG’ option. Raven is Flash-​​based, but it’s otherwise free and writes quite decent code too.
  • SVG Edit: My personal favorite, Google’sPavol Rusnak’s SVG Edit (hosted on Google Code)
That last one might be a surprise as Google Code isn’t exactly the first place you think when it comes to visual design tools. As yes, it’s as ugly as a bulldog eating porridge. But happily there’s also a lot to like about it:
  1. It’s free, and we always like free.
  2. It’s built from plain ol’ JavaScript, HTML, CSS and SVG so you can either use Google’s online version or run a copy on your local system.
  3. It writes tight, clean, readable markup.
  4. SVG Edit is designed to keep you in close contact with the SVG code all the way through the process.
That last point is important. As front-​​end designers, we tend to naturally distrust WYSIWYG tools that hide the code away from us. Indeed, most of us take pride in hand-​​coding our hex colors, font stacks, line-​​height settings and base line rhythms. Hell, some of us even choose our t-​​shirts color based on their RGB values (no pointing). SVG Edit encourages you to ‘get up close and personal’ with your SVG in the same way. That’s going to become increasingly useful the more you work with SVG.
That’s enough theory. Let’s run through the simplest of SVG examples.

Example 1: Simple linear gradients

1. Open SVG Edit in your browser (from the link the Demos section).
The blank SVG Edit
The blank SVG Edit Application
2. Before we do anything, try clicking that rather hideous ‘SVG’ button at the top left. A popup showing your blank SVG document will appear. You’ll notice the document size is set to 640px by 480px.Try changing the width to 480px and hit ‘Apply Changes’ to see your changes back in the Visual Editor view.
Nice. You just wrote some SVG.
3. Next select the rectangle tool from the left toolbar and drag out a box roughly the same size as your document. Your rectangle will likely have a stroke outline by default. At the bottom of your editor you’ll notice your fill and stroke controls. Delete the stroke by setting your stroke-​​width to 0px.
4. Switching back to the SVG code view, you’ll see atag has been added with a set of properties for color, stroke and size. The great thing about SVG is it’s generally very human-​​readable. Since we got rid of the stroke, you can delete the stroke-​​width and stroke color properties if you like. If your rectangle doesn’t quite fit your canvas, manually edit the width and height to match the document. It should look something like this:
1<svg width="640" height="480" xmlns="http://www.w3.org/2000/svg">
2  <!-- Created with SVG-edit - http://svg-edit.googlecode.com/ -->
3 <g> <title>Layer 1</title>
4   <rect fill="#ff0000" x="0" y="0" height="480" width="640" id="svg_1"/>
5 </g>
6</svg>
5. Cross back to Visual Editor view. With your new rectangle selected, click on the Fill color chip (near the bucket). A tabbed popup launches which lets you choose between solid color, linear and radial gradients. Let’s face it: solid SVG color is less useful than plain ol’ CSS here, but the other two tabs are very useful.
Try the linear gradient panel. You can choose your starting and ending colors with the color picker. The ‘x’ and ‘y’ settings control the direction of your gradient. The top-​​left corner is position 0:0 and the bottom-​​right corner is 1:1, so a begin-​​stop of 0:0 and an end-​​stop of 1:1 will give you a top-​​left to bottom-​​right diagonal gradient.
Changing your end stop to 0:1 will switch your gradient from horizontal to vertical. You’ll notice there is a slider along the bottom that lets you control the opacity of the entire object. Alternatively, you can set different transparency levels for each stop with the color picker. Try picking a starting and ending color and then have a peek at the SVG. It should look something like this.
1<svg width="640" height="480" xmlns="http://www.w3.org/2000/svg">
2 <!-- Created with SVG-edit - http://svg-edit.googlecode.com/ -->
3 <defs>
4  <linearGradient y2="1" x2="1" y1="0" x1="0" id="svg_2">
5   <stop stop-color="#ff0000" offset="0"/>
6   <stop stop-color="#ffff00" offset="1"/>
7  </linearGradient>
8 </defs>
9 <g>
10  <title>Layer 1</title>
11  <rect id="svg_1" height="480" width="640" y="0" x="0"  fill="url(#svg_2)"/>
12 </g>
13</svg>
That markup should feel familiar. You’ll see that the gradient is defined at the top between the tags and has a unique ID — “svg_​2”. The stops denote our starting and ending colors. Below that you’ll see the tag that is referencing the gradient via it’s ID (id=“svg_2”).
Let’s throw it at a browser.
Open the SVG code view, select the markup and copy it to your clipboard. Next open your favorite code editor, create a new document and paste your SVG in.
Save your document as ‘gradient.svg’ to a testing folder. Next create a blank HTML document and in the head, add the following code.
1<style> html { background: url(gradient.svg); background-size:100% 100%; height:100%; } </style>
Save the HTML document with your SVG and preview it in a browser. You should be looking at an elegant two color gradient that effortlessly scales to fill ANY current browser window.
But what about three or more color gradients?’ you say.
Easy. Switch back to ‘gradient.svg’ in your code editor. You’ll notice the stops in our linear gradient have offsets of 0 and 1 respectively. Add a new with an offset of 0.5 and any color you like, and slot it in between your other two stops like this:
1<stop stop-color="#ff0000" offset="0"/>
2<stop stop-color="#00ff00" offset=".5"/>
3<stop stop-color="#ffff00" offset="1"/>
Save and reload and you should now have a three color rainbow.
Ok, so you might be thinking ‘Fine, but we can do that with CSS3 gradients — what’s the big deal?’ This is very true, but the SVG does have some nice advantages.
  • Our CSS is so clean you could eat off it.
  • We can tweak and tune our gradient without leaving our code editor.
  • We now have a single set of numbers to manage rather than 4,5 or 6 syntaxes. (Even assuming we still might have to hack for older IE’s)
That’s nice’n’all what about an example that can’t be replicated with a pure CSS solution.

What about Radial Gradients?


SVG Edit — Radial gradient panel
Back to SVG Edit. Select your rectangle again, click on the fill-​​color chip, and switch to the radial gradient tab. You’ll see a control panel not unlike the linear gradient panel.
By default, gradients are centered on your shape (e.g. position x=0.5, y=0.5) but you’re free to drag and reposition the center point whenever you like.
The outer radius can be adjusted via the radius slider on the right. However I think the really nifty part is being able to have a separate center point and focal point.
Simply uncheck the ‘match center’ box and position the ‘F’ and ‘C’ icons wherever you like. As you can see in the screencap, this lets you stretch and contort your radial gradient in ways that are reminiscent of spotlights and shadow.
Even the hippest of new browsers can’t do that with pure CSS.
Billy Wilder's 'Sunset Boulevard' 1950 - http://en.wikipedia.org/wiki/Sunset_Boulevard
Great film directors like Billy Wilder and Alfred Hitchcock use light and shadow to tell their story.

Example 2 –Lighting Effects

Recently I watched the old classic Sunset Boulevard and it got me thinking about how brilliantly directors like Billy Wilder and Alfred Hitchcock used shadow and light to create drama. The shadows almost become another character in the story.
seamless-brick tile
Seamless tiles are great but they HAVE to be flat-​​lit to work
To me, this highlights a natural limitation with seamless tiling bitmaps on the web: By definition they need to be boringly, almost clinically flat-​​lit.
More precisely, if you want the edges to blend, you can’t make the tile lighter on one side than the other.
What if we were to combine the strengths SVG with tiling bitmaps to get the best of both worlds?
Let’s start with this basic seamless brick tile (left). Rendered by itself, it’s not particularly impressive. Sure, we could use the cicada principle to randomize the brick pattern, but I think it’s the flat, sterile lighting that really makes it dull. Let’s fix that.
Using SVG Edit, I’ve created a simple, midnight blue rectangle with a semi-​​transparent radial gradient center.
I’ve skewed the focal point toward the top left corner to give it a spotlight feel.
Radial gradients in SVG
Radial gradients in SVG
Copying the code into a new SVG file should look something like this:
1<svg width="640" height="480" xmlns="http://www.w3.org/2000/svg">
2 <!-- Created with SVG-edit - http://svg-edit.googlecode.com/ -->
3 <defs>
4  <radialGradient id="svg_3" cx="0.5" cy="0.5" r="0.5" fx="0.19" fy="0.19">
5   <stop offset="0" stop-color="#000000" stop-opacity="0"/>
6   <stop offset="1" stop-color="#120747"/>
7  </radialGradient>
8 </defs>
9 <g>
10  <title>Layer 1</title>
11  <rect fill="url(#svg_3)" x="0" y="0" height="480" width="640" id="svg_1" fill-opacity="0.72"/>
12 </g>
13</svg>
As you can see, it’s still very concise and easy to read.
Firstly we’ll set up a fallback background for older (mostly IE) browsers that don’t support multiple backgrounds. We’ll also set the height of the HTML element to 100% to ensure our image fills the browser.
1html { background:url(bricks3.jpg) ; height:100%; }
Next we’ll add our multi-​​image background. The brick tile is at the back (last) and our SVG overlays it.
1html {
2 background:url(bricks3.jpg) ;
3  background:url(spotlight.svg), url(bricks3.jpg) ;
4height:100%;
5}
Note I’m using the shorthand ‘background’ CSS property rather than ‘background-​​image’ as older IE’s seem to try to read multiple images as one long image when you use the more specific notation. Derp.
Obviously background images will tile by default. That’s perfect for our brick graphic but we want our SVG to conform to the browser window, so we’ll need to specify the background-​​size like this:
1html { background:url(bricks3.jpg) ;
2background:url(spotlight.svg), url(bricks3.jpg);
3background-size:100% 100%, auto; /* auto is tile */
4 height:100%;
5}
Hey teacher! Leave them kids alone.
Combining SVG with a simple tile.
Finally we add a dollop of web fonts and a pinch of CSS3 and we get something like this. You can poke at a live example here.
I think there are a number of points worth noting.
The first thing that strikes me is how much impact a very simple SVG can have on a flat-​​looking tile. The changing light means even tightly-​​repeating bricks seem far, far less noticeable than they were before.
Secondly, from a technical perspective, you’ll notice that the SVG scales to any browser window instantly and remains crystal clear regardless of screen resolution.
Support for SVG in CSS backgrounds is good in all current mainstream browsers.
  • Chrome (since May 2010)
  • Firefox (March 2011)
  • IE9 (March 2010 beta)
  • Safari (June 2010)
  • Opera (June 2008).
Older browsers will happily render the bricks but not the SVG — for this example, that’s not a bad result. I’m not going to go deeply into workarounds here, but you might consider fallbacks for Firefox 3.6 and IE8 — your call.

Example 3 — Vintage gallery


Use the polygon tool to create a rectangle with slightly bowed-​​out edges.
Time to get serious and set SVG a bigger challenge. So far our first two examples used just a single rectangle, but — unlike CSS3 gradients — there’s nothing stopping us from combining one, two or more shapes and gradients into a single more complex SVG.
For this last example we’re going to create a loose picture gallery with a vintage feel. The HTML for our gallery is super clean — an unordered list of Flickr images courtesy of the National Library or Ireland. I want to create an auto-​​resizing SVG frame for each image.
Jumping back into SVG Edit, I’m going to create this new graphic from two simpler shapes.
The first shape is created using the polygon tool. It’s essentially a rectangle with its edges bowed out slightly — almost as if the shape was over-​​inflated. This shape will be my shadow, so I want to remove the stroke and give it a semi-​​opaque, 30% black fill.
Happily, SVG also supports a handy set of built-​​in filters, and we can use one of these to give our shadow a gentle blurred edge.
Select the shape and you should see the blur filter controls appear at the top of the editor. I’ve set my blur to 6 to get the soft edge you see below.
The blur filter
Setting the blur filter to 6 gives us a soft shadow effect.
The second shape is just a rectangle. It has a light gray border and a linear gradient of whites and light grays gives it a glossy ‘photographic paper’ look.
Grab the code from the editor and paste it into a new SVG document. We now have a simple, scalable frame graphic that’s perfect for our needs.
Let’s look at the CSS.
1ul li { background: url(polaroid.svg);
2 -moz-background-size: 100% 100%;
3 background-size: 100% 100%;
4 display:inline-block;
5 padding:2% 2.5% 3% 2%;
6 list-style: none;
7  ... }
Photopaper SVG
Using simple SVG to mimic the look of glossy photographic paper
Firstly, we apply our new SVG to the background of the list item, and use background-size to make it match the size for each list-​​item.
I’m also using display:inline-block on the list-​​items to make the photos stack across the screen. This generally makes them stack more elegantly than floating each list-​​item.
We also need to add some padding to make sure the photo doesn’t totally obscure our frame.
The gallery
Our SVG adapts effortlessly to any image regardless of size or orientation.
Finally, I’m using a touch of CSS3 rotation to give the gallery a loose, scattered feel. I’m not going to break down the code for that here, but Frank Fuchs developed this idea in back in May.
Here’s the result.

What do you think?

For me there’s a lot to like about this approach. The strengths of SVG seem clear. Each occurrence of the SVG scales, rotates and reshapes itself without any distortion or loss of image quality. The file size is an order of magnitude smaller — measured in bytes, not kilobytes.
It’s also nice to be able to adjust the blur or opacity with a few lazy keystrokes.
Sure, there are some drawbacks, but not too many.
Currently IE9 supports SVG nicely, but unfortunately not the SVG filters, so our shadow edge appears sharp.
Unsurprisingly, earlier IE’s ignore SVG, but it’s actually their lack of support for the background-size CSS3 property that is the real showstopper here. Scaling SVG is where the real power is. You might be able to concoct an IE8 fallback with Microsoft filters. Good luck with that. Either way, CSS3 gradients certainly aren’t your saviour.
The only other significant non-​​SVG supporting browser seems to any Firefox before version 4, and with Mozilla’s move to automatic upgrades, this becomes less of an issue every day. Bizarrely, Firefox has actually supported SVG beautifully since 2008 — just not in CSS backgrounds where they’re so useful. Again I say, derp.
Anyway, I hope this article has sparked a few ideas on how you might weave some sexy vectors into your work. If not, it should’ve at least been a good SVG primer.
Bring on the Era of SVG!

Friday, January 20, 2012

Focus on Typography: Space

Typography

There are a number of ways to add breathing space to your type. White space or negative space helps to focus attention on the words, while line spacing and letter spacing can help to make your text more readable. Let’s take a look at each of these.

White Space

White space is the space between graphics, text elements and between columns on a page. It’s one of the most important principles in layout and design. It’s essential not to think of the space around your type (or graphics) as just blank space. The space is actually a design element all of its own and is used to create a classic or elegant appearance, and as I mentioned previously, this is a decision that you as a designer needs to make. Don’t just let your page fall together.
A web page with very little white space can appear too busy and may be difficult to read and focus on. However some simple ways to increase the amount of white space around your type, are to use
  • bulleted items
  • headlines and sub headlines
  • padding around images
  • margins

Line Spacing (Leading)

By changing the amount of space between lines, you increase the readability of a passage of text. If the line spacing is too tight, it becomes difficult to separate the words and is tiring on the eye. In the example below, you can see a paragraph of 12 pt text. As the line spacing in increased, the text becomes easier to read, and a nice airy effect is achieved.
image0025
As a rule of thumb, you may need to increase line spacing:
  • If the typeface you choose has a large x-​​height, which many san-​​serif fonts do
  • If you are reversing the type out, for example light blue font on a dark navy background.
You may need to decrease line spacing if your typeface has a very small x-​​height, as this will naturally create more space between the lines (the x-​​height is the distance between the baseline of a line of type and the tops of the main body of lower case letters).
From the Wikimedia Commons
From the Wikimedia Commons

Letter Spacing (Tracking)

Some typefaces have connecting letters that are designed to sit very close together. You can set the space between letters (known traditionally as tracking) to make your text more readable. If you’re using the “web safe” fonts, Georgia, Verdana, Arial and so on, for your body text, you can usually leave the letter spacing as is. However you might like to change the spacing between letters for headings and sub-​​headings to achieve that airy feel and also stretch the length of your text without distortion.
image0044
There are no hard and fast rules when it comes to letter spacing. You really need to trust your eyes and own judgment. If the letters look too close together, they most likely are.

Kerning

Kerning is sometimes confused with tracking. It refers to the adjustment of spacing of two particular characters to correct visually uneven spacing. This is used mainly in print design.
Let’s take a look at some examples of web sites which make good use of space to create clean, legible designs.
Matt Lawrence’s blog is mainly text based, yet still achieves a sense of airy-​​ness by using large heading and sub-​​headings and body text with large line spacing. The background image on the right hand side blends with the page and adds to the sense of spaciousness.
image0064
Simpleart leaves plenty of room around the main image on the page and separates three columns of text with gutters. A large margin underneath the three columns gives a nice impression (in this case) of floor space.
image0083
Daily Bath & Body is simple, clean and elegant. There’s lots of space around the logo in the header and between the main image and the headline “Indulge daily”. The headline itself uses wide tracking, while the body text is easy to read with good line spacing.
image0103
A simple measure is a slightly busier site than the others shown here, but it does a nice job putting a fair amount of information onto one page whilst keeping it legible and attractive. Each section of the page is allowed to breathe, either by using large margins or by using simple graphics such as the pencils in the navigation area.
image0122
Finally, just to show that white space does not have to be white, Nine Lion combines a colourful graphic with large headline type. The small body text is cleanly separated from the graphic using a margin.
image0142
I’d love to see some of your links to pages you like using white space as a design element. Share them in the comments!