Analysis and Solution of restrictions on CSS style sheets by IE

Source: Internet
Author: User

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

Mark with Link

<LINK rel = "stylesheet" type = "text/CSS" href = "sheet.css"/>
Use style element

<Style type = "text/CSS">
Body {Background: # FFF ;}
H1 {font-size: 2em ;}
</Style>
Use the @ import command

<Style type = "text/CSS">
@ Import url(sheet1.css );
@ Import "sheet2.css ";
</Style>
Inline Style)

<P style = "color: # f00;"> This Is a red word </P>
In practical applications, inline styles using the style attribute are not recommended. xhtml1.1 has set this standard as not recommended, because it is very simple that this method is no better than font marking, this weakens CSS's ability to centrally control the appearance of the entire document. The first three methods use link tag and STYLE tag, which have the following restrictions in IE (including IE6, IE7, and IE8 beta1:

Only the first 31 links or CSS associated with style labels can be applied in this document.

Starting from 32nd, all associated CSS tags will be invalid. Internet Explorer official documentation all style tags after the first 30 style tags on an HTML page are not applied in Internet Explorer also mentions this restriction, including in use.. XML files also have this restriction. However, it seems that the number of errors is incorrect. See in IE:

Example 1: 34 style labels are applied simultaneously
Example 2: Two style tags and 34 link tags are applied simultaneously.
A style tag is only valid for the first 31 @ import commands.

Ignore from 32nd @ import commands. See:

Example 3: 34 @ import commands are used in a style tag.

A css file is only valid for the first 31 @ import commands.

Ignore from 31st @ import commands. See:

Example 4: Use link flag to introduce a CSS file using 34 @ import commands
Example 5: Use style labels to introduce a CSS file with 34 @ import commands
Example 6: Use the link and style labels to introduce a CSS file that uses more than 31 @ import commands.
A css file cannot exceed 288kb?

This message comes from Internet Explorer CSS file size limit.

@ Import: the number of layers under the command cannot exceed 4

When the CSS file is introduced using the @ import command in IE, the layer 5th fails. This restriction comes from Cascade limit via @ import rule. In fact, the browser does not fully support multi-layer nesting, so even if you have to use the @ import command to introduce CSS files, there should be no more than two layers.

In most cases, the restrictions on CSS are not met by IE. Even in the case of the best solution, it should be manually or through the backendProgramCombine CSS files and response tags. minimizing the number of HTTP requests is the first principle to optimize page rendering.

In IE, you can use the document. stylesheets object (supported by Firefox, opera9, and safari3.1) to modify the values of the inline and embedded styles. This object is only available when the document contains style or link elements. In fact, the document. stylesheets. Length indicates that the maximum value of this object in IE is 31. The following describes how to use JavaScript to combine link and style tags to solve the restrictions in 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 <32 |! Asheet1_02.16.css text ){
// Document.stylesheets.css text only supports IE
Return;
}
VaR acsstext = [], aclonelink = [];
// Save the style in the style label and delete the label, but keep the first
// Because the return value of the getelementsbytagname method is nodelist, the reverse order is used in the cycle when the object is deleted.
For (VAR I = astyle. Length-1; I>-1;-I ){
VaR o = astyle [I];
Acsstext. Push (O. innerhtml );
If (I> 0 ){
O. parentnode. removechild (O );
}
}
// Only link tags in IE can get stylesheet.css text through stylesheet.css text.
// Unable to obtain and copy to an array aclonelink
For (VAR I = alink. Length-1; I>-1;-I ){
VaR o = alink [I];
If (O. getattribute & O. getattribute ('rel ') = 'stylesheet '){
If (O. stylesheet ){
Acsstext.push(o.stylesheet.css text );
} Else {
Aclonelink. Push (O. clonenode (true ));
}
If (I> 0 ){
O. parentnode. removechild (O );
}
}
}
VaR ohead = Document. getelementsbytagname ('head') [0];
// With the previous deletion, only two links or styles can be marked at most.
// Activate the stylesheet attribute by adding a 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.css text );
Ohead. removechild (O );
}
// Copy all styles to the first tag
Asheet1_02.16.css text + = acsstext. Join (");
}
The above is just a simple and rough solution. For demonstration, see examples 1 and 2. The improvements are as follows:

Media is not taken into account. If multiple media files are included, the effects of link-marked rel = "alternate stylesheet" are not considered. However, we recommend that you use the @ media command to write the corresponding style in the same file to reduce the number of HTTP connections.
The @ import command has not been fixed for 31 times. In fact, you can extract its href value and activate it. However, we recommend that you replace the @ import command with the link flag in actual applications. The @ import command in IE should be equivalent to the link mark at the bottom of the document, this will cause no style issues instantly when loading ie5/6 pages. The learning name is "flash of unstyled content" (fouc for short, of course, you can avoid this bug by placing a link or script element in the document header.
Generally, many link or style labels on the page may be the same. You can remove the same items before acsstext merge to reduceCodeQuantity.
If you do not use an existing style element in the Dom to add the style code directly through the csstext attribute, you can create a new style element to add it, be sure to add the newly created style element to the DOM first, and then add the style code through the csstext attribute. On the other hand, the style code it adds seems to be parsed by the style parser of IE6 before it is added! Both imporant and hack will be invalid. See example 7. It is not recommended to add a new style by adding a new style element, so that it is easy to meet the restrictions of IE.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.