1. 使用Reset但并非全局Reset
不同瀏覽器元素的默認屬性有所不同,使用Reset可重置瀏覽器元素的一些默認屬性,以達到瀏覽器的兼容。但需要注意的是,請不要使用全局Reset:
| *{ margin:0; padding:0; }*{ margin:0; padding:0; } | 
這不僅僅因為它是緩慢和低效率的方法,而且還會導致一些不必要的元素也重置了外邊距和內邊距。在此建議參考YUI Reset和Eric Meyer的做法。我跟Eric Meyer的觀點相同,Reset并不是一成不變的,具體還需要根據項目的不同需求做適當的修改,以達到瀏覽器的兼容和操作上的便利性。我使用的Reset如下:
| /** 清除內外邊距 **/ body, h1, h2, h3, h4, h5, h6, hr, p, blockquote, /* structural elements 結構元素 */ dl, dt, dd, ul, ol, li, /* list elements 列表元素 */ pre, /* text formatting elements 文本格式元素 */ form, fieldset, legend, button, input, textarea, /* form elements 表單元素 */ th, td, /* table elements 表格元素 */ img/* img elements 圖片元素 */{ border:medium none; margin: 0; padding: 0; } /** 設置默認字體 **/ body,button, input, select, textarea { font: 12px/1.5 '宋體',tahoma, Srial, helvetica, sans-serif; } h1, h2, h3, h4, h5, h6 { font-size: 100%; } em{font-style:normal;} /** 重置列表元素 **/ ul, ol { list-style: none; } /** 重置超鏈接元素 **/ a { text-decoration: none; color:#333;} a:hover { text-decoration: underline; color:#F40; } /** 重置圖片元素 **/ img{ border:0px;} /** 重置表格元素 **/ table { border-collapse: collapse; border-spacing: 0; }/** 清除內外邊距 **/ body, h1, h2, h3, h4, h5, h6, hr, p, blockquote, /* structural elements 結構元素 */ dl, dt, dd, ul, ol, li, /* list elements 列表元素 */ pre, /* text formatting elements 文本格式元素 */ form, fieldset, legend, button, input, textarea, /* form elements 表單元素 */ th, td, /* table elements 表格元素 */ img/* img elements 圖片元素 */{ border:medium none; margin: 0; padding: 0; } /** 設置默認字體 **/ body,button, input, select, textarea { font: 12px/1.5 '宋體',tahoma, Srial, helvetica, sans-serif; } h1, h2, h3, h4, h5, h6 { font-size: 100%; } em{font-style:normal;} /** 重置列表元素 **/ ul, ol { list-style: none; } /** 重置超鏈接元素 **/ a { text-decoration: none; color:#333;} a:hover { text-decoration: underline; color:#F40; } /** 重置圖片元素 **/ img{ border:0px;} /** 重置表格元素 **/ table { border-collapse: collapse; border-spacing: 0; } | 
2. 良好的命名習慣
無疑亂七八糟或者無語義命名的代碼,誰看了都會抓狂。就像這樣的代碼:
| .aaabb{margin:2px;color:red;}.aaabb{margin:2px;color:red;} | 
我想即使是初學者,也不至于會在實際項目中如此命名一個class,但有沒有想過這樣的代碼同樣是很有問題的:
新聞熱點
疑難解答