102

I have an HTML page from page builder, and it injects style attribute directly to the element. I found it's considered as element.style.

I want to override it using CSS. I can match the element, but it doesn't override it.

screenshot

How can I override the style using CSS?

Andrew T.
  • 4,701
  • 8
  • 43
  • 62
D-W
  • 5,201
  • 14
  • 47
  • 74
  • 3
    Find the source of the style injection and remove it? If it isn't in your document source, it is probably coming from JavaScript somewhere. – cimmanon Feb 16 '13 at 13:13
  • 2
    How can you find the source? – Shailen Sep 05 '13 at 11:30
  • 2
    command + shift + f (on a mac) or ctrl + shift + f (on a pc) – maahd Apr 17 '15 at 09:25
  • Note an alternative in JS may be to remove the added property using [`removeProperty()`](https://developer.mozilla.org/docs/Web/API/CSSStyleDeclaration/removeProperty), i.e. e.g. `document.querySelector("li").style.removeProperty("display");` Then you also do not need to provide a new default value (like you have to do with `!important`, but the inheritance chain just "jumps in". – rugk Mar 20 '20 at 15:23

8 Answers8

142

Although it's often frowned upon, you can technically use:

display: inline !important;

It generally isn't good practice but in some cases might be necessary. What you should do is edit your code so that you aren't applying a style to the <li> elements in the first place.

DiscoInfiltrator
  • 1,989
  • 1
  • 18
  • 21
  • 1
    In jQuery when using the show() method, display: block; is applied to the element styles. I wanted it to be display: inline-block; though. I first solved it by not using this method and using .css('display', 'inline-block'); instead. However, after reading this example I think I will change to using !important. – Simon Bengtsson Dec 29 '13 at 01:01
  • jQuery sets the display property to whatever the default is for the element that it's showing. – folktrash Feb 04 '14 at 16:54
  • 2
    Literally just exclaimed "gtf outta here" when this worked for me, upset my girlfriend – jag Oct 20 '16 at 04:10
  • 3
    Most of these comments assume they have access to the source code. I wouldn't need to do these bad practices if coders didn't put out libraries with inline styles added. – TallOrderDev May 03 '19 at 04:14