Learning how to create CSS classes is one of the most important and frequently used skills in any CSS tutorial — CSS classes let you apply the same styles to multiple HTML elements, group elements for specific styling, reuse code efficiently, and build clean, maintainable websites. Unlike tag selectors that style all instances of an element, it gives you precise control — style one paragraph red, another yellow, or give special treatment to selected images, buttons, or sections.
In this 2025–2026 Progressive Robot guide, you’ll master exactly how to create CSS classes: class selector syntax (.class-name), applying classes to text and images, using multiple classes on one element, real-world examples, best practices, and common patterns. All examples build on your css-practice project.
Prerequisites
- Your css-practice project folder from previous lessons:
- index.html (linked to css/styles.css)
- css/styles.css
- images/ folder (with at least one image)
- Visual Studio Code (or any editor) with Live Server recommended
- Browser for preview
1. How Does It Work – The Core Concept
These are defined with a dot . followed by your class name:
/* This creates a CSS class called "red-text" */
.red-text {
color: red;
}
To use the class in HTML:
This text will be red.
The class name is not prefixed with . in HTML — only in CSS.
2. Create Your First CSS Classes – Hands-On
Open css/styles.css and add this rule:
.red-text {
color: red;
}
Save the file.
Now open index.html and add a paragraph with the class:
A Sample Title
Here is the first sample of paragraph text.
Save and refresh — the paragraph text should turn red.
This is how to create CSS classes and apply them — one class, many elements.
3. Create More CSS Classes for Different Styles
Add these rules to styles.css:
.yellow-background-text {
background-color: yellow;
padding: 15px; /* inner spacing */
border-radius: 8px; /* rounded corners */
}
.large-heading {
font-size: 48px;
text-align: center;
color: #0066cc;
margin: 40px 0;
}
Update index.html to test them:
Welcome to Progressive Robot
This text is red – created with CSS classes.
This paragraph has a yellow background and rounded corners.
This combines both CSS classes – red text on yellow background!
Refresh — you’ll see:
- Large centered blue heading
- Red text paragraph
- Yellow background paragraph
- Combined classes paragraph (red text + yellow background)
This shows the power of CSS classes — combine any number on one element.
4. Apply CSS Classes to Images
Add this rule to styles.css:
.profile-image {
border: 5px solid #0066cc;
border-radius: 50%; /* perfect circle */
width: 200px;
height: 200px;
object-fit: cover; /* crop nicely */
box-shadow: 0 4px 12px rgba(0, 0, 0, 0.2);
}
.hover-effect {
transition: transform 0.3s ease;
}
.hover-effect:hover {
transform: scale(1.1);
}
Update index.html with your image:

Refresh — your image is now:
- Circular with blue border
- Shadowed
- Zooms on hover
You just used CSS classes to completely transform an image.
5. Best Practices & Modern Tips
- Name classes semantically: .profile-image, .warning-text, .primary-button
- Avoid overly generic names: .red → .error-text
- Use multiple classes for flexibility: class=”profile-image hover-effect”
- Prefer classes over inline styles or tag selectors for maintainability
- Use BEM naming (Block__Element–Modifier) for large projects:
.card__title--large { ... }
- Add comments for complex classes:
/* Primary call-to-action button – used on landing pages */
.cta-button { ... }
FAQs
- How do I create?
.class-name { property: value; } — basic syntax to create CSS classes. - How do I apply It to HTML elements?
class=”class-name” or multiple class=”class1 class2″ - Can one element have multiple CSS classes?
Yes — class=”red-text yellow-background large-text” — all styles apply. - What’s the difference between class and ID in CSS?
Class (.class) — reusable on many elements; ID (#id) — unique per page. - Why use it instead of tag selectors?
Classes give precise control — style specific elements without affecting all <p> tags.
Summary
You now know exactly how to create CSS classes: class selector syntax (.class-name), applying to text and images, using multiple classes, real-world styling (borders, backgrounds, hover effects), and Progressive Robot best practices.
Mastering CSS classes is the key to professional, maintainable styling — reuse styles, target specific elements, and build complex designs efficiently.
Keep your css-practice folder open — in the next lessons you’ll learn CSS IDs, the box model, Flexbox/Grid layouts, and responsive design.