:target-within
Quick Summary for :target-within
Experimental: This is an experimental technology
Check the Browser compatibility table carefully before using this in production.
Check the Browser compatibility table carefully before using this in production.
Code Usage for :target-within
/* Selects a <div> when one of its descendants is a target */ div:target-within { background: cyan; }
More Details for :target-within
:target-within
Experimental: This is an experimental technologyCheck the Browser compatibility table carefully before using this in production.
The :target-within
CSS pseudo-class represents an element that is a target element or contains an element that is a target. A target element is a unique element with an id
matching the URL's fragment. In other words, it represents an element that is itself matched by the :target
pseudo-class or has a descendant that is matched by :target
. (This includes descendants in shadow trees.)
/* Selects a <div> when one of its descendants is a target */ div:target-within { background: cyan; }
Syntax
:target-within
Examples
Highlighting an article
The :target-within
pseudo-class can be used to highlight the article if anything inside it has been directly linked to. The :target
pseudo-class is also being used to show which item has been targeted.
<h3>Table of Contents</h3> <ol> <li><a href="#p1">Jump to the first paragraph!</a></li> <li><a href="#p2">Jump to the second paragraph!</a></li> </ol> <article> <h3>My Fun Article</h3> <p id="p1">You can target <i>this paragraph</i> using a URL fragment. Click on the link above to try out!</p> <p id="p2">This is <i>another paragraph</i>, also accessible from the links above. Isn't that delightful?</p> </article>
CSS article:target-within { background-color: gold; } /* Add a pseudo-element inside the target element */ p:target::before { font: 70% sans-serif; content: "►"; color: limegreen; margin-right: .25em; } /* Style italic elements within the target element */ p:target i { color: red; }
ResultSpecifications
Specification |
---|
Selectors Level 4 # the-target-within-pseudo |
See also
:target
Select your preferred language English (US)Français Change language
Posted in
12:22 am, February 22, 2022
Add Comment
Other Items in CSS
rgb()
rgba()
:right
right
@right-bottom
:root
rotate
rotate()
rotate3d()
rotateX()
rotateY()
rotateZ()
row-gap
ruby-align
ruby-position
saturate()
scale
scale()
scale3d()
scaleX()
scaleY()
scaleZ()
:scope
scroll-behavior
scroll-margin
scroll-margin-block
scroll-margin-block-end
scroll-margin-block-start
scroll-margin-bottom
scroll-margin-inline
scroll-margin-inline-end
scroll-margin-inline-start
scroll-margin-left
scroll-margin-right
scroll-margin-top
scroll-padding
scroll-padding-block
scroll-padding-block-end
scroll-padding-block-start
scroll-padding-bottom
scroll-padding-inline
scroll-padding-inline-end
scroll-padding-inline-start
scroll-padding-left
scroll-padding-right
scroll-padding-top
scroll-snap-align
scroll-snap-stop
scroll-snap-type
@scroll-timeline