An Exercise for Emerging CSS Experts: Avoid IDs and Classes
Published on October 21, 2008 (ā» 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.
To gain more expertise with CSS, thereās a great bonus level: Try to avoid IDs and classes altogether. Thatās right: Write your markup without any IDs and classes.
Though I already mentioned this in a recent post about best practices for ID and class names, hereās the reason: Avoiding id
and class
attributes in their role as style sheet selectors requires a different mindset; it prepares for a new paradigm we face with CSS 3, where all the new selectors will contribute to a shift when it comes to HTML development when the need for styling āhooksā will not entirely vanish, but go down significantly.
To approach modern web development this way, grab a small project, some page, or even a document solely for testing purposes. In complex projects, avoiding IDs and classes can move much to the styling side, meaning a challenge to prevent harm on the markup side; in some cases doing completely without IDs and classes might not be possible. Also, the more complex the desired layout, the more markup might be requiredāyet when doing this kind of exercise, avoiding extra markup is key.
Why am I bringing this up? I felt reminded of former projects, and the lessons I learned in 2004 and 2005 have been of great value, when not just this siteāsee an archived version, albeit without stylingāas well as UITest.comāsee respective snapshot, tooāhave actually been done without using IDs and classes. When I realized that CSS was not supported well enough to go for more advanced styling, and that itās fine to make use of id
and class
, I adjusted as is recommendedābut kind of forgot that what I had learned was invaluable.
Even when you already tried, or when you donāt want to try right now, give it another thought: How many other elegant ways are there to get the HTML right and challenge (and grow) oneās CSS skills?
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 October 21, 2008, 23:06 CEST, pepelsbey said:
Hmm.
And what about reusable blocks conception? I mean special way of using blocks in many various combinations, even nested? unfortunally in Russian, but you can translate it via Google or try to understand pictures and code samples.
This is very helpfull and flexible method especially for really big services. We using it at Yandex.
-
On October 21, 2008, 23:12 CEST, Keonne said:
This is an interesting concept. I will take on the challenge and give it a try š
-
On October 21, 2008, 23:25 CEST, Kroc Camen said:
Fantastic, Iāve been hoping to find more examples of sites done without IDs and Classes!
Itās not that they shouldnāt be used, but people should learn to first do without, so that they better appreciate how to carefully select where they are appropriate.
My site has no DIVs too, try that out for a real challenge! It really makes you wrangle your mind for the semantics of all those HTML tags you have at your disposal!
-
On October 23, 2008, 1:37 CEST, Santhos said:
Quite interesting but a bridge to far for me I guessā¦ It will definitely bring up some problems which I donāt have time for at the moment š
It might be more relevant to completely xml-driven websites because itās more structure oriĆ«ntated. The way I code my sites right now I just couldnāt do without the idās and classes!
-
On October 23, 2008, 4:13 CEST, Judd said:
Great post!
You can get a ton of mileage out of descendant selectors in many cases. Working with jQuery selectors has me so looking forward to CSS3.
-
On October 23, 2008, 11:53 CEST, Kroc Camen said:
@Judd: Looking forward to CSS3?
You can use it right now!I wrote my site because there just wasnāt the right real-world example out there. Demos are good, but plenty of CSS3 is supported now, so used it should be, for real purposes.
So, I apologise, Jens, if it always seems like Iām hijacking your blog posts to spam my website; but everytime I hear people talk about HTML5 and CSS3 as if theyāre a distant pipe-dream, I have to point them to the only concrete example I know, camendesign.com
So Judd, hereās CSS3 being fully used, in the now: http://camendesign.com/design/
P.S. Jens, you have much better writing skill than me, and your topics are superb-I truly would love to see this site written in HTML5/CSS3 with no classes/IDs/DIVs.
-
On October 23, 2008, 14:05 CEST, Jens Oliver Meiert said:
ŠŠ°Š“ŠøŠ¼, well, I can make some sense out of that doc, however it mainly seems to target context-based styling? This is somewhat implied by the post, however it seems to deserve a few extra notes. (By the way, Iād love to follow up on your āCode Responsiblyā comment.)
Keonne, Santhos, yes, give it a try and donāt forget to share your experience š
Judd, thanks!
Kroc, canāt say anything but āagreed,ā āno worries,ā and, referring to your post scriptum, that a) many of my sites are formally based on HTML 5 and all of them make some use of CSS 3, b) some things deserve some consideration of cost of problem, especially regarding this site, and c) you can rest assured that some things will happen, as itās the process thatās so important (and as I donāt deny suffering from perfectionism) š
-
On October 24, 2008, 14:43 CEST, Niels Matthijs said:
Sounds like a fishy idea to me.
Classes and ids should reference components used on a site, adding extra semantics to the html.
Even with html5 or xhtml2 weāll be lacking way too many elements. Of course, styling is possible but it will make our css files and selectors almost unreadable. Alos, adaptations to our html will influence our css, which should not happen.
Itās nice as an experiment, but in the real world where flexibility will save you money and css files are a complete mess even with the help of classes and ids I donāt see how this might help.
Like I said before, unless the html tag is a complete match for the semantics of the component, donāt add html selectors to your stylesheet.
-
On October 28, 2008, 14:47 CET, James Leslie Miller said:
This is an excellent way to train people to get into the proper mindset for what HTML should be used for—providing structure and meaning to your content.
The key phrase in this article is, “Avoiding id and class attributes in their role as style sheet selectors”.
Keep fighting the good fight, Jens!
-
On October 28, 2008, 14:56 CET, Jason Robb said:
Thanks Jens! Your reminder couldnāt have come at a better time for me. I got a case of class-itis earlier this week. Youāve successfully purged my desire to flagrantly use classes and IDs.
Rock on. Thanks again! =)
-
On October 28, 2008, 14:59 CET, Sarah Hall said:
bringing up the rear on this - I would love to see colleges and universities training like this - it would certainly be a really good base to start from and THEN build upon - I certainly wish I had been taught like this and am most definitely going to try this out, I am always looking for ways to improve, lighten the code, get a better understanding š thank you
-
On October 29, 2008, 12:43 CET, Justin Sinclair said:
Iām seconding Niels comment. We should actually encourage the appropriate use of classes and IDās to improve the semantics of a page. While some elements or groupings of elements contain enough semantics in and of themselves (very few people are going to be confused about a paragraph), a lot of the time we want to convey more information about an element or a group of elements.
You do this yourself - with good reason - on this site. As an example, you have two ulās side by side labelled with the classes ānavā and ānav auxā. While in this simple example, I could probably guess fairly quickly what these ulās were doing, the classes make it immediately obvious.
Apart from semantics, these classes also make the css more readable. Would you prefer to see:
.nav {blah} .nav.aux{blah}
or
h1 + ul {blah} ul + ul {blah}
Iād much prefer the first to the second. It also makes the CSS more robust. What happens when you change the markup slightly? In the first case, hopefully your css should survive, and if it fails itāll be easy to diagnose. In the second āh1+ulā might no longer apply and how am I to remember that ul + ul once meant the secondary navigation ul?
I agree that classes and IDās often get overused, and that well constructed CSS can reduce the need for extraneous markup. However, I think we should encourage the use of classes and IDās, to increase the semantics of a page. The limited set of elements we have available to us now and in the future will never be enough.
-
On October 30, 2008, 10:59 CET, Jens Oliver Meiert said:
Niels, Justin, donāt think of this as a recommendation for everyday work but an exercise that offers good lessons.
Niels, as for the
html
element as a type selectorāthereās nothing wrong with that, in fact it makes proper sense for document-wide declarations (e.g. document fonts or background).James, Jason, thanks!
Sarah, yes, that would be interesting. After all many things are about methodology and processes, and approaching web development this way can make some other things a bit clearer.
-
On October 30, 2008, 17:47 CET, Alan Gresley said:
I agree with this approach to a certain extent but if a page has quite a few wrappers, then selectors like this
div>div+div ul
may target more elements than what is desired. My test page template has 7 IDs and classes on divs in total. Now look at the CSS and see how it can get very unspecified.
-
On October 30, 2008, 23:26 CET, Justin Sinclair said:
Jens: Cool š Itās definitely an interesting exercise, and a really good way to get more preficient at using CSS selectors.
Alan: Interestingly I think this excercise is perfect for you. Having a look at your test page you could probably get rid of a bunch of the wrappers while still achieving the same visual effect. As a very quick example you could remove div#container from the markup and apply those rules to the body element.
Also, the class and idās youāre using are very presentational and not descriptive. Why have div#wrapper2 when it could be called div#content? Iāve been guilty of excessive markup and presentational labelling in the past. What helped me most with this - itās like AA, take one day at a time š - was Andy Clarkeās book Transcending CSS. I highly recommend it, itās one of my favourite web design books.
-
On October 31, 2008, 13:18 CET, Alan Gresley said:
Justin: You are correct. In other places, I have stated that this is a working template which should be finished around 2010. If you look at the templateās schematic layout you will see that one selector
div+div+div {..}
is targeting 4 elements. The divs with the IDs source1 and source2 are source ordered by using display:table (not the usual float layout with negative margin) for the good browsers (including IE8) and a old IE bug with clears on floats for IE7 and below. The ID and class names that you see are just there to help me with the CSS as I develop the template.
-
On November 5, 2008, 13:30 CET, Jens Nedal said:
I like this approach Jens. I think this paradigm can be applied to anything concerning CSS, HTML and Javascript.
Keep it simple. Keep it clean. Keep it small. Donāt add clutter where it is not necessary, ever!
Even if a structure gets more complex because of a complex design, try to keep it as simple as possible. This way one will also run into less problems that usually occur when markup gets to complex which will also make it complicated to deal with.
-
On November 5, 2008, 22:51 CET, Buzu said:
I didnāt read everything, but thereās is an important point I want to mention. There is no reason to avoid using Ids and Classes. These two attributes bring more sense to our documents at the same time they help us write more semantic markup. Iām completely aware of the different selector types you can use when writing CSS, but those are just more options and not a replacement for IDs and Classes. Classes serve to group elements that fall into the same category. For example, I could use a class to group all my external links. That makes a lot of sense at the same time that helps me create a visible difference between external and internal links. IDs help identify unique elements so that a websiteās markup can be more readable. For example, by using IDs we can identify the different sections of a page; header, main content, menu, etc., could be identified by using ids. Itās my humble opinion that a better approach is to use a combination of both techniques, which Iāve been doing. I find it very useful for it reduces costs and increases productivity.
Read More
Maybe of interest to you,Ā too:
- Next: Thoughts on Disclaimers
- Previous: The Most Annoying Yet Most Important Task in Website Management
- More under Development
- More from 2008
- 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.