HTML & CSS Intro Course: CSS Classes
Master CSS Classes for Targeted Element Styling
This video transcription covers the fundamental concept of CSS classes, building upon basic HTML and CSS knowledge. Classes provide a way to apply specific styling to selected elements without affecting all elements of the same type.
CSS Targeting Methods
Tag Selectors
Target all elements of a specific type (p, h1, div). Most generic approach with broad application across the entire page.
ID Selectors
Target a single unique element using #idname. Can only be used once per page, making it very specific but limited.
Class Selectors
Target multiple elements using .classname. Reusable throughout the page and more specific than tag selectors.
ID vs Class Comparison
| Feature | ID Attribute | Class Attribute |
|---|---|---|
| Usage Frequency | Once per page only | Multiple times per page |
| CSS Selector | #idname | .classname |
| Specificity | Very high | Medium |
| Flexibility | Limited | High |
Creating and Styling CSS Classes
Add Class Attribute
Add class attribute to HTML element: <p class="author">. Class names are case-sensitive and cannot contain spaces.
Create CSS Rule
Write CSS selector with period prefix: .author { }. The period indicates you're targeting a class, not an HTML tag.
Apply Styles
Add CSS properties inside curly braces: font-size, color, text-transform, etc. Multiple properties can be applied to the same class.
Reuse Class
Apply the same class to multiple HTML elements throughout your page. All elements with that class will inherit the styling.
When you don't know a CSS property, Google is your best friend. Search for 'CSS' plus what you want to achieve (like 'CSS uppercase text'). The W3C specifications exist but are dense - practical searches yield better results for day-to-day development.
Common CSS Properties for Text Styling
Font Properties
font-size controls text size (pixels, em, rem). font-weight makes text bold or lighter. font-family changes the typeface.
Text Transform
text-transform: uppercase converts text to all caps. Also supports lowercase, capitalize, and none values.
Color and Opacity
color changes text color using hex codes from designers. opacity controls transparency from 0 (invisible) to 1 (fully visible).
When multiple CSS rules target the same element, specificity determines which rule wins. Classes are more specific than tag selectors, so .author will override p styling. Order of CSS rules matters less than specificity.
A class rule is more specific and therefore wins over a more generic tag style. The order of these styles do not matter.
Working with Span Elements
Identify Ungrouped Content
Find text or inline content that needs styling but isn't wrapped in an HTML tag. Plain text cannot have classes applied directly.
Add Span Tag
Wrap the content in <span> tags. Span elements are invisible containers that don't change visual appearance by default.
Apply Class to Span
Add class attribute to the span: <span class="muted">. Now you can target this specific section of content with CSS.
Style the Span Class
Create CSS rule for the span class: .muted { opacity: 0.5; }. This allows precise control over inline content styling.
CSS Class Best Practices
Avoid case sensitivity issues and maintain consistency across your codebase
Names like 'author' or 'muted' describe purpose better than 'red-text' or 'small'
Spaces are not allowed in class names, so use 'author-bio' or 'author_bio'
Build from general to specific styling for maintainable CSS architecture
Designers provide exact hex codes to ensure consistent brand colors
Key Takeaways