简体   繁体   中英

When and how to use !important CSS property in a bookmarklet with cross domains?

I have a bookmarklet and recently i have changed the CSS properties, using cleanslate css because the bookmarklet design was not the same on third party websites, when used.

This css file, should reset all the properties of all the tags within a specific tag, and it does that, but when i need to manipulate the settings, i cant;

<div class="mainclass">
<div id="login">a</div>
<div id="form">b</div>
<div id="details">c</div>
</div>

I have many forms, wich i hide and unhide at specific actions, like: i show the login form, if the user in not logged in, i show the the detail form, if the user is logged in and if i know the history of a url, and other forms;

So, if i set the div id details display property to block , and the rest to none , and then try to set all to none and the div id form display property to block , the css it doesnt do anything, but i can see that the javascript runs the actions

This css should do the job, but because i used the !important property on all the tags properties, i cant set from javascript new values to properties, like:

document.getElementById("form").style.display = "none";

在此处输入图片说明

in this image, i use the old css, a reset method was used, and also, the js code worked fine, but the design was not the same on third party websites

in the actual project, the design is the same on all the third party websites, but the js code cant set css propertyes

Important is a really bad idea in general and should try to be avoided as it breaks the rules of specificity in css. Is there no way you can make your css more specific to overide the current styles rather than using !importnat.

You can still apply importnant by doing some thing like this

element.style.setProperty("display", "none", "important");

Or jquery makes it quite easy

element.css("display","none !important");

But again best to just not use important

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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