CSS經驗分享:如何書寫可維護的CSS代碼

來源:互聯網
上載者:User

一、在樣式表開頭添加一個註解區塊,用以描述這個樣式表的建立日期、建立者、標記等備忘資訊。

Example Source Code:
/* 
---------------------------------
Site: Site name 
Author: 52CSS.com
Updated: Date and time 
Updated by: Name 
--------------------------------- 
*/

二、包括公用顏色標記

Example Source Code:
/* 
--------------------------------- 
COLORS
Body background: #def455
Container background: #fff 
Main Text: #333 
Links: #00600f 
Visited links: #098761 
Hover links: #aaf433 
H1, H2, H3: #960 
H4, H5, H6: #000 
---------------------------------
*/

三、給ID和Class進行有意義的命名

不推薦的命名方式:

Example Source Code:
.green-box { ... } 
#big-text { ... }

推薦使用的命名方式:

Example Source Code:
.pullquote {... }
#introduction {... }

四、將關聯的樣式規則進行整合

Example Source Code:
#header { ... } 
#header h1 { ... } 
#header h1 img { ... }
#header form { ... }
#header a#skip { ... }
#navigation { ... }
#navigation ul { ... }
#navigation ul li { ... }
#navigation ul li a { ... }
#navigation ul li a:hover { ... }
#content { ... }
#content h2 { ... }
#content p { ... }
#content ul { ... }
#content ul li { ... }

五、給樣式添加清晰的注釋

Example Source Code:
/*
--------------------------------- 
header styles 
--------------------------------- 
*/ 
#header { ... }
#header h1 { ... }
#header h1 img { ... }
#header form { ... }
/* 
--------------------------------- 
navigation styles
--------------------------------- 
*/
#navigation { ... }

 
相關文章

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.