Continue to learn to code HTML & CSS in our NYC Web Development classes. For those outside New York, find and compare the best HTML & CSS classes near you or live online HTML & CSS classes.
Video Transcription
Understanding how CSS classes work is fundamental to writing maintainable, scalable code. Classes provide a powerful way to name elements and apply targeted styling—a skill that separates professional developers from beginners. Let's examine how to leverage this essential tool effectively.
Consider a webpage where certain paragraphs need different styling than others. While we might have a general paragraph rule that applies to all <p> tags, how do we differentiate specific paragraphs without affecting the entire set? This is where naming conventions become crucial, and we have two primary options: IDs and classes.
An ID serves as a unique identifier—think of it as a Social Security number for HTML elements. You can only use a specific ID once per page. If you create an ID called "author," that name is reserved for a single element on that webpage. While you could reuse the same ID on different pages, within any given HTML document, each ID must be unique.
Classes offer more flexibility. Unlike IDs, classes function as reusable labels that can be applied to multiple elements. Think of real-world classifications: social classes, academic classes, or professional categories. Just as multiple people can belong to the same socioeconomic class, multiple HTML elements can share the same CSS class name. This reusability makes classes the preferred choice for most styling scenarios.
The beauty of classes lies in their versatility. You can apply a class to any HTML element—paragraphs, headers, divs, spans, images, or any other tag. The class serves as a "hook" that allows your CSS to target specific elements with surgical precision. When you assign a class to an element, nothing visually changes immediately—the class name itself isn't displayed. However, you've created a connection point that CSS can reference.
To target a class in CSS, you prefix the class name with a period (.). This syntax distinguishes class selectors from element selectors. Without the period, CSS would interpret "author" as an HTML tag (like <author>), which doesn't exist in standard HTML. The period tells CSS: "find all elements with this specific class name."
Here's a practical example: suppose you want author bylines to appear smaller and less prominent than regular paragraph text. You might apply these styles to your .author class:
```css
.author {
font-size: 10px;
text-transform: uppercase;
font-weight: bold;
color: #666666;
}
```
When you're uncertain about specific CSS properties, remember that modern web development relies heavily on research and documentation. The W3C (World Wide Web Consortium) and WHATWG (Web Hypertext Application Technology Working Group) maintain the official specifications, but these comprehensive documents can be overwhelming—like reading a dictionary to learn a language.
In practice, targeted Google searches prove more efficient. Searching "CSS text uppercase" or "CSS make text all caps" will quickly lead you to the text-transform: uppercase property. This approach mirrors real-world development, where even senior developers regularly reference documentation and search for specific solutions. The key is knowing what you want to achieve, then finding the right property to implement it.
Understanding CSS specificity becomes crucial when multiple rules target the same element. In our author example, both the general paragraph style and the specific author class style apply to the same elements. When conflicts arise—such as different font sizes—CSS must determine which rule takes precedence.
CSS specificity follows a hierarchy: more specific selectors override less specific ones. A class selector (.author) is more specific than a general element selector (p), so class rules win conflicts with element rules. This isn't about the order of your CSS declarations—specificity trumps order in most cases. The element retains non-conflicting properties from both rules, building up a "cascade" of styles that gives CSS its name.
Sometimes you need to create a class target where no suitable HTML element exists. The <span> tag serves this purpose perfectly—it's a semantic-neutral container that groups content without adding visual formatting. Spans are inline elements that wrap around text or other inline content, providing a hook for CSS styling without disrupting document flow.
For instance, to style a subtle headline like "Latest news from The Onion," you might wrap it in a span with a descriptive class name:
```html
<span class="heading-muted">Latest news from The Onion</span>
```
CSS class names follow specific rules: no spaces (use hyphens or underscores instead), case-sensitive, and typically lowercase for consistency. Descriptive names like "heading-muted," "author-byline," or "featured-content" make your code self-documenting and easier to maintain.
The opacity property provides an elegant way to de-emphasize content. Values range from 0 (completely transparent) to 1 (fully opaque). Setting opacity: 0.6 creates 60% opacity, making text visually recede while maintaining readability. Note that leading zeros are optional in CSS—opacity: .6 works identically.
This foundation in CSS classes opens the door to more advanced techniques. Professional developers use classes to create modular, reusable components, implement design systems, and maintain consistent styling across large applications. Master these fundamentals, and you'll have the building blocks for sophisticated web development—whether you're crafting a simple website or contributing to enterprise-level applications.
The principles demonstrated here—semantic naming, strategic specificity, and thoughtful organization—scale from personal projects to complex applications used by millions. Start with solid fundamentals in class usage, and you'll develop the instincts that separate skilled developers from those still learning the craft.
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