Difference between :first-child and :first-of-type selector in CSS Last Updated : 13 Jun, 2024 Comments Improve Suggest changes Like Article Like Report The :first-child and :first-of-type selectors in CSS are used to target specific child elements within a parent. The :first-child selector targets an element if it is the first child of its parent, regardless of type. On the other hand, the :first-of-type selector targets the first occurrence of a specified element type within its parent. Understanding their differences enhances the precision and flexibility of web designs. This article explores the differences between these selectors with examples to help you understand their unique applications in web design. Example: html <!DOCTYPE html> <html> <head> <title>:first-child and first-of-type selector</title> <style> h1 { color: green; } h1, h2 { text-align: center; } p:first-child { background: red; } h4:first-of-type { background: green; } </style> </head> <body> <h1>GeeksforGeeks</h1> <h2>This is :first-child and first-of-type selector</h2> <div> <p>I am the First Child</p> <h4>I am the first of type Child</h4> <h4>I am the third Child</h4> <p>I am the fourth Child</p> </div> </body> </html> Output:Â Difference between :first-child and :first-of-type selector:Â :first-child:first-of-typeThis selector only choose the define element if the element is the first child of the parent. If the define element comes second position then this selector can not select that element.This selector chose the define element if the element comes 2nd, 3rd or 4th any place but have to be the first of it's type.In the above example you can see inside the div tag the childs are p, h4, h4 and p. If you change 1st p tag into a another tag then :first-child selector can not select any child.In the above example you can see inside the div tag the childs are p, h4, h4 and p. If you change 1st p tag into h4 tag then :first-of-child selector will select the first child of that parent which is also be the first child of the define element.Conclusion:Understanding the :first-child and :first-of-type selectors is crucial for precise CSS styling. While :first-child targets the very first child element, :first-of-type focuses on the first instance of a particular type within a parent. Mastering these selectors allows for more nuanced and efficient CSS, enhancing your web design capabilities. Comment More infoAdvertise with us Next Article Difference between :first-child and :first-of-type selector in CSS S skyridetim Follow Improve Article Tags : Web Technologies CSS CSS-Misc Similar Reads Difference Between " . " and " # " Selector in CSS In CSS, selectors define which HTML elements receive specific styles.Class Selector (.): Targets elements with a specified class attribute, allowing multiple elements to share the same styling.ID Selector (#): Targets a single element with a unique ID attribute, ensuring that styles are applied to o 3 min read Difference between ID and Class Selector in jQuery jQuery ID Selector: The #id selector specifies the id for an HTML element to be selected. It should not begin with a number and the id attribute must be unique within a document which means it can be used only one at a time. Syntax: $("#id")id is the element's specific id. Example: The following cod 2 min read What's the difference between selector and filter() in jQuery? jQuery Selectors: allow us to select and manipulate HTML element(s). It is used to select HTML elements such as ids, classes, types, attributes, etc using jQuery selectors and then add any CSS properties or events to the selected HTML elements. Syntax: For selecting a button tag the syntax would be 2 min read Difference between find() and closest() in jQuery Before looking at the differences between the find() and closest() methods, let us briefly understand what are these and what they do. 1. find() Method: This method is used to get all the filtered descendants of each element in the current set of matched elements. Syntax: $(selector).find('filter-ex 3 min read Difference Between Pseudo-Classes and Pseudo-Elements in CSS Pseudo-classes and pseudo-elements in CSS are selectors that allow the styling of specific states or parts of elements. Pseudo-classes target states like ': hover' or ': active', while pseudo-elements like ' ::before ' or:: after' style specific parts of an element. They enhance CSS styling by provi 2 min read Like