Markup that relies on HTML to represent an element's state is one way to create building blocks for user interface layouts, but it is hardly the only one. Mixing HTML attributes with CSS allows for a smarter UI coding experience.
HTML attributes are an ideal way to store data about the current state of a DOM element. However, not all information about an HTML element should be stored as attributes. This is true when your UI components represent a state that affects the appearance of the HTML element. In this situation, the DOM element should only reflect the current state of the UI component (e.g., when a button's isChecked attribute is true, you can style it).
In most cases, the appearance of an HTML element should be independent of its state. This means that the state of a UI component, represented by its attributes, should be the only information that influences how the element is styled. The problem is that sometimes you need more than an attribute's value to determine how an element is styled. This is the case for the class
attribute of HTML elements. For example, class
attributes can specify the appearance of all table elements (e.g., classes such as .table-striped
or .header-background
), but you might also want to apply different styles to a table element depending on whether it's in the middle of a form or not.
A CSS class selector consists of a type selector that identifies the type of element, and one or more selectors that specify how the CSS style should be applied to the element (e.g., a different background color for a .blue
class). A CSS class selector can combine a number of different selectors to select all HTML elements with the given class. In addition to combining attributes with a single class name, you can use the class
attribute with a space to indicate that the class name is a comma-separated list (e.g., class="blue, header-background"
). You can then select all the .header-background
and .blue
elements in your HTML document with the style tag. This is the same as listing them in a class selector, but does not have the same effect of making the CSS styles apply to the elements. For more information click here https://www.aleksandrhovhannisyan.com/blog/represent-state-with-html-attributes-not-class-names/.