不同瀏覽器對 CSS 的解析存在細微差異,實務上常需要借助 hack 來針對特定瀏覽器寫入專屬樣式。以下整理幾種常見寫法,方便隨時查閱。
@media all and (min-width: 0px)
{/* Safari and Opera rules here */}
或者:
@media screen and (-webkit-min-device-pixel-ratio:0)
{/* Safari and Opera rules here */}
註:此處所指的 Safari 與 Opera 一般為當時的最新版本。
selector, x:-moz-any-link, x:default
{/* Firefox 3 and IE7 rules here */}
使用示範:在下面的結構中:
@media screen and (-webkit-min-device-pixel-ratio:0)
{
.box{width:100px;}
/* Safari and Opera rules here */
}
藉由這類針對性的 hack,我們就能在不影響其他瀏覽器的前提下,分別為 Safari/Opera 與 Firefox3/IE7 寫入各自專屬的樣式,達到跨瀏覽器的一致呈現。