美文网首页
提升css的五个技巧

提升css的五个技巧

作者: 一沭丶 | 来源:发表于2016-08-29 16:42 被阅读11次

    看看人家写的
    简单的说下就是
    1、用这种

    html, body, div, h1, h2, h3, h4, h5, h6, ul, ol, dl, li, dt, dd, p, blockquote,
    pre, form, fieldset, table, th, td { margin: 0; padding: 0; }
    

    而不用

    * { margin: 0; padding: 0; }
    

    2、
    写样式的时候可以用字母排序的方式写,如

    div#header h1 {
        z-index: 101;
        color: #000;
        position: relative;
        line-height: 24px;
        margin-right: 48px;
        border-bottom: 1px solid #dedede;
        font-size: 18px;
    }
    
    div#header h1 {
        border-bottom: 1px solid #dedede;
        color: #000;
        font-size: 18px;
        line-height: 24px;
        margin-right: 48px;
        position: relative;
        z-index: 101;
    }
    

    这两段里找到margin-right这个属性,明显的下面的比较快(讲真,我第一眼没看出来是按字母排序的)。
    3、按你的布局来写注释,像这样

    /*****Reset*****/
    Remove margin and padding from elements
     
    /*****Basic Elements*****/
    Define styles for basic elements: body, h1-h6, ul, ol, a, p, etc.
     
    /*****Generic Classes*****/
    Define styles for simple things like floating to the sides, removing a bottom margin on elements, etc
    Yes, these may not be as semantic as we would all like, but they are necessary for coding efficiently 
     
    /*****Basic Layout*****/
    Define the basic template: header, footer, etc. Elements that help to define the basic layout of the site
     
    /*****Header*****/
    Define all elements in the header
     
    /*****Content*****/
    Define all elements in the content area
     
    /*****Footer*****/
    Define all elements in the footer
     
    /*****Etc*****/
    Continue to define the other sections one by one
    

    写注释很重要、尤其是有人来接管你的代码的时候,或者改代码的时候,你总不希望人家过来一看,怕改了你这边的,会动到其他的地方,然后人家再打电话给你来确定能不能改,或者让你亲自来改吧。与人方便自己方便。

    4、关于css的代码是写在同一行还是多行的写,这个就看你自己的喜好了,该文作者比较是按三个属性以下是一行,超过三个多行写的

    div#header { float: left; width: 100%; }
    div#header div.column {
        border-right: 1px solid #ccc;
        float: right;
        margin-right: 50px;
        padding: 10px;
        width: 300px;
    }
    div#header h1 { float: left; position: relative; width: 250px; }
    

    我个人觉得,现在工具这么多,写一行也行,多行也行,开心就好,比较有压缩和美化工具,写的快,好,可用性高,保证质量就好。

    5、最后一点就是不要一味的用class和id,考虑一下css的选择器。

    写的时候注意上下文,考虑一下,不要局限于当前的代码,想想会不会有其他情况,多考虑实际情况,比如设计稿上,一句话只有3个字、然后你就写了个宽度20px,不去考虑当他字多的时候的处理,那到时候数据多了(万一是十个字)就惨了。发现好多人刚学的时候都不考虑,要多提疑问,自己回答。

    2016/08/30添加 重置样式表,仅供参考Killer Collection of CSS Resets

    相关文章

      网友评论

          本文标题:提升css的五个技巧

          本文链接:https://www.haomeiwen.com/subject/eboxettx.html