Home >Web Front-end >HTML Tutorial >div css browser compatibility and understanding of css hack_html/css_WEB-ITnose

div css browser compatibility and understanding of css hack_html/css_WEB-ITnose

WBOY
WBOYOriginal
2016-06-24 12:30:311002browse

After writing div css code for so long, I really don’t know what this hack is? I checked online carefully. Simply put, the process of writing different CSS codes for different browsers is called CSS hack, also called writing CSS hack.

Principle of CSS hack: Because different browsers have different support and parsing results for CSS, and also due to the priority relationship in CSS. We can use this to write different CSS for different browsers.

For example, IE6 can recognize underline "_" and asterisk "*", IE7 can recognize asterisk "*", but cannot recognize underline "_", and firefox cannot recognize both. Wait

The writing order is generally to write the CSS of browsers with strong recognition capabilities at the end

Browser priority level: FF < IE7 < IE6, CSS hack writing order is generally FF IE7 IE6

Let’s take a look at the following examples of div css browser compatibility:

#demo {width:100px;} /* Executed by FIREFOX, IE6, IE7.*/

* html #demo {width:120px;} /* will be executed by IE6, and the previous definition will be overwritten by the later one, so the width of #demo will be 120px in IE6; */

* html # demo {width:130px;} /* will be executed by IE7*/

So in the end, the width of #demo is interpreted by the three browsers as:

firefox:100px;

ie6:120px;

ie7:130px;

The above example can also be solved using the method in my previous article "div css browser compatibility!" No more examples. ”

div css browser compatibility tips:

① Vertically centered. Set line-height to the same height as the current div, and then pass vertical-align: middle.( Be careful not to wrap the content.)

② Center horizontally. margin: 0 auto; (Of course it is not a panacea)
③ If you need to add styles to the content in the a tag, you need to set display: block; (common in Navigation tag)
④ 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 being doubled under IE.
⑤ The ul tag under FF has list-style and padding by default. . It is best to declare it in advance to avoid unnecessary trouble. (Common in navigation tags and content lists)

⑥ Do not set the height of the div as an external wrapper. It is best to add overflow: hidden. to achieve the goal. Height adaptive.

⑦ Regarding the hand cursor. cursor: pointer. And hand is only applicable to IE.

⑧ Setting padding on div under FF will cause the width and height to increase, but IE does not Yes. (can be solved with !important)

Note: Most of the css styles for firefox IE6 and IE7
are now hacked with !important. For IE6 and firefox tests, it can be displayed normally,
but IE7 !important can be interpreted correctly, but it will cause the page not to be displayed as required! Find a pin
A good hack for IE7 is to use "*html". Now browse it with IE7 and there should be no problem.


Now write a browser compatible one:
#cshk{ height:20px; } /* Moz */
* html #cshk {height:50px; } /* IE6 */
* html #cshk {height:80px; } /* IE7 */
Then the height under firefox is 20, the height under IE6 is 50, and the height under IE7 is 80.

Let me give you some advice: Is it better to use CSS Hack or not? In fact, when designing a website, it would be best if you can consider some possible places where CSS bugs may occur. I try to avoid CSS bugs when designing a website. Most experts don’t use hacks.

Statement:
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