CSS Practice: Namespaces in Complex Projects
Published on Mar 21, 2007 (updated Feb 5, 2024), filed under development, css, maintainability (feed). (Share this on Mastodon or Bluesky?)
This and many other posts are also available as a pretty, well-behaved ebook: On Web Development.
Working in complex projects or in projects that donât provide a good overview of forthcoming page types and elements may require a defensive strategy for writing CSS. Such a defensive strategy rests on certain safety measures to ensure better maintainability, and CSS âpseudo-namespacesâ are such a measure.
This namespace concept is only roughly similar to that in XML. There are no ârealâ namespaces in CSS (therefore âpseudo-namespaceâ).
Examples for CSS Namespaces
Special icons for links are a somewhat artificial example yet also a good start: Assume you want to equip certain links (for example those for file types or actions) with different icons. Given that thereâs no reliable context (as with, say, PDF links in your projectâs sidebar), you could add specific classes to these links:
<a href="" class="doc"></a>
<a href="" class="pdf"></a>
<a href="" class="ppt"></a>
This is fine (though you could complement that with an additional file
class, for example). But considering that you a) donât know what other page elements are to come, or b) donât or wonât work alone on that project, you might want to make sure that there wonât be any conflicts when, for example, a document preview module with a doc
class gets introduced.
You can certainly address this issue through context-dependent formatting (a.doc
vs. div.doc
). Yet this may result in more code than is necessary, especially when it comes to more complex projects where youâd end up with dozens of much longer selectors; it doesnât solve the problem that you donât know whoâs modifying the code next, either.
A namespace can solve this:
<a href="" class="nf-doc"></a>
<a href="" class="nf-pdf"></a>
<a href="" class="nf-ppt"></a>
The nf-
prefix is a little arbitrary, in this case meaning ânamespace [for] format.â The ânâ is both a proposed name token for these namespaces as well as yet another safety measure, meaning that it makes it even more unlikely that someone else comes up with such an abbreviated name. Since the nf-
prefix could be limited to links, youâd need to set up a convention for that (and every other) namespace, and document these accordingly. (As mentioned, itâs a safety measure so that another person introducing a doc
class wonât cause trouble, but you also want to avoid inconsistencies with ID and class names. Clearly a case for a little documentation.)
Beside the decreased risk of later conflicts, itâs now possible to write simpler CSS code:
.nf-doc {}
.nf-pdf {}
.nf-ppt {}
âŠand you could continue with other types of elements, for example certain input element sizes:
.ni-s {}
.ni-m {}
.ni-l {}
âŠwhere ni
could mean ânamespace [for] input [elements],â and where you can use very short yet still understandable class names (âsmall,â âmedium,â and âlargeâ input elements, where the names are intended rather to illustrate the relation of the affected elements than to be presentational) without much risk.
Pros and Cons of Namespaces
I hope I managed to demonstrate the concept without provoking too much focus on the detail (it wonât be meaningful here to discuss file
class names and stuff). So here are the disadvantages of CSS namespaces:
- They require more code conventions and therefore more documentation since their short form might confuse new project contributors;
- they may threaten the CSS codeâs consistency (âby designâ since theyâre primarily intended to prevent unforeseeable formatting problems).
On the other hand, namespaces have some important advantages:
- They mean more safety when it comes to
- new page elements,
- foreign code injections, and
- new team members;
- they preserve ID and class name semantics while
- they also give more flexibility due to now possible and not necessarily confusing reuse of useful ID and class names.
The bigger and the more inscrutable a project becomes, the more one benefits from this namespace concept.
About Me
Iâm Jens (long: Jens Oliver Meiert), and Iâm a web developer, manager, and author. Iâve been working as a technical lead and engineering manager for companies youâve never heard of and companies you use every day, Iâm an occasional contributor to web standards (like HTML, CSS, WCAG), and I write and review books for OâReilly and Frontend Dogma.
I love trying things, not only in web development and engineering management, but also in other areas like philosophy. Here on meiert.com I share some of my experiences and views. (I value you being critical, interpreting charitably, and giving feedback.)