CSS

CSS Overview Selectors Declarations Rule Sets Comments Reset/Normalize Origin Importance Order Specificity Inheritance Cascade Layers Inline Styles Internal Styles External Styles @import Absolute Units Relative Units Dynamic Viewport Angle Units Time Units Frequency Units: Resolution Units Keywords Color Formats Color Functions & Spaces Background Color Background Image Background Repeat Background Position Background Size Background Attachment Background Shorthand Multiple Backgrounds Linear Gradients Radial Gradients Conic Gradients Font Family Font Size Font Weight Font Style Line Height Text Align Text Decoration Text Transform Letter Spacing Word Spacing Text Shadow Text Wrap Variable Fonts Content Box Padding Box Border Box Margin Box Box Sizing Property Margin Padding Border Outline Box Shadow Block Display Inline Display Inline Block Display Static Position Relative Position Absolute Position Fixed Position Sticky Position Top Element Position Right Element Position Bottom Element Position Left Element Position Z Index Flexbox Box Layout Display Flex Flex Direction Flex Wrap Justify Content Align Items Align Content Gap, Row Gap & Column Gap Flex Grow Flex Shrink Flex Basis Flex Shorthand Order Property Align Self Practical Applications CSS Grid Layout Grid Display Grid Template Columns Grid Template Rows Grid Template Areas Justify Items Grid Auto Columns Justify Self Nested Grids Responsive Web Design Min Width Max Width Orientation Logical Operators Pointer Hover Prefers Color Scheme Fluid Images Flexible Typography Viewport Width Advanced CSS Features Defining variables Using variables Scope & Inheritance of CSS Variables Property Value Fallback Practical Applications :Hover :Active :Focus :Focus Visible :Visited :Link :First Child :Last Child :Nth Child :Nth of Type :Empty :Checked :Disabled :Enabled :Valid :Invalid :Required :Has :Not :Is :Where ::Before Pseudo Element ::After Pseudo Element ::First Letter ::First Line ::Selection ::Marker CSS 2D Transformations CSS 3D Transformations Transform Origin Transform Style Backface Visibility Transition Property Transition Duration Transition Timing Function Transition Delay Transition Shorthand Ease Timing Function Linear Timing Function Ease In Timing Function Ease Out Timing Function Ease In Out Timing Function Cubic Bezier Function Animations (@keyframes) Defining @keyframes Animation Name CSS Animation Duration Animation Timing Function CSS Animation Delay CSS Animation Iteration Count Animations Direction CSS Animation Fill Mode CSS Animation Play State CSS Filter Blur CSS Filter Brightness CSS Filter Contrast() CSS Drop Shadow CSS Grayscale CSS Hue Rotate CSS Invert CSS Opacity CSS Saturate CSS Sepia Mix Blend Mode Background Blend Mode Object Fit & Object Position Scroll Snap Type Scroll Snap Align Scroll Padding Scroll Margin Scroll Triggered Animations JS Variables

Specificity


This tutorial will clarify CSS Specificity, a crucial concept for controlling which styles apply when multiple rules target the same element. Understanding specificity ensures your web designs render exactly as intended.

Specificity: Calculating Specificity, Understanding its Impact

Specificity is a set of rules that determines which CSS declaration is applied to an element when multiple declarations conflict. It's essentially a scoring system that browsers use to decide which style "wins" and takes precedence. Higher specificity means a rule is more "important" and will override less specific rules.

Example 1: Element vs. Class Specificity

/* Specificity: 0,0,0,1 (Element) */
p {
  color: blue; /* Less specific */
}

/* Specificity: 0,0,1,0 (Class) */
.my-text {
  color: red; /* More specific */
}

Explanation Here, a paragraph with class="my-text" will be red. The class selector .my-text has a higher specificity score (0,0,1,0) than the element selector p (0,0,0,1), making its style take precedence.


Example 2: ID vs. Class Specificity

/* Specificity: 0,0,1,0 (Class) */
.header-title {
  font-size: 24px;
}

/* Specificity: 0,1,0,0 (ID) */
#main-header {
  font-size: 36px; /* Most specific */
}

Explanation If an element has both a class .header-title and an ID #main-header, the ID's style will apply. ID selectors have a specificity of (0,1,0,0), which is significantly higher than a class's (0,0,1,0).


Example 3: Inline Styles Highest Specificity

<p style="color: purple;">This text is purple due to inline style.</p>

 

/* Specificity: 0,0,1,0 (Class) */
.intro-text {
  color: green;
}

/* Specificity: 0,0,0,1 (Element) */
p {
  color: orange;
}

Explanation Inline styles, defined directly in the style attribute of an HTML element, have the highest specificity (represented as (1,0,0,0)). They will always override styles from stylesheets, regardless of other selectors.


Example 4: Specificity Tie-breaking (Order of Appearance)

/* Specificity: 0,0,1,0 */
.button {
  background-color: blue;
}

/* Specificity: 0,0,1,0 */
.button {
  background-color: green; /* This one wins due to order */
}

Explanation When two or more selectors have the exact same specificity score, the rule that appears later in the stylesheet (or linked CSS files) will be applied. This is known as the "last declared wins" rule.


Example 5: Combined Selectors and Specificity Calculation

/* Specificity: 0,1,1,1 (1 ID, 1 Class, 1 Element) */
#navigation ul.menu li {
  list-style: none; /* Strong specificity */
}

/* Specificity: 0,0,1,1 (1 Class, 1 Element) */
.menu li {
  list-style: disc;
}

Explanation Specificity is calculated by summing the values of different selector types: Inline styles (1,0,0,0), IDs (0,1,0,0), classes/attributes/pseudo-classes (0,0,1,0), and elements/pseudo-elements (0,0,0,1). The first rule has higher specificity because it includes an ID selector.