The Web Standards Sherpas

Just realizing that with the busy Spring I’ve missed announcing a recently launched project that I’m honored to be serving on the editorial team for.

Web Standards Sherpa’s experts provide helpful, pragmatic and up-to-date advice on best practices for web professionals everywhere.

Back in March, Web Standards Sherpa, the brainchild of some of the crew at the Web Standards Project and support from Microsoft, Mozilla, Opera, the W3C, and others, was born. The new site aims to provide guidance on the real world usage of web standards and surrounding best practices through reviews and feedback on existing web sites. Now, with the 10th article published yesterday, I think we’re well on our way down this path of building a great, relevant, and current resource to help web professionals on their journey. Already I am finding that I’m referencing many of the pieces in conversations with clients or colleagues.

The Path We’ve Started On

Sherpa Current Issue

Current Sherpa Issue: Overlays and Lightboxes: Keys to Success by Derek Featherstone

Here’s an introduction to the current staff of Sherpas guiding you and the types of wisdom they’ve been dropping along the way.

Join The Trek

Feed Shirley

Check out the great team behind Web Standards Sherpa and learn a bit more about the project by watching Aaron Gustafson’s talk from MIX11. And if you have a project that you’ve recently launched or that you’re looking to redesign soon and you’re wrestling with a topic such as accessibility, performance, semantics or content strategy drop the Sherpas a line via the submit a site form. Or just come by the site and join the discussion.

PNHTagTest Now On GitHub

Taking a moment away from a truly busy summer of client work and other fun projects to bring you this little announcement:

There’s now a permanent home on GitHub for PNHTagtest, a little piece code that I’ve had floating around for ages as an aid for building and reviewing CSS on a project.

Sample of PNHTagTest contents

I’ve been starting every web site project with a document containing a mix of HTML tags and common markup patterns like nested lists, links found in headings, tables, blockquotes and pre/code blocks. The contents of the test file are meant to be copy & pasted into an html document, added as a page or blog post in the CMS de jour, injected via server side includes.

Meant to sit along side of your style guides, widget inventories or whatever tools you’re using to organize the site build, this document provides a place to go to review the styling, typographic conventions, and readability of a site in ways that standard dummy copy, such as a headline followed by 5 identical paragraphs of Lorem Ipsum text, can’t. Those pesky forgettable tags like sup, dl, and abbr that may not show up in a site’s content until 3 months after launch are all well represented here so you won’t forget to style them!

PNHTagTest was previously posted on this blog way back in 2006.

Minimized HTML5 Attributes, Selectors & jQuery

After working with some HTML5 web forms attributes on a small project I have come to the conclusion that for now it is best to…

Use <input required="required"> not <input required>

Some backstory — A few weeks back I was working on a small non-public web site heavy on forms and thought it would be a good fit as an HTML5 test case. For a variety of technical reasons [input formatting in particular] I didn’t go whole hog into it using all the various input type attributes, but did use the required attribute as a hook for JavaScript based form validation and styling. What I found was that generally there was adequate support for styling and selecting based on this new, and unknown to many browsers, required attribute — Yay! we can use this stuff today! However, there were a few browser CSS selector and jQuery 1.3.2 quirks that lead me to the conclusion that it is safest to use the expanded form of the required attribute and not the minimized or shorter form as HTML5 allows. This gives you the most solid and flexible options when choosing selectors in CSS or jQuery code.

Required Attribute Test Cases

I put together a simple test page and did some browser testing. The test cases and results chart are posted here.

Required attribute test cases

What you can see from these tests is that all modern browsers have strong support in CSS for the attribute selector and its use against a possibly unknown attribute. As one would hope hope they select all required fields, both shorthand and long form, based on [required] selector string [green text] and only the long form based on [required=required] [bolded]. Furthermore, Opera 10 also supports selection based on the :required pseudo selector.

The puzzling behavior comes in the few scenarios where the CSS selector behaves properly but the selection of elements via the jQuery library and JavaScript fails for those same selectors. This is seen in the Firefox 3.0/Gecko 1.9.0 based browsers where $("[required]") selects only the case where the attribute has been explicitly given a value.

One of the positive things you’ll see from these tests is that an unknown value for the input type attribute (in this case type="number") did not impact the selector behaviors in any browser.

Some Followup Investigation Needed

The linked test cases were quickly pulled together and I haven’t taken the time to look at what is going on from a more general angle. If any core jQuery or browser selector engine gurus want to chime in there are still a few things I want to chase down…

  • Are the test results different for a minimized attribute like disabled or checked that a non-HTML5 browser may understand?
  • I’d like to run the jQuery test cases against a trunk build or early 1.4 build, and figure out what is a jQuery bug, browser selection bug, and what bugs need to be filed to get the library working more consistently across the platforms it supports, I’d also like to pull jQuery out of the equation and just use the browser’s native selection engines.
  • Check up on the documentation and mailing list discussions to see if there some magical handling of the $(":required") selector coming in some future jQuery build because right now the results are all over the map.
  • I don’t even want to begin to think about how capitalization and attribute case may have an impact across browsers.

Ahhh, ‘web standards’… why do you bring so much pain with your promise?

Pseudo Pseudo-Classes

Some of the most powerful CSS2 and CSS3 selectors defined in the specs are avoided by web developers because they’re not supported by commonly used web browsers. Sometimes in order to work around these shortfalls solutions with large overhead such as javascript libraries are used or code becomes littered with many specialized classes and sites become difficult to maintain.

Over time I’ve developed the habit of using specifically named class attributes to represent exactly where a pseudo-class would have applied. To aid in clarity and maintenance these classes are named with the same text as the name of the pseudo-class being represented:

  • :first-child becomes .first-child
  • :last-child becomes .last-child
  • :first-of-type becomes .first-of-type
  • :nth-child(odd) becomes .nth-child-odd
  • :o nly-child becomes .only-child
  • :empty becomes .empty

And so on.

Your Typical List of Links

Some of these classes I use on every site I build. First-child [or last-child] are particularly handy when dealing with horizontal lists or menus where you want different delimiters or padding around the items on the edges vs the items in the middle of the list. A simple example would be to use vertical borders to separate the items. If you put a left border on all of the items you need to find some way to not have a border on the left most item. In a perfect world the HTML and css would looks something like this:

ul li { float:left; padding: 0 1em; border-left: 1px solid black; }
ul li:first-child { border-left: none; }
<ul>
<li><a href="index.html">Home</a></li>
<li><a href="page2.html">Page 2</a></li>
<li><a href="page3.html">Page 3</a></li>
</ul>

Leaving no extra classes in the HTML at all. However, if your project must support browsers that fail rendering the above styles you’d update the HTML and CSS with the following simple additions:

ul li { float:left; padding: 0 1em; border-left: 1px solid black; }
ul li:first-child,
ul li.first-child { border-left: none; }
<ul>
<li class="first-child"><a href="index.html">Home</a></li>
<li><a href="page2.html">Page 2</a></li>
<li><a href="page3.html">Page 3</a></li>
</ul>

Remember, the class you’re adding should always represent exactly how the real pseudo-element behaves and thus the placement of the first-child class is important. For example you would not want to write the following HTML:

<ul>
<li><a class="first-child" href="index.html">Home</a></li>
<li><a href="page2.html">Page 2</a></li>
<li><a href="page3.html">Page 3</a></li>
</ul>

Even if you could style this example and the previous example to appear alike the danger is in the details. If in a later site revision you replaced the class selectors with the real pseudo-class selectors you’d wind up with totally different styling — in fact the pseudo-class version would apply to all the anchors in the list because they’re all first-children of their respective parent list item.

Legibility and Maintenance

Tossing classes into the HTML to have more ‘hooks’ to style with is nothing special. The key to these pseudo pseudo-classes working is the use of established names and established behaviors. Any developer can look at an element with class="first-child" and know exactly what it represents. Usage of words like “first” or “linkA” or “item1” might be used in the same ways, but they might not evoke the exact same meaning. Perhaps the “link1” is the first link, but it may not be the parent elements first child. Or perhaps one project will use an “even” class on alternating list items, where another might call them “off” or “alternate”.

So there’s power in the distinction between “linkA” and “first-class” and there’s time saved by setting coding standards up front.

What about Pseudo-Elements?

CSS3 pseudo-elements are a bit trickier to duplicate by using using class attributes alone. But the principle of standardizing on the use of class names that are similar to the items they’re replacing. If I wanted to stylize the first letter of a paragraph in some special means I’d use a css selector such as p span.first-letter {} and the necessary HTML to match instead of picking some other class name out of thin air.

Remember, the goal of pseudo pseudos is that there is a predefined behavior that you wish you could leverage, and you’re just looking for a way to duplicate that behavior in older browsers with the minimal amount of overhead and maximal amount of clarity and simplicity.

Further Reading