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

How to force increase priority in css

Sep 06, 2022 pm 05:49 PM
css

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!

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to use bootstrap in vue How to use bootstrap in vue Apr 07, 2025 pm 11:33 PM

Using Bootstrap in Vue.js is divided into five steps: Install Bootstrap. Import Bootstrap in main.js. Use the Bootstrap component directly in the template. Optional: Custom style. Optional: Use plug-ins.

The Roles of HTML, CSS, and JavaScript: Core Responsibilities The Roles of HTML, CSS, and JavaScript: Core Responsibilities Apr 08, 2025 pm 07:05 PM

HTML defines the web structure, CSS is responsible for style and layout, and JavaScript gives dynamic interaction. The three perform their duties in web development and jointly build a colorful website.

Understanding HTML, CSS, and JavaScript: A Beginner's Guide Understanding HTML, CSS, and JavaScript: A Beginner's Guide Apr 12, 2025 am 12:02 AM

WebdevelopmentreliesonHTML,CSS,andJavaScript:1)HTMLstructurescontent,2)CSSstylesit,and3)JavaScriptaddsinteractivity,formingthebasisofmodernwebexperiences.

How to set up the framework for bootstrap How to set up the framework for bootstrap Apr 07, 2025 pm 03:27 PM

To set up the Bootstrap framework, you need to follow these steps: 1. Reference the Bootstrap file via CDN; 2. Download and host the file on your own server; 3. Include the Bootstrap file in HTML; 4. Compile Sass/Less as needed; 5. Import a custom file (optional). Once setup is complete, you can use Bootstrap's grid systems, components, and styles to create responsive websites and applications.

How to write split lines on bootstrap How to write split lines on bootstrap Apr 07, 2025 pm 03:12 PM

There are two ways to create a Bootstrap split line: using the tag, which creates a horizontal split line. Use the CSS border property to create custom style split lines.

How to insert pictures on bootstrap How to insert pictures on bootstrap Apr 07, 2025 pm 03:30 PM

There are several ways to insert images in Bootstrap: insert images directly, using the HTML img tag. With the Bootstrap image component, you can provide responsive images and more styles. Set the image size, use the img-fluid class to make the image adaptable. Set the border, using the img-bordered class. Set the rounded corners and use the img-rounded class. Set the shadow, use the shadow class. Resize and position the image, using CSS style. Using the background image, use the background-image CSS property.

How to use bootstrap button How to use bootstrap button Apr 07, 2025 pm 03:09 PM

How to use the Bootstrap button? Introduce Bootstrap CSS to create button elements and add Bootstrap button class to add button text

How to resize bootstrap How to resize bootstrap Apr 07, 2025 pm 03:18 PM

To adjust the size of elements in Bootstrap, you can use the dimension class, which includes: adjusting width: .col-, .w-, .mw-adjust height: .h-, .min-h-, .max-h-

See all articles