class attribute

Multiple HTML elements can share the same class. The 'class' attribute is often used to point to a class name in a style sheet. It can also be used by a JavaScript to access and manipulate elements with the specific class name. The class attribute can be used on any HTML element. The class name is case sensitive! To create a class; write a period (.) character, followed by a class name. Then, define the CSS properties within curly braces {}. HTML elements can belong to more than one class. To define multiple classes, separate the class names with a space, e.g. <div class="city main">. The element will be styled according to all the classes specified. Different HTML elements can point to the same class name.

Yunnan

Kunming

            <div class ="first">
</div>
<div class ="first second">
</div>
<!-- Different HTML elements can point to the same class name. -->
<p class ="first"> </p>

The class name can also be used by JavaScript to perform certain tasks for specific elements. JavaScript can access elements with a specific class name with the getElementsByClassName() method:

            <script>
                const x = document.getElementsByClassName("dark");
                // x = night 
            </script>
<div class ="dark"> night </div>


Back to