导航

北京网络营销/小健_IT互联网营销_SEO博客专栏

>>宽容是金,成事于心。

« 09年中国互联网站品牌频道揭晓 搜狐娱乐上榜百度谷歌相继推春运地图:谁更懂中国用户 »

CSS经验分享:书写高效CSS注意的七个方面

着CSS网页布局的应用越来越广泛,更多的CSSer开始书写CSS,如何才能写出高效规范的CSS代码呢,今天向大家介绍,必须要注意的七个方面:

一、使用外联样式替代行间样式或者内嵌样式

不推荐使用行间样式

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

 不推荐使用内嵌样式

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

推荐使用外联样式

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

二、建议使用 link 引入外部样式表

为了兼容老版本的浏览器,建议使用 link 引入外部样式表的方来代替 @import导入样式的方式.

译者注: @import是CSS2.1提出的所以老的浏览器不支持。

@import和link在使用上会有一些区别, 利用二者之间的差异,可以在实际运用中进行权衡。

关于@import和link方式的比较在52CSS.com上有几篇文章可以拓展阅读。

不推荐@import导入方式

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

推荐引入外部样式表方式

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

三、使用继承

低效率的

CSS代码
  1. p{ font-familyarialhelveticasans-serif; }   
  2. #container { font-familyarialhelveticasans-serif; }   
  3. #navigation { font-familyarialhelveticasans-serif; }   
  4. #content { font-familyarialhelveticasans-serif; }   
  5. #sidebar { font-familyarialhelveticasans-serif; }   
  6. h1 { font-family: georgia, times, serif; }   

高效的

CSS代码
  1. body { font-familyarialhelveticasans-serif; }    
  2. body { font-familyarialhelveticasans-serif; }   
  3. h1 { font-family: georgia, times, serif; }  

四、使用多重选择器

低效率的

CSS代码
  1. h1 { color#236799; }    
  2. h2 { color#236799; }   
  3. h3 { color#236799; }   
  4. h4 { color#236799; }  

高效的

CSS代码
  1. h1, h2, h3, h4 { color#236799; }  

五、使用多重声明

低效率的

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

译者注: 对于十六进制颜色值,个人偏向于色值不缩写且英文字母要大写的方式.

高效的

CSS代码
  1. p { margin: 0 0 1em; background#dddcolor#666; }  

六、使用简记属性

低效率的

CSS代码
  1. body { font-size: 85%; font-familyarialhelveticasans-serifbackground-imageurl(image.gif); background-repeatno-repeatbackground-position: 0 100%; margin-top: 1em; margin-right: 1em; margin-bottom: 0; margin-left: 1em; padding-top10pxpadding-right10pxpadding-bottom10pxpadding-left10pxborder-stylesolidborder-width1pxborder-colorredcolor#222222;  

高效的

CSS代码
  1. body { font: 85% arialhelveticasans-serifbackgroundurl(image.gif) no-repeat 0 100%; margin: 1em 1em 0; padding10pxborder1px solid redcolor#222; }  

七、避免使用 !important

慎用写法

CSS代码
  1. #news { background#ddd !important; }  

特定情况下可以使用以下方式提高权重级别

CSS代码
  1. #container #news { background#ddd; }   
  2. body #container #news { background#ddd; }  

作者:小健@北京网络营销_小健SEO博客专栏
原文链接:http://www.seosz.com/blog/post/gaoxiecss.html

版权所有,此文由小健收集整理,转载请以链接形式。

  • 相关文章:

发表评论:

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。

Copyright 北京网络营销 SEO实战·愉悦网络 Your WebSite. Some Rights Reserved.