:target

Baseline Widely available

This feature is well established and works across many devices and browser versions. It’s been available across browsers since July 2015.

La pseudo-classe :target permet de cibler l'unique élément (s'il existe) dont l'attribut id correspond au fragment d'identifiant de l'URI du document.

css
/* Cible un élément dont l'identifiant      */
/* correspond au fragment de l'URL courante */
:target {
  border: 2px solid black;
}

Les URI comportant des fragments d'identifiant peuvent être utilisées pour créer des liens vers un élément donné d'un document qu'on appellera l'élément cible (target element). Voici un exemple d'URI qui pointe vers une ancre intitulée section2 :

http://exemple.com/chemin/document.html#section2

L'élément suivant sera donc ciblé par le sélecteur :target avec l'URL précédente :

html
Exemple

Note : L'attribut id a été ajouté avec HTML 4 (décembre 1997). Dans les anciennes versions de HTML, était nécessairement l'élément cible. La pseudo-classe :target permet également de gérer ces cibles.

Syntaxe

Error: could not find syntax for this item

Exemples

Exemples simples

css
/* Exemple de code pouvant être utilisé dans une feuille
   de style utilisateur. Une flèche rouge/jaune indique
   l'élément cible. */

:target {
  box-shadow: 0.2em 0.2em 0.3em #888;
}

:target:before {
  font:
    70% Arial,
    "Nimbus Sans L",
    sans-serif !important;
  content: "\25ba"; /* ► */
  color: red;
  background: gold;
  border: solid thin;
  padding-left: 1px;
  display: inline-block;
  margin-right: 0.13em;
  vertical-align: 20%;
}

Manipuler les éléments avec display: none

La pseudo-classe :target s'accommode également des éléments qui ne sont pas affichés.

CSS

css
#newcomment {
  display: none;
}

#newcomment:target {
  display: block;
}

HTML

html

Ajouter un commentaire

Saisir un commentaire :

Résultat

Révéler des éléments

:target peut être utile afin de révéler/masquer certains éléments invisibles.

CSS

css
div.lightbox {
  display: none;
  position: fixed;
  left: 0;
  top: 0;
  width: 100%;
  height: 100%;
}

div.lightbox:target {
  display: table;
}

div.lightbox figure {
  display: table-cell;
  margin: 0;
  padding: 0;
  width: 100%;
  height: 100%;
  vertical-align: middle;
}

div.lightbox figure figcaption {
  display: block;
  margin: auto;
  padding: 8px;
  background-color: #ddbbff;
  height: 250px;
  position: relative;
  overflow: auto;
  border: 1px #000000 solid;
  border-radius: 10px;
  text-align: justify;
  font-size: 14px;
}

div.lightbox figure .closemsg {
  display: block;
  margin: auto;
  height: 0;
  overflow: visible;
  text-align: right;
  z-index: 2;
  cursor: default;
}

div.lightbox figure .closemsg,
div.lightbox figure figcaption {
  width: 300px;
}

.closemsg::after {
  content: "\00D7";
  display: inline-block;
  position: relative;
  right: -20px;
  top: -10px;
  z-index: 3;
  color: #ffffff;
  border: 1px #ffffff solid;
  border-radius: 10px;
  width: 20px;
  height: 20px;
  line-height: 18px;
  text-align: center;
  margin: 0;
  background-color: #000000;
  font-weight: bold;
  cursor: pointer;
}

.closemsg::before {
  content: "";
  display: block;
  position: fixed;
  left: 0;
  top: 0;
  width: 100%;
  height: 100%;
  background-color: #000000;
  opacity: 0.85;
}

HTML

html

Un texte pour l'exemple…

[ Ouvrir l'exemple n°1 | Ouvrir l'exemple n°2 ]

Un autre texte pour l'exemple…

Résultat

Spécifications

Specification
HTML
# selector-target
Selectors Level 4
# target-pseudo

Compatibilité des navigateurs

Voir aussi