CSS Element Element Selector Last Updated : 15 Apr, 2025 Comments Improve Suggest changes Like Article Like Report The element element selector (without the > symbol) selects all descendant elements of a specified parent element. It applies styles not only to direct children but also to any deeper nested elements, making it more general than the direct child selector.Descendant selection: Targets all nested elements, not just direct children.Universal coverage: Affects every level of nesting within the selected parent.Broader selection scope: Useful for styling entire sections or elements regardless of depth. HTML <!--Driver Code Starts--> <html> <head> <!--Driver Code Ends--> <style> .container p { color: red; } </style> <!--Driver Code Starts--> </head> <body> <div class="container"> <p>Directly inside the container.</p> <div> <p>Inside a nested div.</p> <span>Span is also inside the nested div.</span> </div> </div> </body> </html> <!--Driver Code Ends--> In this example:The container p selector targets all <p> elements that are descendants of the .container div, whether they are direct children or deeply nested.Both <p> elements (inside and outside the nested <div>) will be styled with the color blue.Use Cases of descendant selector1. Changing List Item Style in a Specific ListBy using the descendant selector, you can style only the list items within a particular list by targeting ul#main-list li. This approach helps differentiate styles for different lists on the page. HTML <!--Driver Code Starts--> <html> <head> <!--Driver Code Ends--> <style> ul#main-list li { font-weight: bold; } </style> <!--Driver Code Starts--> </head> <body> <ul id="main-list"> <li>Item 1</li> <li>Item 2</li> </ul> <ul> <li>Item A</li> <li>Item B</li> </ul> </body> </html> <!--Driver Code Ends--> 2. Styling paragraphs Inside a Specific divStyling paragraphs inside a specific <div> allows you to target and apply CSS rules to only the paragraphs within that particular div. By using a class or ID selector on the div, you can control the appearance of the paragraphs contained within it without affecting others on the page." HTML <!--Driver Code Starts--> <html> <head> <!--Driver Code Ends--> <style> div.special p { color: blue; } </style> <!--Driver Code Starts--> </head> <body> <div class="special"> <p>This paragraph inside the 'special' div.</p> <h4>This is also inside</h4> </div> <div> <p>This paragraph is outside the 'special' div.</p> </div> </body> </html> <!--Driver Code Ends--> 3. Styling Form Inputs in a Specific FormYou can apply styles specifically to form inputs inside a particular form using the descendant selector. For instance, form#contact-form input will target inputs inside the form with the ID contact-form. HTML <!--Driver Code Starts--> <html> <head> <!--Driver Code Ends--> <style> form#contact-form input { padding: 10px; background-color: lightgray; } </style> <!--Driver Code Starts--> </head> <body> <form id="contact-form"> <input type="text" placeholder="Your Name"> </form> <form> <input type="text" placeholder="Another Form Name"> </form> </body> </html> <!--Driver Code Ends--> Why Use the Element Element Selector?Broad application: Ideal for when you want to apply styles to all descendant elements within a container, regardless of their depth.Simplicity: The selector is simple to use and doesn’t require you to explicitly specify direct children or nesting levels.Less specificity: Works well when you don’t need precise control over specific child elements but want to style a broad range of descendants.Pros and Cons of Using the Element Element SelectorProsConsApplies styles to all descendants, making it perfect for broad styling needs.Can lead to unintended styles being applied to nested elements deeper than expected.Reduces the need for writing extra classes or IDs for deep nesting.May cause performance issues in very large documents with deeply nested structures.Simple to use and highly flexible.Might override styles that are meant for specific elements, especially in complex layouts. Comment More infoAdvertise with us V Vishal Chaudhary 2 Follow Improve Article Tags : Web Technologies CSS CSS-Selectors Similar Reads CSS [attribute*=value] Selector The [attribute*="str"] selector targets the elements whose attribute values contain a specified substring. This substring can appear anywhere within the attribute's value â beginning, end, or middle.Syntax:element [attribute*="str"] { // CSS Property} Example: In the following example, the <p> 2 min read CSS [attribute=value] Selector The [attribute=value] selector in CSS is used to select those elements whose attribute value is equal to "value".Syntax: element [attribute = "value"] { // CSS Property}Note: <!DOCTYPE> must be declared for IE8 and earlier versions.Example 1: In this example, The selector h1[id="geeks"] target 2 min read CSS [attribute$=value] Selector The [attribute$=âvalueâ] selector is used to select those elements whose attribute value ends with a specified value "value". The value need not to be present as separate word. It may be a part of another word or expression but it needs to be present at the end. Syntax:[attribute$="value"] { // CSS 2 min read CSS [attribute|=value] Selector The [attribute|=value] selector is used to select those elements whose attribute value is equal to "value" or whose attribute value started with "value" immediately followed by a hyphen (-).Note: Use <!DOCTYPE> to run [attribute|=value] selector in IE8 or earlier versions.Syntax:[attributeType 2 min read CSS [attribute~=value] Selector The [attribute~="value"] selector is used to select those elements whose attribute value contains a specified word. The "value" must be present in the attribute as a separate word and not as part of the other word i.e. if [title~=Geeks] is specified then all elements with Geeks title get selected.Sy 2 min read CSS [attribute^=value] Selector The [attribute^=value] selector selects elements whose attribute value begins with a given attribute.Syntax:[attribute^=value] { // CSS Property}Example: In this example, The CSS selector p[class^="for"] targets <p> elements with a class attribute that starts with "for" and applies a green bac 2 min read CSS #id Selector The ID selector in CSS is used to select a single element on a page by referencing its id attribute. This attribute must be unique within a page, meaning no two elements can have the same id. The ID selector is prefixed with a hash (#) symbol in CSS.Basic ID SelectorThe ID selector allows you to sty 7 min read CSS * (Universal) Selector The universal selector (*) applies styles to all elements on a page or within a specified context, regardless of their type, class, or ID. It's commonly used for global resets or universal styling. * { /* styles */}1. Basic Use case of universal selectorThe universal selector applies styles to all e 4 min read CSS :active Selector The: active selector is used in styling an active link of the web page. Style display when the user clicks on the link. This selector is different from :link, :visited and: hover selectors. The main use of : active selector is on the links but it can be used on all elements.Syntax: :active{ //CSS pr 2 min read CSS ::after Selector ::after selector is used to add the same content multiple times after the content of other elements. This selector is the same as ::before selector. Syntax:::after{ content:}Below HTMl/CSS code shows the functionality of ::after selector : HTML <!DOCTYPE html> <html> <head> <sty 2 min read Like