Home > Web Front-end > Front-end Q&A > How to force increase priority in css

How to force increase priority in css

青灯夜游
Release: 2022-09-06 17:49:50
Original
4590 people have browsed it

In css, you can force an increase in priority by setting the "!important" statement; this statement is used to increase the application priority of the specified CSS style rule and will be added to the end of the CSS style value to give it Styles have more weight, and the syntax is "selector {property:value!important;}". Using "!important" rules will break the natural cascading effect of style sheets, making the code difficult to maintain; therefore, unless absolutely necessary, use !important rules should be avoided as much as possible, and it should only be used in special circumstances.

How to force increase priority in css

The operating environment of this tutorial: Windows 7 system, CSS3&&HTML5 version, Dell G3 computer.

In css, you can force the priority to increase by setting the "!important" statement.

!important declaration

!important is used to increase the application priority of the specified CSS style rule; it is added to the CSS value at the end to give it more weight.

选择器{属性:值 !important;}
Copy after login

Note: Attribute: value !important Attribute values ​​can be separated by spaces.

In CSS, style rules are applied to elements in a cascading fashion. The higher the weight in the following list, the smaller the weight:

● Browser style: It is the default style declared by the web browser.

● User-declared styles: Custom styles set by users using browser options or modified through developer debugging tools.

● Style declared during development: It is the style declared by the website developer in the CSS style sheet.

● Developer-declared styles with !important rules.

● User style with !important rules.

!important provides developers with a way to increase the weight of styles, which is higher than directly setting the CSS declaration in the element's style attribute. It is generally used when CSS overrides JavaScript settings.

Example:

<!DOCTYPE HTML>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <title>测试!Important</title>
</head>
<style type="text/css">
    #Box div
    {
        color: red;
    }
    .important_false
    {
        color: blue;
    }
    .important_true
    {
        color: blue !important;
    }
</style>
<body>
    <div id="Box">
        <div class="important_false">
            这一行末使用important</div>
        <div class="important_true">
            这一行使用了important</div>
    </div>
</body>
</html>
Copy after login

Rendering:

How to force increase priority in css

The first line of CSS code sets the font color of all divs in the box to red , both the second and third lines use class to redefine the font color of the own div to be blue.

The difference is that the second line does not use !important, while the third line uses !

Summary:

The font color of the first line is red. It can be proved that in the css style setting, the priority of id is greater than class, and this line of text is still red.

The font color of the second line is blue. It can be proved that !important has the highest priority, the css style of important_true takes effect, and this line of text turns blue!

When to use !important rules?

You should not use !important rules unless absolutely necessary; using !important rules will break the natural cascading effect of the style sheet, making the code difficult to maintain. However, there are some situations where you have to use it! important:

1. When testing and debugging the website, the !important rule is very useful.

If we have some CSS issues in our code and want to make sure a specific style is applied, we can use the !important rule to temporarily fix the problem on the site until we find a better way (which may take some time).

2. Output style sheet

!important rules can also be used for output style sheets to ensure that styles are applied without being overwritten by anything else.

Conclusion

Using !important has no negative impact on performance; but from the perspective of maintainability, unless absolutely necessary, use !important should be avoided as much as possible rule, it should only be used in special circumstances.

The above is the entire content of this article, I hope it will be helpful to everyone's study. For more exciting content, you can pay attention to the relevant tutorial columns of the PHP Chinese website! ! !

Special note:

!important is not recognized in IE6, for example:

.testClass{ 
    color:blue !important; 
    color:red;
}
Copy after login

This writing method cannot be recognized under IE6. .testCalss is finally displayed in red, but you can also change the writing method to make IE6 recognize !important

.testClass{ 
    color:blue !important; 
} 
.testClass{ 
    color:red; 
}
Copy after login

(Learning video sharing: web front-end development)

The above is the detailed content of How to force increase priority in css. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
css
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template