search
HomeWeb Front-endCSS TutorialAnalysis and solutions to IE's restrictions on CSS style sheets_Experience exchange

There are four common ways to associate HTML documents with CSS:

Use link tags


Use the style element


Inline style using the style attribute (inline style)

This is red text


In actual applications, the use of inline styles using the style attribute is not recommended. XHTML1.1 has standardized its use as not recommended. The reason is very simple. This method is not much better than the font tag and weakens the centralized control of CSS. Advantages of overall document appearance. The first three methods use link tags and style tags, and have the following restrictions in IE (including IE6, IE7 and IE8 beta1):

Only the CSS associated with the first 31 link or style tags in the document can be applied.

Starting from the 32nd one, the CSS associated with its tag will be invalid. IE's official documentation All style tags after the first 30 style tags on an HTML page are not applied in Internet Explorer also mentions this limitation, including the use of .xsl .xml files also have this limitation. But it seems that the wrong quantity was written. Please see it in IE:

Example 1: 34 style tags are applied at the same time
Example 2: 1 style tag and 34 link tags are applied at the same time
A style tag only has the first 31 @import instructions Effective application.

Ignore starting from the 32nd @import directive. Please see:

Example 3: Using the @import directive 34 times in a style tag.

Only the first 31 @import directives of a css file are effectively applied.

Ignore starting from the 31st @import directive. Please see:

Example 4: Use the link tag to introduce a css file that uses the @import directive 34 times
Example 5: Use the style tag to introduce a css file that uses the @import directive 34 times
Example 6: Use link and style tags to introduce a CSS file that uses the @import directive more than 31 times.
A CSS file cannot exceed 288kb?

This news comes from Internet Explorer CSS File Size Limit.

The cascading limit under the @import directive cannot exceed 4 layers.

When introducing css files through the @import directive under IE, the fifth layer will be invalid. This limit comes from Cascade limit via @import rule. In fact, due to the imperfect support of browsers for multi-level nesting, even if you have to use the @import directive to introduce CSS files, do not exceed 2 levels.

IE's restrictions on CSS will not be encountered in most cases. Even if you encounter the best solution, you should merge the CSS files and response tags manually or through a back-end program. , minimizing the number of http requests is the first principle of optimizing page rendering.

In IE, the values ​​of inline and embedded styles can be modified through the document.styleSheets object (supported by Firefox, Opera9 and Safari3.1). This object is only available when the document contains style or link elements. In fact, using document.styleSheets.length you can see that the maximum value under IE is 31.The following is using Javascript to merge link and style tags to solve the limitations under IE:

var fnMergeStyleSheet = function(){
if(!document.styleSheets){
return;
}
var aSheet = document.styleSheets,
aStyle = document.getElementsByTagName('style'),
aLink = document.getElementsByTagName('link');
if(aStyle.length aLink.length //document.styleSheets.cssText is only supported by IE
return;
}
var aCssText = [],aCloneLink = [];
//Save the style in the style tag, and then delete the tag, but keep the first one
//Because the value returned by the getElementsByTagName method is nodeList, the reverse order is used when deleting
for(var i =aStyle.length-1;i>-1;–i){
var o = aStyle[i];
aCssText.push(o.innerHTML);
if(i>0){                o.parentNode.removeChild(o); Get copied to an array aCloneLink
for(var i=aLink.length-1;i>-1;–i){
var o = aLink[i];
if(o.getAttribute && o.getAttribute('rel')==='stylesheet'){
                                                                                                                                         aCloneLink .push(o.cloneNode(true));
                                                                              .removeChild(o);
                                                                                                                                                                                                                                                                                                ; There are only 2 style tags left at most
//Activate its styleSheet attribute by re-adding the link node to obtain the style
for(var i = aCloneLink.length-1;i>-1;–i) {
var o = aCloneLink[i];
oHead.appendChild(o);
aCssText.push(o.styleSheet.cssText);
oHead.removeChild(o);
}
//Copy all styles to the first tag
aSheet[0].cssText = aCssText.join(”);
}
The above is just a simple and rough solution , please see Example 1 and Example 2 for demonstration. The areas that can be improved are:

The media attribute is not considered. If there are multiple media, they should be merged separately. Of course, the rel="alternate stylesheet" of the link tag is not considered. "The impact brought about. But I recommend writing the corresponding styles in the same file through the @media directive, which can at least reduce the number of HTTP connections.
It does not solve the problem of the 31 times limit of the @import directive. In fact, you can extract other The href value is then activated. However, in actual applications, it is recommended to use the link tag to replace the @import directive. This is because the @import directive in IE is equivalent to writing the link tag at the bottom of the document, which will cause the IE5/6 page to load instantly. Unstyled problem, the scientific name is "Flash of Unstyled Content" (abbreviated as FOUC) bug. Of course, this bug can be avoided by placing a link or script element in the document header.
Generally speaking, there are a lot of "Flash of Unstyled Content" (FOUC) bugs in the page. There are probably many link or style tags that are the same. You can remove the same items before merging aCssText to reduce the amount of code.
If you do not use the existing style elements in the DOM to directly add style code through the cssText attribute, but create To add a new style element, be sure to add the new style element to the DOM first, and then add the style code through the cssText attribute. On the contrary, the style code it adds seems to be parsed by IE6's style parser before being added, so both the !imporant and the hack will be invalid. Please see Example 7. It is not recommended to add new styles by adding new style elements, as this can easily reach the limitations of IE.

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Internet Explorer 打开 Edge:如何停止 MS Edge 重定向Internet Explorer 打开 Edge:如何停止 MS Edge 重定向Apr 14, 2023 pm 06:13 PM

长期以来,InternetExplorer的失宠一直不是秘密,但随着Windows11的到来,现实开始了。Edge将来不再有时取代IE,它现在是微软最新操作系统中的默认浏览器。目前,您仍然可以在Windows11中启用InternetExplorer。但是,IE11(最新版本)已经有了一个正式的退役日期,即2022年6月15日,时间在流逝。考虑到这一点,您可能已经注意到InternetExplorer有时会打开Edge,而您可能不喜欢它。那么为什么会这样呢?在

css与xml的区别是什么css与xml的区别是什么Apr 24, 2022 am 11:21 AM

区别是:css是层叠样式表单,是将样式信息与网页内容分离的一种标记语言,主要用来设计网页的样式,还可以对网页各元素进行格式化;xml是可扩展标记语言,是一种数据存储语言,用于使用简单的标记描述数据,将文档分成许多部件并对这些部件加以标识。

win11无法使用ie11浏览器怎么办?(win11用不了ie浏览器)win11无法使用ie11浏览器怎么办?(win11用不了ie浏览器)Feb 10, 2024 am 10:30 AM

越来越多的用户开始升级win11系统,由于每个用户的使用习惯不同,还是有不少用户在使用ie11浏览器,那么win11系统用不了ie浏览器,该怎么办呢?windows11还支持ie11吗?下面就来看看解决办法。win11无法使用ie11浏览器的解决方法1、首先右键开始菜单,选择“命令提示符(管理员)”打开。2、打开之后,直接输入“Netshwinsockreset”,回车确定。3、确定之后再输入“netshadvfirewallreset&rdqu

Win10打开IE自动跳转到Edge怎么取消_IE浏览器页面自动跳转的解决办法Win10打开IE自动跳转到Edge怎么取消_IE浏览器页面自动跳转的解决办法Mar 20, 2024 pm 09:21 PM

近期不少的win10用户们在使用电脑浏览器的时候发现自己的ie浏览器总是自动的跳转到edge浏览器,那么win10打开ie自动跳转edge怎么关闭?。下面就让本站来为用户们来仔细的介绍一下win10打开ie自动跳转edge关闭方法吧。1、我们登录edge浏览器,点击右上角...,找下拉的设置选项。2、我们进入设置后,在左侧栏点击默认浏览器。3、最后我们在兼容性中,勾选不允许IE模式下重新加载网站,重启ie浏览器即可。

rtl在css是什么意思rtl在css是什么意思Apr 24, 2022 am 11:07 AM

在css中,rtl是“right-to-left”的缩写,是从右往左的意思,指的是内联内容从右往左依次排布,是direction属性的一个属性值;该属性规定了文本的方向和书写方向,语法为“元素{direction:rtl}”。

ie快捷方式无法删除如何解决ie快捷方式无法删除如何解决Jan 29, 2024 pm 04:48 PM

ie快捷方式无法删除的解决办法:​1、权限问题;2、快捷方式损坏;3、软件冲突;4、注册表问题;5、恶意软件;6、系统问题;7、重新安装IE;8、使用第三方工具;9、检查快捷方式的目标路径;10、考虑其他因素;11、咨询专业人士。详细介绍:​1、权限问题,右键点击快捷方式,选择“属性”,在“安全”选项卡中,确保有足够的权限删除该快捷方式,如果没有,可以尝试以管理员身份运行等等。

css怎么设置i不是斜体css怎么设置i不是斜体Apr 20, 2022 am 10:36 AM

在css中,可以利用“font-style”属性设置i元素不是斜体样式,该属性用于指定文本的字体样式,当属性值设置为“normal”时,会显示元素的标准字体样式,语法为“i元素{font-style:normal}”。

一个时代的结束:Internet Explorer 11 已退役,这是你需要知道的一个时代的结束:Internet Explorer 11 已退役,这是你需要知道的Apr 20, 2023 pm 06:52 PM

2022年6月15日是Microsoft结束对InternetExplorer11(IE11)的支持并关闭其旧版浏览器章节的日子。一段时间以来,该公司一直在提醒用户注意这一生命周期结束日期,并呼吁他们计划迁移到MicrosoftEdge。Microsoft将IE11与Windows8.1捆绑在一起,作为Windows的现代默认Web浏览器。尽管它从未达到Chrome的(当前)高度,但它是2014年使用量第二大的桌面浏览器,仅次于IE8。当然,随着20

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft