class and id attributes are used to identify elements for styling and scripting.
class: reusable label you can apply to multiple elements.id: unique identifier for one element in the page (use once).
class and id attributes are used to identify elements for styling and scripting.
class: reusable label you can apply to multiple elements.id: unique identifier for one element in the page (use once).
<p class="intro">This is an intro paragraph.</p>
<div id="main-content">
...
</div>
/* CSS */
.intro { font-weight: bold; }
#main-content { padding: 12px; background: #f9f9f9; }
<span class="badge primary rounded shadow">HTML</span>
<button id="clickMe">Click</button>
<p id="status"></p>
<!-- JS -->
<script>
document.getElementById('clickMe').addEventListener('click', function() {
document.getElementById('status').textContent = 'Button clicked!';
});
</script>
id more than once in a page.
<h2 id="contact">Contact Us</h2>
<a href="#contact">Go to Contact section</a>
/* Specificity: ID > Class > Element selector */
h2 { color: #333; } /* lowest */
.title { color: #0a7; } /* class */
#hero-title { color: #e91; } /* highest */
id sparingly to avoid specificity battles.
/* element with class */
button.primary { background: #0275d8; color: #fff; }
/* element id + class (be cautious: very specific) */
#cta.primary { border: 2px solid #014c8c; }
/* attribute selector */
a[target="_blank"] { text-decoration: underline; }
Tips:
class="btn primary rounded".btn, card, alert, hero, etc.