{"id":68,"date":"2026-04-06T17:49:00","date_gmt":"2026-04-06T17:49:00","guid":{"rendered":"https:\/\/gigz.pk\/css\/?post_type=lesson&#038;p=68"},"modified":"2026-04-18T09:35:16","modified_gmt":"2026-04-18T09:35:16","slug":"specificity-and-priority-in-css","status":"publish","type":"lesson","link":"https:\/\/gigz.pk\/css\/lesson\/specificity-and-priority-in-css\/","title":{"rendered":"Specificity and Priority in CSS"},"content":{"rendered":"\n<p>Specificity and priority determine which CSS rule is applied when multiple rules target the same element. Understanding this concept is essential for writing clean, predictable styles.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">What is CSS Specificity<\/h3>\n\n\n\n<p>Specificity is a ranking system used by the browser to decide which CSS rule is more important. Each selector type has a different weight.<\/p>\n\n\n\n<p>Inline styles have the highest specificity. They are written directly inside HTML elements using the style attribute.<\/p>\n\n\n\n<p>ID selectors are very strong and override class and element selectors.<\/p>\n\n\n\n<p>Class selectors, attribute selectors, and pseudo classes have medium strength.<\/p>\n\n\n\n<p>Element selectors and pseudo elements have the lowest strength.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Specificity Hierarchy<\/h3>\n\n\n\n<p>Inline styles override everything else.<\/p>\n\n\n\n<p>ID selectors override class selectors and element selectors.<\/p>\n\n\n\n<p>Class selectors override element selectors.<\/p>\n\n\n\n<p>Element selectors have the lowest priority.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Example of Specificity<\/h3>\n\n\n\n<pre class=\"wp-block-preformatted\">p {<br>  color: blue;<br>}.text {<br>  color: green;<br>}#title {<br>  color: red;<br>}<\/pre>\n\n\n\n<p>If all these rules target the same paragraph, the text will appear red because the ID selector has higher specificity.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">What is CSS Priority<\/h3>\n\n\n\n<p>Priority refers to the order in which styles are applied when specificity is equal.<\/p>\n\n\n\n<p>When two selectors have the same specificity, the rule written last in the CSS file will be applied.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Example of Priority<\/h3>\n\n\n\n<pre class=\"wp-block-preformatted\">p {<br>  color: blue;<br>}p {<br>  color: green;<br>}<\/pre>\n\n\n\n<p>The paragraph will be green because the second rule comes later.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Important Rule<\/h3>\n\n\n\n<p>The important keyword overrides all other rules, regardless of specificity.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">p {<br>  color: blue !important;<br>}#title {<br>  color: red;<br>}<\/pre>\n\n\n\n<p>The text will remain blue because the important rule takes priority.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">How Specificity is Calculated<\/h3>\n\n\n\n<p>Inline styles have the highest value.<\/p>\n\n\n\n<p>ID selectors have higher value than classes.<\/p>\n\n\n\n<p>Classes have higher value than elements.<\/p>\n\n\n\n<p>The browser compares these values to decide which rule to apply.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Best Practices<\/h3>\n\n\n\n<p>Avoid using the important keyword unless absolutely necessary.<\/p>\n\n\n\n<p>Use classes instead of IDs for better flexibility.<\/p>\n\n\n\n<p>Keep your CSS simple and organized.<\/p>\n\n\n\n<p>Write styles in a logical order to avoid conflicts.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Summary<\/h3>\n\n\n\n<p>Specificity decides which rule is stronger.<\/p>\n\n\n\n<p>Priority decides which rule is applied when strength is equal.<\/p>\n\n\n\n<p>Understanding both helps you control your website design more effectively.<\/p>\n\n\n\n<p>If you want, I can also create exercises or quizzes for this topic.<\/p>\n\n\n<div class=\"yoast-breadcrumbs\"><span><span><a href=\"https:\/\/gigz.pk\/css\/\">Home<\/a><\/span> \u00bb <span class=\"breadcrumb_last\" aria-current=\"page\">CSS Fundamentals (Beginner) > Selectors &#038; Properties > Specificity &#038; Priority<\/span><\/span><\/div>\n\n\n<div class=\"schema-faq wp-block-yoast-faq-block\"><div class=\"schema-faq-section\" id=\"faq-question-1776504894179\"><strong class=\"schema-faq-question\"><\/strong> <p class=\"schema-faq-answer\"><\/p> <\/div> <\/div>\n\n\n\n<div class=\"schema-faq wp-block-yoast-faq-block\"><div class=\"schema-faq-section\" id=\"faq-question-1776504893939\"><strong class=\"schema-faq-question\"><\/strong> <p class=\"schema-faq-answer\"><\/p> <\/div> <\/div>\n","protected":false},"menu_order":0,"template":"","class_list":["post-68","lesson","type-lesson","status-publish","hentry"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.5 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Specificity and Priority in CSS - CSS learning mastery<\/title>\n<meta name=\"description\" content=\"Learn CSS specificity and priority with simple examples. Understand selector rules, cascade, and how to control styles effectively.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/gigz.pk\/css\/lesson\/specificity-and-priority-in-css\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Specificity and Priority in CSS - CSS learning mastery\" \/>\n<meta property=\"og:description\" content=\"Learn CSS specificity and priority with simple examples. Understand selector rules, cascade, and how to control styles effectively.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/gigz.pk\/css\/lesson\/specificity-and-priority-in-css\/\" \/>\n<meta property=\"og:site_name\" content=\"CSS learning mastery\" \/>\n<meta property=\"article:modified_time\" content=\"2026-04-18T09:35:16+00:00\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data1\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":[\"WebPage\",\"FAQPage\"],\"@id\":\"https:\\\/\\\/gigz.pk\\\/css\\\/lesson\\\/specificity-and-priority-in-css\\\/\",\"url\":\"https:\\\/\\\/gigz.pk\\\/css\\\/lesson\\\/specificity-and-priority-in-css\\\/\",\"name\":\"Specificity and Priority in CSS - CSS learning mastery\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/gigz.pk\\\/css\\\/#website\"},\"datePublished\":\"2026-04-06T17:49:00+00:00\",\"dateModified\":\"2026-04-18T09:35:16+00:00\",\"description\":\"Learn CSS specificity and priority with simple examples. Understand selector rules, cascade, and how to control styles effectively.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/gigz.pk\\\/css\\\/lesson\\\/specificity-and-priority-in-css\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/gigz.pk\\\/css\\\/lesson\\\/specificity-and-priority-in-css\\\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/gigz.pk\\\/css\\\/lesson\\\/specificity-and-priority-in-css\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/gigz.pk\\\/css\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"CSS Fundamentals (Beginner) > Selectors & Properties > Specificity & Priority\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/gigz.pk\\\/css\\\/#website\",\"url\":\"https:\\\/\\\/gigz.pk\\\/css\\\/\",\"name\":\"CSS learning mastery\",\"description\":\"\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/gigz.pk\\\/css\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Specificity and Priority in CSS - CSS learning mastery","description":"Learn CSS specificity and priority with simple examples. Understand selector rules, cascade, and how to control styles effectively.","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:\/\/gigz.pk\/css\/lesson\/specificity-and-priority-in-css\/","og_locale":"en_US","og_type":"article","og_title":"Specificity and Priority in CSS - CSS learning mastery","og_description":"Learn CSS specificity and priority with simple examples. Understand selector rules, cascade, and how to control styles effectively.","og_url":"https:\/\/gigz.pk\/css\/lesson\/specificity-and-priority-in-css\/","og_site_name":"CSS learning mastery","article_modified_time":"2026-04-18T09:35:16+00:00","twitter_card":"summary_large_image","twitter_misc":{"Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":["WebPage","FAQPage"],"@id":"https:\/\/gigz.pk\/css\/lesson\/specificity-and-priority-in-css\/","url":"https:\/\/gigz.pk\/css\/lesson\/specificity-and-priority-in-css\/","name":"Specificity and Priority in CSS - CSS learning mastery","isPartOf":{"@id":"https:\/\/gigz.pk\/css\/#website"},"datePublished":"2026-04-06T17:49:00+00:00","dateModified":"2026-04-18T09:35:16+00:00","description":"Learn CSS specificity and priority with simple examples. Understand selector rules, cascade, and how to control styles effectively.","breadcrumb":{"@id":"https:\/\/gigz.pk\/css\/lesson\/specificity-and-priority-in-css\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/gigz.pk\/css\/lesson\/specificity-and-priority-in-css\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/gigz.pk\/css\/lesson\/specificity-and-priority-in-css\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/gigz.pk\/css\/"},{"@type":"ListItem","position":2,"name":"CSS Fundamentals (Beginner) > Selectors & Properties > Specificity & Priority"}]},{"@type":"WebSite","@id":"https:\/\/gigz.pk\/css\/#website","url":"https:\/\/gigz.pk\/css\/","name":"CSS learning mastery","description":"","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/gigz.pk\/css\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"}]}},"_links":{"self":[{"href":"https:\/\/gigz.pk\/css\/wp-json\/wp\/v2\/lesson\/68","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/gigz.pk\/css\/wp-json\/wp\/v2\/lesson"}],"about":[{"href":"https:\/\/gigz.pk\/css\/wp-json\/wp\/v2\/types\/lesson"}],"wp:attachment":[{"href":"https:\/\/gigz.pk\/css\/wp-json\/wp\/v2\/media?parent=68"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}