- 2
- 0
- 约2.39千字
- 约 3页
- 2017-07-09 发布于河南
- 举报
如何编写兼容多浏览器的CSS代码
一个优秀的网站要尽量兼容所有浏览器,但由于浏览器类型多样(IE 5, IE 6, IE7, IE 8, Firefox, Chrome, Safari),以及浏览器对CSS高版本的支持问题,这就给网页开发者带来诸多困难。为了降低网页开发者寻找解决方案及编写代码的时间,我们收集了一些方法,以期能缩短开发时间,挺高网页在多浏览器中的兼容性。IE浏览器兼容性解决方案通过使用IE中的条件注释 (Conditional comments)。条件注释只能用于IE 5以后版本的浏览器,其他类型的浏览器将会忽略此注释。如果你安装了多个IE,条件注释(Conditional comments)将会以最高版本的IE为标准。条件注释(Conditional comments)示例:!--[if IE] style#logo {margin-left: 20px; }/style![endif]--条件注释(Conditional comments)说明:条件注释的基本结构和HTML的注释(!-- --)是一样的。因此IE以外的浏览器将会把它们看作是普通的注释而完全忽略它们。IE将会根据if条件来判断是否如解析普通的页面内容一样解析条件注释里的内容。条件注释使用的是HTML的注释结构,因此他们只能使用在HTML文件里,而不能在CSS文件中使用。我们来测试一下条件注释(Conditional comments)的实际效果。代码如下:!--[if IE] 根据条件判断,这是Internet Explorerbr / ![endif]--!--[if IE 5] 根据条件判断,这是Internet Explorer 5br / ![endif]--!--[if IE 5.0] 根据条件判断,这是Internet Explorer 5.0br / ![endif]--!--[if IE 5.5] 根据条件判断,这是Internet Explorer 5.5br / ![endif]--!--[if IE 6] 根据条件判断,这是Internet Explorer 6br / ![endif]--!--[if gte IE 5] 根据条件判断,这是Internet Explorer 5 或者更高br / ![endif]--!--[if lt IE 6] 根据条件判断,这是版小于6的Internet Explorerbr / ![endif]--!--[if lte IE 5.5] 根据条件判断,这是Internet Explorer 5.5或更低br / ![endif]--注:gt代表大于, lte代表小于或等于。预览 条件注释(Conditional comments)的实际效果重置CSS各个元素的属性值由于各个浏览器对CSS元素默认的属性值进行解析时,可能有所差异,所以我们尽量重置所需的CSS元素的属性值(CSS reset styles)。我们常见如下所示的重置CSS默认属性值的代码:* { margin: 0; padding: 0; }但是仅对margin和padding重置也许远远不够,下面的代码对常用的CSS元素进行了重置。html, body, div, span, applet, object, iframe,h1, h2, h3, h4, h5, h6, p, blockquote, pre,a, abbr, acronym, address, big, cite, code,del, dfn, em, font, img, ins, kbd, q, s, samp,small, strike, strong, sub, sup, tt, var,dl, dt, dd, ol, ul, li,fieldset, form, label, legend,table, caption, tbody, tfoot, thead, tr, th, td {margin: 0;padding: 0;border: 0;outline: 0;font-weight: inherit;font-style: inherit;font-size: 100%;font-family: inherit;vertical-align: baseline;}/* remember to define focus styles! */:focus {outline: 0;}body {line-height: 1;color: black;background: white;}ol, ul {list-style: none;}/* tables still need cellspacing=0 in the markup */ta
原创力文档

文档评论(0)