Skip to main content
March 23, 2026/5 min read

HTML & CSS Intro Course: CSS Classes

Master CSS Classes for Targeted Element Styling

Course Foundation

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.

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

FeatureID AttributeClass Attribute
Usage FrequencyOnce per page onlyMultiple times per page
CSS Selector#idname.classname
SpecificityVery highMedium
FlexibilityLimitedHigh
Recommended: Use classes for styling multiple elements and IDs for unique page elements that need JavaScript interaction.

Creating and Styling CSS Classes

1

Add Class Attribute

Add class attribute to HTML element: <p class="author">. Class names are case-sensitive and cannot contain spaces.

2

Create CSS Rule

Write CSS selector with period prefix: .author { }. The period indicates you're targeting a class, not an HTML tag.

3

Apply Styles

Add CSS properties inside curly braces: font-size, color, text-transform, etc. Multiple properties can be applied to the same class.

4

Reuse Class

Apply the same class to multiple HTML elements throughout your page. All elements with that class will inherit the styling.

CSS Property Research

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).

CSS Specificity Rules

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.
Understanding CSS specificity is crucial for predictable styling behavior.

Working with Span Elements

1

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.

2

Add Span Tag

Wrap the content in <span> tags. Span elements are invisible containers that don't change visual appearance by default.

3

Apply Class to Span

Add class attribute to the span: <span class="muted">. Now you can target this specific section of content with CSS.

4

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

0/5

Key Takeaways

1CSS classes allow you to apply specific styling to selected elements without affecting all elements of the same HTML tag type, providing more granular control than tag selectors alone.
2Classes can be reused multiple times on a page, unlike IDs which can only be used once, making them ideal for consistent styling patterns across multiple elements.
3CSS specificity rules determine which styles apply when multiple rules target the same element, with classes being more specific than tag selectors regardless of order in the stylesheet.
4The period prefix (.classname) is essential when writing CSS rules for classes, distinguishing them from HTML tag selectors and ID selectors which use the hash symbol.
5Span elements serve as invisible containers for applying classes to inline content that isn't already wrapped in HTML tags, enabling precise styling control.
6When elements have multiple applicable styles (like both paragraph and class rules), CSS combines them rather than replacing them entirely, with more specific rules overriding conflicting properties.
7Professional development often involves copying exact specifications like colors and fonts from designers, emphasizing the collaborative nature of web development projects.
8Google searching for CSS properties is more practical than reading W3C specifications directly, making 'CSS' plus your desired effect an efficient learning approach.

RELATED ARTICLES