0

Does a css class selector always require a definition? For example, if you found in the html: div class="banner", should you always find a .banner in a css file? I ask this question as I've been looking at some website themes and I sometimes find these selectors without any other reference. I'm just not sure if it's an oversight or something common.

Stephan Muller
  • 27,018
  • 16
  • 85
  • 126
digby
  • 1
  • 2
    Sometimes class attributes are added to HTML elements to select a group of related elements. It isn't required to have a CSS definition for this case. – Cameron Tinker Mar 25 '15 at 14:12
  • 2
    Somewhat related: [Can I use non existing CSS classes?](http://stackoverflow.com/questions/18701670/can-i-use-non-existing-css-classes) – Josh Crozier Mar 25 '15 at 14:12
  • 1
    No, it does not require a definition, it could be added for js use or it could of been in use then was removed and the class removal was forgotten. It isn't wrong to have class without definition in the css. It is valid but you should make sure each class has a point to it to keep the html clean – Huangism Mar 25 '15 at 14:21

1 Answers1

0

There are many reasons to have class names on your HTML elements without having CSS rules associated with them. A couple of examples:

  1. More readable markup. If a component is properly labeled, it's easier to find, debug, or work collaboratively on.
  2. Javascript. Sometimes an element requires some Javascript behaviors, but doesn't inherently need CSS styling itself.

So to answer your question: No, you do not need to define each class or selector in your CSS.

Jape
  • 1
  • 1