Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I use non existing CSS classes?

I have a table where I show/hide a full column by jQuery via a CSS class that doesn't exist:

<table>    <thead>       <tr>          <th></th>          <th class="target"></th>          <th></th>       </tr>    </thead>    <tbody>       <tr>          <td></td>          <td class="target"></td>          <td></td>       </tr>       <tr>          <td></td>          <td class="target"></td>          <td></td>       </tr>    </tbody> </table> 

With this DOM I can do this in one line via jQuery: $('.target').css('display','none');

This works perfectly, but is it valid to use CSS classes that aren't defined? Should I create an empty class for it?

<style>.target{}</style> 

Are there any side effects or is there a better way to do this?

like image 991
totymedli Avatar asked Sep 09 '13 15:09

totymedli


People also ask

Can classes be reused in CSS?

They're not reusable. The module can't be reused in different locations without broken styles. Non-reusable styles create monolithic CSS collections.

How do you select non classes in CSS?

The :not() pseudo-class requires a comma-separated list of one or more selectors as its argument.

Does CSS class order matter?

CSS Order Matters In CSS, the order in which we specify our rules matters. If a rule from the same style sheet, with the same level of specificity exists, the rule that is declared last in the CSS document will be the one that is applied.

Do classes override CSS?

To override the CSS properties of a class using another class, we can use the ! important directive. In CSS, ! important means “this is important”, and the property:value pair that has this directive is always applied even if the other element has higher specificity.


1 Answers

"CSS class" is a misnomer; class is an attribute (or a property, in terms of scripting) that you assign to HTML elements. In other words, you declare classes in HTML, not CSS, so in your case the "target" class does in fact exist on those specific elements, and your markup is perfectly valid as it is.

This doesn't necessarily mean that you need to have a class declared in the HTML before you can use it in CSS either. See ruakh's comment. Whether or not a selector is valid depends entirely on the selector syntax, and CSS has its own set of rules for handling parsing errors, none of which concern the markup at all. Essentially, this means HTML and CSS are completely independent of each other in the validity aspect.1

Once you understand that, it becomes clear that there is no side effect of not defining a .target rule in your stylesheet.2 When you assign classes to your elements, you can reference those elements by those classes either in a stylesheet, or a script, or both. Neither has a dependency on the other. Instead, they both refer to the markup (or, more precisely, its DOM representation). This principle applies even if you're using JavaScript to apply styles, as you're doing in your jQuery one-liner.

When you write a CSS rule with a class selector, all you're saying is "I want to apply styles to elements that belong to this class." Similarly, when you write a script to retrieve elements by a certain class name, you're saying "I want to do things with elements that belong to this class." Whether or not there are elements that belong to the class in question is a separate issue altogether.


1This is also why a CSS ID selector matches all elements with the given ID regardless of whether the ID appears exactly once, or multiple times (resulting in a non-conforming HTML document).

2The only situation I'm aware of where an empty CSS rule like that is necessary is when some browsers refuse to apply certain other rules properly as the result of a bug; creating an empty rule will cause those other rules to be applied for some reason. See this answer for an example of such a bug. However this is on the CSS side and therefore should have nothing to do with the markup.

like image 178
BoltClock Avatar answered Oct 11 '22 12:10

BoltClock