0

0

为什么我们应该使用!important?

王林

王林

发布时间:2023-08-23 21:45:02

|

1282人浏览过

|

来源于tutorialspoint

转载

为什么我们应该使用!important?

In CSS, ‘!important’ is a keyword that we use with the values of the CSS properties. When we use the ‘!important’ with the CSS property value, the browser gives more importance to that property value over the other property values on the same element.

Here are some use cases where we require to use the ‘!important’ with the CSS property values.

  • In the content management System (CMS), we can’t edit the CSS of the web page. So, if we add extra CSS to the web page, it can’t get applied to the specific element, but if we use ‘!important’ with CSS, we can override the initial values of particular CSS properties.

  • Whenever we use any UI library in web frameworks such as ReactJS, Svelte, etc., we sometimes can’t edit the CSS of the UI component. In such cases, we can use the ‘!important’ with CSS properties to override their values.

  • The real-time application contains large CSS files. Sometimes, CSS doesn’t affect the element due to unknown overriding. In this case, we can use ‘!important’ to override all values of a particular property on the particular element.

Syntax

Users can follow the syntax below to use the ‘!important’ with the CSS property values.

CSS-property: value !important

In the above syntax, CSS-property can be any CSS property such as margin, padding, padding-left, font-size, etc., and value be respected value for the CSS property.

Example

In the example below, we have created three div elements with the class names ‘black’, ‘grey’, and ‘red’. In CSS, we have given the background color to the div element based on the class names.

Also, we have applied the ‘background-color: pink’ CSS property to all the div elements with the ‘!important’ keyword. In the output, users can observe that all div’s background color becomes pink as we have used the ‘!important’.



   


   

Using the !important with CSS property values

This is a black div.
This is a grey div.
This is a red div.

Example

In the example below, we have created two different paragraphs. One paragraph contains the ‘one’ class name, and another contains the ‘two’ as a class name. We have applied some CSS to all

随缘网络php企业网站管理系统2.0
随缘网络php企业网站管理系统2.0

随缘网络PHP企业网站管理系统V2.0正式发布,该企业网站管理系统采用PHP+MYSQL编写,界面色调风格延续之前1.0版管理系统简洁浅蓝色风格,稍有所变动。变更分类树形目录方式采用jquery库,产品,文章三级无限分类。希望大家能够喜欢。系统中难免有些小问题,希望大家在使用中有什么问题可到本站论坛提出,我们将总结各问题后给予修正并升级。本站再次声明对于免费版系列系统本站不提供QQ电话等技术咨询服

下载

elements.

To override the CSS of an element with the class name ‘one’, we have used the ‘!important’ keyword to override the style applied on the ‘

’ element. In the output, users can observe that the first paragraph has different styles.



   


   

Using the !important with CSS property values

This is a paragraph.

This is another paragraph.

Example

We will learn to override the ‘!important’ by using another ‘!important’ via the example given below. We have created the three div elements with the ‘initial’, ‘middle’, and ‘final’ class names.

We have used the ‘!important’ with the background-color CSS property, which we are applying to div elements. So, Initially, all div elements’ background is aqua. After that, we again used the ‘!important’ with the background-color CSS property while applying it to the elements with the class names ‘middle’ and ‘final’.

In the output, users can observe that blue and lightsalmon colours override the ‘aqua’ colour.



   


   

Using the !important with CSS property values

This is an initial Div element.
This is a middle Div element.
This is a final Div element.

Conclusion

Users learned to use the ‘!important’ in CSS. Basically, we should avoid the overuse of the ‘!important’; otherwise, it can become a headache for anyone to solve lots of ‘!important’ as it overrides the CSS for the element.

However, users can use ‘!important’ while updating the CSS of any library's content management system or a UI component.

本站声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn

相关专题

更多
PHP WebSocket 实时通信开发
PHP WebSocket 实时通信开发

本专题系统讲解 PHP 在实时通信与长连接场景中的应用实践,涵盖 WebSocket 协议原理、服务端连接管理、消息推送机制、心跳检测、断线重连以及与前端的实时交互实现。通过聊天系统、实时通知等案例,帮助开发者掌握 使用 PHP 构建实时通信与推送服务的完整开发流程,适用于即时消息与高互动性应用场景。

2

2026.01.19

微信聊天记录删除恢复导出教程汇总
微信聊天记录删除恢复导出教程汇总

本专题整合了微信聊天记录相关教程大全,阅读专题下面的文章了解更多详细内容。

39

2026.01.18

高德地图升级方法汇总
高德地图升级方法汇总

本专题整合了高德地图升级相关教程,阅读专题下面的文章了解更多详细内容。

99

2026.01.16

全民K歌得高分教程大全
全民K歌得高分教程大全

本专题整合了全民K歌得高分技巧汇总,阅读专题下面的文章了解更多详细内容。

148

2026.01.16

C++ 单元测试与代码质量保障
C++ 单元测试与代码质量保障

本专题系统讲解 C++ 在单元测试与代码质量保障方面的实战方法,包括测试驱动开发理念、Google Test/Google Mock 的使用、测试用例设计、边界条件验证、持续集成中的自动化测试流程,以及常见代码质量问题的发现与修复。通过工程化示例,帮助开发者建立 可测试、可维护、高质量的 C++ 项目体系。

57

2026.01.16

java数据库连接教程大全
java数据库连接教程大全

本专题整合了java数据库连接相关教程,阅读专题下面的文章了解更多详细内容。

40

2026.01.15

Java音频处理教程汇总
Java音频处理教程汇总

本专题整合了java音频处理教程大全,阅读专题下面的文章了解更多详细内容。

19

2026.01.15

windows查看wifi密码教程大全
windows查看wifi密码教程大全

本专题整合了windows查看wifi密码教程大全,阅读专题下面的文章了解更多详细内容。

107

2026.01.15

浏览器缓存清理方法汇总
浏览器缓存清理方法汇总

本专题整合了浏览器缓存清理教程汇总,阅读专题下面的文章了解更多详细内容。

44

2026.01.15

热门下载

更多
网站特效
/
网站源码
/
网站素材
/
前端模板

精品课程

更多
相关推荐
/
热门推荐
/
最新课程
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送

Copyright 2014-2026 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号