CSS: Style the Non-Obvious

Published on March 18, 2009 (ā†» February 5, 2024), filed under (RSS feed for allĀ categories).

This and many other posts are also available as a pretty, well-behaved ebook: On Web Development.

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 much like that.

The interesting part, as always (recall 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, too, 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 to highlight the active nav items by a bold font face, whereas the standard items that are all exemplified by an anchor would use a normal font weight.

Iā€™m not sure whether the point needs further explanation just now, I just wanted to get it out. There are reasons why great CSS techniques are great (and called CSS, not HTML techniques). Experienced CSS developers always try not to alter markup to accomplish certain styling.

Was this useful or interesting? Share (toot) this post, or maybe treat me to a coffee.Ā Thanks!

About Me

Jens Oliver Meiert, on September 30, 2021.

Iā€™m Jens, and Iā€™m an engineering lead and author. Iā€™ve worked as a technical lead for companies like Google and as an engineering manager for companies like Miro, Iā€™m close to W3C and WHATWG, and I write and review books for Oā€™Reilly and FrontendĀ Dogma.

With my current move to Spain, Iā€™m open to a new remote frontend leadership position. Feel free to review and refer my CV or LinkedInĀ profile.

I love trying things, not only in web development, but also in other areas like philosophy. Here on meiert.com I share some of my views andĀ experiences.

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 Oliver 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 Oliver 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 Oliver 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 believe that this depends. It clearly wouldnā€™t be wise to sacrifice CSS efficiency if thereā€™s a legit workaround, e.g. by tweaking document semantics.