專家提醒 編寫(xiě)CSS時(shí)注意的七個(gè)方面
隨著CSS網(wǎng)頁(yè)布局的應(yīng)用越來(lái)越廣泛,但是如何才能寫(xiě)出高效規(guī)范的CSS代碼呢,這里向大家描述一下編寫(xiě)CSS時(shí)注意的七個(gè)方面,主要包括使用外聯(lián)樣式替代行間樣式或者內(nèi)嵌樣式,建議使用 link 引入外部樣式表等內(nèi)容。
編寫(xiě)CSS時(shí)注意的七個(gè)方面
隨著CSS網(wǎng)頁(yè)布局的應(yīng)用越來(lái)越廣泛,更多的CSSer開(kāi)始書(shū)寫(xiě)CSS,如何才能寫(xiě)出高效規(guī)范的CSS代碼呢,今天向大家介紹,必須要注意的七個(gè)方面:
一、使用外聯(lián)樣式替代行間樣式或者內(nèi)嵌樣式
◆不推薦使用行間樣式
XML/HTML代碼
- <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
 - "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 - <html xmlns="http://www.w3.org/1999/xhtml">
 - <head>
 - <meta http-equiv="Content-Type" content="text/html;
 - charset=utf-8" />
 - <title>Page title - book.chinaz.com</title>
 - </head>
 - <body>
 - <p style="color: red"> ... </p>
 - </body>
 - </html>
 
◆不推薦使用內(nèi)嵌樣式
XML/HTML代碼
- <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
 - "http://www.w3.org/TR/html4/strict.dtd"> <html lang="en">
 - <head>
 - <meta http-equiv="Content-Type" content="text/html;
 - charset=utf-8" />
 - <title>Page title - book.chinaz.com</title>
 - <style type="text/css" media="screen">
 - p { color: red; }
 - </style>
 - </head>
 - <body>... </body>
 - </html>
 
◆推薦使用外聯(lián)樣式
XML/HTML代碼
- <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
 - "http://www.w3.org/TR/html4/strict.dtd">
 - <html lang="en">
 - <head>
 - <meta http-equiv="content-type" content="text
 - <title>Page title - book.chinaz.com</title>
 - <link rel="stylesheet" href="name.css"
 - type="text/css" media="screen" />
 - < /head>
 - <body> ... </body>
 - </html>
 
#p#二、建議使用 link 引入外部樣式表
為了兼容老版本的瀏覽器,建議使用 link 引入外部樣式表的方來(lái)代替 @import導(dǎo)入樣式的方式.
譯者注: @import是CSS2.1提出的所以老的瀏覽器不支持。
@import和link在使用上會(huì)有一些區(qū)別, 利用二者之間的差異,可以在實(shí)際運(yùn)用中進(jìn)行權(quán)衡。
關(guān)于@import和link方式的比較在52CSS.com上有幾篇文章可以拓展閱讀。
◆不推薦@import導(dǎo)入方式
XML/HTML代碼
- <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
 - "http://www.w3.org/TR/html4/strict.dtd">
 - <html lang="en">
 - <head>
 - <meta http-equiv="content-type" content="text
 - <title>Page title - 52css.com</title>
 - <style type="text/css" media="screen">
 - @import url("styles.css");
 - </style>
 - </head>
 - <body> ... </body>
 - </html>
 
◆推薦引入外部樣式表方式
XML/HTML代碼
- <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
 - "http://www.w3.org/TR/html4/strict.dtd"> <html lang="en">
 - <head>
 - <meta http-equiv="content-type" content="text
 - <title>Page title - blog.huangchao.org</title>
 - <link rel="stylesheet" href="name.css" type="text/css"
 - media="screen" />
 - </head>
 - <body> ... </body>
 - </html>
 
三、使用繼承
低效率的
CSS代碼
- p{ font-family: arial, helvetica, sans-serif; }
 - #container { font-family: arial, helvetica, sans-serif; }
 - #navigation { font-family: arial, helvetica, sans-serif; }
 - #content { font-family: arial, helvetica, sans-serif; }
 - #sidebar { font-family: arial, helvetica, sans-serif; }
 - h1 { font-family: georgia, times, serif; }
 
高效的
CSS代碼
- body { font-family: arial, helvetica, sans-serif; }
 - body { font-family: arial, helvetica, sans-serif; }
 - h1 { font-family: georgia, times, serif; }
 
#p#四、使用多重選擇器
低效率的
CSS代碼
- h1 { color: #236799; }
 - h2 { color: #236799; }
 - h3 { color: #236799; }
 - h4 { color: #236799; }
 
高效的
CSS代碼
- h1, h2, h3, h4 { color: #236799; }
 
五、使用多重聲明
低效率的
CSS代碼
- p { margin: 0 0 1em; }
 - p { background: #ddd; }
 - p { color: #666; }
 
譯者注: 對(duì)于十六進(jìn)制顏色值,個(gè)人偏向于色值不縮寫(xiě)且英文字母要大寫(xiě)的方式.
高效的
CSS代碼
- p { margin: 0 0 1em; background: #ddd; color: #666; }
 
#p#六、使用簡(jiǎn)記屬性
低效率的
CSS代碼
- body { font-size: 85%; font-family: arial, helvetica, sans-serif;
 - background-image: url(image.gif); background-repeat: no-repeat;
 - background-position: 0 100%; margin-top: 1em; margin-right: 1em;
 - margin-bottom: 0; margin-left: 1em; padding-top: 10px;
 - padding-right: 10px; padding-bottom: 10px; padding-left: 10px;
 - border-style: solid; border-width: 1px;
 - border-color: red; color: #222222;
 
高效的
CSS代碼
- body { font: 85% arial, helvetica, sans-serif;
 - background: url(image.gif) no-repeat 0 100%;
 - margin: 1em 1em 0; padding: 10px;
 - border: 1px solid red; color: #222; }
 
七、避免使用 !important
慎用寫(xiě)法
CSS代碼
- #news { background: #ddd !important; }
 
特定情況下可以使用以下方式提高權(quán)重級(jí)別
CSS代碼
- #container #news { background: #ddd; }
 - body #container #news { background: #ddd; }
 
【編輯推薦】
- clear屬性在CSS中的妙用
 - JavaScript動(dòng)態(tài)創(chuàng)建div屬性和樣式
 - 14大CSS工具提高網(wǎng)頁(yè)設(shè)計(jì)效率
 - 五大CSS3新技術(shù)用法指導(dǎo)
 - 解讀DIV CSS網(wǎng)頁(yè)布局中CSS無(wú)效十個(gè)原因
 















 
 
 






 
 
 
 