Skillversitas

HTML Class and ID

Class and ID are used to give HTML elements an identity for styling and manipulation.

Class

<div class="container">Content</div>
<p class="text bold">Paragraph</p>
<div class="card header">Card</div>

CSS Class

.container {
    max-width: 1200px;
    margin: 0 auto;
}

.text {
    color: #333;
}

.bold {
    font-weight: bold;
}

ID

<div id="header">Header</div>
<div id="content">Content</div>
<div id="footer">Footer</div>

CSS ID

#header {
    background: #333;
    color: white;
}

#content {
    padding: 20px;
}

#footer {
    background: #333;
    color: white;
}

Differences Between Class and ID

Feature Class ID
Quantity Many One
Selector .classname #idname
Specificity Low High
Reuse Yes No

Case Study: Campus Dashboard

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Campus Dashboard</title>
    <style>
        .container {
            max-width: 1200px;
            margin: 0 auto;
            padding: 20px;
        }

        .card {
            background: white;
            border: 1px solid #e5e7eb;
            border-radius: 8px;
            padding: 20px;
            margin: 10px 0;
        }

        .card-primary {
            border-left: 4px solid #1e40af;
        }

        .card-success {
            border-left: 4px solid #059669;
        }

        .card-warning {
            border-left: 4px solid #f59e0b;
        }

        #sidebar {
            width: 250px;
            background: #1f2937;
            color: white;
            padding: 20px;
        }

        #main-content {
            flex: 1;
            padding: 20px;
        }
    </style>
</head>
<body>
    <div class="container">
        <div id="sidebar">
            <h2>Menu</h2>
            <ul>
                <li>Dashboard</li>
                <li>Students</li>
                <li>Lecturers</li>
            </ul>
        </div>

        <div id="main-content">
            <h1>Dashboard</h1>

            <div class="card card-primary">
                <h3>Active Students</h3>
                <p>1,234 students</p>
            </div>

            <div class="card card-success">
                <h3>Lecturers</h3>
                <p>56 lecturers</p>
            </div>

            <div class="card card-warning">
                <h3>Study Programs</h3>
                <p>12 programs</p>
            </div>
        </div>
    </div>
</body>
</html>

Everyday Case Study

Case: A developer needs to create a form with validation.

<style>
    .form-group {
        margin-bottom: 15px;
    }

    .form-label {
        display: block;
        margin-bottom: 5px;
        font-weight: bold;
    }

    .form-input {
        width: 100%;
        padding: 10px;
        border: 1px solid #ccc;
        border-radius: 4px;
    }

    .form-input.error {
        border-color: red;
    }

    .error-message {
        color: red;
        font-size: 12px;
        display: none;
    }

    #form-submit {
        background: #1e40af;
        color: white;
        padding: 10px 20px;
        border: none;
        border-radius: 4px;
        cursor: pointer;
    }
</style>

<form>
    <div class="form-group">
        <label class="form-label" for="name">Name</label>
        <input type="text" id="name" class="form-input" required>
        <span class="error-message">Name is required</span>
    </div>

    <button type="submit" id="form-submit">Save</button>
</form>

Best Practices

  1. Use classes: For reusable styling
  2. Use IDs: For unique elements
  3. Don't use IDs for CSS: Use classes
  4. Naming convention: Use snake_case or camelCase
  5. Specificity: Understand the priority order

Summary

Class and ID give HTML elements an identity. Use classes for styling and IDs for JavaScript manipulation.