CSS Practice: Namespaces in Complex Projects
Published on March 21, 2007 (⻠February 5, 2024), filed under Development (RSS feed for all categories).
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 frontend engineering leader and tech author/publisher. Iâve worked as a technical lead for companies like Google and as an engineering manager for companies like Miro, Iâm a contributor to several web standards, 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. (Please be critical, interpret charitably, and give feedback.)
Comments (Closed)
-
On March 21, 2007, 21:55 CET, Duluoz said:
Youâve hit on a great practice here. Iâve been using this practice for some time now myself, but never came up with a clever name.
The problem with creating a global style sheet to handle this possible problem of others injecting their classes, etc, is that you want that sheet to be first in inheritance. Therefore anything that gets defined later may over-ride your classes. However by using a psedo-namespaces technique youâve eliminated that risk. I always set globals with â.g-â myself. đ
Very good!
-
On March 21, 2007, 22:13 CET, medyk said:
well.. there are real namespaces in CSS but theyâre in CSS Level 3 and are not supported yet by most browsers đ
-
On March 21, 2007, 22:19 CET, Jens Oliver Meiert said:
medyk, right, though the concept is a little different⊠I missed a âyetâ đ Iâm focusing on current web development practice, then.
-
On March 22, 2007, 8:56 CET, Robert Nyman said:
While I definitely understand the concerns and problems its trying to solve, I personally wouldnât use such a practice.
Why? A couple of reasons:
- I think the class names lose some of their semantic meaning, with semi-abbreviations that doesnât mean anything to people outside of the project.
- Maintainability. If you didnât come up with all the pseudo-namespaces in the project yourself, you will need some index to look up what it actually means. I prefer code where you can read the class name and understand its meaning and usage without some kind of reference.
Iâd rather say: go for proper class names with regular meaning, and if new members are introduced to the project, they can read a swift introduction. In the long run, if you stumble upon conflicts, tools like the wonderful Firebug add-on for Firefox can help you find and address them in a swift manner.
-
On March 22, 2007, 11:01 CET, Jens Oliver Meiert said:
Robert, thatâs what I hoped to cover with the disadvantages. You can address both issues by documentation and employing âspeakingâ (easier to understand) ID and class names.
-
On March 22, 2007, 19:04 CET, Nic Johnson said:
Iâm the design lead on an intranet overhaul and weâve been using a similar technique because of the technology weâre bound to. Weâre using BEAâs Aqualogic Interaction product as a portal and thereâs no way to guarantee the object ids and classes coming from various sources. So, to avoid conflicts, we simply use a certain prefix on all of our markup.
-
On April 4, 2007, 3:54 CEST, Ben Buchanan said:
Itâs worth noting that microformats essentially operate on this basis đ Hence why the idea has been tossed around that all microformats should be pseudo-namespaced uf- (u for micro⊠iffy, but it works).
In your example I think the loss of readability far outweights the gains from shorter class names. You might save a byte or two, but youâll waste developer time trying to remember what âni-lâ means in a yearâs time đ
-
On August 21, 2007, 14:31 CEST, Jens Oliver Meiert said:
This post is also available in German at âDr. Webâ.
-
On December 12, 2008, 15:08 CET, Neal G said:
This is sort of a poor example for this considering you can already select pdf, doc, and ppt files with CSS3 selectors - i.e. a[href$=â.pdfâ]. I like the idea, just not the example.
-
On December 12, 2008, 15:28 CET, Jens Oliver Meiert said:
Neal, true (I wrote about CSS 3 in that context in 2005). From that angle the examples are not ideal.
-
On June 24, 2009, 14:39 CEST, Clément said:
I prefer namespacing using a parent class. For instance:
.context1 h1 a{ color:red; } .context2 h1 a{ color:green; }
-
On December 8, 2010, 2:51 CET, Rebecca Cannon said:
We need namespaces for a large project with many embedded includes, and embedded namespaces would make it a lot easier.
eg CSS having the ability to do parent classes. EG
.ns {
.doc {}
.pdf {}
.nppt {}
} -
On January 6, 2011, 23:18 CET, Jason said:
Iâve done something similar for creating complex widget libraries for web apps. I experimented with this method, and it seemed to work, but Iâm not sure itâs technically valid:
<div class="UiWidget/MenuBar"> <ul class="UiWidget/Menu"> <li><a href="#">Item</a></li> </ul> </div>
styled in CSS:
.UiWidget/MenuBar { background-color:#333; } .UiWidget/Menu { float:left; }
Read More
Maybe of interest to you, too:
- Next: Photos Make Websites More Credible
- Previous: Why I Love DreamHost
- More under Development
- More from 2007
- Most popular posts
Looking for a way to comment? Comments have been disabled, unfortunately.
Get a good look at web development? Try WebGlossary.infoâand The Web Development Glossary 3K. With explanations and definitions for thousands of terms of web development, web design, and related fields, building on Wikipedia as well as MDN Web Docs. Available at Apple Books, Kobo, Google Play Books, and Leanpub.