Class, ID, and Attributes in HTML
Learn how to use HTML classes, IDs, and attributes to make dynamic, customizable web pages.
In this lesson, we will explore the class
, id
, and other attributes, which are essential for structuring HTML documents, adding metadata, and connecting HTMl with CSS and JavaScript.
Understanding the class
attribute
The class
attribute is used to assign one or more class names to an HTML element. Classes are helpful for applying CSS styles or for JavaScript to identify and manipulate elements on the page. A class name is reusable, meaning multiple elements can share the same class.
<tag class="classname">Content</tag>
Consider the following example where both (div
and p
) elements can be targeted using their class names in CSS or JavaScript.
<div class="container">This is a container</div><p class="text-primary">This is primary text</p>
In the above code:
Line 1: The
div
element has a class namedcontainer
.Line 2: The
p
element has a class namedtext-primary
. ...