專家提醒 編寫CSS時注意的七個方面
隨著CSS網(wǎng)頁布局的應用越來越廣泛,但是如何才能寫出高效規(guī)范的CSS代碼呢,這里向大家描述一下編寫CSS時注意的七個方面,主要包括使用外聯(lián)樣式替代行間樣式或者內(nèi)嵌樣式,建議使用 link 引入外部樣式表等內(nèi)容。
編寫CSS時注意的七個方面
隨著CSS網(wǎng)頁布局的應用越來越廣泛,更多的CSSer開始書寫CSS,如何才能寫出高效規(guī)范的CSS代碼呢,今天向大家介紹,必須要注意的七個方面:
一、使用外聯(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 引入外部樣式表的方來代替 @import導入樣式的方式.
譯者注: @import是CSS2.1提出的所以老的瀏覽器不支持。
@import和link在使用上會有一些區(qū)別, 利用二者之間的差異,可以在實際運用中進行權衡。
關于@import和link方式的比較在52CSS.com上有幾篇文章可以拓展閱讀。
◆不推薦@import導入方式
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; }
譯者注: 對于十六進制顏色值,個人偏向于色值不縮寫且英文字母要大寫的方式.
高效的
CSS代碼
- p { margin: 0 0 1em; background: #ddd; color: #666; }
#p#六、使用簡記屬性
低效率的
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
慎用寫法
CSS代碼
- #news { background: #ddd !important; }
特定情況下可以使用以下方式提高權重級別
CSS代碼
- #container #news { background: #ddd; }
- body #container #news { background: #ddd; }
【編輯推薦】
- clear屬性在CSS中的妙用
- JavaScript動態(tài)創(chuàng)建div屬性和樣式
- 14大CSS工具提高網(wǎng)頁設計效率
- 五大CSS3新技術用法指導
- 解讀DIV CSS網(wǎng)頁布局中CSS無效十個原因