Document directory
No P! Haha
CSS compatibility with browsers is sometimes a headache. Maybe when you understand the skills and principles, you will feel that it is not difficult to collect IE7 from the Internet, 6. Solve the problem of compatibility with fireofx and sort it out. for Web, try to write code in XHTML format, and doctype affects CSS processing. As the W3C standard, doctype Declaration must be added.
CSS skills
1. Vertical center of Div
Vertical-align: middle; Increase the line spacing to the line-Height: 200px as high as the entire div; then insert the text to center vertically.
The disadvantage is that you need to control the content and do not wrap the line. 2. The problem of margin doubling is that the DIV set to float will double the margin set under IE. This is a bug in IE6.
The solution is to add display: inline In the div; for example:
<# Div id = "imfloat"> the corresponding CSS is # iamfloat {float: Left; margin: 5px;/* IE is interpreted as 10px */display: inline; /* in IE, It is understood as 5px */}
3. Double distance produced by floating IE
# Box {float: Left; width: 100px; margin: 0 0 0 100px; // in this case, ie generates a PX distance.
Display: inline; // ignore float}
Here, let's take a closer look at the block and inline elements: the block elements always start on a new line, and can be controlled by the height, width, row height, and margin (block element ); the Inline element is not controllable (embedded element) on the same line as other elements );
# Box {display: block; // you can simulate embedded elements as block elements.
Display: inline; // display the result of the same row arrangement;
4 issues with IE and width and height
IE does not recognize the Min-definition, but in fact it treats normal width and height as Min conditions. In this case, the problem is big. If only the width and height are used, the values in the normal browser will not change. If only Min-width and Min-height are used, the width and height under IE are not set at all.
For example, to set a background image, the width is important.
To solve this problem, you can:
# Box {width: 80px; Height: 35px ;}
HTML> body # box {width: auto; Height: auto; Min-width: 80px; Min-Height: 35px ;}
5. Minimum page width
Min-width is a very convenient CSS command, which can specify that the element is at least or less than a certain width, so as to ensure correct layout. But IE does not recognize this, but it actually uses width as the minimum width.
To make this command usable on IE, you can place <div> under the <body> label and specify a class for div. Then, CSS is designed as follows:
# Container {Min-width: 600px; width: expression (document. Body. clientwidth <600? "600px": "Auto ");}
The first Min-width is normal, but the width of line 2nd uses JavaScript, which is recognized only by IE, which will make your HTML document not formal. It actually achieves the minimum width through javascript judgment.
6. The bug that DIV floating ie text produces 3 pixelsThe left object floats, and the left margin of the outer patch is used to locate the patch on the right. The text in the right object is 3 px 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 critical}
<Div id = "box"> <Div id = "Left"> </div> <Div id = "right"> </div>
7. Questions about Internet Explorer
When the DIV application is complicated, there are some links in each column. When the DIV application is complicated, it is prone to the "hide and seek" issue. Some content cannot be displayed. When you select this area, the content is displayed on the page. Solution: Use the line-height attribute for # layout or use fixed height and width for # layout. The page structure should be as simple as possible.
8. Float Div closure; clear floating; adaptive height
① Example: <# Div id = "floata"> <# Div id = "floatb"> <# Div id = "notfloatc">
The notfloatc here does not want to continue translation, but wants to move down. (The attributes of floata and floatb have been set to float: Left;) This code has no problem in IE, and the problem lies in ff.
The reason is that notfloatc is not a float label and must be closed.
Add <# Div class = "floatb"> <# Div class = "notfloatc"> <# Div class = "clear"> the position of the div, it must be at the same level as the two DIV with the float attribute. No nested relationship exists between them; otherwise, an exception occurs. And define the clear style as follows:
. Clear {clear: Both ;}
② Do not set the height of the external wrapper Div. To make the height automatically fit, add overflow: hidden in 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 ;}
③ For typographical layout, the most commonly used CSS description may be float: Left. Sometimes we need to make a unified background behind the float DIV in the N column, for example:
<Div id = "page"> <Div id = "Left"> </div> <Div id = "center"> </div> <Div id = "right"> </div>
For example, we want to set the background of the page to Blue to make the background color of all three columns blue, but we will find that as the left center right is stretched down, the storage height of the page remains unchanged. The problem is that the page is not a float attribute. Because the page is centered, it cannot be set to float, so we should solve this problem.
<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>
Embed a float left and the DIV with a width of 100%.
④ Universal float closure (very important !) For details about the principle of clear float, see [How to clear floats without structural markup]. Add the following code to global CSS and add the DIV to be closed.
/* Clear fix */. clearfix: After {content :". "; display: block; Height: 0; clear: Both; visibility: hidden ;}. clearfix {display: inline-block;}/* Hide from IE Mac */. clearfix {display: block;}/* end hide from IE Mac * // * end of Clearfix */
Or set it as follows:
. Hackbox {display: Table; // display the object as a table at the block element level}
11. highly unsuitable
Height mismatch means that the height of the outer layer cannot be automatically adjusted when the height of the inner layer object changes, especially when the inner layer object uses margin or paddign. Example:
# Box {background-color: # Eee;} # Box P {margin-top: 20px; margin-bottom: 20px; text-align: center ;} <Div id = "box"> <p> content in the p object </P> </div>
Solution: Add two empty Div objects to the upper and lower sides of the P object. CSS code:. 1 {Height: 0px; overflow: hidden;} or add the border attribute to the div.
12. Why is there a gap in the image under IE6?
There are also many ways to solve this bug. You can change the HTML layout, set IMG to display: block, or set vertical-align to vertical-align: top | bottom | Middle | both text and bottom can be solved.
13. Align text and text input boxes
Add vertical-align: middle; <style type = "text/CSS"> <! -Input {width: 200px; Height: 30px; Border: 1px solid red; Vertical-align: middle;}-> </style>
14. What is the difference between ID and class defined in web standards?
I. duplicate IDs are not allowed in web standards. For example, div id = "AA" cannot be repeated twice, while class defines classes, which can be infinitely repeated in theory, in this way, you can use the definition that requires multiple references.
2. the attribute priority problem ID has a higher priority than the class. See the example above.
III. convenience for JS and other client scripts. If you want to perform script operations on an object on the page, you can define an ID for it. Otherwise, you can only find it by traversing the page element and adding specific attributes, this is a relatively time-consuming resource, which is far less simple than an ID.
15. How to display the content in Li with ellipsis after the content length exceeds
This method is applicable to IE and opbrowser.
<Style type = "text/CSS"> <! -Li {width: 200px; white-space: nowrap; text-overflow: ellipsis;-o-text-overflow: ellipsis; overflow: hidden;}-> </style>
16. Why can't I set the color of the scroll bar in Web Standard ie?
The solution is to change the body to HTML.
<! Doctype HTML public "-// W3C // dtd xhtml 1.0 strict // en" "http://www.w3.org/tr/xhtml1/dtd/xhtml1-strict.dtd">
<Meta http-equiv = "Content-Type" content = "text/html; charset = gb2312"/>
<Style type = "text/CSS">
<! -
HTML {scrollbar-face-color: # f6f6f6; scrollbar-Highlight-color: # FFF; scrollbar-shadow-color: # eeeeee; scrollbar-3dlight-color: # eeeeee; scrollbar-arrow-color: #000; scrollbar-track-color: # FFF; scrollbar-darkshadow-color: # FFF ;}
->
</Style>
17. Why cannot I define a container with a height of around 1 px?
In IE6, this problem is caused by the default Row Height, and there are many solutions, such:
Overflow: hidden | ZOOM: 0.08 | Line-Height: 1px
18. How can I display layers on flash?
The solution is to set transparency for flash. <Param name = "wmode" value = "Transparent"/>
19. How to center a layer vertically in the browser
Here we use the absolute percentage position, and the method of negative value of the external patch, the negative value is its own width and height divided by two
<Style type = "text/CSS">
<! -
Div {position: absolute; top: 50%; lef: 50%; margin:-100px 0 0-100px; width: 200px; Height: 200px; Border: 1px solid red ;}
->
</Style>
Ff and IE
1. Div Center
If the DIV sets margin-left and margin-Right to auto, it is already centered. If IE is not, you need to set the body to center. First, define text-algin: center in the parent element;
This means that the content in the parent element is centered.
2. Border and background of the Link (a tag)
Add the border and background color to the link. 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.
3.Hover style after hyperlink accessThere is no problem to be clicked to access the hyperlink style does not have hover and active, many people should have encountered this problem, the solution is to change the CSS attribute arrangement order: L-V-H-A code:
<Style type = "text/CSS">
<! -
A: link {} A: visited {} A: hover {} A: Active {}
->
</Style>
4.CursorCursor: pointer can display the cursor finger shape in IE ff at the same time, hand only ie Can
5.Ul's padding and margin ulIn ff, labels have padding values by default, while in IE, only margin has default values. Therefore, defining ul {margin: 0; padding: 0 ;}can solve most of the problems.
6. Form tag
In IE, this label will automatically margin some margins, while in FF, margin is 0. Therefore, if you want to display consistency, it is best to specify margin and padding in CSS, for the above two problems, I usually use this style ul in CSS first, FORM {margin: 0; padding: 0 ;}to define it as dead, so it won't be a headache.
7. Inconsistency of Box Model Interpretation
The box model interpretation in ff and IE is inconsistent, resulting in a 2px difference
Solution: div {margin: 30px! Important; margin: 28px ;}
Note that the order of the two margin values must not be reversed. The attribute IE of important cannot be recognized, but can be recognized by other browsers.
So in IE, it is actually explained as follows:
If Div {maring: 30px; margin: 28px} is repeatedly defined, it is executed according to the last one. Therefore, you cannot write only
Margin: xx px! Important; # box {width: 600px; // For ie6.0-w \ idth: 500px; // for FF + ie6.0} # box {width: 600px! Important // for FF width: 600px; // for FF + ie6.0 width/**/: 500px; // For ie6.0 -}
8. Attribute Selector
(This is not compatible, it is a bug that hides CSS.) P [ID] {} Div [ID] {}
This is hidden for versions earlier than ie6.0 and ie6.0, and works with ff and opera. there is a difference between the property selector and the Child selector. The range of the Child selector is reduced in the form, and the range of the property selector is relatively large, such as in P [ID, all P tags have IDs in the same style.
9. The most cruel means -! Important;
If there is no way to solve some detailed problems, 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
10. Questions about default values of IE and FF
Maybe you have been complaining about why you want to write different CSS for IE and ff. Why is it so troublesome for IE? Then write CSS while cursing the hateful M $ IE.
In fact, for the standard support of CSS, ie is not as bad as we think. The key is that the default values of IE and FF are different. I have mastered this technique, you will find that writing CSS compatible with ff and IE is not that difficult. You may not need to write simple CSS "! Important.
We all know that when a browser displays a webpage, it will decide how to display it based on the CSS style sheet of the webpage. However, we may not describe all the elements in the style sheet, of course, there is no need to do this, so for those attributes that are not described, the browser will use the built-in default mode for display, such as text, if you do not specify the color in CSS, the browser uses black or system color to display the background of the Div or other elements. If it is not specified in CSS, the browser sets it to white or transparent, and so on. Therefore, the root cause of the difference between FF and IE display is that the default display is different, how to display the default style? I know there are no corresponding standards in W3 to define, so don't blame ie for this.
11. why can't the FF text support the height of the container?
Containers with fixed height values in the standard browser won't be opened as in IE6, so I want to fix the height and how can I set it to be supported? The method is to remove the Min-Height: 200px setting for height. Here, we can define this to take care of IE6 that does not know Min-height:
{Height: Auto! Important; Height: 200px; Min-Height: 200px ;}
12. How to enable automatic line feed for continuous long fields in Firefox
We all know that you can use Word-wrap: Break-word directly in IE. In ff, we use JS insert & #10; to solve this problem.
<Style type = "text/CSS">
<! -
Div {width: 300px; Word-wrap: Break-word; Border: 1px solid red ;}
-> </Style>
<Div id = "FF"> commandid </div> <SCR effecpt type = "text/javascr effecpt">
/* <! [CDATA [*/
Function tobreakword (El, intlen ){
VaR export bj = Document. getelementbyid (EL );
VaR strcontent = obj. innerhtml;
VaR strtemp = "";
While (strcontent. length> intlen ){
Strtemp + = strcontent. substr (0, intlen) + "& #10;"; strcontent = strcontent. substr (intlen, strcontent. Length );
}
Strtemp + = "& #10;" + strcontent; obj. innerhtml = strtemp;
}
If (document. getelementbyid &&! Document. All) tobreakword ("FF", 37 );
/*]> */
</Scr platinum Pt>
13. Why is the container width and FF interpretation different under IE6?
<? XML version = "1.0" encoding = "gb2312"?>
<! Doctype HTML public "-// W3C // dtd xhtml 1.0 strict // en" "http://www.w3.org/tr/xhtml1/dtd/xhtml1-strict.dtd">
<Meta http-equiv = "Content-Type" content = "text/html; charset = gb2312"/>
<Style type = "text/CSS">
<! -Div {cursor: pointer; width: 200px; Height: 200px; Border: 10px solid red}->
</Style>
<Div export nclick = "alert (this. offsetwidth)"> make Firefox compatible with IE </div>
The difference is whether the overall width of the container includes the Border width. Here, IE6 is interpreted as 200px, while FF is interpreted as 220px, so how did it cause the problem?
If you remove the XML at the top of the container, you will find that the original problem exists. The declaration at the top triggers the qurks mode of IE. For more information about qurks mode and standards mode, see http: // www.microsoft.com/china/msdn/library/webservices/asp.net/ aspnetusstan. mspx? MFR = true
IE6, IE7, and FF ie7.0 are coming out, and there are new problems with CSS support. When there are more browsers and the webpage compatibility is worse, we are still exhausted. To solve the problem of ie7.0 compatibility, I found the following article: I am using most of them now! Important comes to hack. It can be displayed normally for IE6 and Firefox tests, but IE7 is correct! Important can be correctly explained, and the page will not be displayed as required! The following shows the compatibility of the three browsers.
The first is the CSS hack method.
Height: 20px;/* For Firefox */* Height: 25px;/* For IE7 & IE6 */_ Height: 20px;/* For IE6 */pay attention to the sequence.
This also belongs to CSS hack, but it is not as concise as above. # Example {color: #333;}/* Moz */* html # example {color: #666;}/* IE6 */* + html # example {color: #999;}/* IE7 */
<! -Other browsers->
<LINK rel = "stylesheet" type = "text/CSS" href?”css.css "/>
<! -[If IE 7]> <! -Suitable for IE7-> <LINK rel = "stylesheet" type = "text/CSS" href?”ie7.css "/> <! [Endif]->
<! -[If lte ie 6]> <! -Suitable for IE6 and later-> <LINK rel = "stylesheet" type = "text/CSS" href?”ie.css "/> <! [Endif]->
Third, the CSS Filter method, which is translated from a foreign website as a classic.
Create a CSS style as follows:
# Item {width: 200px; Height: 200px; Background: red ;}
Create a new Div and use the CSS style defined above:
<Div id = "item"> some text here </div>
Add the lang attribute in the body representation. The Chinese character is Zh: <body lang = "en"> now, define another style for the DIV element:
*: Lang (en) # item {Background: Green! Important ;}
This is intended for use! Important overwrites the original CSS style. Because the Lang selector ie7.0 does not support this sentence, it does not have any effect on this sentence, so it achieves the same effect in ie6.0, unfortunately, Safari does not support this attribute, so you need to add the following CSS style:
# Item: Empty {Background: Green! Important}: The empty selector is a css3 specification. Although safari does not support this specification, this element is still selected, whether or not this element exists, now green will be available in browsers other than IE versions. For compatibility with IE6 and FF, consider the previous! Important is preferred