Jens Meiert

CSS: Style the Non-Obvious

Post from March 18, 2009 (↻ August 19, 2013), reflecting Jens the .

I struggled but it had to come out: One of the qualities you have to acquire as a web developer is to see the non-obvious, and to use that skill to your code’s advantage. Let me explain by two simple examples.

<div id="breadcrumb">
  <a href="/">Home</a> → <a href="/whatever">Whatever</a> → <em>Anyway</em>
</div>

First, let’s take some classic code for breadcrumb trails. What do you do if you like to adjust the styling of the arrow delimiters?

The novice to intermediate web developer will do something like adding another element, for instance a span element (<span>→</span>). Maybe he’ll garnish it with a class. We’ve seen all that.

The interesting part, as always (read Gary Klein?), is adding in expertise. The markup used in the breadcrumb navigation example leaves plenty of options to adjust styling of the arrows by just using the outer container (#breadcrumb) as a styling hook and to overwrite styling of the inner elements if necessary. For instance, you could use relative positioning for the a and em elements to visually move up or down the greater than signs. This is “styling the non-obvious.”

Another simple and popular case is a plain navigation menu:

<ul id="nav">
  <li>Home
  <li><a href="/however">However</a>
  <li><a href="/whatever">Whatever</a>
</ul>

In practice, if the goal is to highlight the location you’re currently at, many web developers add extra markup, like a special ID or class or even another element. In most cases you don’t need to do that, you’d just style the “invisible”:

li { font-weight: bold; }
li a { font-weight: normal; }

…which here allows you to highlight the active nav items by a bold font face, whereas the standard items that are all characterized by an anchor would use a normal font weight.

I’m not sure whether or not my point is way too simple and too obvious itself, I just figured it might not have been said this way. There are reasons why great CSS techniques are great (and called CSS, not HTML techniques). Experienced CSS developers always try not to alter the markup to accomplish certain styling.

Comments (Closed)

  1. On March 18, 2009, 21:05 CET, Harry Roberts said:

    Ah, finally, someone saying what I’ve always thought. Good point well made sir.

  2. On March 19, 2009, 1:23 CET, Louis said:

    CSS with the tree architechture of the DOM is finally reduced to an ensemblist model. The “non-obvious” being the complement in this metaphor.

  3. On March 19, 2009, 1:50 CET, e2l said:

    Your “nav” example:

    you lose the “meaning” for users with deactivated CSS (textbrowser, visually impaired, …), don’t you?

  4. On March 19, 2009, 13:56 CET, Ivan said:

    @e2l, not really. Even with the styles turned off you can still differ bitween normal text and links and you get them on separate lines.

  5. On March 21, 2009, 18:35 CET, Jens O. Meiert said:

    Harry, e2l, Ivan, thanks :)

    Louis, looks like it, yes :)

  6. On March 23, 2009, 21:04 CET, Peter said:

    An experienced CSS developer will always aim for an accessible and semantic document tree, and will always try to be creative to make sure no additional empty elements are needed just to style the website.

    Good point Jens, I agree. :)

  7. On March 24, 2009, 17:15 CET, Niels Matthijs said:

    Experienced css designers will write lean, flexible and strong code. They will not fiddle and make spaghetti out of their css documents. They will write understandable css which others will be able to work with.

    And most of all, they are not afraid to add an extra element to their xhtml if this means cleaner, leaner and more flexible css.

    Anyway, there is no way to differentiate between active and disabled pages in your navigation item. How’s that for lack of semantics.

  8. On March 24, 2009, 17:52 CET, Thomas Thomassen said:

    I go for lists for breadcrubs or navigation bars.
    If I don’t simply use space inbetween, but insert some ornament I either use a positioned background image, if I want to be really conservative in my backwards compatibility, or I just use :after to insert an extra element. Often this is followed by an :last-child:after statement that removes the appended element for the last item in the list.

  9. On March 24, 2009, 21:35 CET, Rene Saarsoo said:

    I mostly agree with Niels Matthijs - this tehnique can severely effect the readability of your CSS. Styling the non-obvious can lead to non-obvious code.

    I used to apply this techique more when I was younger and meaner CSS hacker, but now I’m mostly steering towards more maintainable CSS.

    But like any powerful tool, it can be quite useful when applied with care.

  10. On March 25, 2009, 11:48 CET, Jens O. Meiert said:

    Peter, exactly; thanks!

    Niels, apart from raising the question how adding something makes anything leaner, I just like to say that this post is not about being “afraid to add an extra element” or not. I hope we can agree that all this “depends.”

    Thomas, fair enough, although one might object against using lists for breadcrumbs (and both against single and nested lists), as the elements of a breadcrumb trail rather reflect a hierarchy.

    Rene, nice hearing from you again! Abstractly, there can be issues, yet I think everyone got the point of the post. Other than that I cannot do the thinking for other people, so the minimum goal here is to inspire.

  11. On March 28, 2009, 18:35 CET, Jordan Clark said:

    I do tend to find myself doing this quite a lot sub-consciously. I do, however, agree with e2l about the nav issue: I think a <strong> element around “Home” would help improve accessibility for non-visual users, and those with CSS disabled.

    Great tip, though!

  12. On March 31, 2009, 9:37 CEST, Niels Matthijs said:

    >> Niels, apart from raising the question how adding something makes anything leaner

    It doesn’t make the html leaner, but it sure helps to make the css leaner, as the separate elements can be styled in a more conform way. Your example will become messy and unmanageable when you’ll start adding margins or paddings to the a-tag for example. And I think we all know how quickly design-changes like that can be made.

    And what if they want the “home” text in bold, italic, font-size higher and bordered? I’ve seen worse requests in my life.

    As a self-proclaimed non-novice and non-intermediate web developer, I’d add a span around the Home text, even add a class on it so they could all be styled based on one selectable item (of course, the class is only there because ie6 has no child selector support).

    And in the end, my css file will be a lot cleaner and easier to understand, grouping styling declaration for common elements. And often leaner too, as it you don’t need to overwrite unnecessary rules.

  13. On March 31, 2009, 15:50 CEST, Jens O. Meiert said:

    When it comes to adding emphasis to the “Home” location in the second example (now mentioned a couple of times), arguably that’s a matter of preference but not a matter of accessibility. I hope those who prefer adding emphasis understand the point regardless :)

    Niels, I understand where you’re heading, however I’m confident that we can agree that this just depends. It clearly wouldn’t be wise to sacrifice CSS efficiency if there’s a legit workaround, e.g. by tweaking document semantics.

Read More

Have a look at the most popular posts, eventually including:

Or maybe say hi on Google+, Twitter, or LinkedIn?

Found a mistake? Reward! Email me, jens@meiert.com.

You are here: meiert.comArchive for 2009 → CSS: Style the Non-Obvious

Last update: August 19, 2013.