How to target components around or within other elements in CSS ? Last Updated : 27 Jul, 2022 Summarize Comments Improve Suggest changes Share Like Article Like Report In this article, we will see how can we target components around or within other elements. This task can be achieved by using the target selector. The target selector in CSS is used to represent a unique element with an id attribute matching the URL’s fragment and It can be used to style the currently active target element. URLs with a # followed by an anchor tag name link to a certain element within a document and the element being linked to is the target element. Syntax: :target { CSS Property } Example 1: In the below example, we will see how would we target components around or within other elements. HTML <!DOCTYPE html> <html> <head> <title>CSS :target Selector</title> <style> body { background-color: lightgrey; } button { background-color: #4CAF50; /* Green */ border: none; color: white; padding: 15px 32px; text-align: center; text-decoration: none; display: inline-block; font-size: 16px; } :target { border: 2px solid black; background-color: grey; color: white; padding: 5px; font-size: 15px; width: 250px; margin-left: 200px; } </style> </head> <body style="text-align:center"> <h1 style="color:green"> GeeksforGeeks </h1> <h3> Targeting the components around or within other elements </h3><br> <p> <button> <a href="#gfg"> Click here to find GeeksforGeeks </a> </button> </p> <div id="gfg"> <h3>GeeksforGeeks</h3> </div> <div> <h3>DSA</h3> </div> <div> <h3>JAVA</h3> </div> <div> <h3>Operating System</h3> </div> </body> </html> Output: Example 2: This is another example that displays how would we target components around or within other elements. HTML <!DOCTYPE html> <html> <head> <title>CSS :target Selector</title> <style> button { background-color: #4CAF50; border: none; color: white; padding: 15px 32px; text-align: center; text-decoration: none; display: inline-block; font-size: 16px; } /* CSS property of target selector */ :target { border: 2px solid black; background-color: green; color: white; padding: 5px; font-size: 15px; } </style> </head> <body style="text-align:center"> <h1 style="color:green"> GeeksforGeeks </h1> <h3> Targeting the components around or within other elements </h3> <p> <button> <a href="#i1"> Find gfg from paragraph one and make it bold. </a> </button> </p> <p> <button> <a href="#i2"> Find gfg from paragraph two and make it bold. </a> </button> </p> <div> <p>This is <i id="i1">gfg</i> paragraph one </p> </div> <div> <p> This is <i id="i2">gfg</i> paragraph two. </p> </div> </body> </html> Output: Comment More infoAdvertise with us Next Article How to Set the background-color of different Elements in CSS? K krishna_97 Follow Improve Article Tags : Web Technologies CSS CSS-Properties HTML-Questions CSS-Questions +1 More Similar Reads How to Target a Component in Svelte with CSS? Targeting a component in Svelte means applying CSS styles specifically to a particular component to control its appearance and behavior. This can be achieved by using scoped styles within the component or applying global styles that affect the component. Proper targeting ensures that styles are enca 3 min read How to Set the background-color of different Elements in CSS? Background colors in CSS set the color behind an element's content and padding. Use the background-color property to assign colors to elements, specifying values in hex, RGB, or color names. This allows customization of elements' backgrounds for design and readability.Using Background Color Property 2 min read How to add Borders and Shadows to Elements in CSS ? Apply box shadows to elements to create a 3D effect, whereas adding borders to containers or boxes can enhance the structural clarity of a layout. In this article, we will learn how to add borders and shadows to the elements in CSS. We will explore two different approaches to adding borders and shad 2 min read How to affect other elements when one element is hovered in CSS ? Hover effects are commonly used to make websites interactive. They are especially useful for navigation menus or buttons, providing visual feedback when users move their mouse over an element. In CSS, you can change the style of one element based on the state of another, but this works best if the e 3 min read How to Style Parent Element when Hover Over a Child Element in CSS ? To Style Parent Element When Hovering Over A Child Element is a technique of changing how a parent looks when you hover over a child element can make your designs more engaging and interesting and enhance user interaction on your website or app.Syntax:child-element { pointer-events: auto } child-ele 2 min read How to click through a div to its underlying elements in CSS ? Assume you have an HTML div element and other elements. You want to click on these underlying elements via the div. This causes issues for most developers because they can only click underlying elements when they are outside of the overlay div. For instance, you have an anchor tag, but it is obscure 3 min read Like