這裡收集了一批 div+CSS 開發中非常實用的小技巧,涵蓋鏈接狀態、背景圖、表格邊框、定位、文本截斷、字體調用等各個方面,值得反覆翻閱。
定義鏈接的四種狀態要遵循固定順序:Link → Visited → Hover → Active。而與內容無關的裝飾性圖片,應使用 background 而非 <img> 引入。
在 IE5、IE6、IE7 及 FF 中均可通過的寫法:
table{ border-collapse: collapse; }
td{ border: #000 solid 1px; }
/* 方法二 */
table{ border-top: 1px solid red; border-left: 1px solid red; }
td{ border-bottom: 1px solid red; border-right: 1px solid red; }
margin 取負值時,可在絕對定位場景下起到相對定位的效果。頁面居中顯示時,絕對定位的層不適合直接用 left:XXpx,把它放進一個相對定位的標籤中、再用負 margin 調整是個好辦法。此外,絕對定位時用 margin 定位是相對於元素自身所在位置,這與 top/left 相對於窗口邊緣的定位截然不同,其優勢在於讓其他元素忽略它的存在。
<div style="width:120px;height:50px;border:1px solid blue;
overflow:hidden;text-overflow:ellipsis">
<NOBR>這是一行很長、表格一行顯示不下的文字</NOBR>
</div>
IE 註釋重複問題:可改寫為 <!?[if !IE]>...<![endif]?>。
用 CSS 調用外部字體:語法為 @font-face{font-family:name;src:url(url);},name 為字體名稱,url 指定 OpenType 字體文件路徑。
文本框文字垂直居中:在 FF 中行高等於高度無效,改用定義上下 padding 即可實現。
A 標籤定義注意:寫 a{color:red;} 時代表四種狀態的樣式;若只定義了 a:link,切記要把其他三種狀態也補全。
不必所有樣式都簡寫:當基礎樣式已定義四向 padding 時,後續只需覆蓋特定方向,如 padding-top:5px;padding-right:6px;,避免重複定義、也免去查對原值之勞。
大型站點先規劃:項目越大、樣式越多,越要在動手前做好命名規則、區塊劃分與內部樣式分類的充分策劃。
text-align:justify;text-justify:inter-ideograph;overflow:hidden;text-overflow:ellipsis;white-space:nowrap;(僅 IE 支持省略號,FF 只隱藏)table-layout:fixed;word-break:break-all;<acronym title="提示文字" style="cursor:help;">文字</acronym>.halfalpha{background-color:#000;filter:Alpha(Opacity=50)}(IE 私有)<param name="wmode" value="transparent">,Firefox 給 <embed> 加 wmode="transparent".pictures img{filter:alpha(opacity=45);} .pictures a:hover img{filter:alpha(opacity=90);}