提升css的五個技巧

看看人家寫的
簡單的說下就是
1、用這種

html, body, div, h1, h2, h3, h4, h5, h6, ul, ol, dl, li, dt, dd, p, blockquote,
pre, form, fieldset, table, th, td { margin: 0; padding: 0; }

而不用

* { margin: 0; padding: 0; }

2、
寫樣式的時候可以用字母排序的方式寫,如

div#header h1 {
    z-index: 101;
    color: #000;
    position: relative;
    line-height: 24px;
    margin-right: 48px;
    border-bottom: 1px solid #dedede;
    font-size: 18px;
}
div#header h1 {
    border-bottom: 1px solid #dedede;
    color: #000;
    font-size: 18px;
    line-height: 24px;
    margin-right: 48px;
    position: relative;
    z-index: 101;
}

這兩段里找到margin-right這個屬性,明顯的下面的比較快(講真,我第一眼沒看出來是按字母排序的)。
3、按你的布局來寫注釋,像這樣

/*****Reset*****/
Remove margin and padding from elements
 
/*****Basic Elements*****/
Define styles for basic elements: body, h1-h6, ul, ol, a, p, etc.
 
/*****Generic Classes*****/
Define styles for simple things like floating to the sides, removing a bottom margin on elements, etc
Yes, these may not be as semantic as we would all like, but they are necessary for coding efficiently 
 
/*****Basic Layout*****/
Define the basic template: header, footer, etc. Elements that help to define the basic layout of the site
 
/*****Header*****/
Define all elements in the header
 
/*****Content*****/
Define all elements in the content area
 
/*****Footer*****/
Define all elements in the footer
 
/*****Etc*****/
Continue to define the other sections one by one

寫注釋很重要、尤其是有人來接管你的代碼的時候,或者改代碼的時候,你總不希望人家過來一看,怕改了你這邊的,會動到其他的地方,然后人家再打電話給你來確定能不能改,或者讓你親自來改吧。與人方便自己方便。

4、關于css的代碼是寫在同一行還是多行的寫,這個就看你自己的喜好了,該文作者比較是按三個屬性以下是一行,超過三個多行寫的

div#header { float: left; width: 100%; }
div#header div.column {
    border-right: 1px solid #ccc;
    float: right;
    margin-right: 50px;
    padding: 10px;
    width: 300px;
}
div#header h1 { float: left; position: relative; width: 250px; }

我個人覺得,現在工具這么多,寫一行也行,多行也行,開心就好,比較有壓縮和美化工具,寫的快,好,可用性高,保證質量就好。

5、最后一點就是不要一味的用class和id,考慮一下css的選擇器。

寫的時候注意上下文,考慮一下,不要局限于當前的代碼,想想會不會有其他情況,多考慮實際情況,比如設計稿上,一句話只有3個字、然后你就寫了個寬度20px,不去考慮當他字多的時候的處理,那到時候數據多了(萬一是十個字)就慘了。發現好多人剛學的時候都不考慮,要多提疑問,自己回答。

2016/08/30添加 重置樣式表,僅供參考Killer Collection of CSS Resets

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容