How to overrule !important? [duplicate] - html

This question already has answers here:
How to override !important?
(12 answers)
Closed 9 years ago.
I wrote a bit of html with some css styling injected into a third party site. But, their styling is messing with mine due to some !important declarations. I don't want this, and I don't want to use !important in my styles.
What can I do to prevent this?
Example at jsFiddle

The !important declaration overrides everything else, even inline styles and more specific hierarchy. The only way to override an !important is with an even more specific !important.
And this is why !important should be avoided.

You can overwrite !important contrary to what most people believe. You can even use this technique to overwrite inline styles applied by JS or 3rd party plugins (ex. Facebook!!) The most powerful way is like so:
td[style] {height: 110px !important;}
It acts as if you injected the style inline to the html because you are applying the styles to the actual style attribute of the tag. The combo of being inline and !important should trump everything, except for a style that is applied later and also inline and !important
Here's a fiddle with the working code: http://jsfiddle.net/9LvzP/
You can see that even though background-color: green !important comes before background-color: blue blue is more powerful and gets applied

Define the class with proper hierarchy that will work for you.
.list .row span{
color:red !important;
}
<div class="list">
<div class="row">
<span>Your text </span>
</div>
Try to do something similar to what I have created here.

Related

How do I override css with html to float inline pull-quote left?

I am trying to format an article hosted by a third-party blogging website where I do not have access to the css style sheet.
I would like to float a pull-quote to the left, instead of the automatic right as predefined in the pull-quote class. Since I cannot edit the style sheet, I was hoping for an inline style tag in html that I may use to override the css.
Currently I have: <div class="pullquote">insert pullquote here</div>
I am extremely new with html and css, so anything is much appreciated.
For your case you can just use inline css.ie specifying style as an attribute of the div.
For your information the order of invocation of css is as follows
Inline CSS
Embedded CSS
External CSS
More details can be found at W3Schools
If that doesnt solve your problem,
You can override any css using !important.It means, essentially, what it says; that 'this is important, ignore subsequent rules, and any usual specificity issues, apply this rule!'
According to the docs
When an !important rule is used on a style declaration, this
declaration overrides any other declaration made in the CSS, wherever
it is in the declaration list. Although, !important has nothing to do
with specificity. Using !important is bad practice because it makes
debugging hard since you break the natural cascading in your
stylesheets.
So try this to override the float right
<div class="pullquote" style="float:left !important;">insert pullquote here</div>
The above example is what is called the inline css which uses !important also.
As #j0861 mentioned in his comments Using !important is bad practice because it makes debugging hard
you can use:
<div class="pullquote" style="float:left">insert pullquote here</div>
You can apply inline-styling to any HTML element.
Where you may have a stylesheet that looks like this:
p {
color: #000;
font-weight:bold;
text-decoration:underline
}
You can directly apply this, and any other combination of styling attributes to an element (for this example a single paragraph line) by using the style attribute like this:
<p style="color:#000;font-weight:bold;text-decoration:underline">Some text here</p>
In your case you would just need to add:
<div class="pullquote" style="float:left;">insert pullquote here</div>

How to make css !important property work for all elements new style in only one declaration?

I'm making my blog design responsive on small screen sizes but when i want to change the style of a pre-styled element it changes only when !important is added to its css style. This is css example -
.post {margin-left:10px!important;height:300px!important;}
.post img {width:200px!important;height:200px!important;}
a:link {color:green!important;}
HTML-
<div class='post'>
<img src='photo.gif'> Here is the link of a very informative aricle Link
</div>
This is only a example, my css and html code is very long . Here in css style you can see each style contains !important property. Almost in every element's css i have to add !important otherwise element style becomes inherited. Is there any way in css to declare !important property only once and element's new style will work without adding !important to it.
First of all don't use !important like that unless it's only needed. !Importatn forces teh browser to render something as you require. Using it carelessly is going to cause messy code and real headaches for anyone that's maintaining the site.
You should either make the appropriate fixes where it is needed. Keep in mind that:
inline css + !important > css file !important > inline css > css file.
Also if you declare something twice for example a class .some-class{} the properties of the later one will overwrite the properties of the previous classes. But keep in mind the !important thingy...
I would suggest you do it once as you should and save your self from future headaches...
And for further reference: http://www.w3schools.com/css/ & http://www.w3schools.com/css/css_mediatypes.asp
There is no way !important can be used to apply for whole of the CSS file. However you can change the order of your CSS files the way they are called. For example for iphone web pages you can use the meta tag and your CSS will be applied accordingly.
<meta name="viewport" content="width=320"> // iphone
//CSS Rules for iphones
<meta name="viewport" content="width=768"> //tablets
//CSS Rules for tablets
I can think of no reason to use important in this way, if your styles inherit something you don't want chances are you have a selector issue.
If you need to override default styles for a particular reason try using an ID.
I suggest to you reading on selectors best ways to use them. !important should not be used for reasons other than not having access to original css.

Can I override inline !important?

If you have
<div style="display: none !important;"></div>
Is there a way to override that in the style sheet to make it displayed?
Preferably using something similar to this:
div { display: block !important; }
Let me begin by saying that generally inline styles can be overridden:
.override {color:red !important;}
<p style="color:blue;">I will be blue</p>
<p style="color:blue;" class="override">But I will be red</p>
This behavior is described in W3 specs, where it is stated that !important declarations do not alter the specificity, but rather take precedence over "normal" declarations.
That being said, when conflicting rules both have the !important flag, specificity dictates that an inline rule is applied - meaning that for OP's scenario, there's no way to override an inline !important.
You cannot override inline CSS if it has !important. It has higher precedence than the style in your external CSS file.
However, if you want it to change some actions later on, you can use a bit of JavaScript.
You can not override inline CSS having !important, because it has higher precedence, but, using JavaScript, you can achieve what you want.
You cannot override inline style having !important. First preference is inline style.
For eg: we have a class
.styleT{float:left;padding-left:4px;width:90px;}
and in jsp
<div class="styleT" id="inputT" style="padding-left:0px;">
here doesn't take the padding-left:4px; .It takes class styleT except the padding-left:4px;.
There will be padding-left:0px;.
Here is a simple jQuery solution.
$(document).ready(function() {
$('div').css('display','block');
})
You can see this example! There are several rules for CSS selector. The strongest selector is inline (if same level with/without !important). Next ones: id, class, etc. So if a tag is already stylized by inline css with !important, you just have a way: use Javascript to change.
var pid = document.getElementById('pid');
var button = document.getElementById('button');
button.addEventListener('click', function(){
pid.style.color = '';
});
p{color:violet !important;}
*{color:blue !important;}
html *{color:brown !important;}
html p{color:lighblue !important;}
.pclass{color:yellow !important;}
#pid{color:green !important;}
<p class="pclass" id="pid" style="color:red !important;">
Hello, stylize for me !
</p>
<button id='button'>Change color by JS</button>
As you see, the style by inline css was removed and the id is the strongest selector now !
Precedence rules when two CSS properties apply to the same node:
!important beats not-!important. If equally !important, ...
style attribute beats css in a file. If both are in css files...
an ID in the CSS selector beats no ID. And more IDs beat less. (and you thought there was no reason for two IDs in a selector.) If same ID count...
Classes, or attributes like [name] in the selector, count them; more beats less. If all those are the same...
tag names like span or input, more beats less.
So you see the inline !important is the highest precedence.

Is there a way to "sandbox" an HTML block away from its page's CSS without using iframes?

Is it possible, for example, to have a div that completely ignores CSS rules, no matter what classes and ids it contains?
Nope, this is (sadly) not possible without an iframe.
You would have to reset every existing CSS rule for that div like so:
div.sandbox
{
font-size: ....
font-family: ..........
margin: .........
padding: .........
line-height: .........
}
while difficult and never 100% reliable, it might be possible to achieve a usable result this way. You could look at one of the "reset stylesheets" like Eric Meyer's to get a list of important properties to reset; here is what claims to be a complete list of CSS 2.1 properties - excluding CSS 3 and vendor specific ones, which you would have to take into consideration as well.
Providers of 3rd party widgets often hard-code their "reset CSS" as inline CSS inside the HTML element to override any !important rules that threaten to override the sandbox class's rules.
May give results:
div {
all:unset !important;
clear:both !important;
margin: 0 !important
}
Inline style will overide any css styles.
This also mean that any styles set by javascript will overide any css rules, just because javascript is setting styles on a inline manner.
As well any styles between style tag directly on the html part will overide the styles set on the css file.
On any case inlines style are ruling any others hierarchicly.

Override child css-property

How do I override a child css-property.
Example, the text should be black:
<div style="color: Black;">
<div style="color: Red;">Red text that should be black.</div>
</div>
Since I got some answers that suggest that I should not use inline styles, I should tell you that this is not an option, at least not for the inner div.
don't use inline styles. control them from your CSS in the tags or CSS file. Then you can use inheritance, specificty and !important to override. You can't do it with inline styles as you have it in your code.
You should use css classes and ids and use internal or external stylesheets do not use inline style for anything as far as possible. As far as making the text black you cant do it because you have it inline and that has preference over all and will be applied last. It is usually follows this order so the styles are applied starting from left to right-
External Stylesheet -> Internal
Stylesheet -> Inline Styling
Look here to know more information on how to use stylesheets.