美文网首页
HTML Classes

HTML Classes

作者: 春暖花开奇奇乐乐 | 来源:发表于2018-11-16 15:24 被阅读11次

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:

相关文章

网友评论

      本文标题:HTML Classes

      本文链接:https://www.haomeiwen.com/subject/avemfqtx.html