书写高效CSS注意的七个方面

开发 前端
随着CSS网页布局的应用越来越广泛,更多的CSSer开始书写CSS,如何才能写出高效规范的CSS代码呢,本文向大家介绍一下必须要注意的七个方面。

你对如何书写高效CSS是否熟悉,这里和大家分享一下书写高效CSS注意的七个方面,主要包括使用外联样式替代行间样式或者内嵌样式,建议使用link引入外部样式表等内容,相信本文介绍一定会让你有所收获。

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

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

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

  不推荐使用行间样式

ExampleSourceCode

  1. "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
  2. <htmlxmlnshtmlxmlns="http://www.w3.org/1999/xhtml"> 
  3. <head> 
  4. <metahttp-equivmetahttp-equiv="Content-Type"content="text/html;charset=utf-8"/> 
  5. <title>Pagetitle-52css.comtitle> 
  6. head> 
  7. <body> 
  8. <pstylepstyle="color:red">...p> 
  9. body> 
  10. html> 
  11.  

   不推荐使用内嵌样式

ExampleSourceCode

  1. "http://www.w3.org/TR/html4/strict.dtd"><htmllanghtmllang="en"> 
  2. <head> 
  3. <metahttp-equivmetahttp-equiv="Content-Type"content="text/html;charset=utf-8"/> 
  4. <title>Pagetitle-52css.comtitle> 
  5. <styletypestyletype="text/css"media="screen"> 
  6. p{color:red;}  
  7. style> 
  8. head> 
  9. <body>...body> 
  10. html> 
  11.  

   推荐使用外联样式

ExampleSourceCode

  1. "http://www.w3.org/TR/html4/strict.dtd"> 
  2. <htmllanghtmllang="en"> 
  3. <head> 
  4. <metahttp-equivmetahttp-equiv="content-type"content="text  
  5. <title>Pagetitle-52css.comtitle> 
  6. <linkrellinkrel="stylesheet"href="name.css"type="text/css"media="screen"/> 
  7. head> 
  8. <body>...body> 
  9. html> 
  10.  

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

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

译者注:@import是CSS2.1提出的所以老的浏览器不支持,点击查看@import的兼容性。

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

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

  不推荐@import导入方式

ExampleSourceCode

  1.  
  2. "http://www.w3.org/TR/html4/strict.dtd"> 
  3. <htmllanghtmllang="en"> 
  4. <head> 
  5. <metahttp-equivmetahttp-equiv="content-type"content="text  
  6. <title>Pagetitle-52css.comtitle> 
  7. <styletypestyletype="text/css"media="screen"> 
  8. @importurl("styles.css");  
  9. style> 
  10. head> 
  11. <body>...body> 
  12. html> 
  13.  

   推荐引入外部样式表方式

ExampleSourceCode

  1. "http://www.w3.org/TR/html4/strict.dtd"><htmllanghtmllang="en"><head> 
  2. <metahttp-equivmetahttp-equiv="content-type"content="text  
  3. <title>Pagetitle-52css.comtitle> 
  4. <linkrellinkrel="stylesheet"href="name.css"type="text/css"media="screen"/> 
  5. head> 
  6. <body>...body> 
  7. html> 
  8.  

 三、使用继承

ExampleSourceCode

  1. 低效率的  
  2.  
  3. p{font-family:arial,helvetica,sans-serif;}  
  4. #container{font-family:arial,helvetica,sans-serif;}  
  5. #navigation{font-family:arial,helvetica,sans-serif;}  
  6. #content{font-family:arial,helvetica,sans-serif;}  
  7. #sidebar{font-family:arial,helvetica,sans-serif;}  
  8. h1{font-family:georgia,times,serif;}  
  9.  
  10. 高效的  
  11.  
  12. body{font-family:arial,helvetica,sans-serif;}  
  13. body{font-family:arial,helvetica,sans-serif;}  
  14. h1{font-family:georgia,times,serif;}  

#p#

四、使用多重选择器

ExampleSourceCode

  1. 低效率的  
  2.  
  3. h1{color:#236799;}  
  4. h2{color:#236799;}  
  5. h3{color:#236799;}  
  6. h4{color:#236799;}  
  7.  
  8. 高效的  
  9.  
  10. h1,h2,h3,h4{color:#236799;}  
  11.  

 五、使用多重声明

ExampleSourceCode

  1. 低效率的  
  2.  
  3. p{margin:001em;}  
  4. p{background:#ddd;}  
  5. p{color:#666;}  
  6.  
  7. 译者注:对于十六进制颜色值,个人偏向于色值不缩写且英文字母要大写的方式.  
  8.  
  9. 高效的  
  10.  
  11. p{margin:001em;background:#ddd;color:#666;}  
  12.  

 六、使用简记属性

ExampleSourceCode

  1. 低效率的  
  2.  
  3. body{font-size:85%;font-family:arial,helvetica,sans-serif;  
  4. background-image:url(image.gif);background-repeat:no-repeat;  
  5. background-position:0100%;margin-top:1em;margin-right:1em;  
  6. margin-bottom:0;margin-left:1em;padding-top:10px;  
  7. padding-right:10px;padding-bottom:10px;padding-left:10px;  
  8. border-style:solid;border-width:1px;border-color:red;color:#222222;  
  9.  
  10. 高效的  
  11.  
  12. body{font:85%arial,helvetica,sans-serif;  
  13. background:url(image.gif)no-repeat0100%;margin:1em1em0;  
  14. padding:10px;border:1pxsolidred;color:#222;}  
  15.  

 七、避免使用!important

ExampleSourceCode

  1. 慎用写法  
  2.  
  3. #news{background:#ddd!important;}  
  4. 特定情况下可以使用以下方式提高权重级别  
  5. #container#news{background:#ddd;}  
  6. body#container#news{background:#ddd;}  
  7.  

  那么,如何让(后续)维护你站点的人更容易理解你的样式代码呢?我们在51cto.com以后的文章中和大家共同讨论学习。

【编辑推荐】

  1. ***实现CSS页面居中方法揭秘
  2. 八个困扰新手的DIV CSS网页布局问题
  3. 深入剖析CSS层叠与继承的使用
  4. 专家推荐 10款优秀CSS框架
  5. 实例解析清除CSS float浮动的三种方法

 

责任编辑:佚名 来源: 52css.com
相关推荐

2010-08-30 13:38:10

CSS

2010-09-09 17:06:12

CSS

2023-03-29 18:40:00

2009-02-16 16:49:53

DBA经验

2022-04-14 10:40:11

领导者IT团队远程团队

2023-08-01 10:41:27

分派IT工作CIO

2022-03-18 08:23:43

人工智能神经网络失败

2021-10-18 13:26:15

大数据数据分析技术

2020-03-23 10:59:52

CISO网络安全漏洞

2022-06-27 14:03:06

IT治理首席信息官

2022-05-25 10:35:21

资产管理者SAM

2011-06-01 09:27:08

JavaScript

2011-01-11 08:45:17

JavaScript

2019-07-10 11:35:46

防火墙技术云计算

2023-08-22 10:25:19

CSS动画网页

2016-09-28 09:58:59

网络安全应用安全

2012-07-02 10:14:56

2022-06-17 09:58:23

JVM应用程序

2016-05-31 15:02:23

Linux桌面Distrowatch
点赞
收藏

51CTO技术栈公众号