CSS bug是布局中最頭疼的問(wèn)題。我們需要兼顧各種瀏覽器,以期待獲得一致的效果。
非常遺憾的是各廠(chǎng)商之間的競爭導致很多問(wèn)題的存在。而IE6與IE7在很多問(wèn)題上也存在著(zhù)很大的差別。
輕松的解決CSS bug是我們必須掌握的技能?,F在整理出最常用的12種CSS BUG解決方法以及CSS BUG類(lèi)的小技巧。
希望對您的學(xué)習、工作有所幫助,如果您依然有疑問(wèn),
一、 針對瀏覽器的選擇器
這些選擇器在你需要針對某款瀏覽器進(jìn)行css設計時(shí)將非常有用.
IE6及其更低版本
* html {}
IE7及其更低版本
*:first-child+html {} * html {}
僅針對IE7
*:first-child+html {}
IE7和當代瀏覽器
html>body{}
僅當代瀏覽器(IE7不適用)
html>/**/body{}
Opera9及其更低版本
html:first-child {}
Safari
html[xmlns*=""] body:last-child {}
要使用這些選擇器,請將它們放在樣式之前. 例如:
Example Source Code
[www.mb5u.com]#content-box {
width: 300px;
height: 150px;
}
Example Source Code
[www.mb5u.com]* html #content-box {
width: 250px;
}
二、讓IE6支持PNG透明 一個(gè)IE6的Bug引起了大麻煩, 他不支持透明的PNG圖片。
你需要使用一個(gè)css濾鏡
Example Source Code
[www.mb5u.com]*html #image-style {
background-image: none;
filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(src="fil
ename.png", sizingMethod="scale");
}
三、移除超鏈接的虛線(xiàn) FireFox下,當你點(diǎn)擊一個(gè)超鏈接時(shí)會(huì )在外圍出現一個(gè)虛線(xiàn)輪廓. 這很容易解決, 只需要在標簽樣式中加入:
Example Source Code
[www.mb5u.com]outline:none.
a{
outline: none;
}
四、給行內元素定義寬度 如果你給一個(gè)行內元素定義寬度,那么它只是在IE6下有效. 所有的HTML元素要么是行內元素要么就好是塊元素. 行內元素包括: <span>, <a>, <strong> 和 <em>. 塊元素包括<div>, <p>, <h1>, <form>和<li> . 你不能定義行內元素的寬度, 為了解決這個(gè)問(wèn)題你可以將行內元素轉變?yōu)閴K元素.
Example Source Code
[www.mb5u.com]span { width: 150px; display: block }
五、讓固定寬度的頁(yè)面居中 為了讓頁(yè)面在瀏覽器居中顯示, 需要相對定位外層div, 然后把margin設置為auto.
Example Source Code
[www.mb5u.com]#wrapper {
margin: auto;
position: relative;
}
七、IE6雙倍邊距的bug 給此對象加上display:inline即可解決問(wèn)題。
八、圖片替換技術(shù) 用文字總比用圖片做標題好一些. 文字對屏幕閱讀機和SEO都是非常友好的.
Example Source Code
[www.mb5u.com]HTML:
<h1><span>Main heading one</span></h1>
CSS:
h1 { background: url(heading-image.gif) no-repeat; }
h1 span {
position:absolute;
text-indent: -5000px;
}
你可以看到我們對標題使用了標準的<h1>作為標簽并且用css來(lái)將文本替換為圖片. text-indent屬性將文字推到了瀏覽器左邊5000px處, 這樣對于瀏覽者來(lái)說(shuō)就看不見(jiàn)了.
關(guān)掉css,然后看看頭部會(huì )是什么樣子的.
九、 最小寬度 IE6另外一個(gè)bug就是它不支持 min-width 屬性. min-width又是相當有用的, 特別是對于彈性模板來(lái)說(shuō), 它們有一個(gè)100%的寬度,min-width 可以告訴瀏覽器何時(shí)就不要再壓縮寬度了.
除IE6以外所有的瀏覽器你只需要一個(gè) min-width: Xpx; 例如:
Example Source Code
[www.mb5u.com].container {
min-width:300px;
}
為了讓他在IE6下工作, 我們需要一些額外的工作. 開(kāi)始的時(shí)候我們需要創(chuàng )建兩個(gè)div, 一個(gè)包含另一個(gè):
Example Source Code
[www.mb5u.com]<div class="container">
<div class="holder">Content</div>
</div>
然后你需要定義外層div的min-width屬性,
Example Source Code
[www.mb5u.com].container {
min-width:300px;
}
這時(shí)該是IE hack大顯身手的時(shí)候了. 你需要包含如下的代碼:
Example Source Code
[www.mb5u.com]* html .container {
border-right: 300px solid #FFF;
}
* html .holder {
display: inline-block;
position: relative;
margin-right: -300px;
}
As the browser window is resized the outer div width reduces to suit until it shrinks to the border width, at which point it will not shrink any further. The holder div follows suit and also stops shrinking. The outer div border width becomes the minimum width of the inner div.
十、隱藏水平滾動(dòng)條 為了避免出現水平滾動(dòng)條, 在body里加入 overflow-x:hidden .
Example Source Code
[www.mb5u.com]body { overflow-x: hidden; }
當你決定使用一個(gè)比瀏覽器窗口大的圖片或者flash時(shí), 這個(gè)技巧將非常有用.