7 Tips to Clean CSS You May or May Not Know

  1. Clean VS. Messy Code – If you are making a CSS intensive site, sitting and staring at a mess of code can rack your brain. However, if you use comments, you can make it easier for yourself, and/or other people.
  2. Comments – Comments can easily explain code that you used to anybody viewing the code, and are not valid code. For instance, if you wanted to let people know the color codes for different Hexadecmial Values you can make a “Guide” using comments. An example of this would be something like

    /************************************************
    *************************************************
    COLOR GUIDE

    # Dark Grey (Text): #333
    # Dark Blue (Headings, links): #000066
    # Mid Blue (Header): #333399
    # Light Blue (Top Navagation): #CCCCFF
    # Mid Grey: #666

    *************************************************
    *************************************************/

    This is obvious to the reader but the browser does not “see it”.

  3. Shorthand – If you have have to change five or so lines of code just for thing to happen, it may frustrate you. That is why depending on what you are styling, you can just go to one line of code and change what you want. For Example: Instead of typing

    body {
    margin-top: 20px;
    margin-right: 20px;
    margin-bottom: 20px;
    margin-left: 20px;
    }

    You can type:

    body {
    margin: 20px 20px 20px 20px;
    }

  4. Master CSS – To save loading time, you can create a Master CSS file. What this allows you to do is import multiple CSS files to one and use only that in your HTML. This way, the page only has to load one file as apposed to countless numbers of them.
  5.  Structure Template – If you are planning on making many websites using a similar design, you may want to use a template to reduce coding time, if you have already coded it.
  6. Unnecessary Line Breaks – If you have too much white space, it will take longer for the page to load, and is actually not as clean as less white space. If you only have one expression in a set of brackets, just use one line.
  7. Remove Unused Selectors – If you forget about a selector, or refer to it somewhere else, you should delete it. It causes the page to load slower and uses unneeded lines of code that may or may not do anything.

 

Also from watching the CSS Visual Optimization course on Lynda.com, I leaned that I need to comment more and use less white space.

Leave a Reply

Your email address will not be published. Required fields are marked *