时光博客 - WEB开发中的可用性和用户体验
Tag: browers

[译]min-width的跨浏览器兼容解决方法

项目中用到min-width,找了很多解决方案,都不管用,上蓝色经典问了些高人。版主推荐了一篇国外技术文章,觉得价值比较大,就翻译一下,收藏起来。英文比较差,如果翻译有错,还请指正。

介绍
min-width这个属性除了IE6,以及IE5.X以外的浏览器一般都支持.所以本文主要是针对IE的css hack!
对网页设计人员来说,缺乏支持的最小宽度min-width在Internet Explorer中造成了许多问题.到现在为止,唯一模拟min-width的方法是使用JavaScript或Internet Explorer的expressions语法(间接的JavaScript).经过几个小时的尝试,我发现了一个纯CSS的方法.我的方法需要额外的一些div控制宽度和最小宽度,但我相信这是一个很小的代价,一个非 JavaScript方法工程跨浏览器(甚至在Mac IE5)

方法
基本思想是为那些理解min-width的浏览器提供如下正常的方法,对IE提供他独特的风格(我会解释一下教程)

The Css

  1. body {  
  2.  background:#fff url(rule.gif) 20px 0;  
  3.  color:#000;  
  4.  font-family:"trebuchet ms""times new roman", times, serif;  
  5.  margin:20px;  
  6.  padding:0;  
  7. }  
  8. .width {  
  9.  width:50%;  
  10.  min-width:300px;  
  11.  background:#fff;  
  12. }  
  13.  
  14. .content {  
  15.  border:1px solid #c00;  
  16.  padding:5px;  
  17. }  
  18.  
  19. .rule {  
  20.  width:300px;  
  21.  background:#c00;  
  22.  color:#fff;  
  23.  margin:1em 0;  

Browers Meta Tag X-UA-Compatib...

打开浏览器查看liupeng.us的页面源代码,细心的朋友会发现meta信息里面多了个tag标签

  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
  2. <html xmlns="http://www.w3.org/1999/xhtml"> 
  3. <head> 
  4. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
  5. <meta http-equiv="Content-Language" content="utf-8" /> 
  6. <meta http-equiv="x-ua-compatible" content="IE=7" /> 
  7. <meta http-equiv="Pragma" content="no-cache" /> 
  8. <meta content="关注WEB标准,用户体验,关注前端技术,网页设计师,前端开发工程师,liupeng,Beijing China" name="keywords" /> 
  9. <meta content="关注WEB标准,用户体验,关注前端技术,网页设计师,前端开发工程师,liupeng,Beijing China" name="description" /> 
  10. <meta content="SaBlog" name="copyright" /> 

里面多了一句

  1. <meta http-equiv="X-UA-Compatible" content="IE=7" />  

X-UA-Compatible 是什么?

看看MSDN上的资料说明:

In IE8 Beta 1, that option is the “IE=7” X-UA-Compatible tag, which instructs IE8 to display content in IE7 Standards mode. However, the scenario this doesn’t address is when IE=7 is applied as an HTTP header to a site that contains Quirks mode pages. The IE=7 HTTP header will force all pages – both Quirks and Standards – to display in IE7 Standards mode. Developers using this header while updating their sites would then have to add the “IE=5” <META> tag to each page they want to keep in Quirks mode. This logic is fine for many websites. However, if a site has lots of Quirks mode pages, or for the case where pages with frames host a mix of Strict and Quirks mode content – as brought to light by IE8 Beta 1 user feedback – the compatibility opt-out adds a bit more work than we intended.

主要意思我总结一下:

X-UA-Compatible是针对ie8新加的一个设置,对于ie8之外的浏览器是不识别的,这个区别与content="IE=7"在无论页面是否包含<!DOCTYPE>指令,都像是使用了 Windows Internet Explorer 7的标准模式。而content="IE=EmulateIE7"模式遵循<!DOCTYPE>指令。对于多数网站来说,它是首选的兼容性模式。

目前IE8尚在测试版中,所以为了避免制作出的页面在IE8下面出现错误,建议直接将IE8使用IE7进行渲染。也就是直接在页面的header的meta标签中加入如下代码:

  1. <meta http-equiv="X-UA-Compatible" content="IE=7" /> 

这样我们才能使得页面在IE8里面表现正常!

 

参考资料:Introducing IE=EmulateIE7