`
love~ruby+rails
  • 浏览: 849670 次
  • 性别: Icon_minigender_1
  • 来自: lanzhou
社区版块
存档分类
最新评论

5 Rules To Write More Readable CSS Files

阅读更多

1. Order CSS properties alphabetically

When you start adding properties to a CSS element, probably you don’t pay attention to the order you are using to list them. Without doubt, a better approach to proceed is to use alphabetical order like in the following example:

#nav{
    border: solid 1px #DEDEDE;
    color: #000;
    padding: 10px;
    width: 650px;
} 

In this way, if you need to change a specific properties, will be simpler to find it at a glance.

2. Indent child elements

Indenting child elements of a main element (in this example #nav ) is a good way to highlight dependencies between related portions of code.

#nav{
    width:650px;
}
    #nav ul li{
        float:left;
    }
        #nav ul li a{
            display:block;
        }

3. Use comments to separate logical sections of code

Comment lines are really useful to improve code readability because, if used with certain criteria (and not abused), can help you separate sections of CSS code related to the structure of the HTML document:

/*-------------------
HEADER 
------------------- */
#header{width:650px;}
    #header a:link, 
    #header a:visited{
        color:#0033CC;
    }

/*-------------------
NAVIGATION BAR
------------------- */
#nav{width:650px;}
    #nav ul li{
        float:left;
        padding:0 10px;
}

4. Use extra spaces and tabulations to separate single properties from their values

I think this way to organize CSS code, by using tabulations to separate properties from their values, noticeably improves the readability of CSS files but in general is rarely used:

#main{
    width: 650px;
}
    #main h1{
        color:          #000;
        font-size:      22px;
        font-weight:    bold;
    }
    #main p{
        color:          #000;
        font-size:      12px;
        padding:        10px;
    }

Group elements with the same properties

If you have a group of element with the same properties and values you can think to group them in a single declaration and manage separately their difference in order to optimize your code. For example take a look at the code below:

.icon-facebook {
   background:url(facebook.gif);
    padding-left:  26px;
    margin-right:  20px;
    width:        100px;
}
.icon-twitter {
   background:url(twitter.gif);
    padding-left:  26px;
    margin-right:  20px;
    width:        100px;
}
.icon-delicious {
   background:url(delicious.gif);
    padding-left:  26px;
    margin-right:  20px;
    width:        100px;
}

You can improve the previous code in this way:

.icon-facebook,
.icon-twitter,
.icon-delicious {
    padding-left:  26px;
    margin-right:  20px;
    width:        100px;
}
.icon-facebook{background:url(facebook.gif);}
.icon-twitter{background:url(twitter.gif);}
.icon-delicious{background:url(delicious.gif);}

分享到:
评论

相关推荐

    更多可用和灵活的yolov5_带更多键盘的yolov5_More_readable_and_fle

    更多可用和灵活的yolov5_带更多键盘的yolov5_More_readable_and_flexible_yolov5_with_more_backbo_flexible-yolov5

    How.to.Use.Objects.Code.and.Concepts.0321995546

    Applying language idioms and connotations to write more readable and maintainable code Using design-by-contract to write code that consistently does what it’s supposed to do Coding and architecting ...

    Simplifying [removed] Writing Modern JavaScript with ES5, ES6, and Beyond pdf

    Learn to write modern JavaScript not by memorizing a list of new syntax, but with practical examples of how syntax changes can make code more expressive. Starting from variable declarations that ...

    英文原版-Functional PHP 1st Edition

    What you will learn Declare functions/callables and find out how to manipulate and call them Write pure functions to get more robust code that can be easily tested Learn to compose function using ...

    前端开源库-readable-error

    在前端开发中,错误处理是不可或缺的一部分,而`readable-error`是一个专为前端开源库设计的可读错误工具。这个库的目的是提供一种方式,使得在应用中产生的错误信息更加清晰、易读,便于开发者理解和调试。在本文中...

    创建可读段落插件Readable.zip

    link rel="stylesheet" href="readable/readable.css">  </head>  <body> <!-- your website here -->  <!-- readable scripts --> [removed][removed] [removed]...

    Functional PHP

    Write pure functions to get more robust code that can be easily tested Learn to compose function using various techniques Use a functional approach to find readable solutions to common issues Utilize ...

    LINQ to Objects Using C# 4.0: Using and Extending LINQ to Objects and Parallel LINQ

    Using LINQ to Objects, .NET developers can write queries over object collections with the same deep functionality that was once available only with SQL and relational databases. Now, for the first ...

    Less Web Development Cookbook

    Less is a dynamic style sheet language to help you make your CSS code more maintainable, readable, and reusable. It provides impressive features to enhance your web development skills with complex ...

    Parsing JSON in Swift

    - Simple strategies you can use to make your JSON deserialization more concise, readable, and elegant - Whether to return nil, empty, or throw an error when transforming JSON into an array of model ...

    Fluent Python

    By taking you through Python’s key language features and libraries, this practical book shows you how to make your code shorter, faster, and more readable all at the same time—what experts consider...

    Setupimgburn_2.7.7.0.exe

    Build - Create an image file from files on your computer or network - or you can write the files directly to a disc Write - Write an image file to a disc Verify - Check a disc is 100% readable. ...

    fluent python

    By taking you through Python’s key language features and libraries, this practical book shows you how to make your code shorter, faster, and more readable all at the same time—what experts consider...

    SystemVerilog Assertions - Design Tricks and SVA Bind Files_pres

    For example, using shorthand operators and removing unnecessary parentheses can make assertions more readable. Additionally, consistent formatting and indentation can help in quickly identifying the ...

    vs 2005 10款插件

    Test-driven development is the practice of writing unit tests before you write code, and then writing the code to make those tests pass. By writing tests before you write code, you identify the exact ...

Global site tag (gtag.js) - Google Analytics