An Exercise for Emerging CSS Experts: Avoid IDs and Classes
Published on Oct 21, 2008 (updated Feb 5, 2024), filed under development, html, css (feed). (Share this on Mastodon or Bluesky?)
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 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.)