Publishers of technology books, eBooks, and videos for creative people

Home > Articles > Design > Voices That Matter

Designing with Progressive Enhancement: Applying Styles Effectively

  • Print
  • + Share This
In this chapter, learn the best practices for applying CSS with progressive enhancement, including a review of how to divide styles into basic and enhanced style sheets, and techniques to improve the accessibility, usability, and performance of styles.
This chapter is from the book

Cascading Style Sheets (CSS) define the visual styling of a page and allow for a clean separation of presentation from content. A range of advanced typography, visual effects, and sophisticated layouts are all possible with CSS. Applied correctly, these styles can have a huge impact on both the aesthetic appeal and usability of a site.

One of the basic principles of progressive enhancement, and of modern coding practices in general, is to separate presentation from the markup by grouping all style rules into one or more external style sheets. While writing all styles into an external style sheet is an essential best practice for progressive enhancement, on its own this practice does not guarantee a satisfying experience for all users. Every browser will attempt to render any style sheet referenced on the page, regardless of whether or not it properly supports the CSS properties specified. When rendered in capable devices, a style sheet can make a page easier to read or a workflow simpler to follow, but when rendered in browsers that only partially support CSS, it can make the experience confusing or unusable.

When we develop with progressive enhancement, we take the basic principle of separating presentation a step further: we separate styles into "safe" styles that can be served with the foundation markup to all browsers (including those that only partially support CSS), and layer on more complex style enhancements and CSS techniques only after we've tested the browser with our EnhanceJS testing suite and determined that a browser is capable of rendering them properly.

In this chapter, we'll review the best practices we commonly employ when applying CSS with progressive enhancement, including a review of how to divide styles into basic and enhanced style sheets, and highlight techniques to improve the accessibility, usability, and performance of styles.

Applying CSS to the page

There are a number of CSS best practices that apply globally to both basic and enhanced experiences. It's essential to structure styles in external style sheets so they may be logically applied based on browser capabilities, to reference them properly, to target intelligently when appropriate, and to use meaningful naming conventions that characterize styles by their purpose or function rather than their visual characteristics.

Maintaining styles in external style sheets

The first step in maintaining clean separation between markup and styles is to reference all style rules in an external style sheet. This best practice of separating styles from markup has been widely adopted by developers, and is important because:

  • It allows a page to be styled in many ways and easily changed because it doesn't "bake in" a particular appearance
  • It simplifies maintenance by keeping all style rules in one place
  • It minimizes code weight by centralizing shared styles with common classes and IDs rather than repeating them multiple times in the markup

This approach is fundamentally different from the commonly used (but frequently problematic) practice of writing styles inline—for instance, assigning styles to specific elements in the markup with the style attribute (e.g., style="padding: 5px"), or in a style block in the page. Inline styles introduce a number of challenges:

  • They can't be reused across multiple elements on a page or website, meaning inline styles must be duplicated in every place they're needed.
  • They are difficult to maintain and update, because they're embedded within the -content of HTML, rather than being stored in a single external location. Long term, this becomes a maintenance headache, especially if a design changes in any significant way.
  • They won't be controlled by style rules referenced in external CSS. Even if global styles are maintained externally, each instance of an inline style rule in the markup will override any global external styles.
  • Styles served with markup will be rendered by the browser, even if they are not fully supported. Not all browsers handle CSS the same way, or even fully support all CSS2 properties, so in some situations an inline style may render content unusable.

Even in situations where a style is used only once or twice throughout a site, we still recommend listing it in an external style sheet along with all other style rules for the site.

When it's OK to use inline styles: A few exceptions

There are a few very specific cases in advanced UI design where we've found inline styles to be necessary: dynamically positioning elements on the screen, animating elements' style properties, and creating CSS-based charts.

For instance, if you need to center a modal dialog in the screen or iterate the opacity of an element to create a fade transition, it's not practical to create a class for every possible set of position coordinates or for every increment of animated movement. In these cases, it makes sense to use JavaScript to apply a style attribute and modify CSS properties on the fly. We also use inline dimensions to create pure CSS-based charts by styling an element with a percentage width or height for each bar in a chart.

That said, we recommend keeping these inline style rules to an absolute minimum, and using externally referenced CSS for visual presentation whenever possible.

Linking to external style sheets

When we start coding a project, one of the first things we do is create an external style sheet that will house all style rules for the foundation markup, and reference it in the page head:

<link type="text/css" rel="stylesheet" href="basic.css" />

A valid style-sheet link contains the following attributes:

  • type specifies the content type for the server to interpret (for style sheets, it should always be text/css)
  • rel states the relationship of the linked resource to the HTML document (stylesheet)
  • href contains a relative or absolute path to the CSS file

According to the HTML 4 spec, the link element is valid only when listed in the document head, but there's a more important reason to always list it there: any resources listed in the head are loaded before elements in the body. In other words, placing the link element in the head puts styles in place before the page content shows up, so that when it does, it is styled appropriately. Referencing style sheets elsewhere in the page increases the likelihood that the page will load as unstyled HTML, and users will see one or multiple steps of design transformation as styles load—often a suboptimal user experience.

You can also reference multiple style sheets within a single page. When doing so, keep in mind that each style sheet referenced on a page will require a separate HTTP request to the server, and the browser will load every style sheet referenced within the head section of the HTML page before displaying the page content. For these reasons, we recommend combining CSS into the smallest number of files possible.

Targeting particular media types

By default, style sheets referenced with the link element are applied to all media formats, including standard computer screens, mobile devices, and printers.

To flag a style sheet so that it applies only to a particular format, use the media attribute of the link element to apply one or more media types (separating multiple values with commas). For example, this style sheet will apply formatting to print output only:

<link type="text/css" rel="stylesheet" href="basic.css" media="print" />

Media types can also be specified for individual blocks of CSS within a style sheet, to target a few style exceptions to a particular type within a style sheet that applies to a broader range of formats:

@media print {
  h1 { font-size: 16pt; }
  h2 { font-size: 14pt; }
  h3 { font-size: 12pt; }
  #navigation, #advertisement { display: none; }

The HTML specification lists a number of media types to choose from, three of which apply to websites and applications:

  • screen renders all associated styles in browsers on standard computer screens
  • print applies formatting for printer output, where layouts are typically set in points and inches, and dimensions are often based on paper sizes
  • handheld applies styles to handheld devices with presumably low bandwidth connections, smaller screens, and limited graphics capabilities

Of these, the handheld media type is, unfortunately, the least reliable. As of this writing, few mobile browsers actually obey this media type and parse styles accordingly. The best way to ensure that appropriate styles are served to mobile devices is to develop with test-driven progressive enhancement to ensure that low-bandwidth, low-capability devices are served a usable experience.

Using meaningful naming conventions

When naming classes and IDs, we recommend using names that describe the content's purpose or role in the content hierarchy, not how it's presented on the screen. Think of classes and IDs as an extension of semantic markup—does the name accurately categorize and identify the content within?

Choosing meaningful naming conventions is a best practice when developing with progressive enhancement for a couple reasons:

  • An element labeled with a class or ID may be styled differently in the basic and enhanced experiences. For example, a column of text in the basic site may be pulled into a modal dialog in the enhanced version. Naming that content according to its purpose provides a logically described hook for applying both safe and enhanced styles, as well as for applying interactivity with JavaScript.
  • Visual design can change, even within a single development cycle. For example, a left navigation bar may be moved to the top or the right side of the page, in which case an ID of left-nav no longer makes sense. It's better instead to use a name that describes the purpose of the navigation, such as primary-nav or secondary-nav.

This point is particularly relevant when CSS may be maintained or extended by multiple developers. As a professional services firm, 90% of the code we write is handed off to clients or provided as open source code to the public. As such, the naming conventions we use should make sense to developers who were not part of the initial build process. Keeping names simple and purposeful, and avoiding cryptic acronyms or jargon that's known only among members of a development team, strengthens the code throughout the life cycle of a project.

  • + Share This
  • 🔖 Save To Your Account