CSS - Internet Explorer and the
tag backgro

2020-02-26 06:08发布

I have a pretty simple layout which renders fine in both Firefox and Chrome, but Internet Explorer (version 11) seems to be unable to render any kind of background color for the <main> element.

I have the <main> element as a child of the <body> element and neither background or background-color seem to make any difference. <main> will always have the same background as <body>. I haven't found anything that says whether or not this is a bug in IE.

Check out this jsfiddle using Internet Explorer to see what I mean.

Obviously, I could just replace <main> with <div id="main"> and update my CSS selectors but I want to understand why this is happening.

2条回答
2楼-- · 2020-02-26 06:37

IE11 does not support the <main> element natively. You can introduce support for it by either using a script like Modernizr, or a single harmless line of JS:

document.createElement('main');

The element will not be inserted in the DOM, but it will now be recognized as a proper element by IE. After this, it still does not have proper styling. Add the following to your CSS:

main {
    display:block;
}

And all will be fine. The reason you currently see it as not getting any content because IE does not add it to the box model without these 2 steps, and as such it gets no 'layout' or 'size'. It's just invisible, that's why you see the body. It does contain elements, which get rendered (sort of) correctly based on the top left coordinate of the <main> element.

查看更多
等我变得足够好
3楼-- · 2020-02-26 06:56

Simple: The <main> tag is not supported in IE11.

查看更多
登录 后发表回答