3

When I inspect the html with IE Developer tools, I see that there is one inline style for a button:

enter image description here

I don't want any width property for this input element. How can I disable or overwrite this with empty width?

Ozkan
  • 3,880
  • 9
  • 47
  • 78

3 Answers3

5

If you want to override inline styles then you need to add styles in your stylesheet with !important

for e.g.

width: auto !important;

Reference - CSS Specificity

Nikhil Aggarwal
  • 28,197
  • 4
  • 43
  • 59
1

You can disable inline styles for any given element using JavaScript:

  1. Locate the button in the HTML document
  2. Remove the button's style attribute
var button = document.getElementById('myButton');
button.removeAttribute('style');
TylerH
  • 20,799
  • 66
  • 75
  • 101
Rounin
  • 27,134
  • 9
  • 83
  • 108
  • 1
    If I were going after the style attribute in a tag, I'd at least change it to data-style so I could access it later. – Benjamin Jan 11 '22 at 18:17
  • I agree, @Benjamin - I think swapping the `style` attribute for a custom `data-style` attribute (or similar) would be the very best solution. – Rounin Jan 11 '22 at 18:18
0

Update for 2022+

If anyone else stumbles upon this in the present, you can do much more than remove an inline HTML style tag with JavaScript. You can keep the tag around in case you want to return it later. There are two ways you can do it!

One way: "Disabled" HTML property

I've heard tell that using disabled on the HTML tag will stop your browser from processing it. But I had trouble getting this to work on Firefox. Maybe someone can enlighten me about that!

<style class='style-class' disabled>
  body { color: blue; }
</style>

So you'd just

$('.style-class').prop('disabled', true)

Another way: Changing the tag itself

Your browser will only parse styles within a style tag. So if you change the tag to anything else, you'll still be able to inspect it in the DOM just fine, but it won't treat it as a stylesheet.

(jQuery used for the explanation here)

$(style.selector).replaceWith (function () {
  var attributes = $(this).prop("attributes");
  var $newEl = $('<nostyle>')
  $.each(attributes, function() {
    $newEl.attr(this.name, this.value);
  });  
  return $newEl.html($(this).html())
});

Then when you're ready to return the style, use this:

$(notstyle.selector).replaceWith (function () {
  var attributes = $(this).prop("attributes");
  var $newEl = $('<style>')
  $.each(attributes, function() {
    $newEl.attr(this.name, this.value);
  });  
  return $newEl.html($(this).html())
});
Benjamin
  • 180
  • 1
  • 10
  • This is like launching a missile at a building just to squash a bug inside it. Just use `!important` on the style you want to override it in CSS, no need for JavaScript and certainly no need for multiple complex functions using a JavaScript library. – TylerH Jan 11 '22 at 14:19
  • @TylerH - I'm sure there are persuasive arguments for and against, but while I (generally) lean towards using CSS over JS wherever I can (see: _Principle of Least Power_), in this case I'd definitely favour a single line of JS like `document.querySelector('#myButton').removeAttribute('style');` over adding `!important` to my CSS. I tend to regard the latter as only to be used as a temporary _cheat mode_ during development. – Rounin Jan 11 '22 at 15:59
  • This question is about (and my search brought me here for) removing inline styles. So missiles might be useful :) – Benjamin Jan 11 '22 at 18:11
  • I'm also answering for full on HTML tags. Is that not also inline style? If not, I'm happy to remove my answer. – Benjamin Jan 11 '22 at 18:15
  • No need to remove, @Benjamin - your answer is a positive contribution to the knowledge base. – Rounin Jan 11 '22 at 18:22
  • 1
    @Rounin Sure, if you are the one in control over both content and presentation, there's no need for it in either language, so that point is moot. That aside, *this* answer is quite far from "a single line" (which itself, I might point out, is significantly longer than "a single word" that `!important` is). – TylerH Jan 11 '22 at 20:20
  • @Rounin sounds good, thank you! `$('.style-class').prop('disabled', true)` is a single line @TylerH – Benjamin Jan 12 '22 at 17:47
  • I'm being a bit of an evangelist though! I don't like to leave folks dissatisfied, even if we disagree. – Benjamin Jan 12 '22 at 17:49