Wednesday, November 9, 2011

Position Text Labels: Forms Using CSS

In this post, I’ll explain three common approaches to positioning text labels on web forms using CSS:

  1. top-​​positioned text labels
  2. left-​​aligned text labels
  3. right-​​aligned text labels

Using Top-​​positioned Text Labels

Positioning labels at the top of their form elements is probably the easiest layout to achieve, as we only need to tell the label to take up the entire width of its parent element.

As our form elements/​labels are inside ordered list items (which are block elements), each pair will naturally fall onto a new line, as you can see from Figure 9. All we have to do is get the form elements and labels onto different lines.

This exercise is easily completed by turning the label elements into block elements, so that they’ll occupy an entire line:

label {
display: block;
}

It’s a simple change, but one which makes the form much neater, as shown below.

neater

Left-​​aligning Text Labels

When we create a column of text labels to the left of the form elements, we’ll have to do a little bit more work than just to position them at the top. Once we begin floating elements, all hell breaks loose!

In order to position the labels next to the form elements, we float the label elements to the left and give them an explicit width:

label {
float: left;
width: 10em;
margin-right: 1em;
}

We also apply a little bit of margin-right to each label, so that the text of the label can never push right up next to the form element. We must define an explicit width on the floated element so that all the form elements will line up in a neat vertical column. The exact width we apply will depend upon the length of the form labels. If possible, the longest form label should be accommodated without wrapping, but there shouldn’t be such a large gap that the smallest label looks like it’s unconnected to its form element. In the latter scenario, it is okay to have a label width that is smaller than the longest label, because the text will wrap naturally anyway, as you can see below.

wrapped-label

Once we float the label, however, we run into a problem with its containing list item — the list item will not expand to match the height of the floated element. This problem is highly visible in the image below, where we’ve applied a background-color to the list item.

wrapped-label-floated

One markup-​​free solution to ensuring a parent contains any of its floated children is to also float the parent, so that’s what we’ll do:

left-aligned-labels.css (excerpt)
fieldset li {
float: left;
clear: left;
width: 100%;
padding-bottom: 1em;
}

If the list item is floated, it’ll contain all of its floated children, but its width must then be set to 100%, because floated elements try to contract to the smallest width possible. Setting the width of the list item to 100% means that it’ll still behave as if it were an unfloated block element. We also throw a clear :left property declaration in there to make sure that we won’t find any unwanted floating of list items around form elements. clear: left means that the list item will always appear beneath any prior left-​​floated elements instead of beside them.

However, once we float the list item, we find the same unwanted behavior on the fieldset — it won’t expand to encompass the floated list items. So, we have to float the fieldset. This is the main reason that we removed the padding from fieldset earlier — when we set its width to 100%, any padding will throw out our dimensions:

left-aligned-labels.css (excerpt)
fieldset {
float: left;
clear: left;
width: 100%;
margin: 0 0 1.5em 0;
padding: 0;
}

Where will this float madness end? Remain calm. It ends right here, with the submit fieldset. Since it’s the last fieldset in the form, and because it doesn’t need as much special CSS styling as the other fieldsets, we can turn off that floating behavior for good:

left-aligned-labels.css (excerpt)
fieldset.submit {
float: none;
width: auto;
border: 0 none #FFF;
padding-left: 12em;
}

By turning off floating and setting the width back to auto, the final submit fieldset becomes a normal block element that clears all the other floats. This means the form will grow to encompass all the fieldset elements, and we’re back in the normal flow of the document.

None of the elements in the submit fieldset are floated, but we want the button to line up with all of the other form elements. To achieve this outcome, we apply padding to the fieldset itself, and this action pushes the submit button across to line up with all the text fields. It’s best to have the button line up with the form elements, because it forms a direct linear path that the user’s eye can follow when he or she is completing the form.

After all that floating, we now have the layout shown below — a form with a column for the form labels and a column for the form elements.

left-aligned

Right-​​aligning Text Labels

With all that difficult floating safely out of the way, aligning the input labels to the right is a breeze; simply set the text alignment on the label elements to achieve a form that looks like the image below:

right-aligned-labels.css (excerpt)
label {
float: left;
width: 10em;
margin-right: 1em;
text-align: right;
}

right-aligned

And we’re done! Now you can take your pick of whichever form layout best fits your pages, all by changing a little CSS!

Which option do you prefer, and why? Let us know in the comments.

Thursday, October 13, 2011

The Design Process

If you were to ask me to design a bridge, I’d have a lot of questions. Is it going to span the ditch in your front yard or Lake Pontchartrain? Can I use concrete and steel, or does the entire creation need to be constructed with toothpicks? I certainly would stop short of taking your business card at a bar and promising you the Golden Gate before knowing more about who you are and why you think you need it. On the other hand, if you commissioned me to create a sculpture for your garden, I’d probably feel a lot more comfortable aiming to create a unique and artistic piece.

The process of designing a website falls somewhere between bridge building and sculpture. Yes, we want to create an individual site that’s aesthetically pleasing, but our highest priority should be to meet the needs of our client. These needs may be lofty and elaborate, or they might just be about making information available. If we fail to listen carefully, though, the entire project will come falling down, along with our reputation. The technical details of developing, hosting, and maintaining a website or application can be, well, technical. The process of creating a design comp, however, can be boiled down to just three key tasks: discovery, exploration, and implementation.

What’s a Comp? — The word comp is an abbreviation of the phrase “comprehensive dummy,” a term that comes from the print design world. It’s a complete simulation of a printed layout that’s created before the layout goes to press. In translating this term to web design, a comp is an image of a layout that’s created before we begin to prototype the design in HTML. Think of it as a mock-​​up.

Discovery


The discovery component of the design process is about meeting the clients and learning what they do. This may feel a little undesign-​​like, but gathering information about who your clients are and how they run their business is vital in coming up with an appropriate and effective design.

Before you schedule your first meeting with a client, spend some time researching their business. If they’ve asked you to design a website, they may currently be without one, but google them anyway. If you’re unable to find any information about their business specifically, try to learn as much as you can about their industry before the first meeting. Whenever possible, the first meeting with a client should be conducted in person. Sometimes, distance will dictate that the meeting has to occur over the phone; but if the client is in town, schedule a time to meet face-​​to-​​face.

Keep in mind that this meeting is less about impressing the client, selling yourself, or selling a website than about communication, and establishing just what it is the client wants. Try to listen more than you speak, and bring a pad of paper on which you can make notes. If you bring a laptop or tablet with you to talk about website examples, limit the time spent using it. Computers have screens, and people tend to stare at them; hence, if the client isn’t staring at the screen the whole time, you will be as you write your notes. If you must drag some technology into the meeting, use an app like iTalk Recorder for iPhone or Voice Recorder for Android devices to record your conversation — with the client’s permission, of course. In my experience, though, a pad of paper is less threatening and far less distracting to the often not-​​so-​​tech-​​savvy client.

Oh, and remember: client meetings don’t have to take place in an office. Even when I worked for a company in a big office, I had some of my most productive client meetings at a cafĂ© or over lunch. The feasibility of this approach depends on the client. If your contact seems to be more the formal business type, don’t suggest it; in many cases, though, it’s a good way to make a business meeting more personal.

Here are a few of the questions I like to ask in initial client meetings, even if I’ve already established the answer myself via a search engine:

What does the company do?
What is your role in the company?
Does the company have an existing logo or brand? What is your goal in developing a website?
What information do you wish to provide online?
Who comprises your target audience? Do its members share any common demographics, like age, sex, or a physical location?
Who are your competitors and do they have websites?
Do you have examples of websites you like or dislike?
What kind of timeline do you have for the project and what is the budget?
If the project is to redesign an existing website, I also like to ask:

What are your visitors usually looking for when they come to your site?
What are the problems with your current design?
What do you hope to achieve with a redesign?
Are there any elements of the current site that you want to keep?
How do you think your visitors will react to a new site design?
Sometimes I start off with more questions than those listed here. Use your imagination and try to come up with some creative queries that will really give you more insight into the client’s organization. If you’re a programmer, avoid the tech jargon. If you’re a designer, avoid talking specifically about design. Sure, that may be all you’re thinking about, but semantic markup, fluid and fixed layouts, and color schemes will likely mean very little to the client. Worse still, these types of conversations can bring misguided design opinions your way before you have a chance to start thinking about the design yourself.

Exploration


The next stage of the design process is to take the information you’ve learned from the client back to your laboratory for analysis, dissection, and experimentation. You want to gain a grasp on all the information, products, and services they have to offer, and play around with how these should be arranged. Put yourself in the shoes of the website visitors and ask yourself what these people are looking for. If you’re thinking about buying a product, what do you need to know before you buy? If you’re signing up for a service, where would you learn about the different offerings and which level of service you need? What is the clearest title possible for page x, and how many steps does it take to reach page y?

In the world of web design, this is the beginning of a process known as information architecture, or IA for short. For expansive websites and complex web applications, information architecture is a career unto itself, but the guiding principles of this field can provide a solid foundation for even the smallest websites. For the exploration stage of our process, we want to focus on organizing the content and flow of the website into a structure we can design around.

Two of the most essential tools for this task are scrap paper (or a whiteboard if you have one) and a big pad of sticky notes. Make a list of all the bits and pieces of the website and start arranging them into groups and subgroups. These are likely to move around quite a bit, and that’s where the sticky notes come in handy. If you make a note for every section, subsection, and page of the site, you can arrange them on a wall in the order they’ll appear in your site’s navigation. You’ll want to avoid overwhelming the visitors with too many options, but you also don’t want to bury information too deeply within the site; that is, too many clicks away from the home page. There are no hard-​​and-​​fast rules for this activity; just make the information as obvious and as easy to reach as possible.

Implementation


Now that we’ve thought through how we want to organize the information we’re working with, the implementation step of our design process begins with creating a layout. Regardless of the project, try to avoid being caught up in the technology associated with building websites — at least at first. At this point, it’s unimportant whether the site is going to comprise straight HTML, a template for a content management system, or a Ruby on Rails application; the bottom line is that we have an interface to design and a blank sheet of paper. “Paper?” That’s right, paper. Did you really think I was going to let you go back to your precious computer already? No way. Here’s why: it’s easy to lose focus on the design if you start thinking about the layout in front of a computer. If you start out on paper, you can ignore the technical limitations of browsers and CSS, and focus on how you want the final product to look. Now you might think that all good designers carry around fancy hard-​​bound sketchbooks in which they use expensive markers and paintbrushes to design Da Vinci-​​esque renderings of potential web page layouts. For my part, I use a 79-​​cent spiral-​​bound notebook and any writing instrument I can find on my desk that still works.

Fig. 1, One of many sketchbooks available for layout sketching (this one is from webdesign​-sketchbook​.com). Plain paper works perfectly fine too.

I start out by sketching a few possible layouts. After I’ve produced a few, I decide on one I like, jump into Photoshop, and use the rectangle tool to block out the areas I’ve marked down on my paper. Once I’ve defined my layout, I experiment with foreground and background colors until I have a solid color scheme. I continue twiddling the Photoshop knobs and pushing around pixels until, finally, magically, I have a comp to show the client.

Simple, right? Okay, perhaps I skipped a few steps in that brief description. Honestly, though, when people ask me how I do what I do, they usually receive a similar explanation. The truth is that there are bundles of now-​​subconscious information from my past experience and those old college design and art classes that have helped me to define my own design process.

Learning how to design is like learning how to program. Some people have a bit of a knack for it, but anyone can learn. Just as there’s good code and ugly code, there is good design and ugly design. Learning some of the principles and conventions associated with design will help you to understand the difference between the good, the bad, and the ugly, moving you towards establishing your own design process.

The Principles of Beautiful Web Design
This article is from Jason Beaird’s The Principles of Beautiful Web Design book (the second edition of which is out now). This is in fact the start of the first chapter, which we’ll continue shortly as it examines layout and composition.

Wednesday, September 28, 2011

5 of the Best CSS3 Font Tools

Fonts have made the most dramatic visual impact on the web since graphic support was added to browsers. A few years ago, it would be impossible to find a website using anything other than Arial, Verdana, Tahoma, Times New Roman or Georgia (or, heaven forbid, Comic Sans).

While there’s nothing wrong with standard font stacks, they can become a little monotonous. Internet Explorer has supported web fonts for more than a decade but it’s taken competing browsers a little longer to catch up. Licensing is still an issue — you can’t use any commercial font — but you should be able to find one which permits web usage or is similar to your corporate style.

However, with great choice comes great responsibility. Fortunately, there are several online tools which can help you locate and use CSS3 fonts…

1. Google Web Fonts

If you’ve not visited Google Web Fonts, where have you been? Google provides more than 200 fonts and the review tools are amazing.

You can download any font file but the tool also allows developers to import via an HTML link tag, CSS @import declaration or JavaScript snippet. Google highlights potential download speed issues but, since you can use their CDN, there’s little need to worry about hosting.

Did I mention that it’s all free? Google Web Fonts will satisfy the most demanding designer.

2. Font Squirrel

Font Squirrel is excellent — especially if you’re nervous about handing all font responsibilities to Google. The site provides hundreds of quality fonts which are free for commercial use. You can download TTF, EOT, WOFF and SVG font files as well as @font-face kits which provide bulletproof code and samples which work in all major browsers.

Still not satisfied? Upload a licensed font file to the @font-face generator; it’ll be handed back in multiple web formats with sample CSS code. How much would you pay for that service? It won’t cost you a penny.

3. Font Dragr

Just because a font can be used on the web, it doesn’t mean it should. Fonts such as Helvetica and Georgia were designed to work well on-screen but not all fonts are created equal — especially at small sizes.

Therefore, you should test that medieval script font before adding it to your page. Font Dragr makes it easy — drag a font file to the page and the text will magically change.

4. FFFFallback

Not all users will be able to experience your beautiful fonts. Those with older browsers may not support web fonts and there are strong reasons why you should avoid them on mobile devices.

Fortunately, CSS supports fallback font stacks: typefaces are tried in order until one is found. However, fonts have different sizes and spacing so a particular fallback could break your design.

FFFFallback.com is a fantastic bookmarklet which shows how your site looks with different fonts. It overlays a duplicate of your page on top of the original so you can instantly spot formatting issues.

5. WhatFont

If you’ve spotted a lovely font in the wild, WhatFont is another bookmarklet which will help you identify the typeface and size. Launch the tool then hover over or click any element. What could be simpler?

Bonus Font Tools

Hungry for more? If you have the time and inclination, FontStruct and Bitfontmaker allow you to create your own fonts. Both offer a range of free fonts created by talented artists.

Font Matrix and Common fonts list the major typefaces and alternative fallbacks on Windows and Mac. Always check these first — there’s no need to use a web font if it’s universally available on most platforms.

Do you know of any other great font tools?

Friday, July 8, 2011

CSS2.1 & CSS3 Color Module: Official W3C Recommendations

Hats off to the CSS Working Group, it must have been a busy few weeks. Not only have they released several updated specifications, most notably the long awaited publication of the CSS2.1 specification as an official W3C recommendation, but also introduced a major redesign of their home page.

The release of CSS2.1 as an official recommendation also paved the way for the CSS3 Color module to advance to the recommendation stage, becoming the first CSS3 specification to be released as an official W3C Recommendation.

CSS2.1 Published as W3C Recommendation

The first working draft of the CSS2.1 specification was published almost ten years ago in August 2002, with the aim of correcting errors and improving interoperability from the original CSS2 specification (first published as a W3C recommendation in May 1998).

Work on the specification has stepped up in recent months, with a number of updated drafts released, as the working group pushed to complete the specification, a goal which was finally achieved on 7th June 2011.

Here’s an extract from the W3C press release which announced the publication:
W3C, the standards body for the suite of technologies that together provide an Open Web Platform for application development, today announced new levels of support for Cascading Style Sheets (CSS), the language for adding style to Web content. W3C released an update to the core CSS standard (2.1) to reflect the current state of support for CSS features, and to serve as the stable foundation for future extensions.
CSS has been in widespread use as an Open Web technology for more than a decade, but it took many years for implementations and the specification to converge. The collective efforts of the CSS Working Group, implementers, contributors to the CSS Test Suite, and the W3C CSS community have made interoperable CSS a reality for the Open Web. More than 9000 CSS tests have made it easier for designers to create style sheets that work across browsers, and across devices.
“This publication provides me with an opportunity to congratulate and thank the CSS Working Group, and all of the developers that have made CSS a success,” said Bert Bos, co-inventor of CSS and Editor of CSS 2.1. “This publication crowns a long effort to achieve very broad interoperability. Now we can turn our attention to the cool features we’ve been itching to bring to the Web.”
CSS interoperability plays an important role in the rapid adoption of W3C’s Open Web Platform, which also includes HTML5, SVG, WOFF, APIs for geolocation and offline storage, real-time communications, and a host of other technologies for building rich, interactive applications.
With CSS2.1 now an official recommendation, the way should be clear for several of the newer CSS3 modules to also progress to the recommendation stage. Not only will the working group have more time for the development of CSS3, but as the individual CSS3 specifications ‘build on’ the CSS2.1 specification, none have been able to move on to the recommendation stage until CSS2.1 was first released as a recommendation.

In fact it didn’t take long for the first of those modules to be released, CSS3 Color was released as a W3C recommendation the same day.

Peter Linss, co-chair of the CSS Working Group, made the following statement in the W3C press release:
Now that we have published CSS 2.1 as a Recommendation, the Working Group can focus its efforts on rapidly advancing CSS with new modules for improved layout controls, new visual effects, broader international support, and more.
You can view the full CSS2.1 specification here.

CSS3 Color Module Published as W3C Recommendation

The CSS3 Color module introduces several new, more convenient ways to specify colors and transparency for text, borders, and backgrounds, including:
The publication of the CSS3 Color module as an official W3C recommendation on 7th June 2011 marks the end of a long development process, the module was first released as a working draft in June 1999, although not all of the original features have made it into the final specification.
You can find the full specification here.

The New CSS Working Group Home Page

The CSS Working Group’s home page has undergone a major redesign, unveiled to coincide with the publication of CSS2.1. The new design, pictured below, was the brainchild of Divya Manian (although the original design has been reworked by the W3C’s Bert Bos) and brings a much needed fresh new look to the working groups web pages while bringing the design more in line with the new look of the main W3C site, launched in March 2009.


CSS Working Group 2011 Re-design

You can read more about the redesign on the CSS Working Group blog, and you can see the new site in all it’s glory here.

Sunday, March 13, 2011

CSS Sprites: What They Are, Why They’re Cool, & How To Use Them

You've heard of them, but do you really understand them? The name might be a little misleading, because sprites aren't little images like you might be picturing, a sprite is actually one big image. Have you ever seen the CSS technique where the "on" and "off" states of a button are contained within the same image and are activated by shifting the background-position?


Here is an example of that on CSS-Tricks.

Think of CSS Sprites as an extension of that technique. The difference is that instead of just two or three images being combined into one, you can combine an unlimited number of images into one. The origin of the term "sprites" comes from old school computer graphics and the video game industry. The idea was that the computer could fetch a graphic into memory, and then only display parts of that image at a time, which was faster than having to continually fetch new images. The sprite was the big combined graphic. CSS Sprites is pretty much the exact same theory: get the image once, shift it around and only display parts of it, saves the overhead of having to fetch multiple images.

Why combine all those images? Isn't it quicker to have smaller images?

Nope, it's not. Back in the day, everybody and their brothers were "slicing" images to make pages load faster. All that technique did was fool the eye to make it look like the page was loading faster by loading bits and pieces all over at once. Each one of those images is a separate HTTP-Request, and the more of those, the less efficient your page is.
Let's look at a quote from the article "Performance Research, Part 1: What the 80/20 Rule Tells Us about Reducing HTTP Requests" by Tenni Theurer on the Yahoo! User Interface Blog.
Table 1 shows popular web sites spending between 5% and 38% of the time downloading the HTML document. The other 62% to 95% of the time is spent making HTTP requests to fetch all the components in that HTML document (i.e. images, scripts, and stylesheets). The impact of having many components in the page is exacerbated by the fact that browsers download only two or four components in parallel per hostname, depending on the HTTP version of the response and the user's browser. Our experience shows that reducing the number of HTTP requests has the biggest impact on reducing response time and is often the easiest performance improvement to make.
Table 1. Time spent loading popular web sites
Time Retrieving HTMLTime Elsewhere
Yahoo!10%90%
Google25%75%
MySpace9%91%
MSN5%95%
ebay5%95%
Amazon38%62%
YouTube9%91%
CNN15%85%
Every single image, whether it's an <img> tag or an background-image from your CSS is a separate HTTP-Request, so you can imagine how quickly those requests can wrack up.

OK. So how is it done?

I thought you would never ask. Let's start by showing the BEFORE example. Notice in the CSS below how the anchor tag does not get a background-image, but each individual class does.
#nav li a {background:none no-repeat left center}
#nav li a.item1 {background-image:url('../img/image1.gif')}
#nav li a:hover.item1 {background-image:url('../img/image1_over.gif')}
#nav li a.item2 {background-image:url('../img/image2.gif')}
#nav li a:hover.item2 {background-image:url('../img/image2_over.gif')}
...
example1before.png
Using CSS Sprites, we can really lighten this example up. Instead of having ten separate images for the buttons (five default states and five rollover states), we can literally combine all of them into one big long image. I won't go into great detail about how this is done, I'll just give you a basic walkthrough. Create a new image that is as wide as your widest image and and as tall as the combined height of all your images plus X pixels, where X is the number of images you have. Now place you images into this new image, left aligned, one on top of the other with one pixel of white space in between.
Now check out the AFTER example. Notice in the CSS that there is a single background-image applied to the anchor element itself, and the unique classes merely shift the background position with negative Y coordinates.
#nav li a {background-image:url('../img/image_nav.gif')}
#nav li a.item1 {background-position:0px 0px}
#nav li a:hover.item1 {background-position:0px -72px}
#nav li a.item2 {background-position:0px -143px;}
#nav li a:hover.item2 {background-position:0px -215px;}
...
example1after.png
We were able to reduce the number of HTTP-Requests by 9 and the total file size of the image(s) by 6.5 KB. That's a pretty huge improvement for such a little example. Imagine what you could do on a full website.

Ugh. That sounds like a lot of work.

Just remember what Chuck Norris once said: "All great things require great dedication." Actually I'm not sure if he said that or not, but it's good advice anyway. But fortunately for you, there is a web service which makes creating and implementing sprites really easy. There are actually lots of different services designed to help you making sprites easier, but I think hands down, the best one is SpriteMe.

Using SpriteMe

SpriteMe is a bookmarklet. So after you've put it up in your bookmarks bar, just go to any website and click it. It will open up an overlay over the right side of your screen.

The top white box is a list of all the background graphics on your page that it feels like could be combined into a sprite. The boxes below are graphics that probably won't work for sprites (and it will tell you why). If you think differently, you can drag the links in and out of their boxes. Once you have all the images to be combined in that top box, just click the "Make Sprite" button. It will combine them all into a single image (a CSS Sprite!) that you can view right there.

On the current design of this site, this is a (scaled down) version of the end result. (Or see the real thing)

Recently, SpriteMe also made it available to "export" the CSS. Click that button and you'll see some code like this:
A id=home-link
{
  background-image: url(http://css-tricks.com/wp-content/themes/CSS-Tricks-4/images/logo.png)
  background-image: url(http://www.jaredhirsch.com/coolrunnings/public_images/3deb155981/spriteme1.png);
  background-position: -10px -10px;
}
A
{
  background-image: url(http://css-tricks.com/wp-content/themes/CSS-Tricks-4/images/nav.png)
  background-image: url(http://www.jaredhirsch.com/coolrunnings/public_images/3deb155981/spriteme1.png);
  background-position: -10px -56px;
}
The crossed out code is what used to be in your CSS, and what the replacement should be is below it.

What can't sprites do?

They don't do repeating graphics*. Sprites are for graphics that are just single blocks. Icons are a great example candidate for CSS sprites.
*OK, they kinda can do repeating, but it's a little trickier and can only work one-dimensionally (x or y).

Either start from the beginning with Sprites, or do it all at the end

The using of sprites is a no-brainer. You should do it. But what is the workflow when creating a new design? I think you should go one of two routes. The first is to know that you are going with sprites from the get-go and built it as you go along. That means have a Photoshop file open, start from the upper left, and drop stuff in as you need it. If you have another icon (or whatever) that makes sense to put in a sprite drop it in there and resave it.
The other way, which perhaps makes even more sense, is to develop without thinking about sprites at all. Make everything separate individual graphics. Then when the site is "complete" (or at least, ready for release, we all know sites are never "complete"), then use SpriteMe and do the spriting then.

Wednesday, March 9, 2011

CSS letter-​​spacing and Where to Use It

The letter-spacing property is an often forgotten CSS option which allows us to change the tracking between letters. Altering the default spacing is seldom needed as text fonts are designed and optimized for legibility and readability exactly for setting larger blocks of text. However, the granular control of this horizontal spacing between letters can be useful in particular circumstances.

The letter-​​spacing property

The letter-spacing CSS property has been available since CSS1 and enjoys thankfully an almost perfect support from all the main browsers. To employ the property we simply call it and give it a length value:
h1 { letter-spacing: −1px; }
This length value is inserted between characters in addition to the default spacing (called the kerning). Quick note: while negative values are permitted percentages aren’t (for good reason). If you want to ensure your tracking remains relative to the font size (in case it is decreased or increased) set your values in ems.
The property also accepts the descriptive values “normal” and “inherit”, but the latter IE has some problems with.
Tracking also interacts with text justification, which is computed by the user agent (e.g browser) and font rendering engine. When text is justified (e.g. text-align: justify;) the browser will not alter the tracking unless letter-spacing: normal; is set. Remember however that “normal” and a value of “0” is not equivalent in this regard, so if you want to reset inherited letter-​​spacing and ensure it behaves accordingly be sure to set it back to “normal”.
There is another CSS property similar to letter-​​spacing provides control to word-spacing. Use case: just about never.

Use Cases for letter-spacing

Increasing Legibility

First and foremost letter-spacing allows us to insert additional space between text, which can aid in providing better legibility. The two prime examples for this are setting acronyms and strings of lining numbers or figures.

Spacing Acronyms

Acronyms being chiefly a string of uppercase characters are harder to discern than their lowercase equivalents due to the uniformity in height and the baseline the uppercase letters sit on (see my previous post, Top 10 Dos and Don’ts of Web Typography: #6).
A common technique to avoid causing acronyms in a block of text to spring out and act as eyesore is to set them in small-​​capitals (font-variant: small-caps;). By doing so the height of the now “small” capitals aligns to the x-​​height as sits nested better in the text. However as now smaller, the small-​​capitals can become somewhat illegible sitting as close as they are to one another by default.
Here we can call upon tracking by adding a few additional units of spacing between the glyphs. For example:
acronym, .caps, abbr {    text-transform: lowercase;    font-variant: small-caps;    font-style: normal;    letter-spacing: 1px;    }
A paragraph with full-cap acronyms with default tracking.
Same paragraph as above with acronyms set in small-capitals and with slightly enlarged tracking.

Spacing Lining Figures

A second example is additional spacing given occasionally to lining figures, that is figures (i.e. numbers) which are uniform in height and don’t extend beyond the x-​​height or drop below the baseline like text or “old-​​style” figures.
The same principles from setting acronyms apply. Although not chiefly done for figures that reside in body copy (because ideally you’d use text figures) lining figures are commonly used in tabular work: e.g. financial reports. See below for an example where lining figures are intermixed with full-​​caps in the form of ID numbers:
An example of ID numbers composed of lining figures and full-caps tracked loosely.

For Aesthetics

Tracking can be employed for aesthetics too. One particular popular trend is to negatively space Helvetica (or Arial):
An example of both normal and negative tracking applied to Helvetica Bold
Note: be very careful when applying negative spacing as not to make the text illegible and harder to read. The above styling would become difficult to read if set at a smaller size so reserve negative spacing for special occasions and only at ample sizes.

Closing

Tracking is just another tool that allows designers and typographers to fine-​​tune the horizontal spacing of text. Its application should by and large be to increase legibility. Be wary in applying tracking for purely stylistic reasons: don’t alter integral spacings without cause.
by Design Festival

Wednesday, February 9, 2011

CSS: Clearing Floats with Overflow

One of the common problems we face when coding with float based layouts is that the wrapper container doesn’t expand to the height of the child floating elements. The typical solution to fix this is by adding an element with clear float after the floating elements or adding a clearfix to the wrapper. But did you know you can also use the overflow property to fix this problem? It’s not a new CSS trick either. It’s been documented before long long ago. Today I would like to revisit the topic along with a few tips.

View Demo Clear Floats

Demo 1:

The demo below shows the issue with floating child elements where the parent container doesn’t expand. To fix this problem, you can simply add the CSS property overflow:auto (or overflow:hidden) to the wrapper container. This is perhaps the simplest way to clear floats.

overflow auto

.container {
overflow: auto;
}

Demo 2:

Overflow:auto can also be used to prevent content from wrapping around the floated element. Let’s say you are designing a comment list. You would, most likely, have an avatar floating on the left and the comment to the right. To prevent the comment from wrapping around the avatar just add overflow:hidden to the comment container. The advantage of using overflow here is that I don’t have to set a float or width to the comment container. The container automatically aligns after the floated avatar image.

overflow auto

.image {
float: left;
}


.text {
overflow: hidden;
}

Drawbacks (see demo)

Although it is a nice trick to clear floats there are some drawbacks:

  • Using overflow:auto will cause a scrollbar if your content is extending the boundary of the container. For example, if you have a long unbreaking text (ie. long url text) or a large image that is bigger then the container a scrollbar will show.
  • To avoid a scrollbar from showing you should use overflow:hidden. Again, there is a drawback to this method as well. Using overflow:hidden will hide any content that exceeds the boundry of the container.

Word-wrap

To solve the long unbreaking text issue, simply add word-wrap:break-word to the container and it will force the text to wrap onto a new line.

.container img {
max-width: 100%;
height: auto;
}

Max-width

To prevent large images from extending the boundar, add max-width:100% and it will resize the image to the max width of the container.

.container {
word-wrap: break-word;
}

By Web Designer Wall