CSS Portal

:target CSS Pseudo Class

If this site has been useful, we’d love your support! Consider buying us a coffee to keep things going strong!

Description

The :target pseudo-class in CSS is a powerful selector used to style an element that matches the fragment identifier in the current URL. Essentially, when a URL contains a hash (e.g., #section1), the element with the corresponding id becomes the target, and styles defined with :target are applied to it. This makes it particularly useful for creating interactive experiences without relying on JavaScript, such as highlighting the currently visible section, building tab-like interfaces, or implementing simple scroll-to-section effects.

One of the key advantages of :target is its dynamic nature. Unlike static classes, the pseudo-class automatically responds to URL changes. For example, navigating to example.com/page#about would apply the :target styles to the element <div id="about">. This allows designers to provide visual feedback, such as changing background colors, borders, or visibility, to indicate which section is currently active.

When using :target, it’s common to combine it with CSS properties like display, background-color, or opacity to create effects like showing or hiding content. Additionally, it pairs well with elements such as a for navigation links that point to specific sections of a page. However, it only works for elements with an id attribute matching the URL fragment and does not apply if the fragment does not exist or is removed.

Example Usage:

<nav>
  <a href="#section1">Section 1</a>
  <a href="#section2">Section 2</a>
</nav>

<div id="section1" class="content">
  <p>Content of Section 1</p>
</div>
<div id="section2" class="content">
  <p>Content of Section 2</p>
</div>
.content {
  display: none;
  padding: 20px;
  border: 1px solid #ccc;
}

.content:target {
  display: block;
  background-color: #f0f8ff;
}

In this example, only the section corresponding to the fragment identifier in the URL is displayed, while other sections remain hidden. This technique is frequently used for simple tab interfaces, FAQs, or single-page navigation where each link targets a specific part of the page.

Another helpful use case is adding visual emphasis to a section without changing the layout. For example, you could use outline or transform with :target to highlight the element in a subtle, non-intrusive way. This makes :target a versatile tool for enhancing user experience on static web pages.

Syntax

:target {
  /* ... */
}

Example

<nav>
<a href="#apple">Apple</a>
<a href="#orange">Orange</a>
<a href="#banana">Banana</a>
</nav>

<div id="apple" class="content">
<h2>Apple</h2>
<p>Apples are crisp and delicious.</p>
</div>

<div id="orange" class="content">
<h2>Orange</h2>
<p>Oranges are a great source of Vitamin C.</p>
</div>

<div id="banana" class="content">
<h2>Banana</h2>
<p>Bananas are high in potassium.</p>
</div>
/* Standard styling for all sections */
.content {
padding: 20px;
margin: 10px 0;
border: 2px solid transparent;
transition: all 0.3s ease;
border-radius: 8px;
}

/* Styles applied ONLY when the ID matches the URL hash */
.content:target {
background-color: #f0f9ff;
border-color: #007bff;
transform: scale(1.02);
box-shadow: 0 4px 15px rgba(0, 0, 0, 0.1);
}

/* Optional: styling the heading of the targeted section */
.content:target h2 {
color: #007bff;
}

Browser Support

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

This psuedo 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
-

Last updated by CSSPortal on: 31st December 2025

If this site has been useful, we’d love your support! Consider buying us a coffee to keep things going strong!