5 tips to write better CSS

by Omar Abid on August 12, 2009 · 0 comments

in Web Development

CSS is an easy to learn and use language, but working with it in real world problems can be a little challenging especially for newbie. The main cause of trouble is different browser support and rendering for CSS.

  1. Tip 1: Learn how to target IE
  2. IE is known for its’ bad support for the CSS and HTML standards, however 60% of people use it. So it needs a special care which means a special style sheet for it.
    In your page “head”, you can target IE browsers by this tag


    <!--[if IE]>
    <link rel="stylesheet" href="ie.css" type="text/css" />
    <![endif]-->

    Nevertheless, this doesn’t always work. Why? Because IE6 and IE7 are also different, so you need to target each one apart.
    Target Version 6 only:


    <!--[if IE 6]>
    <link rel="stylesheet" href="ie6.css" type="text/css" />
    <![endif]-->

    If you have tweaks for earlier versions of IE, don’t target each one alone, you can target less than IE X


    <!--[if lt IE 7]>
    <link rel="stylesheet" href="ie6.css" type="text/css" />
    <![endif]-->

    And more than IE X


    <!--[if gte IE 7]>
    <link rel="stylesheet" href="ie7.css" type="text/css" />
    <![endif]-->

  3. Tip 2: Use a CSS reset
  4. Browsers have default values for margin, padding. But each browser has its’ default settings.

    To avoid being confused (especially when using margin and padding values), you’d better use a reset to set all values to default.

    Some Popular CSS resets:
    MeyerWeb
    YUI CSS reset

  5. Tip 3: Make your content printable
  6. What about if a user liked a page and desired to print it and read it offline instead?
    Will it print all those widgets, logos and other carps you did add to your website?

    CSS is flexible and offers a way to target other media than screen, like print, phones…

    To include the style in a style sheet:


    @import url("fancyfonts.css") screen;
    @media print {
    /* style sheet for print goes here */
    }

    In a HTML page


    <LINK REL="stylesheet" TYPE="text/css" MEDIA="print, handheld" HREF="foo.css">

    For more information, visit W3C

  7. Tip 4: Use an advanced CSS editor
  8. Get yourself advanced CSS editors and some other professional plug-in.
    Most popular and powerful ones are
    FireBug
    Firebug: a Firefox plug-in for debugging HTML, CSS and JavaScript.
    Stylizer
    Stylizer: not very popular but you are very unlikely to try another if you explored all its’ functionalities.

  9. Tip 5: Compress Vs. lucid your code
  10. When writing, reading and debugging CSS, it’s better to use understandable code, like this.


    Body
    {
    Color: #121212;
    }

    When publishing your website, compressing your code can save you few bits (by suppressing spaces, comments…): up to 40%.


    Body { color: #121212; }

    Clean CSS is a great service that can both compress and uncompress your code.

     

Interested in more tips to improve your coding skills?

53 CSS techniques (Smashing Magazine)
CSS tips and tricks (Blog Herald)
101 CSS techniques (Noupe)

{ 0 comments… add one now }

Leave a Comment

You can use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Previous post: Creating a subscribe button with CSS

Next post: Creating an AJAX Web Form from scratch with Jquery and PHP