写出高效规范的CSS代码

本文介绍了写出高效规范CSS代码的七个关键点,包括优先使用外联样式、用link引入样式表而非@import、利用继承、使用多重选择器、多重声明、简记属性,并避免使用!important。遵循这些最佳实践将使CSS代码更易维护。
摘要由CSDN通过智能技术生成

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

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

  不推荐使用行间样式

div css xhtml xml Example Source Code Example Source Code [www.52css.com]
<!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 - 52css.com</title>
</head> 
<body>
<p style="color: red"> ... </p> 
</body> 
</html> 


  不推荐使用内嵌样式

div css xhtml xml Example Source Code Example Source Code [www.52css.com]
<!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 - 52css.com</title> 
<style type="text/css" media="screen">
p { color: red; } 
</style> 
</head> 
<body>... </body> 
</html> 


  推荐使用外联样式

div css xhtml xml Example Source Code Example Source Code [www.52css.com]
<!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> 
<link rel="stylesheet" href="name.css" type="text/css" media="screen" /> 
< /head> 
<body> ... </body> 
</html> 


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

  为了兼容老版本的浏览器,建议使用 link 引入外部样式表的方来代替 @import导入样式的方式. 
译者注: @import是CSS2.1提出的所以老的浏览器不支持,点击查看 @import的兼容性。
  @import和link在使用上会有一些区别, 利用二者之间的差异,可以在实际运用中进行权衡。 
  关于@import和link方式的比较在52CSS.com上有几篇文章可以拓展阅读。
  不推荐@import导入方式

div css xhtml xml Example Source Code Example Source Code [www.52css.com]
<!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> 


  推荐引入外部样式表方式

div css xhtml xml Example Source Code Example Source Code [www.52css.com]
<!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>
<link rel="stylesheet" href="name.css" type="text/css" media="screen" />
</head> 
<body> ... </body> 
</html>


  三、使用继承 

div css xhtml xml Example Source Code Example Source Code [www.52css.com]
低效率的
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; } 
高效的
body { font-family: arial, helvetica, sans-serif; } 
body { font-family: arial, helvetica, sans-serif; }
h1 { font-family: georgia, times, serif; } 


  四、使用多重选择器 

div css xhtml xml Example Source Code Example Source Code [www.52css.com]
低效率的 
h1 { color: #236799; } 
h2 { color: #236799; }
h3 { color: #236799; }
h4 { color: #236799; } 
高效的
h1, h2, h3, h4 { color: #236799; } 


  五、使用多重声明

div css xhtml xml Example Source Code Example Source Code [www.52css.com]
低效率的
p { margin: 0 0 1em; }
p { background: #ddd; }
p { color: #666; } 
译者注: 对于十六进制颜色值,个人偏向于色值不缩写且英文字母要大写的方式. 
高效的
p { margin: 0 0 1em; background: #ddd; color: #666; } 


  六、使用简记属性

div css xhtml xml Example Source Code Example Source Code [www.52css.com]
低效率的 
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; 
高效的 
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

div css xhtml xml Example Source Code Example Source Code [www.52css.com]
慎用写法
#news { background: #ddd !important; } 
特定情况下可以使用以下方式提高权重级别
#container #news { background: #ddd; }
body #container #news { background: #ddd; }

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

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值