Know and understand characteristics of a style and a class including the difference between them

Published by Patrick Mutisya · 14 days ago

ICT 0417 – Website Authoring: Styles and Classes

21. Website Authoring

Objective

Know and understand the characteristics of a style and a class in CSS, including the difference between them.

What is a Style?

A style is a set of CSS declarations that are applied directly to an HTML element. Styles can be added in three ways:

  • Inline style – using the style attribute inside an element.
  • Internal style sheet – placed within a <style> block in the document’s <head>.
  • External style sheet – linked with a <link rel="stylesheet"> element.

What is a Class?

A class is a reusable selector defined in a style sheet that can be applied to any number of HTML elements using the class attribute.

Key Characteristics

CharacteristicStyle (inline)Class (external/internal)
Location of definitionInside the HTML tag using style="…"In a CSS rule set, usually in a <style> block or external .css file
ReusabilityApplied to a single element onlyCan be applied to many elements across the site
SpecificityHigher specificity than class selectorsLower specificity than inline styles but higher than element selectors
MaintenanceDifficult – changes must be made on each elementEasy – change the class definition once to affect all elements using it
Typical useQuick, one‑off styling or testingConsistent styling for groups of elements (e.g., navigation menus, buttons)
HTML attribute namestyleclass

How to Apply a Style Directly (Inline)

Example:

<p style="color: red; font-weight: bold;">Important notice</p>

How to Define and Use a Class

Define the class in a style sheet:

<style>

.highlight {

color: red;

font-weight: bold;

}

</style>

Apply the class to any element:

<p class="highlight">Important notice</p>

Difference Summarised

  1. Scope – Inline styles affect only the element they are written on; classes can affect many elements.
  2. Maintainability – Classes promote easier maintenance and consistency.
  3. Specificity – Inline styles override class rules unless !important is used.
  4. Best practice – Use classes for regular styling; reserve inline styles for temporary changes or email HTML where external CSS is limited.

Suggested diagram: Flowchart showing when to use an inline style versus a class selector, with examples of each.

Quick Checklist for Choosing Between Style and Class

  • Is the styling needed on only one element? → Consider an inline style.
  • Will the same styling be reused? → Create a class.
  • Do you need to keep the HTML clean and separate content from presentation? → Use a class.
  • Are you working on a large site with many pages? → Classes (external stylesheet) are essential.