How CSS Styles Conflicts Between Selectors ?
Last Updated :
06 Jun, 2023
CSS conflicts between selectors occur when two or more selectors have conflicting styles applied to the same element, leading to unexpected results. In such cases, the browser has to decide which style to apply to the element. In this article, we’ll understand what causes conflicting styles in CSS, and how to avoid and resolve them.
Causes of Conflicting Styles in CSS: There are several ways that conflicting styles can arise in CSS. One common cause is using multiple stylesheets, either external or internal, that have overlapping or conflicting rules. When styles from multiple stylesheets are applied to the same element, the browser will have to decide which style to use, leading to conflicts.
Another cause of conflicting styles is the use of multiple selectors that apply to the same element. If these selectors have conflicting styles, the browser will have to decide which style to apply, based on the specificity and order of the selectors.
There are three reasons for CSS style conflict:
- Specificity
- Inheritance
- !important
Specificity: The more specific selector will override the less specific one. Specificity is calculated based on the number of elements, classes, and IDs in the selector. For example, the selector with an ID has a higher specificity than the one with a class, and the one with a class has a higher specificity than the one with an element.
Syntax:
p {
color: blue;
}
.special {
color: red;
}
Example: In this example, we have two conflicting styles for the color property applied to the same element p.special. However, the .special selector has a higher specificity than the p selector, so the color: red style will override the color: blue style.
HTML
<!DOCTYPE html>
< html lang = "en" >
< head >
< meta charset = "UTF-8" />
< meta http-equiv = "X-UA-Compatible" content = "IE=edge" />
< meta name = "viewport" content =
"width=device-width, initial-scale=1.0" />
< style >
p {
color: blue;
}
.special {
color: red;
}
</ style >
</ head >
< body >
< p class = "special" >This is a red text.</ p >
</ body >
</ html >
|
Output:

Specificity
Inheritance: Inheritance is the process by which styles applied to a parent element are passed down to its child elements. If two selectors have the same specificity, the one that comes later in the stylesheet will override the earlier one. If one of the conflicting styles is inherited, the browser will use the inherited style over the non-inherited style, even if the non-inherited style has a higher specificity or is defined later in the cascade order.
Syntax:
/* Style applied to the parent element */
.parent {
font-size: 14px;
}
/* Conflicting style applied to the child element */
.child {
font-size: 16px;
}
Example: In this example, the parent element has a font size of 14px applied to it, while the child element has a conflicting font size of 16px applied to it. However, because the font size is an inherited property, the child element will inherit the font size of 14px from the parent element, unless a more specific style is applied directly to the child element.
HTML
<!DOCTYPE html>
< html lang = "en" >
< head >
< meta charset = "UTF-8" />
< meta http-equiv = "X-UA-Compatible" content = "IE=edge" />
< meta name = "viewport" content =
"width=device-width, initial-scale=1.0" />
< style >
/* Style applied to the parent element */
.parent {
font-size: 14px;
}
/* Conflicting style applied to
the child element */
.child {
font-size: 16px;
}
</ style >
</ head >
< body >
< div class = "parent" >
< p >
Parent element with font-size of 14px
</ p >
< div class = "child" >
< p >
Child element with conflicting
font-size of 16px
</ p >
</ div >
</ div >
</ body >
</ html >
|
Output:
!important: The styles marked with the !important keyword will always take precedence over any other styles.
Syntax:
p {
color: blue !important;
}
.special {
color: red;
}
Example: In this example, we have two conflicting styles for the color property applied to the same element p.special. However, the color: blue !important style has the highest priority due to the !important keyword, so it will override the color: red style.
HTML
<!DOCTYPE html>
< html lang = "en" >
< head >
< meta charset = "UTF-8" />
< meta http-equiv = "X-UA-Compatible" content = "IE=edge" />
< meta name = "viewport" content =
"width=device-width, initial-scale=1.0" />
< style >
p {
color: blue !important;
}
.special {
color: red;
}
</ style >
</ head >
< body >
< p class = "special" >This is a red text.</ p >
</ body >
</ html >
|
Output:

Similar Reads
How to use @layer rule to overcome cascading styles conflicts in CSS ?
A cascade layer is declared using the @layer CSS at-rule, which can also be used to specify the hierarchy of precedence when there are multiple cascade layers. With this, web developers have more control over cascades as a result of rules that layer cascades together. Any styles that are not in a la
3 min read
Difference between :first-child and :first-of-type selector in CSS
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 s
2 min read
CSS :nth-of-type() Selector
The :nth-of-type() in css Selector is used to style only those elements which are the nth number of child of its parent element. An n may be a number, a keyword, or a formula. Syntax: :nth-of-type(number) { // CSS Property;} Where number is the argument that represents the pattern for matching eleme
2 min read
CSS :last-of-type Selector
The :last-of-type Selector is used to target the last child element of the same type of it's parent for styling. This selector is same as ":nth-last-of-type". Syntax: :last-of-type { //property}Example: [GFGTABS] HTML <!DOCTYPE html> <html> <head> <style> p:last-of-type { bac
2 min read
CSS :first-of-type Selector
The :first-of-type Selector is used to targeting the first child of every element of it's parent. if we want to style the first child of an element without giving a class, we can use it. Syntax: :first-of-type { //property}Example: [GFGTABS] HTML <!DOCTYPE html> <html> <head> <s
2 min read
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
3 min read
Wildcard Selectors (*, ^ and $) in CSS for classes
Wildcard selectors can be used with attribute selectors. The asterisk (*) matches any character, the caret (^) matches the start, and the dollar sign ($) matches the end of an attribute value. For example, .class* selects all elements with a class attribute containing "class" anywhere, .class^ selec
3 min read
Difference between the ânth-child()â and ânth-of-type()â selectors in jQuery
In this article, we will discuss all the differences between nth-child() and nth-of-type() selectors in jQuery. nth-child() Selector: This selector is used to match the elements based on their position in a group of siblings. It matches every element that is the nth-child, regardless of the type, of
2 min read
Why do browsers match CSS selectors from right to left ?
Cascading Style Sheets fondly referred to as CSS, is a simply designed language intended to simplify the process of making web pages presentable. CSS allows you to apply styles to web pages. More importantly, CSS enables you to do this independently of the HTML that makes up each web page. It descri
3 min read
CSS :has() pseudo-class Selector
The CSS :has() pseudo-class is a relatively new feature that allows developers to select elements based on their children. It is a powerful tool for more specific and dynamic styling and can be used in conjunction with other pseudo-classes and selectors. The :has() pseudo-class is used to select par
2 min read