- CSS Tutorial
- CSS - Home
- CSS - Introduction
- CSS - Syntax
- CSS - Selectors
- CSS - Inclusion
- CSS - Measurement Units
- CSS - Colors
- CSS - Backgrounds
- CSS - Fonts
- CSS - Text
- CSS - Images
- CSS - Links
- CSS - Tables
- CSS - Borders
- CSS - Border Block
- CSS - Border Inline
- CSS - Margins
- CSS - Lists
- CSS - Padding
- CSS - Cursor
- CSS - Outlines
- CSS - Dimension
- CSS - Scrollbars
- CSS - Inline Block
- CSS - Dropdowns
- CSS - Visibility
- CSS - Overflow
- CSS - Clearfix
- CSS - Float
- CSS - Arrows
- CSS - Resize
- CSS - Quotes
- CSS - Order
- CSS - Position
- CSS - Hyphens
- CSS - Hover
- CSS - Display
- CSS - Focus
- CSS - Zoom
- CSS - Translate
- CSS - Height
- CSS - Hyphenate Character
- CSS - Width
- CSS - Opacity
- CSS - Z-Index
- CSS - Bottom
- CSS - Navbar
- CSS - Overlay
- CSS - Forms
- CSS - Align
- CSS - Icons
- CSS - Image Gallery
- CSS - Comments
- CSS - Loaders
- CSS - Attr Selectors
- CSS - Combinators
- CSS - Root
- CSS - Box Model
- CSS - Counters
- CSS - Clip
- CSS - Writing Mode
- CSS - Unicode-bidi
- CSS - min-content
- CSS - All
- CSS - Inset
- CSS - Isolation
- CSS - Overscroll
- CSS - Justify Items
- CSS - Justify Self
- CSS - Tab Size
- CSS - Pointer Events
- CSS - Place Content
- CSS - Place Items
- CSS - Place Self
- CSS - Max Block Size
- CSS - Min Block Size
- CSS - Mix Blend Mode
- CSS - Max Inline Size
- CSS - Min Inline Size
- CSS - Offset
- CSS - Accent Color
- CSS - User Select
- CSS Advanced
- CSS - Grid
- CSS - Grid Layout
- CSS - Flexbox
- CSS - Visibility
- CSS - Positioning
- CSS - Layers
- CSS - Pseudo Classes
- CSS - Pseudo Elements
- CSS - @ Rules
- CSS - Text Effects
- CSS - Paged Media
- CSS - Printing
- CSS - Layouts
- CSS - Validations
- CSS - Image Sprites
- CSS - Important
- CSS - Data Types
- CSS3 Tutorial
- CSS3 - Tutorial
- CSS - Rounded Corner
- CSS - Border Images
- CSS - Multi Background
- CSS - Color
- CSS - Gradients
- CSS - Box Shadow
- CSS - Box Decoration Break
- CSS - Caret Color
- CSS - Text Shadow
- CSS - Text
- CSS - 2d transform
- CSS - 3d transform
- CSS - Transition
- CSS - Animation
- CSS - Multi columns
- CSS - Box Sizing
- CSS - Tooltips
- CSS - Buttons
- CSS - Pagination
- CSS - Variables
- CSS - Media Queries
- CSS - Functions
- CSS - Math Functions
- CSS - Masking
- CSS - Shapes
- CSS - Style Images
- CSS - Specificity
- CSS - Custom Properties
- CSS Responsive
- CSS RWD - Introduction
- CSS RWD - Viewport
- CSS RWD - Grid View
- CSS RWD - Media Queries
- CSS RWD - Images
- CSS RWD - Videos
- CSS RWD - Frameworks
- CSS References
- CSS - Questions and Answers
- CSS - Quick Guide
- CSS - References
- CSS - Color References
- CSS - Web browser References
- CSS - Web safe fonts
- CSS - Units
- CSS - Animation
- CSS Resources
- CSS - Useful Resources
- CSS - Discussion
CSS - Pseudo-class :target
The CSS pseudo-class :target points to a specific element, called the target element, whose id matches the fragment identifier in the URL.
Syntax
:target { /* css declarations */ }
CSS :target - Linking Elements
The following example demonstrates the use of :target pseudo-class to highlight the portion of a page that has been linked to anchor tags.
Here we see that <a> tags points to #demo-target1 and #demo-target2. This points to an element called demo-target1 and demo-target2 respectively.
<html> <head> <style> #demo-target1 { background-color: yellow; padding: 10px; margin: 20px 20px 20px 20px; } #demo-target2 { background-color: lightgray; padding: 10px; margin: 20px 20px 20px 20px; } #demo-target2:target { background-color: red; color: white; } #demo-target1:target { background-color: red; color: white; } </style> </head> <body> <div> <a href="#demo-target1">Click here to target the ELEMENT-1 and turn its color to red</a> </div> <br> <div> <a href="#demo-target2">Click here to target the ELEMENT-2 and turn its color to red </a> </div> <div id="demo-target1"> <p>This is the target ELEMENT-1 </p> </div> <div id="demo-target2"> <p> This is the target ELEMENT-2 </p> </div> </body> </html>
CSS :target - No Target Exist
The following example demonstrates the how :target pseudo-class doesn't affect the links that don't have target set.
<html> <head> <title>:target with ID</title> <style> #demo-target { background-color: yellow; padding: 10px; } #demo-target:target { background-color: red; color: white; } </style> </head> <body> <h1>Click on the links below to see the effect of :target with ID</h1> <ul> <li><a href="#demo-target">Target Element</a></li> <li><a href="#other">Other Element</a></li> </ul> <div id="demo-target"> <h2>This is the target element</h2> <p>When this element is the target of the URL fragment identifier, its background color changes to red and the text color changes to white.</p> </div> <div id="other"> <h2>This is another element</h2> <p>This element is not affected by the :target selector.</p> </div> </body> </html>
The :target doesn't work inside a web component, because the shadow root doesn't pass the target element into the shadow tree as it should.
Advertisements
To Continue Learning Please Login
Login with Google