CSS Important Rule
In CSS, !important is a rule that is used to override other conflicting rules. When a CSS property is set to !important, it has a higher priority than other rules that apply to the same element.
The !important rule is used sparingly, as it can make CSS code more difficult to maintain and debug.
Syntax of !important
The !important rule is placed at the end of a CSS property declaration, just before the semicolon.
property: value !important;
- property: The CSS property you want to apply.
- value: The value you want to assign to the property.
- !important: The declaration is marked as important and takes precedence over other styles.
How !important Works
When multiple conflicting rules are applied to the same element, the !important rule will take precedence over the other rules, regardless of their position in the stylesheet or specificity.
Example
/* Normal rule */
h1 {
color: blue;
}
/* Important rule */
h1 {
color: red !important;
}
When to Use !important
- Overriding External Stylesheets: If you're working with a third-party stylesheet or a framework and can't modify the source, you can use !important to override its styles.
/* Override third-party stylesheet */
.button {
background-color: green !important;
}
- Override Inline Styles: Although inline styles generally have the highest specificity, you can use !important to override them.
/* Override inline styles with !important */
.highlight {
color: red !important;
}
- User-specific Customizations: Sometimes, !important is used in user-specific stylesheets or browser extensions to ensure styles apply correctly.
Quickly Find What You Are Looking For
OnlineTpoint is a website that is meant to offer basic knowledge, practice and learning materials. Though all the examples have been tested and verified, we cannot ensure the correctness or completeness of all the information on our website. All contents published on this website are subject to copyright and are owned by OnlineTpoint. By using this website, you agree that you have read and understood our Terms of Use, Cookie Policy and Privacy Policy.
point.com