跨瀏覽器問題的五種解決方案

簡評: 瀏覽器兼容性問題經常讓人頭疼,如下是避免出現這些問題的五個技巧。css

1. 前綴 CSS3 樣式

若是您正在使用任何類型的現代 CSS 片斷,例如框尺寸(box-sizing)或背景剪輯(background-clip),請確保使用適當的前綴。html

-moz- /* Firefox and other browsers using Mozilla's browser engine */
-webkit- /* Safari, Chrome and browsers using the Webkit engine */
-o- /* Opera */
-ms- /* Internet Explorer (but not always) */

2. 使用 reset

您能夠使用 normalize.css,下面是我用的,來自 Genesis Framework Style Sheet。css3

html,body,div,span,applet,object,iframe,h1,h2,
h3,h4,h5,h6,p,blockquote,a,abbr,acronym,address,
big,cite,del,dfn,em,img,ins,kbd,q,s,samp,small,
strike,strong,sub,sup,tt,var,b,u,i,center,dl,dt,
dd,ol,ul,li,fieldset,form,label,legend,table,caption,
tbody,tfoot,thead,tr,th,td,article,aside,canvas,details,
embed,figure,figcaption,footer,header,hgroup,input,menu,
nav,output,ruby,section,summary,time,mark,audio,video {
border: 0;
margin: 0;
padding: 0;
vertical-align: baseline;
}

3. 避免填充寬度

當你添加寬度爲一個元素的填充時,它會變得更大。寬度和填充將被加在一塊兒。web

要解決這個問題,能夠添加這個:編程

* { -webkit-box-sizing: border-box; /* Safari/Chrome, other WebKit */
-moz-box-sizing: border-box; /* Firefox, other Gecko */
box-sizing: border-box; }

4. 清除 float

若是沒有清除,很容易出問題。感興趣的能夠看看 Chris Coyier 的這篇文章。canvas

能夠使用此 CSS 代碼片斷來清除:瀏覽器

.parent-selector:after {
content: "";
display: table;
clear: both;
}

若是你包裝大部分的元素,一個很是簡單的方法是將它添加到你的 wrap 類中。ruby

.wrap:after {
content: "";
display: table;
clear: both;
}

搞定!架構

5. 測試

建立您本身的跨瀏覽器基礎架構或僅使用Endtestapp

若是你讓這些東西成爲一種習慣,大概能夠搞定九成的瀏覽器問題。


原文連接:5 Tricks to Avoid Cross Browser Issues
推薦閱讀YouTube 上有哪些自學編程的優質頻道

歡迎關注:知乎專欄「極光日報」,天天爲 Makers 導讀三篇優質英文文章。

相關文章
相關標籤/搜索