Webpage Design-[solve CSS compatibility issues between IE and Firefox!]

Source: Internet
Author: User

Internet Explorer is replaced by IE and mozzila Firefox is replaced by MF.

1. doctype affects CSS Processing

2. FF: When Div sets margin-left and margin-Right to auto, it is already centered, and IE does not work.

3. FF: when setting text-align for the body, set margin: auto (mainly margin-left, margin-Right) for the DIV to be centered.

4. FF: After padding is set, the DIV will increase the height and width, but IE will not, so you need to use it! Set one more height and width for important.

5. FF: supported! Important, ie is ignored, available! Important sets a special style for ff. It is worth noting that you must set XXXX! The important sentence is placed on the other sentence.

6. Vertical center problem of Div: vertical-align: middle; Increase the line spacing to the same height as the entire Div line-Height: 200px; then insert the text to the vertical center. Disadvantage:ControlDo not wrap content

7. cursor: pointer can display the cursor finger in IE ff at the same time. Hand can only be IE

8. FF: adds a border and a background color to the link. You must set display: block and float: left to avoid line breaks. Refer to menubar to set the height of a and menubar to avoid misplacement of the bottom side. If no height is set, you can insert a space in menubar.

9. The box model interpretation in Firefox and IE is inconsistent, resulting in a 2px difference. Solution: div {margin: 30px! Important; margin: 28px ;}

Note that the order of the two margin entries must not be reversed. According to Alibaba Cloud! The important attribute ie cannot be identified, but other browsers can. So in IE, it is actually explained as follows: div {maring: 30px; margin: 28px}

If the definition is repeated, execute the last statement. Therefore, you cannot write only margin: xxpx! Important;

10. The box interpretations of ie5 and IE6 are inconsistent.

Div {width: 300px; margin: 0 10px 0 10px;} under ie5 ;}

The DIV width is interpreted as 300px-10px (right fill)-10px (left fill). The final Div width is 280px, in IE6 and other browsers, the width is calculated based on 300px + 10px (right fill) + 10px (left fill) = 320px. In this case, we can modify Div {width: 300px as follows! Important; width/**/: 340px; margin: 0 10px 0 10px}

I don't quite understand what/**/is. I only know that both ie5 and Firefox support it, but IE6 does not. If anyone understands it, please let me know. Thank you! :)

11. UL labels have padding values by default in Mozilla, and only margin has a value in IE. Therefore, defining ul {margin: 0; padding: 0 ;} first can solve most of the problems.

FAQ 2:

The notorious bug of IE box-model exists in every previous version of IE6/win. This bug was not released until tantak released the most widely spread hack.

Http://www.tantek.com/css/examples/boxmodelhack.html

Ie5.x/win parses the box-model in the same way. They think the width includes the border and the padding. Fortunately, this situation has improved in IE6, however, IE6 is backward compatible with previous errors. In fact, IE6 has two cores, and he still shows tolerance for errors before the old pages, IE6 can accept the correct box-model only when the doctype statement is strictly added to the document.

Therefore, the hack of tantak must be included in the document with the correct doctype to work properly.

Div. Content {
Width: 400px; // This is the incorrect width, which is read by all browsers.
Voice-family: "\"} \ "; // ie5.x/win ignores the content after "\ "} \"
Voice-family: Inherit;
Width: 300px; // Some browsers including IE6/win read this sentence. The new value (300px) overwrites the old one.
}
HTML> body. Content {// html> body is written in css2.
Width: 300px; // the browser that supports css2 is lucky enough to read this sentence.
}

Now back to the topic, we often see it! Important and (Space)/**/: combine them.What are the mysteries of this writing?

Let's look at the writing method. Here I can provide another writing method to achieve this effect.

Div. Content {
Width: 300px! Important; // This is the correct width, most of which are supported! The browser marked with important uses the value here
Width (Space)/**/: 400px; // IE6/win does not parse this sentence, so IE6/win still thinks that the width value is 300px; when ie5.x/win reads this sentence, the new value (400px) overwrites the old one because! Important flag does not work for them
}
HTML> body. Content {// html> body is written in css2.
Width: 300px; // the browser that supports css2 is lucky enough to read this sentence.
}

Similarly, this method can work normally only after the correct document type declaration, as mentioned earlier.

The document type declaration is like a switch that enables the future of backward compatibility. If it is used incorrectly, It is a Pandora Box

Note:
1. The DIV of float must be closed.

Example: (floata and floatb attributes have been set to float: Left ;)

<# Div id = "floata">
<# Div id = "floatb">
<# Div id = "notfloatc">

The notfloatc here does not want to continue translation, but wants to move down.
This sectionCodeThere is no problem in IE, and the problem lies in ff. The reason is that notfloatc is not a float label and must be closed.
In

<# Div class = "floatb">
<# Div class = "notfloatc">

Add

<# Div class = "clear">

This Div must pay attention to the declaration position. It must be placed in the most appropriate place and must be at the same level as two DIV with the float attribute. No nested relationship exists between them; otherwise, an exception may occur.
And define the clear style as follows:

. Clear {
Clear: Both ;}

In addition, in order to automatically adapt to the height, overflow: hidden should be added to the wrapper;
When a box containing float is automatically adapted to the IE environment, the private attribute layout of IE should be triggered (the Internet Explorer !) Zoom: 1; can be used to achieve compatibility.
For example, a wrapper is defined as follows:

. Colwrapper {
Overflow: hidden;
Zoom: 1;
Margin: 5px auto ;}

2. The problem of doubling margin.

The DIV set to float doubles the margin set in IE. This is a bug in IE6.
The solution is to add the display: inline In the div;
For example:

<# Div id = "imfloat">

The corresponding CSS is

# Iamfloat {
Float: left;
Margin: 5px;/* 10 Px in IE */
Display: inline;/* in IE, It is understood as 5px */}

3. Container inclusion relationships

In many cases, especially when the container has a parallel layout, such as two or three float Divs, the width is prone to problems. In ie, the width of the outer layer is broken by the DIV with a wider inner layer. You must use Photoshop or firework to obtain pixel-level precision.

4. Questions about height

If the content is added dynamically, it is best not to define the height. The browser can automatically scale. However, it is best to set the height of static content. (It seems that sometimes it will not be automatically pushed down and I don't know what's going on)

5. the most cruel means -! Important;

If there is no way to solve some detailsProblem, you can use this method. FF "! Important will automatically give priority to resolution, but IE will ignore it.

. Tabd1 {
Background: URL (/RES/images/up/tab1.gif) No-repeat 0px 0px! Important;/* style for FF */
Background: URL (/RES/images/up/tab1.gif) No-repeat 1px 0px;/* style for IE */}

It is worth noting that XXXX must be added! Important is placed on the other sentence, which has been mentioned above.

Others:
Note: IE can recognize *. standard browsers (such as Firefox, opera, and Netscape) cannot recognize *. IE6 can recognize *, but cannot recognize! Important and IE7 can recognize * and also! Important; FF cannot recognize *, but can recognize it! Important;

Write two lines of code to control a property. The difference between Firefox and IE6 is:
Background: orange; * Background: blue;

// When you use Firefox or another non-IE browser when writing this code, you will find that the background of the region where the code is written is orange. If you use IE browser, it is blue, because IE can recognize *; standard browsers (such as Firefox, opera, and Netscape) cannot recognize *;

Write two lines of code to control an attribute, which distinguishes IE7 from IE6:
Background: Green! Important; Background: blue;

// When this code is written, you can use IE7 to browse and find that the background of the region where the code is written is green. If you use IE6 to browse, it is blue, this is because IE7 can recognize it! Important *. Once it is recognized, it will be executed. The following sentence is ignored, but IE6 cannot be recognized! Important, so the previous part is skipped and the second half is executed directly.

Write two lines of code to control a property. The difference between Firefox and IE is:
Background: orange; * Background: green;

// When this code is written, you can use Firefox to browse and find that the background is orange, While IE is green. It is very simple because Firefox cannot recognize *, while IE6, both IE7 can recognize *

Write three lines of code to control a property, which distinguishes Firefox, IE7, and IE6:
Background: orange; * Background: Green! Important; * Background: blue;

// This sentence will make the background in Firefox orange, green in IE7, and blue in IE6. The principle is the same as the previous one. Firefox cannot recognize *, so neither of the following two sentences will be executed, execute the first sentence directly. IE7 can also execute the first line of code, but because the second sentence can also be identified, the second sentence of code is executed to filter out the previous results, in the last sentence, IE7 cannot be recognized. IE6 cannot be recognized! Imprtant, the first sentence of code is run, and the second sentence cannot be identified, so the last sentence is executed.

Internet Explorer is replaced by IE and mozzila Firefox is replaced by MF.

1. Document. Form. Item Problems

(1) existing problems:

Many statements such as document. formname. Item ("itemname") exist in the existing code and cannot be run in MF.

(2) solution:

Use document. formname. elements ["elementname"]

(3) Others

See 2

2. Collection class Object Problems

(1) existing problems:

In the existing Code, many collection class objects are used (), which is acceptable to IE and cannot be used by MF.

(2) solution:

Use [] as the subscript operation. For example, change document. Forms ("formname") to document. Forms ["formname"].

For example, change document. getelementsbyname ("inputname") (1) to document. getelementsbyname ("inputname") [1]

(3) Others

3. Window. Event

(1) existing problems:

Window. event cannot be run on MF.

(2) solution:

Mf events can only be used in the event. This problem cannot be solved. This can be changed as follows:

Original code (can be run in IE ):

<Input type = "button" name = "somebutton" value = "Submit" onclick = "javascript: gotosubmit ()"/>
...
<Script language = "JavaScript">
Function gotosubmit (){
...
Alert (window. Event); // use window. Event
...
}
</SCRIPT>

New Code (run in IE and MF ):

<Input type = "button" name = "somebutton" value = "Submit" onclick = "javascript: gotosubmit (event)"/>
...
<Script language = "JavaScript">
Function gotosubmit (EVT ){
EVT = EVT? EVT: (window. event? Window. Event: NULL );
...
Alert (EVT); // use EVT
...
}
</SCRIPT>

In addition, if the first line of the new Code is not modified, it is the same as the old code (that is, the gotosubmit call does not provide a parameter), it can only be run in IE, but no error occurs. Therefore, the TPL part of this solution is still compatible with the old code.

4. Question about the ID of an HTML object as the object name

(1) Existing Problems

In ie, the ID of the HTML object can be directly used as the variable name of the subordinate object of the document. It cannot be in MF.

(2) Solution

Use getelementbyid ("idname") instead of idname as the object variable.

5. The problem of getting an object using the idname string

(1) Existing Problems

In IE, Eval (idname) can be used to obtain the HTML object with ID as idname, which cannot be used in MF.

(2) Solution

Use getelementbyid (idname) instead of eval (idname ).

6. The variable name is the same as the ID of an HTML object.

(1) Existing Problems

In MF, because the Object ID is not the name of the HTML object, you can use the same variable name as the HTML Object ID, which cannot be used in IE.

(2) Solution

When declaring variables, add VaR to avoid ambiguity, so that it can run normally in IE.

In addition, it is best not to take the same variable name as the HTML Object ID to reduce errors.

(3) Others

See question 4

7. event. X and event. Y

(1) Existing Problems

In ie, the event object has the X and Y attributes, and MF does not.

(2) Solution

In MF, event. X is equivalent to event. pagex. But event. pagex IE does not.

Therefore, event. clientx is used instead of event. X. This variable is also available in IE.

Event. clientx is slightly different from event. pagex (when the page has a scroll bar), but most of the time it is equivalent.

If it is the same, it may be a little troublesome:

MX = event. X? Event. X: event. pagex;

Use MX instead of event. x

(3) Others

Event. layerx is available in both IE and MF. There is no difference in the specific significance of this function.

8. About Frame

(1) Existing Problems

In ie, the frame can be obtained using window. testframe, but not in MF.

(2) Solution

The main difference between MF and IE in frame usage is:

If the following attributes are written in the frame tag:

<Frame src = "xx.htm" id = "frameid" name = "framename"/>

Then Ie can access the window object corresponding to this frame through ID or name.

However, MF can only access the window object corresponding to this frame through name.

For example, if the above frame label is written in the HTM in the top window, you can access

IE: window. Top. frameid or window. Top. framename to access this window object

Mf: only window. Top. framename can access this window object.

In addition, both mfand iecan use javasdesktop.doc ument. getelementbyid ("frameid") to access the frame tag.

In addition, you can switch the frame content through the parameter top.doc ument. getelementbyid ("testframe"). src = 'xx.htm '.

You can also switch the frame content through window. Top. framename. Location = 'xx.htm '.

For details about frame and window, see 'window and framework' in BBS' Article

And the tests under the/test/JS/test_frame/directory

9. In MF, attributes defined by myself must be obtained by getattribute ().

10. If there is no parentelement parement. Children in MF, use

Parentnode. childnodes

Childnodes has different meanings in IE and MF. MF uses Dom specifications, and blank text nodes are inserted in childnodes.

You can avoid this problem by using node. getelementsbytagname.

When a node in HTML is missing, ie and MF have different interpretations of parentnode, for example

<Form>
<Table>
<Input/>
</Table>
</Form>

In MF, the value of input. parentnode is form, while that of input. parentnode in IE is empty.

The node in MF does not have the removenode method. You must use the following method: node. parentnode. removechild (node)

11. Const Problems

(1) existing problems:

You cannot use the const keyword in IE. For example, const constvar = 32; in IE, This Is A syntax error.

(2) solution:

Use VaR instead of Const.

12. Body object

The MF body exists before the body tag is fully read by the browser, While IE exists only after the body is fully read.

13. url Encoding

In JS, if the URL is written, write directly & do not write & for example, VAR url = 'xx. jsp? Objectname = XX & objectevent = XXX ';

FRM. Action = URL, so it is very likely that the URL will not be properly displayed, so that the parameter is not correctly transmitted to the server.

Generally, the server reports that the error parameter is not found.

Of course, if it is an exception in TPL, because TPL complies with the XML specification, the requirements are as follows &

Generally, MF cannot recognize &

14. nodename and tagname Problems

(1) existing problems:

In MF, all nodes have a nodename value, but textnode does not have a tagname value. In IE, nodename usage seems to be

There is a problem (the test is not performed, but my IE has been killed several times ).

(2) solution:

Use tagname, but check whether it is empty.

15. Element attributes

The input. Type attribute in IE is read-only, but can be modified in MF.

16. Document. getelementsbyname () and document. All [name] Problems

(1) existing problems:

In IE, neither getelementsbyname () nor document. All [name] can be used to obtain the DIV element (whether there are other elements that cannot be retrieved is unknown ).

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.