CSS Portal

:only-of-type CSS Pseudo Class

☕
Enjoying this site?

Help keep it running with a small coffee donation. Every bit of support is appreciated!

Buy us a coffee
:only-of-type
CSS Pseudo ClassThis pseudo class is supported by all modern browsers.
Updated 31st December 2025

Description

The :only-of-type pseudo-class in CSS is used to select an element that is the only one of its type among its sibling elements. In other words, it matches an element if no other sibling shares the same element type. This pseudo-class allows developers to apply styles selectively when an element stands alone in its category within its parent, which can be particularly useful for layouts, typography, or unique visual treatments.

Unlike the :first-child or :last-child pseudo-classes, which only consider an element's position relative to all siblings, :only-of-type specifically checks the type of the element - meaning its tag name - ignoring other types of sibling elements.

One common use case is styling a lone heading in a section differently from multiple headings. For instance, if you want to give special styling to a paragraph that is the only paragraph in a container, :only-of-type can accomplish this without needing to add extra classes.

Here’s an example:

<section>
  <p>This paragraph is the only paragraph in this section.</p>
  <span>Some other element</span>
</section>

<section>
  <p>First paragraph in this section.</p>
  <p>Second paragraph in this section.</p>
</section>
p:only-of-type {
  font-weight: bold;
  color: darkblue;
}

In this example:

  • The paragraph in the first <section> will appear bold and dark blue because it is the only <p> element in that section.
  • Neither paragraph in the second <section> will be affected because there is more than one <p> element there.

The :only-of-type pseudo-class is often combined with other selectors, such as class selectors, to target elements more specifically. For instance:

article p:only-of-type {
  font-style: italic;
}

Here, only paragraphs that are the sole <p> inside an <article> will be italicized. This is useful for maintaining visual consistency when content may vary dynamically.

Additionally, :only-of-type works with pseudo-elements like ::before and ::after for enhanced styling of isolated elements.

Syntax

:only-of-type {
  /* ... */
}

Example

<div class="container">
<h2>Profile Section</h2>
<p>This user has one photo.</p>
<img src="images/2.png" alt="User Profile">
</div>

<hr>

<div class="container">
<h2>Gallery Section</h2>
<p>This user has multiple photos.</p>
<img src="photo1.jpg" alt="Gallery 1">
<img src="photo2.jpg" alt="Gallery 2">
</div>
/* Target the img ONLY if it's the only <img> inside its parent */
img:only-of-type {
border: 5px solid #4A90E2;
border-radius: 10px;
box-shadow: 0 4px 15px rgba(0, 0, 0, 0.2);
transform: scale(1.05);
}

/* General styling for visibility */
.container {
padding: 20px;
font-family: sans-serif;
}

img {
width: 150px;
height: 150px;
margin: 10px;
background-color: #ddd; /* Placeholder color */
}
Open in editor

Browser Support

The following information will show you the current browser support for the CSS :only-of-type pseudo class. Hover over a browser icon to see the version that first introduced support for this CSS pseudo class.

This pseudo class is supported by all modern browsers.

Desktop

Chrome
Edge
Firefox
Opera
Safari

Tablets & Mobile

Chrome Android
Firefox Android
Opera Android
Safari iOS
Samsung Internet
Android WebView
-
Supported Unknown Not supported
☕
Enjoying this site?

Help keep it running with a small coffee donation. Every bit of support is appreciated!

Buy us a coffee