CSS element.class Selector

Beginner
⏱️ 6 min read
📚 Updated: Jul 2026
🎯 4 Examples
Basic Selectors

What You’ll Learn

The element.class selector combines a tag name with a class name for precise targeting. Use it when the same class appears on different HTML elements but needs different styles.

01

Tag + class

No space.

02

p.intro

Paragraphs only.

03

Specificity

Beats .class.

04

div.card

Not span.card.

05

Reuse class

Same name.

06

Precision

Fewer conflicts.

Introduction

The CSS element.class selector lets you apply styles to specific HTML elements that possess a particular class attribute. It combines a type selector (the element name) with a class selector for greater precision than .class alone.

This pattern is a powerful way to target elements with greater control, making your web design more flexible while keeping class names reusable across different tags.

Definition and Usage

Write the HTML tag immediately followed by a dot and the class name — with no space between them. For example, p.highlight matches only <p class="highlight">, not a <div class="highlight">.

💡
Beginner Tip

Do not confuse p.highlight (no space — one element with both tag and class) with p .highlight (with space — a .highlight element nested inside a <p>).

📝 Syntax

The syntax for the element.class selector is:

syntax.css
element.class {
  /* CSS properties */
}

Replace element with the HTML tag and class with the class name. The selector applies only to instances of that element that have the specified class.

Basic Example

element-class-basic.css
h1.title {
  color: #2563eb;
  font-size: 2rem;
}

p.intro {
  font-weight: 700;
  font-style: italic;
}

p.highlight {
  background: #fef9c3;
  border: 1px solid #fbbf24;
  padding: 0.5rem;
}
p.intro div.card button.btn li.nav-item

Specificity Comparison

SelectorSpecificityMatches
.highlight0, 1, 0Any element with class highlight
p.highlight0, 1, 1Only <p class="highlight">
div.card0, 1, 1Only <div class="card">
No space (element.class) p.highlight — one element that is both a <p> and has class highlight.
With space (descendant) p .highlight — any .highlight element inside a <p>.

Syntax Rules

  • Write the element and class with no space between them.
  • Class names are case-sensitive — p.Intro and p.intro are different.
  • The element must match the HTML tag exactly (use lowercase for HTML5 tags).
  • You can chain more classes: button.btn.btn-primary (no spaces between classes).
  • Higher specificity than .class alone — useful for overrides.

Related Selectors

  • .class — matches any element with a class
  • element — matches all elements of a tag type
  • #id — matches one unique element by id
  • element element — descendant combinator (note the space)

⚡ Quick Reference

QuestionAnswer
Selector typeCompound (type + class)
Syntaxelement.classname — no space
Examplep.intro { font-weight: 700; }
Specificity0, 1, 1 (one class + one element)
vs .class aloneMore specific; limits to one tag type
Browser supportAll browsers

When to Use element.class

element.class is ideal when you need tag-specific styling with a shared class name:

  • Reusable components — Style div.card as a block and span.card as an inline badge.
  • Blog typography — Apply p.intro only to paragraph intros, not headings.
  • Button systems — Differentiate button.btn from a.btn link-buttons.
  • Navigation lists — Target li.nav-item without affecting other list items.
  • Override .class rules — Increase specificity without adding extra class names to HTML.

👀 Live Preview

Only elements matching both the tag and class receive styles. Plain paragraphs without a class are unaffected:

Welcome to My Blog

This is a place to share my coding adventures.

Don’t forget to check out the latest updates!

This paragraph has no class and stays unstyled.

Examples Gallery

Practice element.class with blog layouts, card components, button variants, and navigation items.

📜 Core Patterns

Combine tag names with class names for precise targeting.

Example 1 — Blog title, intro, and highlight

Style h1.title, p.intro, and p.highlight on a blog page.

blog-element-class.css
h1.title {
  color: #2563eb;
  font-size: 2.5rem;
}

p.intro {
  font-weight: 700;
  font-style: italic;
}

p.highlight {
  background-color: #fef9c3;
  border: 1px solid #fbbf24;
  padding: 0.5rem;
}
Try It Yourself

How It Works

Each selector targets a specific tag + class pair. A plain <p> without a class is ignored, and a <div class="highlight"> would not match p.highlight.

Example 2 — div.card vs span.card

Reuse the card class on different elements with tag-specific styles.

card-element-class.css
div.card {
  background: #eff6ff;
  border: 1px solid #93c5fd;
  color: #1e40af;
  padding: 0.75rem 1rem;
  border-radius: 8px;
}

span.card {
  background: #fef3c7;
  border: 1px solid #fcd34d;
  color: #92400e;
  padding: 0.35rem 0.65rem;
  border-radius: 999px;
}
Try It Yourself

How It Works

Both elements share class="card", but div.card and span.card produce different visual treatments based on the element type.

📄 Components & UI

Differentiate buttons, links, and navigation with the same class name.

Example 3 — button.btn vs a.btn

Style real buttons and link-buttons differently while sharing a base .btn class.

btn-element-class.css
.btn {
  padding: 0.5rem 1rem;
  border-radius: 0.4rem;
  font-weight: 600;
  cursor: pointer;
}

button.btn {
  background: #2563eb;
  color: #fff;
  border: none;
}

a.btn {
  background: #f0fdf4;
  color: #15803d;
  border: 2px solid #16a34a;
  text-decoration: none;
  display: inline-block;
}
Try It Yourself

How It Works

.btn sets shared sizing and typography. button.btn and a.btn add element-specific colors so actions and navigation links look distinct but feel cohesive.

Example 4 — li.nav-item navigation

Target only list items with the nav-item class inside a navigation menu.

nav-element-class.css
li.nav-item {
  list-style: none;
}

li.nav-item a {
  display: block;
  padding: 0.5rem 0.85rem;
  color: #1e293b;
  text-decoration: none;
  border-radius: 0.35rem;
  font-weight: 600;
}

li.nav-item a:hover {
  background: #e2e8f0;
}
Try It Yourself

How It Works

li.nav-item removes bullets only from navigation list items, leaving other <li> elements elsewhere on the page unaffected.

💬 Usage Tips

  • Use meaningful class names — Names like intro, card, and nav-item make CSS easier to read.
  • Combine with other selectors — Chain with ids or attribute selectors for even more precision when needed.
  • Start with .class — Add the element prefix only when you need tag-specific overrides.
  • Keep HTML semantic — Use the correct tag; do not rely on CSS alone to change element meaning.
  • Document shared classes — When one class name spans multiple tags, note which element.class rules apply where.

⚠️ Common Pitfalls

  • Typos in class names — Class names are case-sensitive; p.Intro will not match class="intro".
  • Accidental spacep .highlight is a descendant selector, not element.class.
  • Wrong tagdiv.highlight will not style <p class="highlight">.
  • Over-specificity — Adding element prefixes everywhere makes CSS harder to override later.
  • Multiple classes — Ensure chained selectors like p.intro.featured match elements that have all listed classes.

♿ Accessibility

  • Use semantic HTML — Choose the correct element (button for actions, a for links) rather than styling the wrong tag.
  • Color contrast — Ensure text styled with element.class meets WCAG contrast requirements.
  • Do not hide content — Avoid using selectors to visually remove important information from sighted users.
  • Focus states — Add :focus-visible styles for interactive elements like a.btn.
  • Meaningful structure — Use headings (h1.title) in proper order for screen reader navigation.

🧠 How element.class Works

1

Browser finds elements

The engine scans the DOM for elements matching the tag name, such as all <p> elements.

DOM
2

Filter by class

Only elements whose class attribute includes the specified class name remain.

Filter
3

Apply CSS rules

Matching elements receive the declared properties from the element.class rule.

Style
=

Precise styling

Only the intended tag + class combinations are styled, reducing unintended side effects.

🖥 Browser Compatibility

The element.class compound selector is supported in all modern browsers and has been available since CSS1.

Baseline · Universal support

Compound selectors everywhere

element.class works reliably in Chrome, Firefox, Safari, Edge, and Opera.

99% Universal support
Google Chrome All versions · Desktop & Mobile
Full support
Mozilla Firefox All versions · Desktop & Mobile
Full support
Apple Safari All versions · macOS & iOS
Full support
Microsoft Edge All versions
Full support
Opera All modern versions
Full support
element.class compound selector 99% supported

Bottom line: element.class is safe for all projects that combine tag names with class attributes.

🎉 Conclusion

The CSS element.class selector is a fundamental tool that empowers you to style elements precisely and effectively. By combining a tag name with a class name, you gain higher specificity and tighter control over which elements receive your styles.

Use it when the same class name appears on different HTML elements but needs different visual treatments — keeping your code organized, reusable, and maintainable.

💡 Best Practices

✅ Do

  • Use element.class when tags need different styles
  • Write tag and class with no space
  • Pair with meaningful, reusable class names
  • Check specificity when overriding rules
  • Prefer semantic HTML tags

❌ Don’t

  • Add a space between element and class
  • Misspell or mismatch class name casing
  • Overuse element prefixes on every rule
  • Style the wrong tag for accessibility
  • Confuse element.class with descendant selectors

Key Takeaways

Knowledge Unlocked

Five things to remember about element.class

Use these points when combining tag names with classes.

5
Core concepts
0,1,1 02

Specificity

Beats .class.

Cascade
div 03

Tag filter

One element.

Precision
card 04

Reuse

Same class.

Pattern
🌐 05

99% support

All browsers.

Compat

❓ Frequently Asked Questions

The element.class selector combines an HTML tag name with a class name — such as p.intro or div.card — to style only elements of that tag that also have the specified class.
.card matches any element with class="card". div.card is more specific — it only matches div elements that have that class, ignoring spans or articles with the same class name.
No. element.class is written with no space (p.highlight). A space would mean a descendant selector: p .highlight matches a .highlight element inside a p.
Yes. div.card (0,1,1) beats .card (0,1,0) because the element type adds one point to the type column in the specificity calculation.
Yes. Combining type and class selectors has been supported in every browser since CSS1 and works reliably in all modern projects.

Practice in the Live Editor

Open the HTML editor and experiment with p.intro, div.card, and other element.class patterns.

HTML Editor →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

5 people found this page helpful