編程學(xué)習(xí)網(wǎng) > 服務(wù)器 > CentOS > 常見的瀏覽器Hack技巧總結(jié)
2014
11-11

常見的瀏覽器Hack技巧總結(jié)

如果你經(jīng)常需要做前端頁面,那么你一定多多少少需要解決頁面的瀏覽器兼容問題。而瀏覽器兼容問題大部分也集中在對IE系列的兼容。這里就總結(jié)一下對IE系列的CSS Hack,記錄一下,方便以后查閱。

IE Hack

IE系列瀏覽器的hack大略如下:

  • _nowamagic:1px;-----------ie6
  • *nowamagic:1px;-----------ie7
  • nowamagic:1px\0;----------ie89
  • nowamagic:1px\9\0;--------ie9
  • :root nowamagic:1px;??? ----ie9(實際情況可能ie9還是有問題,再用這種方式)

這樣就基本上就可以兼容所有IE。

常見的瀏覽器Hack

其中粉紅色部分為屬性hack,黃色部分為選擇器hack,它們可以結(jié)合使用。此外Firefox和Chrome也有它們專有的hack,詳細hack方式及使用示例如下:

Firefox 與 Chrome 的 Hack

Firefox:

@-moz-document url-prefix()    /*寫在選擇器外層時(只可寫在此處):Firefox only*/

Chrome:

@media screen and (-webkit-min-device-pixel-ratio:0)    /*寫在選擇器外層時(只可寫在此處):Chrome only*/

使用示例:

@-moz-document url-prefix()    /*Firefox*/
{
	body
	{
		background-color:pink;
	}
}

瀏覽器對css的解析是從前到后的,并且采用最后一個樣式聲明。

CSS 實例

.color{
    background-color: #CC00FF;        /*所有瀏覽器都會顯示為紫色*/
    background-color: #FF0000\9;    /*IE6、IE7、IE8會顯示紅色*/
    *background-color: #0066FF;        /*IE6、IE7會變?yōu)樗{色*/            
    _background-color: #009933;        /*IE6會變?yōu)榫G色*/
}

IE8是可以和IE7兼容的,簡單一行代碼,讓IE8自動調(diào)用IE7的渲染模式。只需要在頁面中加入如下HTTP meta-tag:<meta http-equiv="X-UA-Compatible" content="IE=EmulateIE7" />,只要IE8讀到這個標簽,它就會自動啟動IE7兼容模式,保證頁面完整展示。

混用起來大約是這樣:

:root .demo {  
	background:#963\9; /* 僅IE9適用 */  
}  
.demo {  
	width: 300px;  
	height: 200px;  
	background: #036; /* 所有瀏覽器都適用 */  
	background: #09F\9; /* IE6~IE9 */  
	background: #09F\0; /* IE8~IE9 */  
	background: #09F\0/; /* IE8 */  
	*background: #F60; /* IE6/IE7 */  
	+background: #F60; /* IE6/IE7 */  
	@background: #F60; /* IE6/IE7 */  
	>background: #F60; /* IE6/IE7 */  
	_background: #ccc; /* IE6 */  
}  
@media all and (min-width:0) {  
	.demo {  
		background: #F06; /* webkit and opera */  
	}  
}  

@media screen and (-webkit-min-device-pixel-ratio:0){  
	.demo {background:#609;}/*webkit (& Opera9.2)*/  
}

掃碼二維碼 獲取免費視頻學(xué)習(xí)資料

Python編程學(xué)習(xí)

查 看2022高級編程視頻教程免費獲取