Classes in HTML
? Classes in HTML
A class is an attribute you add to HTML elements to group them logically and apply CSS styles or target them with JavaScript.
What is a Class?
It’s a name assigned to one or more elements.
Multiple elements can share the same class.
Used mainly for styling and manipulation.
Syntax Example
<p class="highlight">This paragraph is highlighted.</p><div class="container">Content inside a container.</div>Using Classes in CSS
.highlight { background-color: yellow; font-weight: bold;}Applies to all elements with class="highlight".
Multiple Classes on One Element
You can add multiple classes separated by spaces:
<p class="highlight large-text">Big highlighted paragraph</p>CSS:
.large-text { font-size: 24px;}Using Classes in JavaScript
const elements = document.getElementsByClassName("highlight");for(let el of elements){ el.style.color = "red";}Why Use Classes?
Reuse styles on multiple elements
Select groups of elements easily in CSS and JS
Keep HTML and CSS organized
Want me to show examples with CSS styling using classes or JavaScript manipulation?