In CSS, the class selector is displayed with a dot number:
.center{Text-align:center}
In the example above, all HTML elements that have the center class are centered.
In the following HTML code, both the H1 and P elements have the center class. This means that both will adhere to the rules in the. Center selector.
class="center"class="center">this Paragraph'll also be center-aligned.</p>
Note: The first character of the class name cannot use a number! It doesn't work in Mozilla or Firefox.
As with the ID, class can also be used as a derived selector:
.fancy td{color: #f60; background: #666;}
In the above example, the table cells inside the larger element with the class name fancy will display the orange text on a gray background. (a larger element called fancy may be a table or div)
Elements can also be selected based on their classes:
td.fancy{color: #f60; background: #666;}
In the example above, a table cell with the class name fancy will be orange with a gray background.
class="fancy">
You can assign a class fancy to any number of table elements as many times as you like. Those cells marked with fancy will be orange with a gray background. Cells that are not assigned a class named fancy are not affected by this rule. It is also worth noting that the paragraph of class fancy will not be orange with a gray background, and of course, any other elements labeled as fancy will not be affected by this rule. This is due to the way we write this rule, which is limited to the table cell labeled fancy (that is, using the TD element to select the fancy Class).
CSS class Selector detailed
- CSS Selector Grouping
- CSS ID Selector Detailed
Class selectors allow you to specify styles in a way that is independent of document elements.
CSS class Selector
Class selectors allow you to specify styles in a way that is independent of document elements.
The selector can be used alone or in combination with other elements.
Tip: These selectors can be used only when the document is properly tagged, so it is often necessary to make some ideas and plans for using the two selectors.
The most common way to apply styles without considering the elements of a specific design is to use the class selector.
Modify HTML Code
Before you can use the class selector, you need to modify the specific document tags so that the class selector works.
In order to associate the style of a class selector with an element, you must specify class as an appropriate value. Take a look at the following HTML code:
Class= "Important" >this paragraph is very important.</p>
In the preceding code, the class of two elements is specified as important: the first caption (H1 Element), and the second paragraph (p element).
Grammar
We then use the following syntax to apply a style to the elements of these collations, that is, a dot (.) before the class name, and then a wildcard selector:
*.important {color:red;}
If you only want to select all elements with the same class name, you can ignore the wildcard selector in the class selector, which has no bad effect:
. important {color:red;}
Try it yourself.
Combining Element Selectors
Class selectors can be used in conjunction with element selectors.
For example, you might want only paragraphs to appear in red text:
p.important {color:red;}
The selector now matches all p elements that have the class attribute containing important, but none of the other types of elements match, regardless of whether or not this class attribute is present. The selector p.important is interpreted as: "All paragraphs whose class property value is important." Because the H1 element is not a paragraph, the selector of the rule does not match, so the H1 element does not become red text.
If you do want to specify a different style for the H1 element, you can use the selector h1.important:
p.important {color:red;} h1.important {color:blue;}
Try it yourself.
CSS Multi-Class Selector
In the previous section, we dealt with the case where a word was included in the class value. In HTML, a class value may contain a list of words separated by spaces. For example, if you want to mark a particular element as both important (important) and warning (warning), you can write:
Class= "Important warning" >this paragraph is a very important warning.</p>
The order of the two words does not matter, written warning important also can.
We assume that all elements of class important are bold, and that all elements of class warning are italic, and that all elements in class that contain both important and warning have a silver background. You can write:
. important {Font-weight:bold;}. Warning {font-style:italic;}.important.warning {background:silver;}
Try it yourself.
By linking two class selectors together, you can select only the elements that contain these class names (the order of the class names is unlimited).
If a multi-class selector contains a class name that is not in the list of class names, the match fails. Take a look at the following rules:
. important.urgent {background:silver;}
As expected, this selector will only match the P element that contains the word important and urgent in the class attribute. Therefore, if only the word important and warning are in the class attribute of a P element, it will not match. However, it can match the following elements:
Class= "Important Urgent warning" >this paragraph is a very important and urgent warning.</p>
Try it yourself.
Important: In versions prior to IE7, Internet Explorer on different platforms did not handle multi-class selectors correctly.
CSS class Selector