What Is Cascading in CSS?
Last Updated :
14 Jan, 2025
Cascading in CSS refers to the process by which the browser determines which style rules to apply when multiple rules target the same element. The term "cascading" highlights how CSS applies a hierarchy of rules, where certain rules take precedence over others based on defined principles.
For Example,
HTML
<!--Driver Code Starts-->
<html>
<head>
<!--Driver Code Ends-->
<style>
/* Internal Style */
.header {
color: blue;
}
</style>
<!--Driver Code Starts-->
</head>
<body>
<h1 class="header" style="color: red;">Hello, World!</h1>
</body>
</html>
<!--Driver Code Ends-->
Output:
Cascading in CSSIn this example, the inline style color: red; will override the internal style color: blue; because inline styles take priority in the CSS cascade.
How Cascading Works in CSS?
CSS rules can come from multiple sources: user-defined styles, browser default styles, and custom styles from a website. The cascading process determines which of these styles is applied when they conflict.
Here's how it works?
- Specificity: CSS rules with more specific selectors take priority over less specific ones. For example, an ID selector (#header) is more specific than a class selector (.header), so it will override it if both are applied to the same element.
- Importance: Styles marked with !important take precedence over all other styles, even if they are less specific. However, overusing !important can lead to maintenance problems and should be avoided unless absolutely necessary.
- Source Order: When two rules have the same specificity and importance, the order in which the rules are defined matters. The later rule (appearing further down the CSS file or in the later <style> block) will override the earlier one.
Cascading Order Breakdown
Here’s a simplified breakdown of the cascading order from highest priority to lowest priority:
- !important: Highest priority, regardless of source.
- Inline styles: Next in priority, unless overridden by !important.
- Internal CSS: Styles within a <style> tag which apply only to that document.
- External CSS: Styles from external CSS files linked to the HTML document, which apply globally across multiple pages.
- Browser/User styles: Defaults or user-defined preferences.
!important > Inline CSS > Internal CSS > External CSS > Brower/User Styles
HTML
<!--Driver Code Starts-->
<html>
<head>
<!--Driver Code Ends-->
<link rel="stylesheet" href="index.css">
<style>
h1 {
background-color: red;
}
</style>
<!--Driver Code Starts-->
</head>
<body>
<h1 style="background-color: blue;">Cascading in CSS</h1>
</body>
</html>
<!--Driver Code Ends-->
CSS
h1{
background-color: brown !important
}
Output
What Is Cascading in CSS- Cascading OrderIn this example, we can see even after inline CSS and internal CSS, the external CSS having !important overrides all the styles.
CSS Specificity
CSS specificity plays a huge role in the cascade. It is a measure of how specific a selector is, and it helps determine which rule applies when multiple rules match the same element.
CSS specificity is calculated using a point system based on the types of selectors used in a rule:
- Inline styles: 1000 points.
- ID selector: 100 points
- Classes, attributes, and pseudo-classes: 10 points.
- Tags and pseudo-elements: 1 point.
/* Less specific */
div {
color: blue;
}
/* More specific */
#header {
color: green;
}
/* Most specific */
#header .title {
color: red;
}
In this case, if an element matches all three selectors, the style color: red; will be applied because it has the highest specificity.
Rule Overriding
Rule overriding in CSS happens when more than one style is applied to the same element, and one style wins over the other. The most specific style or the one that comes last in the code usually gets applied.
HTML
<!--Driver Code Starts-->
<html>
<head>
<!--Driver Code Ends-->
<style>
p {
color: green;
/* Least specific - applies to all <p> elements */
}
.special {
color: blue;
/* More specific - applies to elements with class "special" */
}
#unique {
color: red;
/* Most specific - applies to element with ID "unique" */
}
</style>
<!--Driver Code Starts-->
</head>
<body>
<p>This text is green.</p>
<p class="special">This text is blue.</p>
<p id="unique" class="special">This text is red (ID overrides class).</p>
</body>
</html>
<!--Driver Code Ends-->
Output:
id>class>element- The first
<p>
is styled by the tag selector, so it's green. - The second
<p>
has a class special
, which overrides the tag selector, making it blue. - The third
<p>
has both a class special
and an ID unique
. The ID rule is more specific, so it’s red.
Similar Reads
What is @apply in CSS ? In this article, we will see the @apply rule property in CSS. This rule is used to implement a bunch of CSS properties which is already been declared in a variable like a method in JAVA. In simple words, we will make a set of CSS properties and store them into a variable for later use and we can use
2 min read
What is Float Containment in CSS ? In this article, we will see what is float containment in CSS. The CSS Float Containment is used to improve the performance of web pages by isolating the subpart of the page from the rest of the page. Once the browsers know that a certain part of code is independent then its rendering can be optimiz
3 min read
What is CSS? CSS, which stands for Cascading Style Sheets is a language in web development that enhances the presentation of HTML elements. By applying styles like color, layout, and spacing, CSS makes web pages visually appealing and responsive to various screen sizes.CSS allows you to control the look and feel
5 min read
Types of CSS (Cascading Style Sheet) CSS is used to style and layout web pages, controlling the appearance of HTML elements. It allows developers to create visually appealing designs and ensure a consistent look across a website.Types of CSSCSS can be implemented in three different ways:Inline CSSInternal or Embedded CSSExternal CSS1.
3 min read
What is grouping in HTML ? Grouping plays a vital role in our web page because it helps the developer to target specific classes and id which makes it easier to position, style, or manipulate the web page with the help of HTML, CSS, or JavaScript. Grouping can be performed with the help of various tags such as <div>,
2 min read