Home Web Front-end CSS Tutorial CSS hack is a general method to achieve CSS perfect compatibility with IE6/IE7/FF_Experience exchange

CSS hack is a general method to achieve CSS perfect compatibility with IE6/IE7/FF_Experience exchange

May 16, 2016 pm 12:06 PM
css hack compatible Perfect

It is a commonplace issue that CSS is compatible with various browsers. There are tutorials all over the Internet. The following content is not too novel and is purely a personal summary. I hope it will be of some help to beginners.

1. CSS HACK
The following two methods can solve almost all HACKs today.

1, !important

With IE7’s support for !important, the !important method is now only for IE6 HACK. (Pay attention to the writing. Remember that the declaration position needs to be in advance.)



2, IE6/IE77 for FireFox

* html and *html are IE-specific tags , firefox does not support it yet. And *html is a unique tag of IE7.



Note:
* html HACK for IE7 must ensure the following statement at the top of the HTML:
" http://www.w3.org/TR/html4/loose.dtd">

2. Universal float closure

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.


3. Other compatibility tips

1. After setting padding for div under FF, it will Causes width and height to increase, but IE will not. (can be solved with !important)
2, centering problem.
1). Vertically centered. Set line-height to the same height as the current div, and then use vertical- align: middle.(Be careful not to wrap the content.)
2). Horizontally centered. margin: 0 auto;(Of course it is not omnipotent)
3. If you need to add style to the content in the a tag, you need to set display: block; (common in navigation tags)
4. The difference in understanding of BOX between FF and IE leads to a 2px difference. There are also problems such as the margin of a div set to float doubling under IE.
5. The ul tag is under FF. By default there are list-style and padding.It is best to declare it in advance to avoid unnecessary trouble. (Common in navigation tags and content lists)
6. Do not set the height of the div as an external wrapper. It is best to add overflow: hidden. to achieve height adaptability. .
7, About the hand cursor. cursor: pointer. And hand is only applicable to IE.

1 CSS styles for firefox ie6 ie7
Now most of them use !important to hack, for The ie6 and firefox tests can display normally,
but ie7 can correctly interpret !important, which will cause the page not to be displayed as required! Find a hack
A good hack for IE7 is to use "* html". Now browse it with IE7 and there should be no problem.
Now write a CSS like this:

#1 { color: #333; } /* Moz */
* html #1 { color: #666; } /* IE6 */ * html #1 { color: #999; } /* IE7 */
Then the font color is displayed as #333 under firefox, #666 under IE6, and #999 under IE7.

2 Centering issues in css layout
The main style definitions are as follows:

body {TEXT-ALIGN: center;}
#center { MARGIN-RIGHT: auto; MARGIN -LEFT: auto; }
Explanation:
First define TEXT-ALIGN: center in the parent element; this means that the content in the parent element is centered; for IE, this setting is enough.
But it cannot be centered in mozilla. The solution is to add "MARGIN-RIGHT: auto;MARGIN-LEFT: auto; " when setting the child element.
It should be noted that if you want to use this method to center the entire page, it is not recommended. Set in a DIV, you can split multiple divs in sequence.
Just define MARGIN-RIGHT: auto;MARGIN-LEFT: auto; in each split div.

3 Different interpretations of the box model

#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-}

4 Double generated by floating ie Distance

#box{ float:left; width:100px; margin:0 0 0 100px; //In this case, IE will generate a distance of 200px display:inline; //Ignore the float}
Let’s talk about the two elements block and inline in detail. The characteristics of the Block element are: it always starts on a new line, and the height, width, line height, and margins can all be controlled (block elements); the characteristics of the Inline element are: and Other elements are on the same line,... cannot be controlled (inline elements);

#box{ display:block; //Can simulate inline elements as block elements display:inline; //Achieve the same The effect of row arrangement diplay:table;

IE does not recognize the definition of min-, but in fact it treats normal width and height as if there is min.This will cause 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, it is equivalent to not setting the width and height under IE. high.
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;}

6 Minimum width of the page

min-width is a very convenient CSS command. It can specify that the minimum width of the element cannot be smaller than a certain width, so that the layout can always be correct. But IE doesn't recognize this,
and it actually treats width as the minimum width. In order to make this command work on IE, you can put a

under the tag, and then specify a class for the div:
Then the CSS is designed like this:
#container{ min-width : 600px; width:expression(document.body.clientWidth The first min-width is normal; but the width in line 2 uses Javascript, which only Only IE can recognize it, which will also make your HTML document less formal. It actually implements the minimum width through Javascript judgment.

7 Clear floats

.hackbox{ display:table; //Display the object as a block element-level table} or .hackbox{ clear:both;}
Or add: after (Pseudo object), sets the content that occurs after the object, usually used in conjunction with content. IE does not support this pseudo object, and non-Ie browsers support it.
So it does not affect IE/WIN browsers. The most troublesome thing about this...#box:after{ content: "."; display: block; height: 0; clear: both; visibility: hidden;}

8 DIV floating IE The text generates a 3-pixel bug

The left object floats, and the right side is positioned using the left margin of the outer patch. The text within the right object will have a 3px spacing 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} HTML code


9 Attribute selector (this is not compatible, it is a bug in hiding css)

p[id]{}div[id]{}
This is for IE6.0 and Versions below IE6.0 are hidden, and the functions of FF and OPera
There are still differences between attribute selectors and sub-selectors. The scope of sub-selectors is narrowed in form, while the scope of attribute selectors is relatively large, such as p[ id], all p tags with ids are of the same style.

10 IE hide-and-seek problem

When the div application is complex, there are some links in each column, DIV At this time, it is easy for hide-and-seek problems to occur.
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.

11 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 hour.
Example:

Content in p object



CSS: #box {background-color: #eee; }
#box p {margin-top: 20px; margin-bottom: 20px; text-align:center; }
Solution: Add 2 empty div object CSS codes above and below the P object :.1{height:0px;overflow:hidden;} or add the border attribute to the DIV.
Statement of this Website
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

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 ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to use bootstrap button How to use bootstrap button Apr 07, 2025 pm 03:09 PM

How to use the Bootstrap button? Introduce Bootstrap CSS to create button elements and add Bootstrap button class to add button text

How to insert pictures on bootstrap How to insert pictures on bootstrap Apr 07, 2025 pm 03:30 PM

There are several ways to insert images in Bootstrap: insert images directly, using the HTML img tag. With the Bootstrap image component, you can provide responsive images and more styles. Set the image size, use the img-fluid class to make the image adaptable. Set the border, using the img-bordered class. Set the rounded corners and use the img-rounded class. Set the shadow, use the shadow class. Resize and position the image, using CSS style. Using the background image, use the background-image CSS property.

How to resize bootstrap How to resize bootstrap Apr 07, 2025 pm 03:18 PM

To adjust the size of elements in Bootstrap, you can use the dimension class, which includes: adjusting width: .col-, .w-, .mw-adjust height: .h-, .min-h-, .max-h-

How to set up the framework for bootstrap How to set up the framework for bootstrap Apr 07, 2025 pm 03:27 PM

To set up the Bootstrap framework, you need to follow these steps: 1. Reference the Bootstrap file via CDN; 2. Download and host the file on your own server; 3. Include the Bootstrap file in HTML; 4. Compile Sass/Less as needed; 5. Import a custom file (optional). Once setup is complete, you can use Bootstrap's grid systems, components, and styles to create responsive websites and applications.

How to set the bootstrap navigation bar How to set the bootstrap navigation bar Apr 07, 2025 pm 01:51 PM

Bootstrap provides a simple guide to setting up navigation bars: Introducing the Bootstrap library to create navigation bar containers Add brand identity Create navigation links Add other elements (optional) Adjust styles (optional)

How to upload files on bootstrap How to upload files on bootstrap Apr 07, 2025 pm 01:09 PM

The file upload function can be implemented through Bootstrap. The steps are as follows: introduce Bootstrap CSS and JavaScript files; create file input fields; create file upload buttons; handle file uploads (using FormData to collect data and then send to the server); custom style (optional).

How to verify bootstrap date How to verify bootstrap date Apr 07, 2025 pm 03:06 PM

To verify dates in Bootstrap, follow these steps: Introduce the required scripts and styles; initialize the date selector component; set the data-bv-date attribute to enable verification; configure verification rules (such as date formats, error messages, etc.); integrate the Bootstrap verification framework and automatically verify date input when form is submitted.

How to remove the default style in Bootstrap list? How to remove the default style in Bootstrap list? Apr 07, 2025 am 10:18 AM

The default style of the Bootstrap list can be removed with CSS override. Use more specific CSS rules and selectors, follow the "proximity principle" and "weight principle", overriding the Bootstrap default style. To avoid style conflicts, more targeted selectors can be used. If the override is unsuccessful, adjust the weight of the custom CSS. At the same time, pay attention to performance optimization, avoid overuse of !important, and write concise and efficient CSS code.

See all articles