自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

專家提醒 編寫CSS時注意的七個方面

開發(fā) 前端
本文和大家重點學習一下編寫CSS時注意的七個方面,隨著CSS網(wǎng)頁布局的應用越來越廣泛,更多的CSSer開始書寫CSS,如何才能寫出高效規(guī)范的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代碼 

  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"   
  2. "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">     
  3. <html xmlns="http://www.w3.org/1999/xhtml">     
  4. <head>     
  5. <meta http-equiv="Content-Type" content="text/html;
  6.  charset=utf-8" />     
  7. <title>Page title - book.chinaz.com</title>     
  8. </head>       
  9. <body>     
  10. <p style="color: red"> ... </p>       
  11. </body>       
  12. </html>   
  13.  

 ◆不推薦使用內(nèi)嵌樣式

XML/HTML代碼 

  1. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"   
  2. "http://www.w3.org/TR/html4/strict.dtd"> <html lang="en">       
  3. <head>     
  4. <meta http-equiv="Content-Type" content="text/html; 
  5. charset=utf-8" />     
  6. <title>Page title - book.chinaz.com</title>       
  7. <style type="text/css" media="screen">     
  8. p { color: red; }       
  9. </style>       
  10. </head>       
  11. <body>... </body>       
  12. </html>     
  13.  

 ◆推薦使用外聯(lián)樣式

XML/HTML代碼 

  1. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"   
  2. "http://www.w3.org/TR/html4/strict.dtd">     
  3. <html lang="en">     
  4. <head>       
  5. <meta http-equiv="content-type" content="text       
  6. <title>Page title - book.chinaz.com</title>       
  7. <link rel="stylesheet" href="name.css"
  8.  type="text/css" media="screen" />       
  9. < /head>       
  10. <body> ... </body>       
  11. </html>     
  12.  

 #p#二、建議使用 link 引入外部樣式表

為了兼容老版本的瀏覽器,建議使用 link 引入外部樣式表的方來代替 @import導入樣式的方式.

譯者注: @import是CSS2.1提出的所以老的瀏覽器不支持。

@import和link在使用上會有一些區(qū)別, 利用二者之間的差異,可以在實際運用中進行權衡。

關于@import和link方式的比較在52CSS.com上有幾篇文章可以拓展閱讀。

◆不推薦@import導入方式

XML/HTML代碼 

  1. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"  
  2.  
  3.  "http://www.w3.org/TR/html4/strict.dtd">       
  4. <html lang="en">     
  5. <head>       
  6. <meta http-equiv="content-type" content="text       
  7. <title>Page title - 52css.com</title>       
  8. <style type="text/css" media="screen">       
  9. @import url("styles.css");      
  10. </style>     
  11. </head>     
  12. <body> ... </body>     
  13. </html>     
  14.  

 ◆推薦引入外部樣式表方式

XML/HTML代碼 

  1. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"   
  2. "http://www.w3.org/TR/html4/strict.dtd"> <html lang="en"> 
  3. <head>     
  4. <meta http-equiv="content-type" content="text       
  5. <title>Page title - blog.huangchao.org</title>     
  6. <link rel="stylesheet" href="name.css" type="text/css" 
  7. media="screen" />     
  8. </head>       
  9. <body> ... </body>       
  10. </html>     
  11.  

 三、使用繼承

低效率的

CSS代碼 

  1. p{ font-family: arial, helvetica, sans-serif; }      
  2. #container { font-family: arial, helvetica, sans-serif; }      
  3. #navigation { font-family: arial, helvetica, sans-serif; }      
  4. #content { font-family: arial, helvetica, sans-serif; }      
  5. #sidebar { font-family: arial, helvetica, sans-serif; }      
  6. h1 { font-family: georgia, times, serif; }      
  7.  

高效的

CSS代碼 

  1. body { font-family: arial, helvetica, sans-serif; }       
  2. body { font-family: arial, helvetica, sans-serif; }      
  3. h1 { font-family: georgia, times, serif; }   
  4.  

 #p#四、使用多重選擇器

低效率的

CSS代碼 

  1. h1 { color: #236799; }       
  2. h2 { color: #236799; }      
  3. h3 { color: #236799; }      
  4. h4 { color: #236799; }    
  5.  

高效的

CSS代碼 

  1. h1, h2, h3, h4 { color: #236799; }    
  2.  

五、使用多重聲明

低效率的

CSS代碼 

  1. p { margin: 0 0 1em; }      
  2. p { background: #ddd; }      
  3. p { color: #666; }     
  4.  

譯者注: 對于十六進制顏色值,個人偏向于色值不縮寫且英文字母要大寫的方式.

高效的

CSS代碼 

  1. p { margin: 0 0 1em; background: #ddd; color: #666; }  
  2.  
  3.    

#p#六、使用簡記屬性

低效率的

CSS代碼 

  1. body { font-size: 85%; font-family: arial, helvetica, sans-serif;   
  2. background-image: url(image.gif); background-repeat: no-repeat;   
  3. background-position: 0 100%; margin-top: 1em; margin-right: 1em;   
  4. margin-bottom: 0; margin-left: 1em; padding-top: 10px;   
  5. padding-right: 10px; padding-bottom: 10px; padding-left: 10px;  
  6.  border-style: solid; border-width: 1px;   
  7. border-color: red; color: #222222;     
  8.  

 高效的

CSS代碼 

  1. body { font: 85% arial, helvetica, sans-serif;   
  2. background: url(image.gif) no-repeat 0 100%;   
  3. margin: 1em 1em 0; padding: 10px;   
  4. border: 1px solid red; color: #222; }    
  5.  

七、避免使用 !important

慎用寫法

CSS代碼 

  1. #news { background: #ddd !important; }     
  2.  

特定情況下可以使用以下方式提高權重級別

CSS代碼 

  1. #container #news { background: #ddd; }      
  2. body #container #news { background: #ddd; }    
  3.  

【編輯推薦】

  1. clear屬性在CSS中的妙用
  2. JavaScript動態(tài)創(chuàng)建div屬性和樣式
  3. 14大CSS工具提高網(wǎng)頁設計效率
  4. 五大CSS3新技術用法指導
  5. 解讀DIV CSS網(wǎng)頁布局中CSS無效十個原因

 

責任編輯:佚名 來源: css3-html5.com
相關推薦

2010-09-01 09:39:07

CSS

2010-08-30 13:38:10

CSS

2010-06-04 14:42:25

2010-06-01 10:37:15

SVN合并

2023-03-29 18:40:00

2017-07-28 14:43:49

大數(shù)據(jù)數(shù)據(jù)可視化秘密

2010-05-26 17:05:13

SVN提交

2023-08-01 10:41:27

分派IT工作CIO

2022-06-27 14:03:06

IT治理首席信息官

2018-10-16 15:12:48

2018-10-18 11:03:06

2022-03-18 08:23:43

人工智能神經(jīng)網(wǎng)絡失敗

2010-08-12 09:39:26

FlexaddChil

2022-11-21 08:54:25

IT對接業(yè)務

2010-07-27 14:17:52

Flex SDK4

2010-09-02 13:28:55

CSS

2024-06-24 10:31:46

2011-06-01 09:27:08

JavaScript

2019-07-10 11:35:46

防火墻技術云計算
點贊
收藏

51CTO技術棧公眾號