惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

WordPress大学
WordPress大学
aimingoo的专栏
aimingoo的专栏
月光博客
月光博客
博客园 - Franky
Martin Fowler
Martin Fowler
U
Unit 42
阮一峰的网络日志
阮一峰的网络日志
Recent Announcements
Recent Announcements
The Cloudflare Blog
博客园 - 聂微东
酷 壳 – CoolShell
酷 壳 – CoolShell
宝玉的分享
宝玉的分享
J
Java Code Geeks
B
Blog RSS Feed
博客园 - 三生石上(FineUI控件)
MongoDB | Blog
MongoDB | Blog
腾讯CDC
博客园_首页
博客园 - 司徒正美
D
DataBreaches.Net
I
InfoQ
GbyAI
GbyAI
IT之家
IT之家
罗磊的独立博客

DEV Community

Authentication Security Deep Dive: From Brute Force to Salted Hashing (With Java Examples) Why AI Systems Don’t Fail — They Drift Spilling beans for how i learn for exam😁"Reinforcement Learning Cheat Sheet" I Replaced Chrome with Safari for AI Browser Automation. Here's What Broke (and What Finally Worked) How Python Borrows Other People's Work The $40 Architecture: Processing 1 Billion API Requests with 99.99% Uptime Vibe Coding: A Workflow Guide (From Zero to SaaS) Most webhook security guides protect the wrong side. The scary part is delivery. Headless CMS for TanStack Start: Build a Blog with Cosmic EU Age Verification App "Hacked in 2 Minutes" — What Actually Happened Comfy Cloud’s delete function does not actually remove files Running AI Models on GPU Cloud Servers: A Beginner Guide Event-driven media intelligence with AWS Step Functions and Bedrock I scored 500 AI prompts across 8 quality dimensions — here's what broke How to Call Google Gemini API from Next.js (Free Tier, No Backend Needed) The Portal Protocol: Reclaiming Human Connection in the Age of AI How to Fix Your Team's Scattered Knowledge Problem With a Self-Hosted Forum Intro to tc Cloud Functors: A Graph-First Mental Model for the Modern Cloud Designing Multi-Tenant Backends With Both Ownership and Team Access I Built a Neumorphic CSS Library with 77+ Components — Here's What I Learned PostgreSQL Performance Optimization: Why Connection Pooling Is Critical at Scale Cómo construí un SaaS multi-rubro para gestionar expensas en Argentina con FastAPI + Vue 3 🚀 I Built an Ethical Hacking Scanner Tool – Open Source Project I Replaced /usage and /context in Claude Code With a Single Statusline A Pythonic Way to Handle Emails (IMAP/SMTP) with Auto-Discovery and AI-Ready Design I Collected 8.9 Million Polymarket Price Points — Here's What I Found About How Markets Really Move EcoTrack AI — Carbon Footprint Tracker & Dashboard Everyone's Using AI. No One Agrees How. 5 self-hosted ebook managers worth trying in 2026 Building Your First AI Agent with LangChain: From Chatbot to Autonomous Assistant
Class and Pseudo Class
Saravanan Lakshmanan · 2026-05-25 · via DEV Community
Cover image for Class and Pseudo Class

Saravanan Lakshmanan

HTML Class Attribute
The class attribute in HTML assigns one or more class names to elements so they can be styled with CSS or manipulated using JavaScript. It helps group elements for consistent design and behavior.

  • Used to apply CSS styles to multiple elements at once.
  • Allows JavaScript to select and manipulate elements.
  • Supports multiple class names in a single element.

Syntax
<tag class="classname"></tag>

Examples of HTML Class Attribute
Here is a basic example of an HTML Class Attribute:

1. Using the Same Class in Multiple HTML Tags
The HTML class attribute can be applied to various tags, allowing multiple elements to share a common classification. This enables consistent styling or functionality across different types of elements, enhancing design cohesion and simplifying maintenance.
Example: This example shows the use of the classes in HTML.

<!DOCTYPE html>
<html>

<head>
    <style>
        .country {
            background-color: black;
            color: white;
            padding: 8px;
        }
    </style>
</head>

<body>
    <h2 class="country">CHINA</h2>

    <p>
        China has the largest population
        in the world.
    </p>

    <h2 class="country">INDIA</h2>

    <p>
        India has the second largest
        population in the world.
    </p>

    <h2 class="country">UNITED STATES</h2>

    <p>
        United States has the third largest
        population in the world.
    </p>
</body>

</html>

  • In the above example each heading (<h2>) is assigned the class "country" using the class attribute.
  • The CSS selector .country targets multiple elements with the class "country" to apply styling.
  • Styling defined for the "country" class is applied uniformly to all headings tagged with it.
  • Using class attributes ensures consistent styling across headings, simplifying design management.

2. Using Multiple Classes on a Single Element
HTML allows an element to have multiple classes by separating class names with spaces. This enables a more modular and flexible approach to styling, where an element can share common styles but also have unique styles.

Example: In this example, we will use more than one class.

<!DOCTYPE html>
<html>

<head>
    <style>
        .country {
            color: white;
            padding: 10px;
        }

        .china {
            background-color: black;
        }

        .india {
            background-color: blue;
        }

        .usa {
            background-color: red;
        }

        center {
            padding: 20px;
        }
    </style>
</head>


<body>
    <center>
        <h2 class="country china">CHINA</h2>
        <h2 class="country india">INDIA</h2>
        <h2 class="country usa">UNITED STATES</h2>
    </center>
</body>

</html>

  • In the above example <h2> elements are assigned the "country" class for shared styling attributes.
  • Additional classes like "china", "india", and "usa" provide unique background colors.
  • Classes set background colors to black, blue, and red, with white text and padding for visual contrast.
  • The <center> tag ensures horizontal alignment of content, improving the presentation and readability of the page.

CSS Pseudo-classes
A pseudo-class is a keyword added to a CSS selector, prefixed by a colon (:), to define a specific state or condition of an element. It is used to style elements like a hovered button, the first child of a container, or checked input fields.
Syntax

selector:pseudo-class {
    /* styles */
}

Interactive/User Action Pseudo-Classes
1. :hover
This applies when the user hovers over an element.

<html>
<head>
    <style>
        button:hover {
            background-color: lightblue;
            color: white;
        }
    </style>
</head>
<body>
    <button>Hover over me!</button>
</body>
</html>

This will change the background color of the button when hovered over.

2. :focus
Applies when an element receives focus (e.g., a text input clicked)

<html>
<head>
    <style>
        input:focus {
            border: 2px solid blue;
            outline: none;
        }
    </style>
</head>
<body>
    <input type="text" placeholder="Click to focus">
</body>
</html>

This will change the border of the input field to blue when it is focused.

3. :active
Applies when an element is being clicked.

<html>
<head>
    <style>
        button:active {
            background-color: darkblue;
            color: white;
        }
    </style>
</head>
<body>
    <button>Click me!</button>
</body>
</html>

This will change the background color of the button when it is being clicked (in the active state).

4. :visited
Applies to links the user has visited.

<html>
<head>
    <style>
        a:visited {
            color: purple;
        }
    </style>
</head>
<body>
    <a href="https://www.example.com//">Visit this link</a>
</body>
</html>

This will change the color of visited links to purple.

5. :link
Applies to links that the user has not visited yet.

<html>
<head>
    <style>
        a:link {
            color: green;
        }
    </style>
</head>
<body>
    <a href="https://www.example.com//">Visit this link</a>
</body>
</html>

This will make unvisited links appear in green.

References:
https://www.geeksforgeeks.org/css/css-pseudo-classes/
https://www.w3schools.com/css/css_pseudo_classes.asp
https://www.geeksforgeeks.org/html/html-class-attribute/
https://www.w3schools.com/html/html_classes.asp
https://pwskills.com/blog/web-development/html-class
https://developer.mozilla.org/en-US/docs/Web/CSS/Reference/Selectors/Pseudo-classes