The class Attribute
The HTML class attribute is used to define equal styles for elements with the same class name.
So, all HTML elements with the same class attribute will have the same format and style.
Tip: The class attribute can be used on any HTML element.
Select Elements With a Specific Class
In CSS, to select elements with a specific class, write a period (.) character, followed by the name of the class:
1. Multiple Classes
HTML elements can have more than one class name, each class name must be separated by a space.
In the example above, the first <h2> element belongs to both the "city" class and the "main" class.
2. Different Tags Can Share Same Class
Different tags, like <h2> and <p>, can have the same class name and thereby share the same style:
3. Using The class Attribute in JavaScript
The class name can also be used by JavaScript to perform certain tasks for elements with the specified class name.
JavaScript can access elements with a specified class name by using the getElementsByClassName() method:
网友评论