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

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

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

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

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

  不推荐使用行间样式

ExampleSourceCode

 
 
 
  1. "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
  2.  
  3.  
  4.  
  5. Pagetitle-52css.com title> </li> <li> head> </li> <li><body> </li> <li><pstylepstyle="color:red">... p> </li> <li> body> </li> <li> html> </li> <li> </li> </ol></pre><p>   不推荐使用内嵌样式</p><p>ExampleSourceCode</p> <pre> <ol> <li></li> <li>"http://www.w3.org/TR/html4/strict.dtd"><htmllanghtmllang="en"> </li> <li><head> </li> <li><metahttp-equivmetahttp-equiv="Content-Type"content="text/html;charset=utf-8"/> </li> <li><title>Pagetitle-52css.com title> </li> <li><styletypestyletype="text/css"media="screen"> </li> <li>p{color:red;}  </li> <li> style> </li> <li> head> </li> <li><body>... body> </li> <li> html> </li> <li> </li> </ol></pre><p>   推荐使用外联样式</p><p>ExampleSourceCode</p> <pre> <ol> <li></li> <li>"http://www.w3.org/TR/html4/strict.dtd"> </li> <li><htmllanghtmllang="en"> </li> <li><head> </li> <li><metahttp-equivmetahttp-equiv="content-type"content="text  </li> <li><title>Pagetitle-52css.com title> </li> <li><linkrellinkrel="stylesheet"href="name.css"type="text/css"media="screen"/> </li> <li> head> </li> <li><body>... body> </li> <li> html> </li> <li> </li> </ol></pre><p>#p# <strong>二、建议使用link引入外部样式表</strong></p><p>  为了兼容老版本的浏览器,建议使用link引入外部样式表的方来代替@import导入样式的方式.</p><p>译者注:@import是CSS2.1提出的所以老的浏览器不支持,点击查看@import的兼容性。</p><p>  @import和link在使用上会有一些区别,利用二者之间的差异,可以在实际运用中进行权衡。</p><p>  关于@import和link方式的比较在52CSS.com上有几篇文章可以拓展阅读。</p><p>  不推荐@import导入方式</p><p>ExampleSourceCode</p> <pre> <ol> <li></li> <li> </li> <li>"http://www.w3.org/TR/html4/strict.dtd"> </li> <li><htmllanghtmllang="en"> </li> <li><head> </li> <li><metahttp-equivmetahttp-equiv="content-type"content="text  </li> <li><title>Pagetitle-52css.com title> </li> <li><styletypestyletype="text/css"media="screen"> </li> <li>@importurl("styles.css");  </li> <li> style> </li> <li> head> </li> <li><body>... body> </li> <li> html> </li> <li> </li> </ol></pre><p>   推荐引入外部样式表方式</p><p>ExampleSourceCode</p> <pre> <ol> <li></li> <li>"http://www.w3.org/TR/html4/strict.dtd"><htmllanghtmllang="en"><head> </li> <li><metahttp-equivmetahttp-equiv="content-type"content="text  </li> <li><title>Pagetitle-52css.com title> </li> <li><linkrellinkrel="stylesheet"href="name.css"type="text/css"media="screen"/> </li> <li> head> </li> <li><body>... body> </li> <li> html> </li> <li> </li> </ol></pre><p> <strong>三、使用继承</strong></p><p>ExampleSourceCode</p> <pre> <ol> <li>低效率的  </li> <li> </li> <li>p{font-family:arial,helvetica,sans-serif;}  </li> <li>#container{font-family:arial,helvetica,sans-serif;}  </li> <li>#navigation{font-family:arial,helvetica,sans-serif;}  </li> <li>#content{font-family:arial,helvetica,sans-serif;}  </li> <li>#sidebar{font-family:arial,helvetica,sans-serif;}  </li> <li>h1{font-family:georgia,times,serif;}  </li> <li> </li> <li>高效的  </li> <li> </li> <li>body{font-family:arial,helvetica,sans-serif;}  </li> <li>body{font-family:arial,helvetica,sans-serif;}  </li> <li>h1{font-family:georgia,times,serif;}  </li> <li></li> </ol></pre><p>#p#</p><p><strong>四、使用多重选择器</strong></p><p>ExampleSourceCode</p> <pre> <ol> <li>低效率的  </li> <li> </li> <li>h1{color:#236799;}  </li> <li>h2{color:#236799;}  </li> <li>h3{color:#236799;}  </li> <li>h4{color:#236799;}  </li> <li> </li> <li>高效的  </li> <li> </li> <li>h1,h2,h3,h4{color:#236799;}  </li> <li> </li> </ol></pre><p> <strong>五、使用多重声明</strong></p><p>ExampleSourceCode</p> <pre> <ol> <li>低效率的  </li> <li> </li> <li>p{margin:001em;}  </li> <li>p{background:#ddd;}  </li> <li>p{color:#666;}  </li> <li> </li> <li>译者注:对于十六进制颜色值,个人偏向于色值不缩写且英文字母要大写的方式.  </li> <li> </li> <li>高效的  </li> <li> </li> <li>p{margin:001em;background:#ddd;color:#666;}  </li> <li> </li> </ol></pre><p> <strong>六、使用简记属性</strong></p><p>ExampleSourceCode</p> <pre> <ol> <li>低效率的  </li> <li> </li> <li>body{font-size:85%;font-family:arial,helvetica,sans-serif;  </li> <li>background-image:url(image.gif);background-repeat:no-repeat;  </li> <li>background-position:0100%;margin-top:1em;margin-right:1em;  </li> <li>margin-bottom:0;margin-left:1em;padding-top:10px;  </li> <li>padding-right:10px;padding-bottom:10px;padding-left:10px;  </li> <li>border-style:solid;border-width:1px;border-color:red;color:#222222;  </li> <li> </li> <li>高效的  </li> <li> </li> <li>body{font:85%arial,helvetica,sans-serif;  </li> <li>background:url(image.gif)no-repeat0100%;margin:1em1em0;  </li> <li>padding:10px;border:1pxsolidred;color:#222;}  </li> <li> </li> </ol></pre><p> <strong>七、避免使用!important</strong></p><p>ExampleSourceCode</p> <pre> <ol> <li>慎用写法  </li> <li> </li> <li>#news{background:#ddd!important;}  </li> <li>特定情况下可以使用以下方式提高权重级别  </li> <li>#container#news{background:#ddd;}  </li> <li>body#container#news{background:#ddd;}  </li> <li> </li> </ol></pre><p>  那么,如何让(后续)维护你站点的人更容易理解你的样式代码呢?我们在以后的文章中和大家共同讨论学习。</p><p>【编辑推荐】</p> <ol> <li>***实现CSS页面居中方法揭秘</li> <li>八个困扰新手的DIV CSS网页布局问题</li> <li>深入剖析CSS层叠与继承的使用</li> <li>专家推荐 10款优秀CSS框架</li> <li>实例解析清除CSS float浮动的三种方法</li> </ol> </p> <p> 网站标题:<a href="http://www.kswsj.com/qtweb/news37/434737.html">书写高效CSS注意的七个方面</a> <br> 转载注明:<a href="http://www.kswsj.com/qtweb/news37/434737.html">http://www.kswsj.com/qtweb/news37/434737.html</a> </p> <p> 网站建设、网络推广公司-成都快上网,一家网站设计、网站制作公司;服务项目有等 </p> <p class="adpic"> <a href="https://www.cdcxhl.com/service/ad.html" target="_blank" class="ad">广告</a> <a href="" target="_blank" class="adimg"><img src=""></a> </p> <p class="copy"> 声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: <a href="http://www.kswsj.com/" target="_blank">成都快上网</a> </p> </div> <div class="newsmorelb"> <p>成都快上网科技猜您喜欢</p> <ul> <li> <a href="/qtweb/news36/434736.html">ip肉鸡是什么意思?(美国肉鸡服务器)</a> </li><li> <a href="/qtweb/news35/434735.html">域名和服务器的实名必须一致吗?(请问这个域名是不是要实名制认证通过才可以正常访问空间)</a> </li><li> <a href="/qtweb/news34/434734.html">云服务器如何格式化</a> </li><li> <a href="/qtweb/news33/434733.html">滚屏截图怎么操作?(windows滚屏截图软件)</a> </li><li> <a href="/qtweb/news32/434732.html">win10正确激活方法?(新笔记本系统如何激活windows10)</a> </li><li> <a href="/qtweb/news31/434731.html">php中row函数_PHP</a> </li><li> <a href="/qtweb/news30/434730.html">有道云里的图片加载不出来怎么办?(图片不显示问题-云服务器问题)</a> </li><li> <a href="/qtweb/news29/434729.html">Cygwin Netbeans安装全过程解析</a> </li><li> <a href="/qtweb/news28/434728.html">net域名注册查询</a> </li> </ul> </div> </div> <div class="col-lg-3 noneb"> <div class="bkright" style="margin-top: 0"> <p><a href="https://www.cdcxhl.com/news/wxxcx/">微信小程序知识</a></p> <ul> <li> <a class="text_overflow" href="/qtweb/news43/286293.html">Python进阶:函数式编程</a> </li><li> <a class="text_overflow" href="/qtweb/news38/507338.html">硬核 JVM 压缩指针详解</a> </li><li> <a class="text_overflow" href="/qtweb/news16/80016.html">从后台取到数据库的值传到前台_前台</a> </li><li> <a class="text_overflow" href="/qtweb/news19/42919.html">三分之一支付赎金的企业仍然无法恢复数据</a> </li><li> <a class="text_overflow" href="/qtweb/news14/27164.html">数据库物理模式设计图:构建高效可靠的数据存储架构(数据库物理模式设计图)</a> </li><li> <a class="text_overflow" href="/qtweb/news24/16874.html">Oracle导出某些用户中所有表的实际操作方法</a> </li><li> <a class="text_overflow" href="/qtweb/news40/432890.html">该网站暂时无法进行访问怎么办?(打不开后台网页也反应慢)</a> </li><li> <a class="text_overflow" href="/qtweb/news19/230219.html">php如何限制恶意访问网站</a> </li><li> <a class="text_overflow" href="/qtweb/news2/295652.html">云效回滚能否加审批卡点,或者是否支持权限配置?</a> </li><li> <a class="text_overflow" href="/qtweb/news17/100817.html">电脑怎么没有声音</a> </li><li> <a class="text_overflow" href="/qtweb/news48/412248.html">国外服务器租用价格一般多少钱?韩国网站服务器租赁</a> </li><li> <a class="text_overflow" href="/qtweb/news32/72332.html">Shell特殊字符:让你的命令行更加高效</a> </li><li> <a class="text_overflow" href="/qtweb/news4/132904.html">如何使用虚拟主机部署Java应用程序?(虚拟主机部署java)</a> </li><li> <a class="text_overflow" href="/qtweb/news19/202619.html">一个局域网内,怎么查询网络服务商提供的固定ip地址?网络服务商提供的ip</a> </li><li> <a class="text_overflow" href="/qtweb/news17/340617.html">APP应用转型:探究其是否拥有数据库? (app有数据库吗)</a> </li> </ul> </div> <div class="bkright tag"> <p><a href="https://www.cdcxhl.com/hangye/" target="_blank">分类信息网</a></p> <ul> <li class="col-lg-6 col-md-6 col-sm-6 col-xs-6"> <a href="https://www.cdcxhl.com/hangye/fadianji/" target="_blank">柴油发电机</a> </li><li class="col-lg-6 col-md-6 col-sm-6 col-xs-6"> <a href="https://www.cdcxhl.com/hangye/suliaodai/" target="_blank">塑料袋</a> </li><li class="col-lg-6 col-md-6 col-sm-6 col-xs-6"> <a href="https://www.cdcxhl.com/hangye/opp/" target="_blank">OPP胶袋</a> </li><li class="col-lg-6 col-md-6 col-sm-6 col-xs-6"> <a href="https://www.cdcxhl.com/hangye/shipindai/" target="_blank">食品包装袋</a> </li><li class="col-lg-6 col-md-6 col-sm-6 col-xs-6"> <a href="https://www.cdcxhl.com/hangye/swzbw/" target="_blank">三维植被网</a> </li><li class="col-lg-6 col-md-6 col-sm-6 col-xs-6"> <a href="https://www.cdcxhl.com/hangye/bolimo/" target="_blank">玻璃贴膜</a> </li><li class="col-lg-6 col-md-6 col-sm-6 col-xs-6"> <a href="https://www.cdcxhl.com/hangye/bdfhw/" target="_blank">被动防护网</a> </li><li class="col-lg-6 col-md-6 col-sm-6 col-xs-6"> <a href="https://www.cdcxhl.com/hangye/sqwhq/" target="_blank">社区文化墙</a> </li><li class="col-lg-6 col-md-6 col-sm-6 col-xs-6"> <a href="https://www.cdcxhl.com/hangye/shipenji/" target="_blank">湿喷机</a> </li><li class="col-lg-6 col-md-6 col-sm-6 col-xs-6"> <a href="https://www.cdcxhl.com/hangye/bxgds/" target="_blank">不锈钢雕塑</a> </li><li class="col-lg-6 col-md-6 col-sm-6 col-xs-6"> <a href="https://www.cdcxhl.com/hangye/bgcl/" target="_blank">办公窗帘</a> </li><li class="col-lg-6 col-md-6 col-sm-6 col-xs-6"> <a href="https://www.cdcxhl.com/hangye/ggsj/" target="_blank">广告设计</a> </li><li class="col-lg-6 col-md-6 col-sm-6 col-xs-6"> <a href="https://www.cdcxhl.com/hangye/sdgz/" target="_blank">水电改造</a> </li><li class="col-lg-6 col-md-6 col-sm-6 col-xs-6"> <a href="https://www.cdcxhl.com/hangye/tadiaozulin/" target="_blank">塔吊租赁</a> </li><li class="col-lg-6 col-md-6 col-sm-6 col-xs-6"> <a href="https://www.cdcxhl.com/hangye/bengche/" target="_blank">混凝土泵车</a> </li><li class="col-lg-6 col-md-6 col-sm-6 col-xs-6"> <a href="https://www.cdcxhl.com/hangye/xiangsu/" target="_blank">橡塑保温</a> </li> </ul> </div> </div> </div> <div class="carousel-inner linkbg" style="background: #fff"> <div class="container"> <a href="http://m.cdcxhl.cn/applets/ " target="_blank">小程序开发</a>    <a href="http://www.nzanhua.com/" target="_blank">雕琢时光茶叶</a>    <a href="http://www.xcwzsj.com/" target="_blank">小谭网创广告</a>    <a href="http://www.lsjierui.cn/" target="_blank">成都LED亮化工程</a>    <a href="http://www.sczjjygc.com/" target="_blank">中建建业环保</a>    <a href="http://chengdu.cdcxhl.com/dingzhi/" target="_blank">APP定制开发</a>    <a href="https://www.cdxwcx.com/jifang/mianyang.html" target="_blank">绵阳主机托管</a>    <a href="https://www.scvps.cn/" target="_blank">租用服务器</a>    <a href="http://www.nzjierui.cn/" target="_blank">成都发电机组维修保养公司</a>    <a href="http://www.qhjierui.cn/" target="_blank">德阳定制网站建设</a>    <a href="https://www.cdxwcx.com/wangzhan/app.html" target="_blank">移动APP</a>    <a href="http://www.cdweb.net/solve/" target="_blank">网站建设方案</a>    <a href="http://www.scmwjz.com/" target="_blank">网站建设开发</a>    <a href="http://www.kswsj.cn/tuiguang/" target="_blank">网站SEO优化排名</a>    <a href="http://www.ycggtw.cn/" target="_blank">金羊影视传媒</a>    <a href="http://m.cdxwcx.com/weihu.html" target="_blank">网站维护</a>    <a href="http://www.cdhthh.cn/" target="_blank">成都纯水机</a>    <a href="http://www.cdxwcx.cn/sheji/" target="_blank">企业网站设计</a>    <a href="http://www.cdkjz.cn/fangan/jianshe/" target="_blank">网站建设报价方案</a>    <a href="http://m.xwcx.net/" target="_blank">响应式建站</a>     </div> </div> <footer> <div class="carousel-inner footjz"> <div class="container"> <i class="icon iconfont zbw"></i> 品质网站建设 <i class="icon iconfont"></i> 多平台展现 <i class="icon iconfont"></i> 600元建站 <i class="icon iconfont"></i> 高效快速 <i class="icon iconfont"></i> 专业用心服务 <button type="button" class="btn btn-default btn-lg" onClick="window.location.href='tencent://message/?uin=631063699&Site=&Menu=yes'"> 立即开始600元网站建设</button> <button type="button" class="btn btn-default btn-xs" onClick="window.location.href='tencent://message/?uin=631063699&Site=&Menu=yes'"> 600元企业网站建设</button> </div> </div> <div class="carousel-inner bqsy"> <div class="container"> <div class="lxfs"> <h4 class="yutelnone">028-86922220 13518219792</h4> <h4 class="yutelblock"><a href="tel:02886922220">028-86922220</a> <a href="tel:13518219792">13518219792</a></h4> <a class="btn btn-default" href="tencent://message/?uin=532337155&Site=&Menu=yes" role="button">网站建设<span>QQ</span>:532337155</a> <a class="btn btn-default" href="tencent://message/?uin=631063699&Site=&Menu=yes" role="button">营销推广<span>QQ</span>:631063699</a> <a class="btn btn1 btn-default" href="mqqwpa://im/chat?chat_type=wpa&uin=532337155&version=1&src_type=web&web_src=oicqzone.com" role="button">网站制作<span>QQ</span>:532337155</a> <a class="btn btn1 btn-default" href="mqqwpa://im/chat?chat_type=wpa&uin=631063699&version=1&src_type=web&web_src=oicqzone.com" role="button">营销推广<span>QQ</span>:631063699</a> <a class="btn btn-default nonea" href="tencent://message/?uin=1683211881&Site=&Menu=yes" role="button">售后QQ:1683211881</a> <div class="dz">成都快上网专注: <a href="http://www.kswsj.com/" target="_blank">成都网站制作</a> <a href="http://www.kswsj.com/" target="_blank">网站设计</a> <a href="http://www.kswsj.com/" target="_blank">成都网站建设</a> <address>地址:成都太升南路288号锦天国际A幢10楼</address> </div> </div> <div class="bzdh dz"><img src="https://www.cdcxhl.com/imges/bottom_logo.png" alt="创新互联"> <p><a href="https://www.cdcxhl.com/menu.html" target="_blank">成都创新互联科技有限公司</a><br> Tel:028-86922220(7x24h)</p></div> </div> </div> </footer> </body> </html> <script> $.getJSON ("../../qtwebpic.txt", function (data) { var jsonContent = { "featured":data } var random = jsonContent.featured[Math.floor(Math.random() * jsonContent.featured.length)]; $(".adpic .adimg").attr("href",random.link) $(".adpic img").attr("src",random.pic); }) </script>