Skip to main content

Tips to Write Better CSS Code



CSS is a language that is not difficult to master, but if you use it for a large project, it can be very difficult to manage if you do not follow a defined approach while writing CSS code. Here are few tips that will help you write better and easy to manage CSS code.

1. Don’t Use Global Reset

Using global reset to remove default margin and padding from all HTML elements is a strict no-no. Not only it is slow and inefficient way but you’ll have to define margin and padding for each element that needs it. Instead use subset of CSS Resets like one from Eric Meyer.
Not Good
  1. *{ margin:0; padding:0; }  
Better
  1. html, body, div, dl, dt, dd, ul,  h1, h2, h3,  pre, form, label, fieldset, input, p, blockquote, th, td { margin:0; padding:0 }  
  2. table { border-collapse:collapseborder-spacing:0 }  
  3. fieldset, img { border:0 }  
  4. ul { list-style:none }  

2. Do not use IE Hacks

Though CSS hacks might be useful to maintain consistent look of the website over older browsers like IE6, but they can be problematic for newer versions of IE as newer versions like IE8 do support CSS standards to a good level and using hacks might break out the layout. You should use conditional statements instead to target specific versions of Internet Explorer.
For example, using the below lines of code within your <head> tag will load the iestyles.css file only when browser is Internet Explorer version 6 or less.
  1. <!--[if lte IE 6]>  
  2. <link rel="stylesheet" type="text/css" href="styles/ie-styles.css" />  
  3. <![endif]-->  
For information on conditional comments, refer to the quirksmode article on CSS Conditional Comments

3. Use Meaningful names for IDs and Classes

Suppose you define your sidebar styles using class .leftbox and after some redesign, you float it to right, then would it be meaningful to have leftbox as name for right floated box. You should put some thought before declaring classes and IDs for elements so that they are meaningful and easy to understand later.

4. Utilize CSS Inheritance

If multiple child elements of a parent element use same styles on your web page, it will be better to define them for their parent element and let the CSS inheritance do all the work. You’ll be able to easily update your code later and it’ll also reduce the CSS file size considerably.
Not Good
  1. #container li{ font-family:Georgia, serif; }  
  2. #container p{ font-family:Georgia, serif; }  
  3. #container h1{font-family:Georgia, serif; }  
Better
  1. #container{ font-family:Georgia, serif; }  

5. Combine multiple Selectors

You can combine multiple CSS selectors into one if they have common style definitions. It’ll save you time and space.
Not Good
  1. h1{ font-family:ArialHelveticasans-seriffont-weight:normal; }  
  2. h2{ font-family:ArialHelveticasans-seriffont-weight:normal; }  
  3. h3{ font-family:ArialHelveticasans-seriffont-weight:normal; }  
Better
  1. h1, h2, h3{ font-family:ArialHelveticasans-seriffont-weight:normal; }  

6. Use Shorthand Properties

Utilize the shorthand properties of CSS to quickly write CSS code and reduce file size. Shorthand notation can be used for margin, padding, border, font, background and also for color values.
Not Good
  1. li{  
  2.     font-family:ArialHelveticasans-serif;  
  3.     font-size: 1.2em;  
  4.     line-height: 1.4em;  
  5.     padding-top:5px;  
  6.     padding-bottom:10px;  
  7.     padding-left:5px;  
  8. }  
Better
  1. li{  
  2.     font: 1.2em/1.4em ArialHelveticasans-serif;  
  3.     padding:5px 0 10px 5px;  
  4. }  
Here’s a complete guide to CSS shorthand properties.

7. Organize CSS Code

Organizing your CSS code in a certain pattern will make it easier to find things at later time and save you a lot of time looking for a specific style definition.
Here is a sample organization that you may use:
  1. /*------------------------- 
  2.     CSS Reset 
  3. -------------------------*/  
  4.   
  5. /*------------------------- 
  6.     Generic Classes 
  7. -------------------------*/  
  8.   
  9. /*------------------------- 
  10.     Layout styles 
  11. -------------------------*/  
  12.   
  13. /*------------------------- 
  14.     Section specific styles 
  15. -------------------------*/  

8. Make CSS Readable

Writing readable CSS will make it easier to find and update a style declaration later. Either group all styles for a selector in one line or each style in its own line with proper indentation. You can also combine these two techniques together.
  1. /*------------------------ 
  2.     Each Style on new line 
  3.     ---------------------*/  
  4. div{  
  5.     background-color:#3399cc;  
  6.     color:#666;  
  7.     font: 1.2em/1.4em ArialHelveticasans-serif;  
  8.     height:300px;  
  9.     margin:10px 5px;  
  10.     padding:5px 0 10px 5px;  
  11.     width:30%;  
  12.     z-index:10;  
  13. }  
  14.   
  15. /*------------------------ 
  16.     All Styles on one line 
  17.     ---------------------*/  
  18. div{ background-color:#3399cccolor:#666font: 1.2em/1.4em ArialHelveticasans-serif;  height:300pxmargin:10px 5pxpadding:5px 0 10px 5pxwidth:30%; z-index:10; }  

9. Add proper Comments

Comments can be used to separate different sections of CSS code
  1. /*-------------------- 
  2.     Header 
  3.     -----------------*/  
  4. #headerheight:145pxposition:relative; }  
  5. #header h1{ width:324pxmargin:45px 0 0 20pxfloat:left;  height:72px;}  
  6.   
  7. /*-------------------- 
  8.     Content 
  9.     -----------------*/  
  10. #content{ background:#fffwidth:650pxfloat:leftmin-height:600pxoverflow:hidden;}  
  11. #content .posts{ overflow:hidden; }  
  12. #content .recent{ margin-bottom:20pxborder-bottom:1px solid #f3f3f3position:relativeoverflow:hidden; }  
  13.   
  14. /*-------------------- 
  15.     Footer 
  16.     -----------------*/  
  17. #footerclear:bothpadding:50px 5px 0; overflow:hidden;}  
  18. #footer h4{ color:#b99d7ffont-family:ArialHelveticasans-seriffont-size:1.1em; }  

10. Order CSS Properties Alphabetically

This might be a difficult way to write CSS but it’ll make it easier for you to find out any property easily at a later stage.
  1. div{  
  2.     background-color:#3399cc;  
  3.     color:  #666;  
  4.     font:   1.2em/1.4em ArialHelveticasans-serif;  
  5.     height300px;  
  6.     margin10px 5px;  
  7.     padding:5px 0 10px 5px;  
  8.     width:  30%;  
  9.     z-index:10;  
  10. }  

11. Use External Stylesheets

It is always a good design practice to separate content from presentation. Place all of your CSS code into external stylesheets and use the <link> to reference stylesheets within a web page. By placing CSS into external files, you can easily update your styles later at one place instead of looking into html templates or files for styles.
Not Good
  1. <style type="text/css" >  
  2.     #container{ .. }  
  3.     #sidebar{ .. }  
  4. </style>  
  5.   
  6. OR  
  7.   
  8. <li style="font-family:Arial, helvetica, sans-serif; color:#666; " >  
Better
  1. <link rel="stylesheet" type="text/css" href="css/styles.css" />  

12. Split CSS into multiple files

If you are working on a large web project that has multiple modules, each with different set of styles and looks, it will be better to split your CSS files into multiple files based on the module they are applied to. You can separate stylesheets like, one for reset, one for layout, one for generic classes and one for module specific styles. This technique will let you organize your code easily in a large project but loading multiple CSS files means more HTTP requests and slower loading time, this is where Bridging CSS files come to rescue. Create a separate CSS file and import other CSS files into it.
  1. @import "style/css/reset.css";  
  2. @import "style/css/typography.css";  
  3. @import "style/css/layout.css";  

13. Compress CSS code

Once you are ready to deploy the web design project, compress your CSS code using tools likeCSS Compressor to reduce file size and improve loading time of webpage.

14. Be Consistent in Writing CSS

When you work on multiple web development projects, it’ll be a wise decision to choose a particular way of organizing and formatting your CSS code and stick to that way for all your projects.
I hope these tips will help you write better and manageable CSS code. If you would like to share a tip or two, feel free to add your comment below.

Comments

Popular posts from this blog

உடல் எடையை குறைக்க வேண்டுமா ?

இன்றைய அவசர உலகின் மிக பெரிய பிரச்சனையாக இருப்பது உடல் எடை அதிகரிப்பது தான்.மனம் போன போக்கில் உணவு கட்டு பாடு இல்லாமல் கண்டதையும் உள்ளே தள்ளுவதும்,உக்காந்த இடத்திலேயே கணணி முன் நேரத்தை விரயமாக்குவதும் தான் இந்த பிரச்சனைக்கு மூல காரணமாகும். அது சரி இந்த பிரச்சனையை எப்படி இல்லாமல் செய்வது அல்லது உடல் எடையை எவ்வாறு குறைப்பது என்பதை பற்றி பாப்போம் , பல வருட ஆரய்சிக்குபின் மருத்துவர்கள்   உடல் எடைய குறைக்க மிகவும் சுலபமான உடற்பயிற்சியை கண்டுபிடித்துள்ளனர்.இது  100% பயனளிக்க கூடியது, எந்த இடத்திலும் எந்தநேரத்திலும் மிக சுலபமா செய்ய கூடிய உடற் பயிற்சியாகும்.இந்த உடற்பயிற்சிகள் படத்துடன் கீழே தரப்பட்டுள்ளது நீங்களும் முயற்சித்து பாருங்கள கண்டிப்பாக பலன் கிடைக்கும்... முதலில் நாற்காலியில் உட்கார்ந்து இட  பக்கம் பார்கவும் .. ..        அடுத்து  நாற்காலியில் உட்கார்ந்து வல  பக்கம் பார்கவும்  ....  நண்பர்கள் யாரவது மச்சி வாடா சின்ன பீஸ் ,இங்க பாரு சூப்பர் அய்டம்னு சொல்லி கால்ல விழுந்து கூப்பிட்டலோ மேற்கூறிய உடற் பயிற்சிகளை முயற்சித்து பார்கவும் கண்டிப்பாக பலன் கிடைக்கும் .

38 (new) web tools to keep you busy

For many of us, the internet represents our daily job, income resource or biggest hobby. Every day we check our emails, read our feeds, visit our websites, find and discuss new things and GOD knows what else. It requires a lot of tools to do all this stuff and sometimes, we forget to search for easier solutions losing valuable time or keeping down the production graph. It's very hard to keep track with everything that's new and popular and this is why we do monthly searches for the best web tools out there. Enjoy!  45+ Web Operating Systems "There are many more web operating systems hoping to bring all your usual desktop applications online in one place - here are more than 45 of our favorites."  15 Ways To Create Website Screenshots "15 Ways To Create Website Screenshots"  Open Source Windows "The promise of open source software is best quality, flexibility and reliability. The only way to have TRUE "Open Source Windows"is

40 Fresh & Beautiful Examples of Websites With Large Backgrounds

Using large sized pictures or illustrations as your website’s background adds a great visual appeal to your website’s design. Many web designers use large images as backgrounds as more and more users are now opting for high resolution monitors and high speed internet connections. Here’s a showcase of 40 Fresh and amazing websites that are using large background images. 1.  The Pixel Blog 2.  Copimaj Interactive 3.  Flourish Web Design 4.  Abduction Lamp 5.  Morphix Design Studio 6.  Final Phase 7.  Make Photoshop Faster 8.  WebSarga 9.  Suie Paparude 10.  Duirwaigh Studios 11.  BlackMoon Design 12.  Sepitra 13.  Le Blog de Gruny 14.  Piipe 15.  Mozi Design Studio 16.  Electric Current 17.  Lora Bay Golf 18.  Life Style Sports 19.  ligne triez 20.  Oliver Kavanagh 21.  World of Merix Studio 22.  Le Web Defi 23.  How host 24.  Productive Dreams 25.  Gary Birnie 26.  Glocal Ventures 27.  GDR UK 28.  Absolute Bica 29.  Le Nordik 30.  Leaf Tea Shop & Bar 31.  Paul Smith 32.  EwingCole