:target

In this chapter you will learn:

  1. Definition for Selector:target
  2. An example showing how to use :target CSS selector

Description

A fragment identifier can be added to a URL to navigate directly to an element based its id attribute. For example, example.html#myelement goes to the element whose id is 'myelement' in example.html.

:target selector matches the element that the URL fragment identifier refers to.

Example

<!DOCTYPE html>//  jav  a  2s .  c o  m
<html>
<head>
<style>
:target{
    border: 1px solid red;
    background-color: gray;
}
</style>
</head>
<body>
   <p>Click on the links in order to see the effect.</p>
   
   <p><a href="#p1">Paragraph 1</a></p>
   <p><a href="#p2">Paragraph 2</a></p>
   <br/><br/><br/><br/><br/><br/><br/><br/>
    

   <p id="p1">>Paragraph 1</b></p>
   <p>this is a paragraph.</p>
   <p>this is a paragraph.</p>
   <p>this is a paragraph.</p>
   <p>this is a paragraph.</p>
   <p>this is a paragraph.</p>
   <p id="p2"><b>Paragraph 2</b></p>

</body>
</html>

Next chapter...

What you will learn in the next chapter:

  1. Definition for Selector:enabled
  2. An example showing how to use :enabled CSS selector
Home » HTML CSS Tutorial » CSS Selector Reference
.class
#id
*
element
element,element
element element
element>element
element+element
element1~element2
:lang
:link
:visited
:active
:hover
:focus
:first-letter
:first-line
:first-child
:last-child
:only-child
:nth-child(n)
:nth-last-child(n)
:before
:after
:first-of-type
:last-of-type
:only-of-type
:nth-of-type(n)
:nth-last-of-type(n)
:root
:empty
:target
:enabled
:disabled
:checked
:not
::selection
[attribute]
[attribute=value]
[attribute~=value]
[attribute|=value]
[attribute^=value]
[attribute$=value]
[attribute*=value]