How to use !important rules in CSS? (code example)
One of the best ways to learn how to code a website is to look at the source code of other websites and learn some useful techniques. When viewing the source code, you may see "!important" in the code; what does this mean? What is the use? The following article will take you through the !important rules, I hope it will be helpful to you.
!What are the important rules? how to use?
!important is a CSS rule used to increase the application priority of a specified CSS style rule; it is added to the end of a CSS value to give it more weight. [Video tutorial recommendation: CSS tutorial]
In CSS, style rules are applied to elements in a cascading manner. 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 rule usage example:
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>php中文网(php.cn)</title> <style> #container{ width: 450px; height: 200px; font-size: 25px; } #example{ color:red !important; } #container #example{ color:pink; } </style> </head> <body> <div id="container"> <div id="example">PHP中文网!</div> </div> </body> </html>
Rendering:
Example description:
In this example, we first have an include! The style of the important rule defines the text color of an element (#example) as red. Then we have another style that changes the text color of this element (#example) to pink. because! important statement, so the text color of the element (#example) is now red instead of pink. If we didn't use it! important, then the color will be pink.
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! ! !
The above is the detailed content of How to use !important rules in CSS? (code example). For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











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.

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

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.

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.

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.

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.

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-

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