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.)