CSS :only-of-type Selector

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

What You’ll Learn

The :only-of-type pseudo-class targets an element that is the only one of its tag type among siblings — other element types may still be present.

01

By type

Same tag only.

02

Solo p, h2, li

Type patterns.

03

vs only-child

Know the diff.

04

= first:last

Of that type.

05

Mixed tags

Div + p OK.

06

Articles

Solo headings.

Introduction

The :only-of-type selector in CSS targets an element that is the only one of its type within its parent. This pseudo-class is useful when you need to style a single paragraph, heading, or list item of a given tag without requiring it to be the only child overall.

It enhances precision in mixed-content layouts where headings, paragraphs, and divs appear together as siblings.

Definition and Usage

Write element:only-of-type to match elements that are the sole sibling of their tag type inside a parent.

💡
Beginner Tip

:only-of-type is equivalent to :first-of-type:last-of-type. A lone <p> beside a <div> matches p:only-of-type but not p:only-child because the div is a sibling.

📝 Syntax

The syntax for the :only-of-type pseudo-class is:

syntax.css
element:only-of-type {
  /* CSS properties */
}
  • element is the tag you want to style (e.g. p, h2, li).
  • The rule applies only when no other sibling of that same tag type exists in the parent.

When It Matches

Parent HTMLDoes p:only-of-type match?
<div><p>Solo</p></div>Yes
<div><p>A</p><p>B</p></div>No (two p siblings)
<div><p>A</p><div>B</div></div>Yes (only p of its type)
<div><span>Note</span></div>No (tag is span, not p)
p:only-of-type h2:only-of-type li:only-of-type article > p:only-of-type

Related Selectors

  • :only-child — sole child with no siblings of any type
  • :first-of-type / :last-of-type — positional among same-type siblings
  • :nth-of-type() — pattern-based selection by type position
  • :not() — combine for exclusions like p:not(:only-of-type)

⚡ Quick Reference

QuestionAnswer
Selector typeStructural pseudo-class
Syntaxelement:only-of-type
Matches whenElement is the only sibling of its tag type
Equivalent to:first-of-type:last-of-type
Common useSolo paragraph, lone heading, single nav item of type
Browser supportAll modern browsers

When to Use :only-of-type

:only-of-type is ideal when tag type uniqueness matters more than overall sibling count:

  • Mixed-content articles — Style a lone <h2> even when paragraphs surround it.
  • Dynamic layouts — Apply special styling when only one paragraph renders in a section.
  • Navigation lists — Highlight a menu when it contains a single <li> of its type.
  • Footer blocks — Emphasize a sole <p> beside a <div> widget.
  • When only-child is too strict — Other tag types may legitimately sit as siblings.

👀 Live Preview

Blue styling applies via p:only-of-type in the first and third boxes. The middle box has two paragraphs, so neither matches:

Solo paragraph.

First paragraph.

Second paragraph.

Lone paragraph.

Footer div

Examples Gallery

Practice :only-of-type with solo paragraphs, mixed siblings, article headings, and navigation lists.

📜 Core Patterns

Style elements that are the only sibling of their tag type inside a parent.

Example 1 — Style the only paragraph in a container

Apply blue bold text to p:only-of-type when a container holds just one paragraph of that type.

only-of-type-basic.css
p:only-of-type {
  color: #1d4ed8;
  font-weight: 700;
}
Try It Yourself

How It Works

The first container has one <p>, so it matches. The second has two paragraph siblings of the same type, so neither matches :only-of-type.

Example 2 — Paragraph beside a div sibling

Show the key difference from :only-child — a <div> sibling does not block p:only-of-type.

only-of-type-mixed.html
<style>
  p:only-of-type {
    background: #dcfce7;
    border-left: 4px solid #16a34a;
    padding: 0.5rem 0.65rem;
  }
</style>

<article>
  <p>Lone paragraph in this section.</p>
  <div class="footer">Share link</div>
</article>
Try It Yourself

How It Works

The paragraph is the only <p> among siblings, so p:only-of-type matches. It is not :only-child because the footer div is also a child.

📄 Articles & Navigation

Target lone headings and single list items in real layouts.

Example 3 — Emphasize a lone heading

Style a single <h2> in an article even when multiple paragraphs follow.

only-of-type-heading.css
h2:only-of-type {
  color: #b91c1c;
  border-bottom: 2px solid #fecaca;
  padding-bottom: 0.35rem;
}
Try It Yourself

How It Works

Only one <h2> exists among siblings, so it matches even though two <p> elements are also present.

Example 4 — Style a navigation list with one item

Give a solo <li> distinct styling when li:only-of-type applies.

only-of-type-nav.css
li:only-of-type {
  background: #fef3c7;
  border-left: 4px solid #d97706;
  padding: 0.65rem 0.9rem;
  border-radius: 0.35rem;
}
Try It Yourself

How It Works

When a nav renders a single list item, li:only-of-type highlights it without adding a utility class. Add a second <li> and the rule stops applying.

💬 Usage Tips

  • Prefer over only-child in mixed layouts — When other tag types may sit as siblings.
  • Scope with parentsarticle > p:only-of-type limits to direct paragraph children.
  • Dynamic content — Great for CMS output where section structure varies.
  • Check same-type siblings — A second paragraph blocks the match, not a div.
  • Combine with :hoverp:only-of-type:hover for interactive solo elements.

⚠️ Common Pitfalls

  • Multiple elements of same type — Two <p> siblings means neither matches p:only-of-type.
  • Confusing with only-child — A lone <p> beside a <div> is not :only-child.
  • Wrong tag in selectorli:only-of-type will not match a solo <div>.
  • Expecting match across parents — The check is among siblings inside one parent only.
  • Specificity conflicts — General element rules may override unless ordered correctly.

♿ Accessibility

  • Contrast on highlights — Solo-type emphasis must still meet WCAG contrast requirements.
  • Do not rely on color alone — Pair visual cues with clear text or structure.
  • Semantic markup — Use proper heading, paragraph, and list tags for screen readers.
  • Reading order unchanged — Positional styling does not affect assistive technology order.

🧠 How :only-of-type Works

1

Filter by element type

The browser gathers siblings matching the tag before :only-of-type.

Type filter
2

Counts same-type siblings

Only elements of that tag type are counted; other types are ignored.

Type count
3

Verifies count equals 1

The selector matches only when exactly one sibling of that type exists.

Count = 1
=

Type-unique styling

Lone paragraphs, headings, and list items get special treatment in mixed layouts.

🖥 Browser Compatibility

:only-of-type is supported in all modern browsers and has been available since CSS3 Selectors.

Baseline · Universal support

Structural selectors everywhere

:only-of-type 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
:only-of-type pseudo-class 99% supported

Bottom line: :only-of-type is safe for articles, lists, and mixed-content layouts in all modern projects.

🎉 Conclusion

The CSS :only-of-type selector provides precise control over elements that are unique of their tag type within a parent. It is especially valuable in mixed-content layouts where :only-child would be too restrictive.

From solo paragraphs beside footer divs to lone headings in articles, mastering :only-of-type helps you write cleaner, more adaptive stylesheets.

💡 Best Practices

✅ Do

  • Use when other tag types may coexist as siblings
  • Scope with parent selectors like article > p:only-of-type
  • Check DevTools when a rule does not apply
  • Prefer over only-child in mixed HTML structures
  • Combine with :hover for interactive solo elements

❌ Don’t

  • Expect a match when two siblings share the same tag
  • Confuse :only-of-type with :only-child
  • Target the wrong element type in the selector
  • Assume it checks across multiple parents
  • Rely on highlight color alone for meaning

Key Takeaways

Knowledge Unlocked

Five things to remember about :only-of-type

Use these points when styling type-unique elements.

5
Core concepts
= 02

first:last

Of that type.

Syntax
03

vs only-child

Type vs all.

Compare
p 04

Mixed tags

Div + p OK.

Pattern
🌐 05

99% support

All browsers.

Compat

❓ Frequently Asked Questions

The :only-of-type pseudo-class matches an element only when it is the sole element of its tag type among its siblings. Other sibling elements of different types do not prevent a match.
Yes. An element that is both the first and last of its type among siblings matches :only-of-type. It is shorthand for that combination.
:only-of-type checks only the element's tag type among siblings. :only-child requires the element to have no siblings of any type. A lone p next to a div is :only-of-type but not :only-child.
Usually because another sibling of the same tag type exists. Two paragraphs in the same parent means neither matches p:only-of-type. Check sibling elements of the same tag in DevTools.
Yes. All modern browsers support :only-of-type. It has been available since CSS3 Selectors and works reliably for articles, lists, and mixed-content layouts.

Practice in the Live Editor

Open the HTML editor and experiment with :only-of-type, mixed siblings, and article layouts.

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