CSS tips and tricks

Welcome visitors from Digg. The Blog Herald is one of the largest sites covering the blogosphere – and has been so for more than four years.

You can subscribe to our feed and visit our archives for more stories.

I’ve been writing CSS for about 2 years now and I still feel like every time I open up a blank file and begin writing CSS for a new design I learn something new.

For those of you that are new to CSS or experts always looking for a new trick, here are some of things I do on a regular basis to keep my code organized (kind of).

1. Size text without using pixels

If you’re wondering how some designers get font sizes to work using em as a unit rather than px, it’s easy. There is a trick that was written about a while ago (maybe on ALA) that resets the font sizes for the entire site so that 1.0em is the same as 10px.

body { font-size: 62.5% }

Simply throw the font-size: 62.5% bit into your body styling and you’re done. Now you can use ems to sizes your fonts rather than pixels.

So your paragraph styles might look something like this:

p { font-size: 1.2em; line-height: 1.5em; }

You might be wondering why it matters how you size fonts? Bulletproof design. Any major site needs to be able to withstand a user enlarging text (old people use the web too!), and setting absolute sizes is not good practice.

2. Make your code easy to read

When I was looking at some of the CSS coded by Rundle I noticed that he separated his heading tags nicely. It looked something like this:

h1 {}
    h1#logo { font-size: 2em; color: #000; }
h2 {}
    h2.title { font-size: 1.8em; font-weight: normal; }

Quickly scanning the CSS for the different heading tags is a breeze if you use this technique. It is also helpful if you’re sharing code or working on a large site where you are using the same heading tags (say, h2) in multiple places since you’ll be able to style each one independently and not worry about child classes inheriting attributes from the parent class.

I also use similar techniques for paragraph tags, anchor tags, and any other tag that requires multiple classes to look correct in every instance.

3. Separate code into blocks

This might be common sense to some of you but sometimes I look at CSS and it’s not broken down into “sections.” It’s easy to do an it makes working with code weeks, months, or years later much easier. You’ll have an easier time finding classes and elements that you need to change.

This is how I usuall break down my site:

/* Structure */

This is where I’d put the primary site structure divs and classes.

/* Typography */

This is where I would list things like paragraphs, headings, and other miscellaneous font styles such as small and strong tags.

/* Links */

This one is simple – all the styling for anchor tags.

/* Lists, images, etc. */

This is where I would style images, lists, and any other elements that didn’t fit into the rest of the section. If I have an unordered list for the navigation I might setup a new section for navigation and setup all the styles for the navigation, including the list and link styles, in this section – it makes editing the navigation much easier.

4. Stop using so many divs!

This has been echoed by a lot of coders and standards nuts, and while I don’t think there is anything wrong with using a lot of block level elements, I laugh a little when I see someone style their article headlines using a div rather than a heading tag. Some people even style their bylines using a div! Try using the small tag or the a span for goodness sake.

5. Style everything at once

I noticed that I was typing “margin: 0; padding: 0;” in just about every element. I remembered seeing someone use “*” to style everything on a page at once. I decided it didn’t make much sense to define margin and padding over and over when I always gave them the same parameters.

It’s easy to do:

* { margin: 0; padding: 0; }

Now you only have to define margin and padding on elements where you actually want some.

Know of any other tips or tricks? Let me know :)

In this moving age of internet many internet providers have also come into the field of web hosting. Some of these companies are working as domain reseller while others are offering complete hosting services along with the basic internet marketing program. Most of these companies offer free wireless internet connection along with their hosting packages. Some internet phone service providers are also offering ppc search engines to their clients. Along with basic hosting and SEO services some of these companies also offer the services of online training. Above all webmasters should not go after the freebies init as these are not considered as recommended servers by most of the experienced webmasters.

Tags:

This post was written by

You can visit the for a short bio, more posts, and other information about the author.

Submissions & Subscriptions

Submit the post to Reddit, StumbleUpon, Digg or Del.icio.us.

Did you like it? Then subscribe to our RSS feed!



  1. Like Your Work » Blog Archive » links for 2006-09-12September 11, 2006 at 7:30 pm
  2. CSS Resource Roundup at The Blog JointSeptember 11, 2006 at 8:49 pm
  3. By George Huff posted on September 12, 2006 at 12:49 am
    Want an avatar? Get a gravatar! • You can link to this comment

    A lot of good tips. Good post.

    I think there is an even better way to organize one’s CSS.

    http://www.tokyocube.com/css/global.css

    Ever since I stumbled on that file, I have copied it. My only addition has been adding “notes” at the top as well.

  4. Bieber Labs » links for 2006-09-12September 12, 2006 at 6:05 am
  5. TechMount » Archive » Daily Friction #145September 12, 2006 at 11:49 am
  6. CSS Best Articles - Spellbook - IT mage’s best friendSeptember 12, 2006 at 12:30 pm
  7. Poteta Blog » Archives » CSS tips and tricks!September 12, 2006 at 5:14 pm
  8. RyanBurwell » Blog Archive » CSS tips and tricks!September 13, 2006 at 12:58 pm
  9. Links for 2006-09-10 (via Cogmios.nl)September 17, 2006 at 11:05 am
  10. Advice on growing a blog at The Blog HeraldOctober 15, 2006 at 12:18 pm
  11. Inserit | Keeping things simple » Top 10 Sites for CSS Tips and TricksOctober 19, 2006 at 1:54 am
  12. Blog Vecindad Gráfica Diseño Gráfico » Blog Archive » Otra y otra lista de soporte a CSSOctober 25, 2006 at 7:36 am
  13. » CSS tips and tricks!October 25, 2006 at 2:21 pm
  14. Minimizr » Blog Archive » Minimal CSSOctober 31, 2006 at 2:35 pm
  15. Webdesigner’s Kit » Blog Archive » Top ten sites for CSS tips and tricksNovember 9, 2006 at 10:20 am
  16. ARTbird309’s Blog » Blog Archive » links for 2006-12-07December 6, 2006 at 8:49 pm
  17. www.craig-russell.co.uk » Blog » CSS TemplateJanuary 17, 2007 at 1:48 pm
  18. Free CSS Editors : lxpages.com blogMarch 29, 2007 at 12:48 pm
  19. ..: CRISTIAN ESLAVA | Diseño Gráfico / Web | Maquetación | Flash | Multimedia | 3D | Fotografía :.. » Editores CSS gratuitos | Free CSS EditorsApril 2, 2007 at 4:39 pm
  20. Jason Bartholme’s SEO Blog » Blog Archive » 101 CSS Resources to Add to Your Toolbelt of AwesomenessApril 2, 2007 at 7:08 pm
  21. Aprendiendo CSS «April 4, 2007 at 8:07 am
  22. Ultimate CSS Resources « BlogtologyApril 13, 2007 at 11:35 pm
  23. Aprendiendo Hojas de Estilo (CSS) « DiseniaMay 6, 2007 at 9:59 am
  24. 70 Expert Ideas For Better CSS Coding | Smashing MagazineMay 10, 2007 at 7:24 am
  25. Webdesign (css, grafica e altro) » Blog Archive » 70 Expert Ideas For Better CSS CodingMay 10, 2007 at 8:03 am
  26. Did you Know? » Blog Archive » 70 Expert Ideas For Better CSS CodingMay 10, 2007 at 4:04 pm
  27. Web-Design Blog » Blog Archive » 70 Expert Ideas For Better CSS CodingMay 10, 2007 at 4:07 pm
  28. CSS Best Articles | Việt Nam SEO - Quang ba websiteMay 16, 2007 at 4:51 pm
  29. CSS tips and tricks, Part 2 : The Blog HeraldMay 22, 2007 at 8:00 am
  30. 80 CSS Web Design Resources: The Killer List | Another vision of the Internet world.May 30, 2007 at 5:07 pm
  31. the designer’s pages » Blog Archive » Expert Ideas For Better CSS CodingJune 12, 2007 at 10:30 am
  32. Stefan PerssonJune 27, 2007 at 2:38 pm
  33. 101 CSS Tips, Tutorials and Examples : lxpages.com blogJuly 1, 2007 at 10:29 pm
  34. Open Source Web Design » Blog Archive » 101 CSS Tips, Tutorials and ExamplesJuly 3, 2007 at 11:18 am
  35. Open Source Web Design » Blog Archive » 70 Expert Ideas For Better CSS CodingJuly 3, 2007 at 11:20 am
  36. Más tutoriales, tips y ejemplos con CSS | Blog Vecindad Gráfica Diseño GráficoJuly 10, 2007 at 10:20 am
  37. Bleebot | Christophe Lefevre » 101 astuces CSS, tutos et examplesJuly 10, 2007 at 6:08 pm
  38. sastgroup.com » Blog Archive » 101 CSS Tips, Tutorials e esempiJuly 12, 2007 at 3:54 am
  39. dot… » 70 Expert Ideas For Better CSS CodingJuly 13, 2007 at 4:52 am
  40. 101 Tips, tutoriales y ejemplos «July 17, 2007 at 10:44 pm
  41. tips on how to blog » How To Blog -July 18, 2007 at 6:30 am
  42. Leo.锦州 - 周亮的博客 » Blog Archive » 101 个经典CSS技巧和实例July 22, 2007 at 1:00 pm
  43. 6 trucchetti per impostare file HTML o CSS più leggibili « MARCORAI’S BLOG (http://marcorai.wordpress.com)July 24, 2007 at 2:44 am
  44. 101 css tips and tutorialsJuly 25, 2007 at 2:10 pm
  45. Maria Langer, the Official Web Site* » links for 2007-08-05August 5, 2007 at 8:19 pm
  46. GC 362 Class Blog » Blog Archive » CSS for BeginnersAugust 28, 2007 at 4:57 pm
  47. CSS Specificity: Things You Should Know at UndocontrolAugust 31, 2007 at 2:35 pm
  48. tips on how to blog » How To Blog -September 5, 2007 at 8:15 am
  49. Professional Web Design Company releases Dos and Donts Guide to Great Web Design | Professional Web Design BlogSeptember 15, 2007 at 1:01 am
  50. Dos and Donts Guide to Great Web Design | SEO | SEM | Affiliate Blog by Andy HuangSeptember 19, 2007 at 1:17 am