{"id":579,"date":"2025-05-19T10:46:04","date_gmt":"2025-05-19T10:46:04","guid":{"rendered":"https:\/\/buhave.com\/courses\/?p=579"},"modified":"2026-06-06T13:21:07","modified_gmt":"2026-06-06T13:21:07","slug":"css-selectors","status":"publish","type":"post","link":"https:\/\/buhave.com\/courses\/css\/css-selectors\/","title":{"rendered":"CSS Selectors"},"content":{"rendered":"<h2>Element, Class, and ID Selectors<\/h2>\n<p>In CSS, selectors are patterns used to target specific HTML elements for styling. <a href=\"https:\/\/buhave.com\/courses\/css\/introduction-to-css\/\">Introduction to CSS<\/a> course provides foundational context.<br \/>\nThree of the most common types of selectors are element selectors, class selectors, and ID selectors.<\/p>\n<p>Understanding how they work is essential for controlling which elements get styled and how.<\/p>\n<h3>1. Element Selector<\/h3>\n<p><strong>Definition:<\/strong><\/p>\n<p>The element selector targets HTML elements directly by their tag name (like h1, p, div, section, etc.).<\/p>\n<p><strong>Syntax:<\/strong><\/p>\n<p style=\"text-align: center\"><em>element-name {<\/em><br \/>\n<em>property: value;<\/em><br \/>\n<em>}<\/em><\/p>\n<p><strong>Example:<\/strong><\/p>\n<p style=\"text-align: center\"><em>p {<\/em><br \/>\n<em>color: blue;<\/em><br \/>\n<em>font-size: 18px;<\/em><br \/>\n<em>}<\/em><\/p>\n<ul>\n<li>This will apply the styles to all &lt;p&gt; elements in the HTML document.<\/li>\n<\/ul>\n<p>Use When:<\/p>\n<p>You want to apply styles uniformly to every instance of an HTML tag.<\/p>\n<p>Tip: When you start combining selectors, refer to <a href=\"https:\/\/buhave.com\/courses\/css\/basic-css-properties\/\">Basic CSS Properties<\/a> to see how individual declarations are defined and applied.<\/p>\n<h3>2. Class Selector<\/h3>\n<p><strong>Definition:<\/strong><\/p>\n<p>A class selector targets one or more elements based on a class attribute.<br \/>\nClasses are reusable and can be applied to multiple elements.<\/p>\n<p><strong>Syntax:<\/strong><\/p>\n<p style=\"text-align: center\"><em>.class-name {<\/em><br \/>\n<em>property: value;<\/em><br \/>\n<em>}<\/em><\/p>\n<ul>\n<li>Always start the selector with a dot (.) before the class name.<\/li>\n<\/ul>\n<p><strong>Example:<\/strong><\/p>\n<p style=\"text-align: center\"><em>.button {<\/em><br \/>\n<em>background-color: green;<\/em><br \/>\n<em>color: white;<\/em><br \/>\n<em>padding: 10px 20px;<\/em><br \/>\n<em>border-radius: 5px;<\/em><br \/>\n<em>}<\/em><\/p>\n<p><strong>HTML:<\/strong><\/p>\n<p style=\"text-align: center\"><em>&lt;button class=&#8221;button&#8221;&gt;Submit&lt;\/button&gt;<\/em><br \/>\n<em>&lt;a href=&#8221;#&#8221; class=&#8221;button&#8221;&gt;Learn More&lt;\/a&gt;<\/em><\/p>\n<ul>\n<li>Both the &lt;button&gt; and &lt;a&gt; elements will share the same styling because they both have the class button.<\/li>\n<\/ul>\n<p>Use When:<\/p>\n<p>You want multiple elements to share the same style without affecting unrelated elements.<\/p>\n<p>For more on text and formatting of elements like buttons and links, see <a href=\"https:\/\/buhave.com\/courses\/html\/text-and-formatting-elements\/\">Text and Formatting Elements<\/a>.<\/p>\n<h3>3. ID Selector<\/h3>\n<p><strong>Definition:<\/strong><\/p>\n<p>An ID selector targets a single, unique element based on its id attribute.<br \/>\nAn ID must be unique across the entire HTML document (should not be reused).<\/p>\n<p><strong>Syntax:<\/strong><\/p>\n<p style=\"text-align: center\"><em>#id-name {<\/em><br \/>\n<em>property: value;<\/em><br \/>\n<em>}<\/em><\/p>\n<ul>\n<li>Always start with a hash (#) before the ID name.<\/li>\n<\/ul>\n<p><strong>Example:<\/strong><\/p>\n<p style=\"text-align: center\"><em>#header {<\/em><br \/>\n<em>background-color: navy;<\/em><br \/>\n<em>color: white;<\/em><br \/>\n<em>text-align: center;<\/em><br \/>\n<em>}<\/em><\/p>\n<p><strong>HTML:<\/strong><\/p>\n<p style=\"text-align: center\"><em>&lt;header id=&#8221;header&#8221;&gt;<\/em><br \/>\n<em>Welcome to My Site<\/em><br \/>\n<em>&lt;\/header&gt;<\/em><\/p>\n<ul>\n<li>Only the &lt;header&gt; with the ID header will receive these styles.<\/li>\n<\/ul>\n<p>Use When:<\/p>\n<p>You want to style a unique, single element separately from others.<\/p>\n<p><strong>Selector Priority (Specificity)<\/strong><\/p>\n<ul>\n<li>ID selectors have the highest priority (specificity).<\/li>\n<li>Class selectors have medium priority.<\/li>\n<li>Element selectors have the lowest priority.<\/li>\n<\/ul>\n<p>Example: If the same element is targeted by an element selector, a class, and an ID, the ID selector\u2019s styles will apply.<\/p>\n<p style=\"text-align: center\"><em>p {<\/em><br \/>\n<em>color: black; \/* Low priority *\/<\/em><br \/>\n<em>}<\/em><\/p>\n<p style=\"text-align: center\"><em>.text {<\/em><br \/>\n<em>color: blue; \/* Medium priority *\/<\/em><br \/>\n<em>}<\/em><\/p>\n<p style=\"text-align: center\"><em>#special {<\/em><br \/>\n<em>color: red; \/* High priority *\/<\/em><br \/>\n<em>}<\/em><\/p>\n<p><strong>HTML:<\/strong><\/p>\n<p style=\"text-align: center\"><em>&lt;p id=&#8221;special&#8221; class=&#8221;text&#8221;&gt;Hello World&lt;\/p&gt;<\/em><\/p>\n<ul>\n<li>The text will be red because the ID selector #special wins.<\/li>\n<\/ul>\n<p><strong>Use When:<\/strong><\/p>\n<p>You want to style a unique, single element separately from others.<\/p>\n<p><strong>Selector Priority (Specificity)<\/strong><\/p>\n<ul>\n<li>ID selectors have the highest priority (specificity).<\/li>\n<li>Class selectors have medium priority.<\/li>\n<li>Element selectors have the lowest priority.<\/li>\n<\/ul>\n<p>Example: If the same element is targeted by an element selector, a class, and an ID, the ID selector\u2019s styles will apply.<\/p>\n<p style=\"text-align: center\"><em>p {<\/em><br \/>\n<em>color: black; \/* Low priority *\/<\/em><br \/>\n<em>}<\/em><\/p>\n<p style=\"text-align: center\"><em>.text {<\/em><br \/>\n<em>color: blue; \/* Medium priority *\/<\/em><br \/>\n<em>}<\/em><\/p>\n<p style=\"text-align: center\"><em>#special {<\/em><br \/>\n<em>color: red; \/* High priority *\/<\/em><br \/>\n<em>}<\/em><\/p>\n<p><strong>HTML:<\/strong><\/p>\n<p style=\"text-align: center\"><em>&lt;p id=&#8221;special&#8221; class=&#8221;text&#8221;&gt;Hello World&lt;\/p&gt;<\/em><\/p>\n<ul>\n<li>The text will be red because the ID selector #special wins.<\/li>\n<\/ul>\n<p><strong>Comparison Table:<\/strong><\/p>\n<table class=\"selector-table\">\n<caption><strong>Summary Table<\/strong><\/caption>\n<thead>\n<tr>\n<th>Selector Type<\/th>\n<th>Syntax<\/th>\n<th>Targets<\/th>\n<th>Reusability<\/th>\n<th>Specificity<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>Element<\/td>\n<td><span class=\"selector-syntax\">p {}<\/span><\/td>\n<td>All &lt;p&gt; tags<\/td>\n<td>All elements<\/td>\n<td class=\"specificity-low\">Low<\/td>\n<\/tr>\n<tr>\n<td>Class<\/td>\n<td><span class=\"selector-syntax\">.class {}<\/span><\/td>\n<td>Any element with class<\/td>\n<td>Many elements<\/td>\n<td class=\"specificity-medium\">Medium<\/td>\n<\/tr>\n<tr>\n<td>ID<\/td>\n<td><span class=\"selector-syntax\">#id {}<\/span><\/td>\n<td>Only the unique ID<\/td>\n<td>Single element<\/td>\n<td class=\"specificity-high\">High<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p><strong>Summary:<\/strong><\/p>\n<ul>\n<li>Element selectors apply styles to all instances of a tag.<\/li>\n<li>Class selectors apply styles to any elements sharing the same class.<\/li>\n<li>ID selectors apply styles to one unique element only.<\/li>\n<\/ul>\n<p>Understanding and properly using selectors helps you build cleaner, organized, and scalable CSS for your web projects.<\/p>\n<h2>Attribute selectors and pseudo-classes (hover, focus, etc.)<\/h2>\n<p>CSS offers powerful ways to target elements based on their attributes or user interactions without needing extra classes or IDs.<br \/>\nTwo essential features for this are attribute selectors and pseudo-classes like :hover, :focus, and many others.<\/p>\n<p>Mastering these tools makes your styles more dynamic, flexible, and user-friendly.<\/p>\n<h3>1. Attribute Selectors<\/h3>\n<p><strong>Definition:<\/strong><\/p>\n<p>Attribute selectors target HTML elements based on the presence or value of their attributes (such as href, src, alt, type, etc.).<\/p>\n<p><strong>Syntax:<\/strong><\/p>\n<p style=\"text-align: center\"><em>element[attribute] {<\/em><br \/>\n<em>property: value;<\/em><br \/>\n<em>}<\/em><\/p>\n<p style=\"text-align: center\"><em>element[attribute=&#8221;value&#8221;] {<\/em><br \/>\n<em>property: value;<\/em><br \/>\n<em>}<\/em><\/p>\n<p><strong>Types of Attribute Selectors:<\/strong><\/p>\n<table class=\"attribute-selectors\">\n<caption><strong>Summary Table<\/strong><\/caption>\n<thead>\n<tr>\n<th>Syntax<\/th>\n<th>Meaning<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td><code class=\"selector\">[attr]<\/code><\/td>\n<td>Selects elements with the attribute<\/td>\n<\/tr>\n<tr>\n<td><code class=\"selector\">[attr=\"value\"]<\/code><\/td>\n<td>Selects elements with an exact value<\/td>\n<\/tr>\n<tr>\n<td><code class=\"selector\">[attr~=\"value\"]<\/code><\/td>\n<td>Value in a space-separated list (one word match)<\/td>\n<\/tr>\n<tr>\n<td><code class=\"selector\">[attr^=\"value\"]<\/code><\/td>\n<td>Value starts with specified string<\/td>\n<\/tr>\n<tr>\n<td><code class=\"selector\">[attr$=\"value\"]<\/code><\/td>\n<td>Value ends with specified string<\/td>\n<\/tr>\n<tr>\n<td><code class=\"selector\">[attr*=\"value\"]<\/code><\/td>\n<td>Value contains specified string<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p><strong>Examples:<\/strong><\/p>\n<p><strong>1. Select all links (&lt;a&gt;) with an href attribute:<\/strong><\/p>\n<p style=\"text-align: center\"><em>a[href] {<\/em><br \/>\n<em>color: blue;<\/em><br \/>\n<em>}<\/em><\/p>\n<p><strong>2. Select only links to external websites:<\/strong><\/p>\n<p style=\"text-align: center\"><em>a[href^=&#8221;http&#8221;] {<\/em><br \/>\n<em>color: green;<\/em><br \/>\n<em>}<\/em><\/p>\n<ul>\n<li>Targets links where href starts with &#8220;http&#8221;.<\/li>\n<\/ul>\n<p><strong>3. Select input fields of type &#8220;email&#8221;:<\/strong><\/p>\n<p style=\"text-align: center\"><em>input[type=&#8221;email&#8221;] {<\/em><br \/>\n<em>border: 2px solid blue;<\/em><br \/>\n<em>}<\/em><\/p>\n<p><strong>4. Select images with alt text containing &#8220;logo&#8221;:<\/strong><\/p>\n<p style=\"text-align: center\"><em>img[alt*=&#8221;logo&#8221;] {<\/em><br \/>\n<em>border: 1px solid gray;<\/em><br \/>\n<em>}<\/em><\/p>\n<p><strong>Best Practice:<\/strong><\/p>\n<p>Use attribute selectors when you want to style elements based on their function or purpose, without adding extra classes or IDs.<\/p>\n<h3>2. Pseudo-Classes<\/h3>\n<p><strong>Definition:<\/strong><\/p>\n<p>Pseudo-classes define a special state of an element.<br \/>\nThey allow you to style elements based on user interaction (like hover or focus), their position in the document, or other characteristics.<\/p>\n<p><strong>Syntax:<\/strong><\/p>\n<p style=\"text-align: center\"><em>selector:pseudo-class {<\/em><br \/>\n<em>property: value;<\/em><br \/>\n<em>}<\/em><\/p>\n<p><strong>Common Pseudo-Classes:<\/strong><\/p>\n<table class=\"pseudo-classes\">\n<caption><strong>Common Pseudo-Classes<\/strong><\/caption>\n<thead>\n<tr>\n<th>Pseudo-Class<\/th>\n<th>Meaning<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td><code class=\"pseudo-class\">:hover<\/code><\/td>\n<td>When the user hovers over an element<\/td>\n<\/tr>\n<tr>\n<td><code class=\"pseudo-class\">:focus<\/code><\/td>\n<td>When an element gains focus (e.g., input box)<\/td>\n<\/tr>\n<tr>\n<td><code class=\"pseudo-class\">:active<\/code><\/td>\n<td>When an element is being clicked<\/td>\n<\/tr>\n<tr>\n<td><code class=\"pseudo-class\">:visited<\/code><\/td>\n<td>A link that has already been visited<\/td>\n<\/tr>\n<tr>\n<td><code class=\"pseudo-class\">:first-child<\/code><\/td>\n<td>Selects the first child of a parent<\/td>\n<\/tr>\n<tr>\n<td><code class=\"pseudo-class\">:last-child<\/code><\/td>\n<td>Selects the last child of a parent<\/td>\n<\/tr>\n<tr>\n<td><code class=\"pseudo-class\">:nth-child(n)<\/code><\/td>\n<td>Selects the nth child of a parent<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p><strong>Examples:<\/strong><\/p>\n<p><strong>1. Change button color on hover:<\/strong><\/p>\n<p style=\"text-align: center\"><em>button:hover {<\/em><br \/>\n<em>background-color: orange;<\/em><br \/>\n<em>color: white;<\/em><br \/>\n<em>}<\/em><\/p>\n<p><strong>2. Highlight an input when it is focused:<\/strong><\/p>\n<p style=\"text-align: center\"><em>input:focus {<\/em><br \/>\n<em>border-color: green;<\/em><br \/>\n<em>outline: none;<\/em><br \/>\n<em>}<\/em><\/p>\n<p><strong>3. Style the first list item differently:<\/strong><\/p>\n<p style=\"text-align: center\"><em>ul li:first-child {<\/em><br \/>\n<em>font-weight: bold;<\/em><br \/>\n<em>}<\/em><\/p>\n<p><strong>4. Style every odd row in a table:<\/strong><\/p>\n<p style=\"text-align: center\"><em>tr:nth-child(odd) {<\/em><br \/>\n<em>background-color: #f9f9f9;<\/em><br \/>\n<em>}<\/em><\/p>\n<p><strong>Best Practice:<\/strong><\/p>\n<p>Use pseudo-classes to create interactive and intuitive experiences without needing JavaScript for simple UI feedback.<\/p>\n<h3>Combining Attribute Selectors and Pseudo-Classes<\/h3>\n<p><strong>You can combine them for advanced, precise styling:<\/strong><\/p>\n<p><strong>Example:<\/strong> Highlight external links when hovered:<\/p>\n<p style=\"text-align: center\"><em>a[href^=&#8221;http&#8221;]:hover {<\/em><br \/>\n<em>text-decoration: underline;<\/em><br \/>\n<em>color: red;<\/em><br \/>\n<em>}<\/em><\/p>\n<ul>\n<li>Only external links (href starts with &#8220;http&#8221;) will change style when hovered.<\/li>\n<\/ul>\n<p><strong>Summary:<\/strong><\/p>\n<table class=\"css-features\">\n<caption><strong>Summary Table:<\/strong><\/caption>\n<thead>\n<tr>\n<th>Feature<\/th>\n<th>Purpose<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td class=\"feature-name\">Attribute Selectors<\/td>\n<td>Style elements based on their attributes<\/td>\n<\/tr>\n<tr>\n<td class=\"feature-name\">Pseudo-Classes<\/td>\n<td>Style elements based on user interaction or structure<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p><strong>Conclusion:<\/strong><\/p>\n<p>Attribute selectors and pseudo-classes are key tools for writing smart, dynamic CSS.<br \/>\nThey help you style based on context, behavior, and user action, creating more flexible and responsive designs \u2014 without cluttering your HTML with extra classes. For accessibility considerations and modern best practices, see <a href=\"https:\/\/buhave.com\/courses\/css\/best-practices-and-optimization\/\">Best Practices and Optimization<\/a>.<\/p>\n<h2>Combinators: descendant, child, adjacent sibling, general sibling selectors<\/h2>\n<p>In CSS, combinators allow you to select elements based on the relationships between HTML elements.<br \/>\nInstead of targeting elements individually, combinators let you style elements based on their position relative to other elements.<\/p>\n<p><strong>There are four main types of combinators:<\/strong><\/p>\n<h3>1. Descendant Selector (space)<\/h3>\n<p><strong>Definition:<\/strong><\/p>\n<p>The descendant selector selects any element that is inside another element, no matter how deeply nested.<\/p>\n<p><strong>Syntax:<\/strong><\/p>\n<p style=\"text-align: center\"><em>parent descendant {<\/em><br \/>\n<em>property: value;<\/em><br \/>\n<em>}<\/em><\/p>\n<p><strong>Example:<\/strong><\/p>\n<p style=\"text-align: center\"><em>div p {<\/em><br \/>\n<em>color: blue;<\/em><br \/>\n<em>}<\/em><\/p>\n<ul>\n<li>This selects all &lt;p&gt; elements that are inside a &lt;div&gt;, whether directly or deeply nested.<\/li>\n<\/ul>\n<p><strong>HTML Example:<\/strong><\/p>\n<p style=\"text-align: center\"><em>&lt;div&gt;<\/em><br \/>\n<em>&lt;p&gt;Paragraph 1&lt;\/p&gt;<\/em><br \/>\n<em>&lt;section&gt;<\/em><br \/>\n<em>&lt;p&gt;Paragraph 2&lt;\/p&gt;<\/em><br \/>\n<em>&lt;\/section&gt;<\/em><br \/>\n<em>&lt;\/div&gt;<\/em><\/p>\n<ul>\n<li>Both paragraphs will be styled because they are inside the div.<\/li>\n<\/ul>\n<p><strong>Use When:<\/strong><\/p>\n<ul>\n<li>You want to target all descendants of an element, regardless of how deep.<\/li>\n<\/ul>\n<h3>2. Child Selector (&gt;)<\/h3>\n<p><strong>Definition:<\/strong><\/p>\n<p>The child selector selects only direct children of a specified element.<\/p>\n<p><strong>Syntax:<\/strong><\/p>\n<p style=\"text-align: center\"><em>parent &gt; child {<\/em><br \/>\n<em>property: value;<\/em><br \/>\n<em>}<\/em><\/p>\n<p><strong>Example:<\/strong><\/p>\n<p style=\"text-align: center\"><em>ul &gt; li {<\/em><br \/>\n<em>list-style-type: square;<\/em><br \/>\n<em>}<\/em><\/p>\n<ul>\n<li>This selects only &lt;li&gt; elements that are direct children of the &lt;ul&gt;.<\/li>\n<\/ul>\n<p><strong>HTML Example:<\/strong><\/p>\n<p style=\"text-align: center\"><em>&lt;ul&gt;<\/em><br \/>\n<em>&lt;li&gt;Item 1&lt;\/li&gt; &lt;!&#8211; styled &#8211;&gt;<\/em><br \/>\n<em>&lt;div&gt;<\/em><br \/>\n<em>&lt;li&gt;Item 2&lt;\/li&gt; &lt;!&#8211; styled &#8211;&gt;<\/em><\/p>\n<ul>\n<li>Only the direct child &lt;li&gt; elements of the &lt;ul&gt; will receive the styles.<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>Element, Class, and ID Selectors In CSS, selectors are patterns used to target specific HTML elements for styling. Introduction to [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":581,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"site-sidebar-layout":"default","site-content-layout":"","ast-site-content-layout":"default","site-content-style":"default","site-sidebar-style":"default","ast-global-header-display":"","ast-banner-title-visibility":"","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","ast-disable-related-posts":"","theme-transparent-header-meta":"","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":"","astra-migrate-meta-layouts":"default","ast-page-background-enabled":"default","ast-page-background-meta":{"desktop":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"ast-content-background-meta":{"desktop":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"footnotes":""},"categories":[12],"tags":[],"class_list":["post-579","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-css"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.7 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>CSS Selectors -<\/title>\n<meta name=\"description\" content=\"CSS selectors target HTML elements to apply specific styles based on element types, classes, IDs, or attributes.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/buhave.com\/courses\/css\/css-selectors\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"CSS Selectors -\" \/>\n<meta property=\"og:description\" content=\"CSS selectors target HTML elements to apply specific styles based on element types, classes, IDs, or attributes.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/buhave.com\/courses\/css\/css-selectors\/\" \/>\n<meta property=\"og:site_name\" content=\"BUHAVE\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/BeYouHave\/\" \/>\n<meta property=\"article:author\" content=\"https:\/\/www.facebook.com\/naveedsafdarawan\/\" \/>\n<meta property=\"article:published_time\" content=\"2025-05-19T10:46:04+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2026-06-06T13:21:07+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/buhave.com\/courses\/wp-content\/uploads\/2025\/04\/CSS-Selectors.webp\" \/>\n\t<meta property=\"og:image:width\" content=\"1200\" \/>\n\t<meta property=\"og:image:height\" content=\"628\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/webp\" \/>\n<meta name=\"author\" content=\"Naveed Safdar\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Naveed Safdar\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"9 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/buhave.com\\\/courses\\\/css\\\/css-selectors\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/buhave.com\\\/courses\\\/css\\\/css-selectors\\\/\"},\"author\":{\"name\":\"Naveed Safdar\",\"@id\":\"https:\\\/\\\/buhave.com\\\/courses\\\/#\\\/schema\\\/person\\\/04fe0254e118521c9fbb3da39de5acca\"},\"headline\":\"CSS Selectors\",\"datePublished\":\"2025-05-19T10:46:04+00:00\",\"dateModified\":\"2026-06-06T13:21:07+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/buhave.com\\\/courses\\\/css\\\/css-selectors\\\/\"},\"wordCount\":1344,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\\\/\\\/buhave.com\\\/courses\\\/#organization\"},\"image\":{\"@id\":\"https:\\\/\\\/buhave.com\\\/courses\\\/css\\\/css-selectors\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/buhave.com\\\/courses\\\/wp-content\\\/uploads\\\/2025\\\/04\\\/CSS-Selectors.webp\",\"articleSection\":[\"CSS Course\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/buhave.com\\\/courses\\\/css\\\/css-selectors\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/buhave.com\\\/courses\\\/css\\\/css-selectors\\\/\",\"url\":\"https:\\\/\\\/buhave.com\\\/courses\\\/css\\\/css-selectors\\\/\",\"name\":\"CSS Selectors -\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/buhave.com\\\/courses\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/buhave.com\\\/courses\\\/css\\\/css-selectors\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/buhave.com\\\/courses\\\/css\\\/css-selectors\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/buhave.com\\\/courses\\\/wp-content\\\/uploads\\\/2025\\\/04\\\/CSS-Selectors.webp\",\"datePublished\":\"2025-05-19T10:46:04+00:00\",\"dateModified\":\"2026-06-06T13:21:07+00:00\",\"description\":\"CSS selectors target HTML elements to apply specific styles based on element types, classes, IDs, or attributes.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/buhave.com\\\/courses\\\/css\\\/css-selectors\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/buhave.com\\\/courses\\\/css\\\/css-selectors\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/buhave.com\\\/courses\\\/css\\\/css-selectors\\\/#primaryimage\",\"url\":\"https:\\\/\\\/buhave.com\\\/courses\\\/wp-content\\\/uploads\\\/2025\\\/04\\\/CSS-Selectors.webp\",\"contentUrl\":\"https:\\\/\\\/buhave.com\\\/courses\\\/wp-content\\\/uploads\\\/2025\\\/04\\\/CSS-Selectors.webp\",\"width\":1200,\"height\":628,\"caption\":\"CSS Selectors\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/buhave.com\\\/courses\\\/css\\\/css-selectors\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Courses\",\"item\":\"https:\\\/\\\/buhave.com\\\/courses\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"CSS Course\",\"item\":\"https:\\\/\\\/buhave.com\\\/courses\\\/learn\\\/css\\\/\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"CSS Selectors\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/buhave.com\\\/courses\\\/#website\",\"url\":\"https:\\\/\\\/buhave.com\\\/courses\\\/\",\"name\":\"BUHAVE\",\"description\":\"Courses - Learn Online for Free\",\"publisher\":{\"@id\":\"https:\\\/\\\/buhave.com\\\/courses\\\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/buhave.com\\\/courses\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\\\/\\\/buhave.com\\\/courses\\\/#organization\",\"name\":\"BUHAVE\",\"url\":\"https:\\\/\\\/buhave.com\\\/courses\\\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/buhave.com\\\/courses\\\/#\\\/schema\\\/logo\\\/image\\\/\",\"url\":\"https:\\\/\\\/buhave.com\\\/courses\\\/wp-content\\\/uploads\\\/2025\\\/03\\\/buhave-course.webp\",\"contentUrl\":\"https:\\\/\\\/buhave.com\\\/courses\\\/wp-content\\\/uploads\\\/2025\\\/03\\\/buhave-course.webp\",\"width\":375,\"height\":75,\"caption\":\"BUHAVE\"},\"image\":{\"@id\":\"https:\\\/\\\/buhave.com\\\/courses\\\/#\\\/schema\\\/logo\\\/image\\\/\"},\"sameAs\":[\"https:\\\/\\\/www.facebook.com\\\/BeYouHave\\\/\",\"https:\\\/\\\/www.linkedin.com\\\/company\\\/buhave\",\"https:\\\/\\\/www.youtube.com\\\/@buhave\"]},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/buhave.com\\\/courses\\\/#\\\/schema\\\/person\\\/04fe0254e118521c9fbb3da39de5acca\",\"name\":\"Naveed Safdar\",\"description\":\"I\u2019m Naveed Safdar - SEO Manager with over 10 years of experience in SEO and Digital Marketing. I\u2019ve had the privilege of working with leading national and international companies including Grafdom, PakWheels, Systems Limited, Confiz, Educative, and Dubizzle Labs. My expertise spans technical SEO, content strategy, organic growth, and performance analytics - helping businesses improve visibility, traffic, and ROI.\",\"sameAs\":[\"https:\\\/\\\/www.linkedin.com\\\/in\\\/naveedsafdar\\\/\",\"https:\\\/\\\/www.facebook.com\\\/naveedsafdarawan\\\/\",\"https:\\\/\\\/www.youtube.com\\\/@naveedsafdar\"],\"url\":\"https:\\\/\\\/buhave.com\\\/courses\\\/author\\\/naveed-safdar\\\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"CSS Selectors -","description":"CSS selectors target HTML elements to apply specific styles based on element types, classes, IDs, or attributes.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/buhave.com\/courses\/css\/css-selectors\/","og_locale":"en_US","og_type":"article","og_title":"CSS Selectors -","og_description":"CSS selectors target HTML elements to apply specific styles based on element types, classes, IDs, or attributes.","og_url":"https:\/\/buhave.com\/courses\/css\/css-selectors\/","og_site_name":"BUHAVE","article_publisher":"https:\/\/www.facebook.com\/BeYouHave\/","article_author":"https:\/\/www.facebook.com\/naveedsafdarawan\/","article_published_time":"2025-05-19T10:46:04+00:00","article_modified_time":"2026-06-06T13:21:07+00:00","og_image":[{"width":1200,"height":628,"url":"https:\/\/buhave.com\/courses\/wp-content\/uploads\/2025\/04\/CSS-Selectors.webp","type":"image\/webp"}],"author":"Naveed Safdar","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Naveed Safdar","Est. reading time":"9 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/buhave.com\/courses\/css\/css-selectors\/#article","isPartOf":{"@id":"https:\/\/buhave.com\/courses\/css\/css-selectors\/"},"author":{"name":"Naveed Safdar","@id":"https:\/\/buhave.com\/courses\/#\/schema\/person\/04fe0254e118521c9fbb3da39de5acca"},"headline":"CSS Selectors","datePublished":"2025-05-19T10:46:04+00:00","dateModified":"2026-06-06T13:21:07+00:00","mainEntityOfPage":{"@id":"https:\/\/buhave.com\/courses\/css\/css-selectors\/"},"wordCount":1344,"commentCount":0,"publisher":{"@id":"https:\/\/buhave.com\/courses\/#organization"},"image":{"@id":"https:\/\/buhave.com\/courses\/css\/css-selectors\/#primaryimage"},"thumbnailUrl":"https:\/\/buhave.com\/courses\/wp-content\/uploads\/2025\/04\/CSS-Selectors.webp","articleSection":["CSS Course"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/buhave.com\/courses\/css\/css-selectors\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/buhave.com\/courses\/css\/css-selectors\/","url":"https:\/\/buhave.com\/courses\/css\/css-selectors\/","name":"CSS Selectors -","isPartOf":{"@id":"https:\/\/buhave.com\/courses\/#website"},"primaryImageOfPage":{"@id":"https:\/\/buhave.com\/courses\/css\/css-selectors\/#primaryimage"},"image":{"@id":"https:\/\/buhave.com\/courses\/css\/css-selectors\/#primaryimage"},"thumbnailUrl":"https:\/\/buhave.com\/courses\/wp-content\/uploads\/2025\/04\/CSS-Selectors.webp","datePublished":"2025-05-19T10:46:04+00:00","dateModified":"2026-06-06T13:21:07+00:00","description":"CSS selectors target HTML elements to apply specific styles based on element types, classes, IDs, or attributes.","breadcrumb":{"@id":"https:\/\/buhave.com\/courses\/css\/css-selectors\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/buhave.com\/courses\/css\/css-selectors\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/buhave.com\/courses\/css\/css-selectors\/#primaryimage","url":"https:\/\/buhave.com\/courses\/wp-content\/uploads\/2025\/04\/CSS-Selectors.webp","contentUrl":"https:\/\/buhave.com\/courses\/wp-content\/uploads\/2025\/04\/CSS-Selectors.webp","width":1200,"height":628,"caption":"CSS Selectors"},{"@type":"BreadcrumbList","@id":"https:\/\/buhave.com\/courses\/css\/css-selectors\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Courses","item":"https:\/\/buhave.com\/courses\/"},{"@type":"ListItem","position":2,"name":"CSS Course","item":"https:\/\/buhave.com\/courses\/learn\/css\/"},{"@type":"ListItem","position":3,"name":"CSS Selectors"}]},{"@type":"WebSite","@id":"https:\/\/buhave.com\/courses\/#website","url":"https:\/\/buhave.com\/courses\/","name":"BUHAVE","description":"Courses - Learn Online for Free","publisher":{"@id":"https:\/\/buhave.com\/courses\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/buhave.com\/courses\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/buhave.com\/courses\/#organization","name":"BUHAVE","url":"https:\/\/buhave.com\/courses\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/buhave.com\/courses\/#\/schema\/logo\/image\/","url":"https:\/\/buhave.com\/courses\/wp-content\/uploads\/2025\/03\/buhave-course.webp","contentUrl":"https:\/\/buhave.com\/courses\/wp-content\/uploads\/2025\/03\/buhave-course.webp","width":375,"height":75,"caption":"BUHAVE"},"image":{"@id":"https:\/\/buhave.com\/courses\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/BeYouHave\/","https:\/\/www.linkedin.com\/company\/buhave","https:\/\/www.youtube.com\/@buhave"]},{"@type":"Person","@id":"https:\/\/buhave.com\/courses\/#\/schema\/person\/04fe0254e118521c9fbb3da39de5acca","name":"Naveed Safdar","description":"I\u2019m Naveed Safdar - SEO Manager with over 10 years of experience in SEO and Digital Marketing. I\u2019ve had the privilege of working with leading national and international companies including Grafdom, PakWheels, Systems Limited, Confiz, Educative, and Dubizzle Labs. My expertise spans technical SEO, content strategy, organic growth, and performance analytics - helping businesses improve visibility, traffic, and ROI.","sameAs":["https:\/\/www.linkedin.com\/in\/naveedsafdar\/","https:\/\/www.facebook.com\/naveedsafdarawan\/","https:\/\/www.youtube.com\/@naveedsafdar"],"url":"https:\/\/buhave.com\/courses\/author\/naveed-safdar\/"}]}},"_links":{"self":[{"href":"https:\/\/buhave.com\/courses\/wp-json\/wp\/v2\/posts\/579","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/buhave.com\/courses\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/buhave.com\/courses\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/buhave.com\/courses\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/buhave.com\/courses\/wp-json\/wp\/v2\/comments?post=579"}],"version-history":[{"count":4,"href":"https:\/\/buhave.com\/courses\/wp-json\/wp\/v2\/posts\/579\/revisions"}],"predecessor-version":[{"id":1152,"href":"https:\/\/buhave.com\/courses\/wp-json\/wp\/v2\/posts\/579\/revisions\/1152"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/buhave.com\/courses\/wp-json\/wp\/v2\/media\/581"}],"wp:attachment":[{"href":"https:\/\/buhave.com\/courses\/wp-json\/wp\/v2\/media?parent=579"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/buhave.com\/courses\/wp-json\/wp\/v2\/categories?post=579"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/buhave.com\/courses\/wp-json\/wp\/v2\/tags?post=579"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}