Can I use non existing CSS classes?
Yes, it's totally fine to use non-existing CSS classes in your HTML. They might have no impact immediately but can act as a handy placeholder for later styling or for reference in your JavaScript.
Then sprinkle in some CSS magic for styling:
It's a brilliant strategy for proactive planning of your website or dynamic content. Now, let's dive deeper into why this works.
The independence of HTML and CSS
In HTML, classes are just attributes that do not need a corresponding CSS rule in order to exist. They're like dog tags for elements—cool right 🐶?
HTML and CSS are two independent warriors in the web development battlefield. HTML structures the content while CSS makes it pretty. A class that you've not styled yet is merely an address without a mailbox—fully good to go, but nobody's dropping any colors or borders there yet.
Even if a CSS class is not declared, there's no side effect or horror movie like screaming errors.
Even if dynamic-color
class didn't exist in your CSS initially, it will still happily accept that shiny blue color from jQuery just like a surprise birthday gift 🎁.
Semantic HTML and JavaScript targeting
The use of non-existing classes gives your HTML more readability like an open book and maintainability tougher than a roach.
Take a glance at this example:
The class save-the-world
is pretty clear about its purpose right? This improves code quality and makes reading it less of a decoding mission.
Best practices for project longevity
In larger projects, separating CSS and JavaScript classes can avoid stepping on developers' toes—trust me, nobody wants that.
It's always better to use class names that clearly describe the content or specific functions as it's more intuitive. Skip dull names like style01
and go for something like death-star-trigger
. Sounds more exciting, right?
Visualization
Let's inspect the scene where non-existent CSS classes come into play.
The seeds 🌱 are representing non-existing classes in your CSS for now.
Finally, when you birth those classes in your CSS, they bloom into fully styled divas 🌷:
Flexibility and expandability
Adding a class to an HTML element which is yet to be styled in the CSS file offers flexibility and expandability.
HTML5 loves non-existing CSS classes. They are seen as general-purpose tokens which can be extensively beneficial for integration of frameworks and libraries, that mostly rely on classes for JavaScript functionality instead of styling (jQuery just nodded!).
Was this article helpful?