Home > Article > Web Front-end > div css compatible with ie6 ie7 ie8 ie9 and FireFox Chrome method_html/css_WEB-ITnose
1. Vertical centering problem of div
vertical-align:middle; Increase the line spacing to the same height as the entire DIV line-height:200px; Then insert text and it will be vertically centered. The disadvantage is that the content must be controlled not to wrap.
2. The problem of doubling the margin
The margin set for a div set to float under IE will be doubled. This is a bug that exists in ie6. The solution is to add display:inline; to this div. For example:
<#div id="imfloat">
#imfloat{
float:left;
margin:5px;/*under IE, it is understood as 10px*/
display:inline;/*under IE, it is understood as 5px*/}
3. Double distance generated by floating IE
#box{ float:left; width:100px; margin:0 0 0 100px; //In this case, IE will generate a distance of 200px display:inline; //Ignore floats}
Let’s talk about the two elements block and inline in detail: The characteristic of the block element is that it always starts on a new line, and the height, width, line height, and margins can all be controlled ( Block element); The characteristic of Inline elements is that they are on the same line as other elements and cannot be controlled (inline elements);
#box{ display:block; //Can simulate inline elements as block elements display:inline; //Achieve the effect of arranging in the same row diplay:table;
4 Problems with IE and width and height
IE does not recognize the definition of min-, but in fact it puts it normally The width and height are treated as if there is min. This is a big problem. If you only use width and height, these two values will not change in a normal browser. If you only use min-width and min-height, the width and height are not set at all under IE.
For example, if you want to set a background image, this width is more important. To solve this problem, you can do this:
#box{ width: 80px; height: 35px;}html>body #box{ width: auto; height: auto; min-width: 80px; min-height: 35px;}
5. The minimum width of the page
min-width is a very convenient CSS command. It can specify that the element must be at least or not less than a certain width, thus ensuring that the layout is always correct. . But IE doesn't recognize this, and it actually treats width as the minimum width. In order to make this command also available on IE, you can put a
#container{ min-width : 600px; width:expression(document.body.clientWidth
The first min-width is normal; but the width in line 2 uses Javascript , which is only recognized by IE, which will also make your HTML document less formal. It actually implements the minimum width through Javascript judgment.
6. DIV floating IE text produces a 3-pixel bug
The object on the left is floating, and the right is positioned using the left margin of the outer patch. The text within the object on the right will be 3px away from the left. .
#box{ float:left; width:800px;}
#left{ float:left; width:50%;}
#right{ width:50%;}
*html #left{ margin-right:-3px; //This sentence is the key}
<div id="box">
<div id="left"></div>
<div id="right"></div>
</div>
7. IE hide-and-seek problem
When the div application is complex, there are Some links, DIVs, etc. are prone to hide-and-seek problems at this time.
Some content cannot be displayed. When the mouse selects this area, it is found that the content is indeed on the page.
Solution: Use line-height attribute for #layout or use fixed height and width for #layout. Keep the page structure as simple as possible.
8. Float div closure; clear float; adaptive height;
①For example:
<#div id="floatA" ><#div id= "floatB" ><#div id="NOTfloatC" >
The NOTfloatC here does not want to continue to translate, but wants to move down. (The attributes of floatA and floatB have been set to float:left;) This code has no problem in IE, but the problem lies in FF. The reason is that NOTfloatC is not a float label, and the float label must be closed. Add
② Do not set the height of the div as an external wrapper. In order to allow the height to automatically adapt, add it to the wrapper. Go to overflow:hidden; When a box containing float is included, the height automatic adaptation is invalid under IE. At this time, the layout private attribute of IE should be triggered (the evil IE!) You can use zoom:1; to achieve this. compatible. For example, a wrapper is defined as follows:
.colwrapper{ overflow:hidden; zoom:1; margin:5px auto;}
③For typesetting, the CSS description we use most is probably float :left. Sometimes we need to create a unified background behind the float div in column n, for example:
<div id="page">
<div id="left"> </div>
<div id=”center”></div>
<div id=”right”></div>
</div> We want to set the background of the page to blue so that the background color of all three columns is blue. However, we will find that as the left center right stretches downward, the page actually saves the height unchanged. The problem arises. , the reason is that page is not a float attribute, and our page cannot be set to float because it needs to be centered, so we should solve it like this
<div id="page">
<div id="bg ” style=”float:left;width:100%”><div id=”left”></div>
<div id=”center”></div>
<div id="right"></div>
</div>
</div>
Then embed a float left DIV with a width of 100% to solve the problem
④Universal float closure (very important!)
For the principle of clear float, please refer to [How To Clear Floats Without Structural Markup]. Add the following code to Global CSS and add the following to the div that needs to be closed. class="clearfix" is enough, it works repeatedly.
/* Clear Fix */
.clearfix { display:inline-block; }
/* Hide from IE Mac */
.clearfix {display:block;}
/* End hide from IE Mac */
/* end of clearfix */
Or set it like this:
.hackbox{ display:table; //Display the object as a block element-level table}
9. Height incompatibility
Height incompatibility means that when the height of the inner object changes, the height of the outer layer cannot be automatically adjusted, especially when the inner object uses margin or paddign.
Example:
#box {background-color:#eee; }
<div id="box">
<p>Content in p object</p>
</div>
Solution: Add 2 spaces above and below the P object The CSS code of the div object: .1{height:0px;overflow:hidden;} or add the border attribute to the DIV.
vertical-align:middle; Increase the line spacing to the same height as the entire DIV line-height:200px; Then insert text and it will be vertically centered. The disadvantage is that the content must be controlled not to wrap.
The corresponding css is
margin:5px;/*under IE, it is understood as 10px*/
display:inline;/*under IE, it is understood as 5px*/}
3. Double distance generated by floating IE
#box{ float:left; width:100px; margin:0 0 0 100px; //In this case, IE will generate a distance of 200px display:inline; //Ignore floats}
Let’s talk about the two elements block and inline in detail: The characteristic of the block element is that it always starts on a new line, and the height, width, line height, and margins can all be controlled ( Block element); The characteristic of Inline element is that it is on the same line as other elements and cannot be controlled (inline element);
#box{ display:block; //Can simulate inline elements as block elements display:inline; //Achieve the effect of arranging in the same row diplay:table;
4 IE and width and height Problem
IE does not recognize the definition of min-, but in fact it treats normal width and height as if there is min. This is a big problem. If you only use width and height, these two values will not change in a normal browser. If you only use min-width and min-height, the width and height are not set at all under IE.
For example, if you want to set a background image, this width is more important. To solve this problem, you can do this:
#box{ width: 80px; height: 35px;}html>body #box{ width: auto; height: auto; min-width: 80px; min-height: 35px;}
5. The minimum width of the page
min-width is a very convenient CSS command. It can specify that the element must be at least or not less than a certain width, thus ensuring that the layout is always correct. . But IE doesn't recognize this, and it actually treats width as the minimum width. In order to make this command also available on IE, you can put a
#container{ min-width : 600px; width:expression(document.body.clientWidth
The first min-width is normal; but the width in line 2 uses Javascript , which is only recognized by IE, which will also make your HTML document less formal. It actually implements the minimum width through Javascript judgment.
6. DIV floating IE text produces a 3-pixel bug
The object on the left is floating, and the right is positioned using the left margin of the outer patch. The text within the object on the right will be 3px away from the left. .
#box{ float:left; width:800px;}
#left{ float:left; width:50%;}
#right{ width:50%;}
*html #left{ margin-right:-3px; //This sentence is the key}
<div id="box">
<div id="left"></div>
<div id="right"></div>
</div>
7. IE hide-and-seek problem
When the div application is complex, there are Some links, DIVs, etc. are prone to hide-and-seek problems at this time.
Some content cannot be displayed. When the mouse selects this area, it is found that the content is indeed on the page.
Solution: Use line-height attribute for #layout or use fixed height and width for #layout. Keep the page structure as simple as possible.
8. Float div closure; clear float; adaptive height;
①For example:
<#div id="floatA" ><#div id= ”floatB” ><#div id=”NOTfloatC” >
The NOTfloatC here does not want to continue to translate, but wants to go down. (The attributes of floatA and floatB have been set to float:left;) This code has no problem in IE, but the problem lies in FF. The reason is that NOTfloatC is not a float label, and the float label must be closed. Add
② Do not set the height of the div as an external wrapper. In order to allow the height to automatically adapt, add it to the wrapper. Go to overflow:hidden; When a box containing float is included, the height automatic adaptation is invalid under IE. At this time, the layout private attribute of IE should be triggered (the evil IE!) You can use zoom:1; to achieve this. compatible. For example, a wrapper is defined as follows:
.colwrapper{ overflow:hidden; zoom:1; margin:5px auto;}
③For typesetting, the CSS description we use most is probably float :left. Sometimes we need to create a unified background behind the float div in column n, for example:
<div id="page">
<div id="left"> </div>
<div id=”center”></div>
<div id=”right”></div>
</div> We want to set the background of the IC Trading Network page to blue so that the background color of all three columns is blue. However, we will find that as the left center right stretches downward, the page actually retains the same height. Here comes the problem. The reason is that page is not a float attribute, and our page cannot be set to float because it needs to be centered, so we should solve it like this
<div id=”page”>
<div id=”bg” style=”float:left;width:100%”>
<div id=”left”></ div>
<div id=”center”></div>
<div id=”right”></div>
</div> >
Then embed a float left DIV with a width of 100% to solve the problem
④Universal float closure (very important!)
For the principle of clear float, please refer to [How To Clear Floats Without Structural Markup], add the following code to Global CSS, and add class="clearfix" to the div that needs to be closed. It works every time.
/* Clear Fix */
.clearfix { display:inline-block; }
/* Hide from IE Mac */
.clearfix {display:block;}
/* End hide from IE Mac */
/* end of clearfix */
Or set it like this:
.hackbox{ display:table; //Display the object as a block element-level table}
9. Height incompatibility
Height incompatibility means that when the height of the inner object changes, the height of the outer layer cannot be automatically adjusted, especially when the inner object uses margin or paddign.
Example:
#box {background-color:#eee; }
<div id="box">
<p>Content in p object</p>
</div>
Solution: Add 2 spaces above and below the P object The CSS code of the div object: .1{height:0px;overflow:hidden;} or add the border attribute to the DIV.