這份清單整理了大量實作中會反覆碰到的細節,涵蓋瀏覽器差異、選擇器優先權、盒模型、文字截斷與字型嵌入等主題,適合當作開發時的速查對照。
ul { margin:0; padding:0; } 可以省掉一堆麻煩。選擇器 { 屬性:B !important; 屬性:A; }。left:XXpx;把它放在一個基準元素旁邊,再用負 margin 位移,往往更好用。<script type="text/javascript">。<h1><span>標題內容</span></h1>,保留語意又便於替換樣式。text-align:justify; text-justify:inter-ideograph;overflow:hidden; text-overflow:ellipsis; white-space:nowrap;(IE5+ 有效,Firefox 當時只會隱藏而不顯示省略號;且僅能處理單行)table-layout:fixed; word-break:break-all;(IE5+)<acronym style="cursor:help;">文字</acronym>。這個效果在國外網站很常見,中文站反而少見。.halfalpha { background-color:#000000; filter:Alpha(Opacity=50) }。這是 IE 私有屬性,Firefox 不支援。.pictures img { filter: alpha(opacity=45); } 搭配 .pictures a:hover img { filter: alpha(opacity=90); }。<param name="wmode" value="transparent">;Firefox 則要在 <embed> 上補 wmode="transparent"。 或 。<!--[if !IE]>Put your commentary in here...<![endif]-->。table { border-collapse:collapse; } td { border:#000 solid 1px; }語法:
@font-face { font-family: name; src: url(url); sRules }
其中 name 是字型名稱,可用任何合法的 font-family 值;url 指向 OpenType 字型檔(.eot 或 .ote);sRules 為樣式表定義。
這個規則能讓網頁使用使用者本機不一定有的字型。字型檔內含可轉為 TrueType 的壓縮字型資料,可用來取代系統中同名的字型,檔案則以 Microsoft WEFT 工具製作。
示例:
@font-face { font-family: comic; src: url(https://valid_url/some_font_file.eot); }
@font-face { font-family: dreamy; font-weight: bold; src: url(https://www.example.com/font.eot); }
工具下載:Microsoft Web Embedding Font Tool,https://www.microsoft.com/typography/web/embedding/weft3/
寫 a { color:red; } 時,它其實同時定義了 A 的四種狀態。若之後只想改滑鼠移入的樣式,寫 a:hover 就夠了,其餘三狀態沿用 a 的定義。但反過來,如果你只定義了 a:link,那一定要記得把其他三種狀態補齊。
另外,並不是所有樣式都該簡寫。若前面已定義 p { padding:1px 2px 3px 4px },後來只想改上 padding 5px、下 padding 6px,不必寫成 p.style1 { padding:5px 6px 3px 4px },寫 p.style1 { padding-top:5px; padding-bottom:6px; } 更好:既不重複宣告,也不必回頭去查原來左右內距是多少,萬一前面的 p 改了,你的覆寫樣式也不會跟著出錯。