CSS Class Selectors: Free HTML & CSS Tutorial
Master CSS Class Selectors and Element Targeting
Core Concepts You'll Learn
Class Attributes
Learn how to add class attributes to HTML elements to create unique identifiers for styling purposes.
CSS Class Selectors
Master the dot notation syntax to target specific elements with custom styles that override default tag selectors.
Span Tag Implementation
Discover how to use span tags to wrap inline content and apply targeted styling to specific text portions.
This exercise builds on previous lessons about CSS tag selectors. Make sure you understand how basic tag selectors work before proceeding with class-based targeting.
Preview the file in your browser to establish a baseline view of the current styling. This will help you clearly see the changes as you implement class-based styling.
TIP: If you're using Visual Studio Code with the open in browser extension installed, press Option–B (Mac) or ALT–B (Windows) to instantly open your HTML file in your default browser. This keyboard shortcut will save you significant time during development.
Quick Setup for New Learners
Open Starting File
Close any open files and locate index-ready-for-classes.html in your News website folder using your code editor.
Save As New Version
Use File > Save As to save the file as index.html, replacing any older version in your project folder.
Preview in Browser
Open the HTML file in your browser to see the current appearance before making changes.
Visual Studio Code users with the open in browser extension can use Option-B (Mac) or ALT-B (Windows) to quickly open HTML files in the default browser.
Tag Selectors vs Class Selectors
| Feature | Tag Selectors | Class Selectors |
|---|---|---|
| Scope | All instances of tag | Specific elements only |
| Syntax | p { } | .classname { } |
| Specificity | Lower priority | Higher priority |
| Reusability | Automatic on all tags | Applied where needed |
Choose class names that describe what the element IS (like 'author') rather than how it LOOKS (like 'uppercase'). This maintains flexibility if you change styling later.
Implementing Your First Class Selector
Add Class Attribute
Find the first 'This report was written by...' paragraph and add class="author" to the opening p tag.
Create CSS Rule
In the style section, add .author selector with font-size: 10px, text-transform: uppercase, font-weight: bold, and color: #a18f81.
Test and Verify
Save the file and reload in browser to see the first author paragraph styled differently from regular paragraphs.
Understanding the Span Element
Purpose
Span is an inline HTML element used to group text or other inline elements for styling purposes without affecting layout.
Use Cases
Perfect for applying styles to portions of text within larger elements, like making part of a heading different.
CSS Properties
Commonly used with opacity, color, font-weight, and other styling properties to create visual emphasis or hierarchy.
CSS Opacity Values Guide
Class names are case sensitive and cannot contain spaces or special characters except hyphens and underscores. Use descriptive names like 'heading-muted' instead of generic names.
Key Takeaways
