Tell us what’s happening:
By adding the !important after the color, it will override all other CSS styles. It can be applied to class or id, but why not onto the style for body element?
Referring the the code that I’ve attached, if I add !important after the color:green under body element, the output doesn’t change into green as I thought. Also I was tryna ask a question, not declare a statement.
In the CSS we are styling the h1 element directly by applying an id, two classes, and an inline style. Directly targeted elements take precedence over inherited styles. The body selector is a parent element.
Styles for a directly targeted element will always take precedence over inherited styles, regardless of the specificity of the inherited rule.
First, the h1 inherits the green color from the body. That color gets overwritten by the pink color, which then gets overwritten by the blue color, which then gets overwritten by the orange color, which then gets overwritten by the white color.
white orange blue pink green
If we set !important on a property of any of the three selectors (the id and two classes) that selectors style will “win”. If we set !important on both one of the class selectors and the id selector, the id selector will win because of its higher specificity.
To make the inherited color “win” we have to explicitly set it.