---
title: "target selector"
url: https://weworkworldwide.com/tutorials/target-selector-2/
description: "When we want to apply a CSS style to an element that its ID appears in a URL, we use this type of selector. Note: when the ID of an element appears in a UR"
date: 2026-09-29T09:00:03+00:00
source: https://weworkworldwide.com/llms.txt
---

# target selector

``` line-numbers
```

When we want to apply a CSS style to an element that its ID appears in a URL, we use this type of selector.

Note: when the ID of an element appears in a URL, that element is called the target element.

Example:

``` line-numbers
<!DOCTYPE html>
<html>
<head>
<title>CSS is fun :)</title>
<style type="text/css">
p:target{
background-color: red;
}
</style>
</head>
<body>
<ul>
<li><a href="#p1">Target the paragraph one</a></li>
<li><a href="#p2"> Target the paragraph two</a></li>
<li><a href="#p3"> Target the paragraph three</a></li>
<li><a href="#p4"> Target the paragraph four</a></li>
</ul>

<h1>header</h1>
<p id="p1">I'm the first of type p element in this div container</p>
<p id="p2">One of many</p>
<p id="p3">One of many</p>
<p id="p4">One of many</p>

</body>
</html>
```

In the example above, any p element that its ID appears in the URL is considered to be the target element.

``` line-numbers
```

As the picture above shows, in this example I have select the “Target the paragraph three” link and as the result of this link selection, the ID of the third paragraph appears in the URL, so now this paragraph is the target and the CSS style of the “p:target” selector applies to this element.

We could be more general and style any element that its ID appears in the URL with only using “:target” as the selector.

Example:

``` line-numbers
<!DOCTYPE html>
<html>
<head>
<title>CSS is fun :)</title>
<style type="text/css">
:target{
background-color: red;
}
</style>
</head>
<body>
<ul>
<li><a href="#header">Target the header</a></li>
<li><a href="#p1"> Target the paragraph two</a></li>
<li><a href="#li1"> Target the list item one</a></li>
<li><a href="#li2"> Target the list item two</a></li>
</ul>

<h1 id="header">header</h1>
<p id="p1">I'm the first of type p element in this div container</p>
<ul>
<li id="li1">List Item one</li>
<li id="li2">List Item two</li>
</ul>

</body>
</html>
```
