Breadcrumb Abstract Shape
Breadcrumb Abstract Shape
Breadcrumb Abstract Shape

HTML Classes & IDs

HTML Classes & IDs

HTML CODES & EXAMPLES

HTML Classes & IDs

Learn how to identify, group and target HTML elements using class and id attributes with practical examples.

01

What are Class and ID?

HTML element identifiers

The class and id attributes are used to identify HTML elements. They are commonly used with CSS for styling and JavaScript for selecting and manipulating elements.

Remember:A class can normally be reused on multiple elements, while an id should identify one unique element within a page.
02

Basic Class

Assign a class to an element

class-basic.html
<p class="course-title">

  Computer Courses

</p>
03

Class with CSS

Style elements using a class selector

class-css.html
<p class="course-title">
  Computer Courses
</p>

<style>

.course-title{

  font-size:28px;

  font-weight:bold;

  text-align:center;

}

</style>
04

Reusable Class

Use one class on multiple elements

reusable-class.html
<div class="course-card">
  Course 1
</div>

<div class="course-card">
  Course 2
</div>

<div class="course-card">
  Course 3
</div>

<style>

.course-card{

  padding:20px;

  margin:10px;

  border:1px solid #ddd;

  border-radius:10px;

}

</style>
05

Basic ID

Identify a unique element

id-basic.html
<h1 id="page-title">

  HTML Classes & IDs

</h1>
06

ID with CSS

Style an element using an ID selector

id-css.html
<h1 id="page-title">
  Welcome to SNCEC
</h1>

<style>

#page-title{

  text-align:center;

  font-size:32px;

}

</style>
08

Multiple Classes

Apply several classes to one element

multiple-classes.html
<div class="card featured">

  <h2>
    Featured Course
  </h2>

</div>

<style>

.card{

  padding:25px;

  border-radius:15px;

}

.featured{

  border:2px solid #1769d5;

}

</style>
09

Class + ID Together

Use both attributes on one element

class-id.html
<div
  id="main-course"
  class="course-card featured">

  <h2>
    Diploma in Computer Application
  </h2>

</div>
10

ID Anchor Link

Jump to a specific section

anchor-id.html
<a href="#courses">

  View Courses

</a>


<section id="courses">

  <h2>
    Our Courses
  </h2>

</section>
11

Back to Top

Use an ID for page navigation

back-to-top.html
<body id="top">

  ...

  <a href="#top">
    Back to Top
  </a>

</body>
12

JavaScript with ID

Select an element using getElementById()

javascript-id.html
<h2 id="message">
  Hello Student
</h2>

<button onclick="changeText()">
  Change Text
</button>

<script>

function changeText(){

  document.getElementById(
    "message"
  ).textContent =
    "Welcome to HTML!";

}

</script>
13

JavaScript with Class

Select class elements

javascript-class.html
<p class="course">
  HTML
</p>

<p class="course">
  CSS
</p>

<script>

const courses =
  document.querySelectorAll(
    ".course"
  );

courses.forEach(function(item){

  item.style.fontWeight =
    "bold";

});

</script>
14

Practical Course Cards

Real-world class usage

course-cards.html
<div class="course-grid">


  <div class="course-card">

    <span class="badge">
      CERTIFICATE
    </span>

    <h2>
      Certificate in Computer Application
    </h2>

    <p>
      Computer fundamentals and
      office productivity.
    </p>

  </div>


  <div class="course-card featured">

    <span class="badge">
      POPULAR
    </span>

    <h2>
      Diploma in Computer Application
    </h2>

    <p>
      Practical computer application
      skills.
    </p>

  </div>


</div>
15

Good Naming Practices

Write clean and maintainable HTML

✓ Recommended course-card student-profile main-navigation course-title
✕ Avoid unclear names box1 x abc thing
17

Practice Task

Build your own HTML component

🎯 Create a Student Profile Card

Create a student profile using class and id attributes.

  • Give the main profile a unique ID.
  • Use a reusable class for the card.
  • Add classes for name, course and status.
  • Style the card with CSS.
  • Add a button that changes the status using JavaScript.
`;document.getElementById( "sncecClassIdPreview" ).srcdoc = code;}document.addEventListener( "DOMContentLoaded", function(){sncecClassIdRun();} );

HTML Runner

  • 16/08/2026

CSS Borders

  • 17/08/2026

Leave a Reply

Your email address will not be published. Required fields are marked *