简体   繁体   English

覆盖CSS remove属性

[英]Override CSS remove property

I'm using a lib and i want to remove a property on a class. 我正在使用一个lib库,我想删除一个类的属性。 What is proper way to do it ? 正确的做法是什么?

Example : Lib.css 例子:Lib.css

div {
    width: 100%;
}

and custom.css 和custom.css

div {
    width: none; //something like that
}

Every rule in CSS has a different default value. CSS中的每个规则都有不同的默认值。 Many might have none or auto as default. 许多可能none或默认为auto Check MDN for Reference. 检查MDN以供参考。 Search for 'Initial value' 搜索“初始值”

Example

https://developer.mozilla.org/en-US/docs/Web/CSS/width https://developer.mozilla.org/zh-CN/docs/Web/CSS/width
Initial value: auto 初始值:自动

Edit 编辑

You can also use the special value initial , if you don't need to support MSIE browsers. 如果不需要支持MSIE浏览器,也可以使用特殊值initial

I encourage you to read about CSS specificity here in the docs: CSS Specificity: Mozilla Developers and check my answer down below. 我鼓励您在文档中阅读有关CSS特殊性的文章: CSS特殊性:Mozilla开发人员,并在下面查看我的答案。

There are several ways to overwrite CSS properties from external libraries. 有几种方法可以从外部库覆盖CSS属性。

Case 1: if you're using Bootstrap or Zurb Foundation via npm package, you need to change a variable value that is responsible for given property and place it after importing all library files to ovewrite correctyly eg. 情况1:如果您通过npm软件包使用Bootstrap或Zurb Foundation,则需要更改负责给定属性的变量值,并在导入所有库文件以正确进行写后将其放置。

import 'files from library.sass';

// my settings
$default-width: 80%;

Case 2: if you're using CDN to deliver your library you can use a more specific CSS selector to overwrite given property eg: 情况2:如果您使用CDN交付库,则可以使用更特定的CSS选择器覆盖给定的属性,例如:

to overwrite div selector 覆盖div选择器

div {}   ----> div.my-class {}

The second technique, but for sure not recommended is to use !important declaration. 第二种技术,但是肯定不建议使用!important声明。 But remember, using !important declaration often causes many problems during the development process. 但是请记住,使用!important声明通常会在开发过程中引起许多问题。 It is always better to find a more specific selector than use !important. 找到比使用!important更具体的选择器总是更好。

我不知道您的项目的结构,但我会在jQuery中使用CSS函数

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM