Class – 20 | HTML Classes and ID’s | HTML Tutorial

HTML Classes and ID - Class - 20
HTML Classes and ID – Class – 20

HTML classes and ID’s are used to define specific styles for specific HTML elements. Classes and ID’s are used in CSS to select and style specific HTML elements on a web page.

Classes in HTML are defined using the class attribute and can be assigned to multiple HTML elements. For example:

<p class="example">This is a paragraph with a class of "example".</p>
<p class="example">This is another paragraph with the same class of "example".</p>

In the above example, both the paragraphs have a class of “example”. This allows you to define the same styles for both paragraphs using CSS, like this:

.example {
  color: blue;
  font-size: 20px;
}

ID’s in HTML are defined using the id attribute and can only be assigned to a single HTML element. For example:

<p id="unique">This is a paragraph with a unique ID of "unique".</p>
<p>This is another paragraph without an ID.</p>

In the above example, only the first paragraph has a unique ID of “unique”. This allows you to define unique styles for that specific paragraph using CSS, like this:

#unique {
  color: red;
  font-size: 24px;
}

It’s important to note that while both classes and ID’s can be used to select elements in CSS, ID’s have a higher specificity and will take precedence over styles defined in classes.

That’s a brief introduction to classes and ID’s in HTML. You can learn more about these concepts and how to use them effectively in your web development projects by exploring HTML and CSS tutorials and resources.

Next Class

Leave a Comment