use initial width for element not working in IE

70,013

Solution 1

Like you said, generally width: auto will have a similar effect. Having the rules:

.my-selector {
    width: auto;
    width: initial;
}

Should cause it to use initial if it's supported and auto otherwise.

Solution 2

Using width: auto; inline, inside the script solves the problem on Chrome, FIrefox and IE 11. Just not sure if there is a better way.

Share:
70,013

Related videos on Youtube

Rikard
Author by

Rikard

Updated on January 21, 2020

Comments

  • Rikard
    Rikard over 4 years

    I have a graph plugin that inserts canvas and a legend <table> to its container. In this plugin the table has no width defined and in my CSS there is a width for tables inside that container were my plugin gets inserted.

    So, the new div is inheriting table{ width: 100%} from the CSS and rendering wrong.

    I tried to use width: initial;, looks good on Chrome but IE doesn't like it check browser compatibility

    I admit changing/forcing a inline CSS in the script/plugin since it has to work in any enviroment.

    What is the best solution here?

    • BuddhistBeast
      BuddhistBeast over 10 years
      Have you tried just setting a minimum width? Does it work?
    • Rikard
      Rikard over 10 years
      @BuddhistBeast, didn't think about that! But tried that now and didn't work on Chrome either.
  • gtramontina
    gtramontina almost 8 years
    As a note, initial is a valid property value (developer.mozilla.org/en-US/docs/Web/CSS/initial). But (if you scroll down to "Browser compatibility" in the previous link), no IE version supports it.
  • chharvey
    chharvey over 7 years
    this is known as a fallback and is a very common coding pattern in CSS. also, using width: auto will not only have a similar effect, but it will have the exact same effect, because auto is defined to be the initial value for the width property.