Can I use non existing CSS classes?

后端 未结 13 1825
北荒
北荒 2020-11-27 12:54

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

相关标签:
13条回答
  • 2020-11-27 13:39

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


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

    2 The 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.

    0 讨论(0)
  • 2020-11-27 13:42

    The moment you add the Class in your HTML the Class will be defined, so your solution is completely fine

    0 讨论(0)
  • 2020-11-27 13:43

    According to HTML5 specification:

    A class attribute must have a value that is a set of space-separated tokens representing the various classes that the element belongs to. ... There are no additional restrictions on the tokens authors can use in the class attribute, but authors are encouraged to use values that describe the nature of the content, rather than values that describe the desired presentation of the content.

    Also, in the version 4:

    The class attribute has several roles in HTML:

    • As a style sheet selector (when an author wishes to assign style information to a set of elements).
    • For general purpose processing by user agents.

    Your use case falls under the second scenario, which makes it a legitimate example of using a class attribute.

    0 讨论(0)
  • 2020-11-27 13:46

    There are no ill effects to use classes which don't have styles. Indeed, that's part of the usefulness of CSS is that it's de-coupled from the markup and can style or not style elements/classes/etc. as needed.

    Don't think of them as "CSS classes." Think of them as "classes" which CSS happens to also use if it needs to.

    0 讨论(0)
  • 2020-11-27 13:47

    You can use CSS classes without using it, but I suggest that if you are adding CSS classes just for the JavaScript/jQuery code, prefix with it js-YourClassName so the front-end developers never use these classes to style the elements. They should understand that these classes can be removed at any time.

    0 讨论(0)
  • 2020-11-27 13:49

    Refer to the jQuery validation engine. Even in there we also use non-existent classes to add validation rules on the HTML attributes. So there is nothing wrong in using classes that are not actually declared in a stylesheet.

    0 讨论(0)
提交回复
热议问题