7 CSS Layout Tips

css-tipsThe most difficult thing in CSS to get right is the layout of your site. Here are a couple of tips dealing just with that. Some of these tips are not exactly new, or rocket science, but hopefully they will save someone a bit of bother somewhere!

Tip 1
Clear out the default padding and margin settings before you start working.
Different browsers have different default margin and padding sizes so you want to start with a clean slate, so to speak. Use this command:

1
2
3
4
5
* {
margin: 0; 
padding: 0;
border: 0
}

to clear all default margin and padding settings. Also note the border, which is set to 0. Please note that if you do this, you will also get rid of the pesky purple border round click-able images, although some people argue that the purple border is necessary for accessibility and usability. But lots of people do not like the purple border round images, and this is one way that you can get rid of it in one fell swoop without having to set img border=0 for each image (which is against the strict markup rules in any case).

Tip 2
To center your layout, use a container div to contain all your content
Declare it as follows:

1
2
3
4
#container{
margin: 0 auto;
width: xxxpx
}

There are a couple of points here to take note of. DO NOT declare the width to be 100%. This defeats the whole object since you will just have to declare the sub elements within the container and then center THEM using margin : 0 auto. This is VERY BAD since it means that instead of declaring the central layout once, you will have to declare it in multiple places for each element within your container.

Tip 3
Work from the top down
Literally start working on your CSS layout starting from the top most elements in your design, as well as the ‘top’ elements in your HTML, such as the body, as well as your main containers.

Declare your CSS commands on the highest level possible and try and declare something once only and let it cascade throughout. Only override the commands at a lower level when strictly necessary. This prevents a verbose CSS file that is difficult to maintain and understand. For example, if you have { margin : 0 auto} settings on each and every sub div within your container – you are in trouble.

Tip 4
Document what you are doing and use Firebug and the Firefox browser to debug
You are not writing your CSS code just for yourself, some day some poor sod will have to debug it. Make numerous comments inside your CSS file to explain why you are doing things in a specific way.

Fitting in with this, you might find yourself having to fix someone else’s CSS more often than you think (or even your own, for that matter). Use the Firebug add-on for Firefox to debug your CSS. This is a life-saver with regards to giving you an insight into exactly where your design might be broken and why.

The only problem with this is that your design might work perfectly in Firefox, but not in IE5, IE6 or IE7. This brings us to the next tip.

Tip 5
Decide which browsers you are going to build your CSS for and test from the start
Some purists insist on making sure that your website work for all possible browsers, others only make it work for the ‘major’ browsers. How do you know exactly which browsers are used the most? Once again W3 Schools come to the rescue.

On the following page, you can see which browsers are the most popular: http://www.w3schools.com/browsers/browsers_stats.asp. From this page you can see that something like IE5 is only used by about 1.1% of browsers. It is up to you whether you consider it worthwhile to build your CSS to be compatible with this browser, or whether you are just going to test your compatibility with IE6, IE7 and Firefox, for example. Whatever you do, when you start building your CSS, start from the top, and test each and every setting in each of the browsers as you go along. There is nothing worse than building a perfect website in Firefox, then finding out right after you have coded a 1000 line css file that it is broken in IE6. To then debug and fix your code after the fact is a nightmare.

Tip 6
Here is an embarrassing little tip for fixing your CSS in IE6 or IE7
Let’s say your design works perfectly in Firefox, but is broken in IE6. You cannot use Firebug to determine where the problem might be since it WORKS in Firefox. You do not have the luxury of using Firebug in IE6, so how do you debug an IE6 or IE7 stylesheet? I often found that it helps to add {border : 1 px solid red} or {border : 1 px solid purple} to the problematic elements. This way you can often see why certain elements do not fit into the space available. It is an embarrassing little tip since it is so primitive and simple, but it works!

Tip 7
Understand floats
Floating of elements is essential to understand, especially in the context of getting your floated elements to work in the different browsers!

Basically elements such as divs are floated to the left or the right (never to the top or the bottom, only sideways). Here are a couple of things to take into consideration with floated elements. Each floated element must have an explicit width specified. If you are making use of floated divs to create a 3 column or a 2 column layout, rather specify the widths in terms of percentages rather than fixed widths, and if you do use percentages, make sure that the percentages do not add up to 100%, this will often cause the right most column to drop below the rest, clearly indicating that you are trying to fit something into the available space that is too wide for it. Rather use percentages that add up to slightly below 100%, such as 25%, 49%, 24% for a left column, middle column and right column.

Floating elements can be extremely complex to understand and it is worth while to spend some time on good sites that provide specific guidelines and tips, such as the Position Is Everything website.

Conclusion
These CSS tips for layout should hopefully save you some time and effort when you next have to panel-beat a table-less design into submission!

Article By: Christine Anderssen
Christine Anderssen is the owner of Tailormade4you, a web design and hosting company in South Africa, catering for small business website owners. Visit our site on www.tm4y.co.za/10-strategies.html to download your free guide on how to make your website really work for you and bring in more customers to your business.

Related posts:

  1. The Five Basic Elements of Web Design The basic elements and principles involved in web design are...
  2. Key Elements of a Good Website There are several factors that go into making a successful...
  3. Speed Up and Reduce Your CSS File Cascading Style Sheets otherwise known as CSS has become one...
  4. Fancy CSS Content Box Strictly for beginners only, if you are a CSS guru...
  5. 10 Simple SEO Tips It is not hard to be barking up the wrong...

23 Comments for "7 CSS Layout Tips"

  1. Achetez des sacs à main Hermès du magasin en ligne

    Reply To This Comment

    Fiona   —   October 11, 2011
  2. Good job.I like you blog!

    Reply To This Comment

    sac cuir femme   —   July 27, 2011
  3. Thanks for your share.Good luck

    Reply To This Comment

    Christian Louboutin   —   July 6, 2011
  4. It´s amazing what bark shock collar can perform to educate your pup.

    Reply To This Comment

    Dustin Knopinski   —   October 24, 2010
  5. Very good tutorial. Float is the major one of these that I still seem to do battle with every so often. The floated elements just seem to have a life of their own.
    I’ve tried to get around it by using fixed positioning – but have not found a way to do that with the {margin: 0 auto;} tag, as this requires the position to be flexible according to the width of the visible window. Do you know a workaround for this? I’ve also tried it using iframes, and importing whole sections of the site which have fixed positioning. This is very probably bad for SEO though. A.

    Reply To This Comment

    Alex @ Titanium   —   July 2, 2010
  6. I also love to see the most beautiful TV shows already aired decades back. I have given a
    thorough search in watch tv series
    online
    to see how I can get old TV shows. One option I could see is the YouTube in
    online. I was going through the YouTube site and other similar video sites to find out the olden
    days classical TV shows.

    Reply To This Comment

    Madno   —   June 10, 2010
  7. Very good information and tips! It’s very useful for me, thanks for sharing.

    Reply To This Comment

    Jimmy   —   February 9, 2010
  8. thank you for your tips for css, they are useful especially i needed tip number 2 becouse we shoundt declare 100% widith, i had trouble with that, it is really useful tips, most of them

    Reply To This Comment

    Star Config   —   January 11, 2010
  9. I am bookmarking this page. I use firefox and come across issues like these quite a bit. Thanks for your helpful tips.

    Reply To This Comment

    bankowned   —   December 22, 2009
  10. Nice tips! Really useful!

    Reply To This Comment

    Antony   —   December 21, 2009
  11. Wow nice and great tips here, although I know alot of them already its great to have them written down for refference.
    Thanks

    Reply To This Comment

    Breaking news   —   December 5, 2009
  12. Good tips you shared here on css designs. I also like the design of your blog, so cute!

    Reply To This Comment

    Lea   —   November 27, 2009
  13. thanks for your share, I will try it.

    Reply To This Comment

    shoes   —   November 27, 2009
  14. I like you tip number 6 with respect to debugging ie6 elements without the use of firbug. this would have saved me easily about an hour of trying to fix an incorrectly floated div a while back.

    Spot on list there mate.

    Reply To This Comment

    JND   —   November 22, 2009
  15. great tips here, although I know alot of them already its great to have them written down for refference.
    Thanks

    Reply To This Comment

    WDSY   —   November 4, 2009
  16. Thanks a lot for the tips, I am bookmarking this page. I use firefox and come across issues like these quite a bit.

    Reply To This Comment

    Eugene   —   October 17, 2009
  17. Very Nice Tutorial! I love it!

    Reply To This Comment

    petsafe dog doors   —   October 17, 2009
  18. This looks like a very interesting “itinerary , Lillie and I am very happy to be able to provide a destination :) )) I look forward to reading all these entries hire.

    Reply To This Comment

    escorts guide   —   October 16, 2009
  19. Awesome post. Bookmarking your site now.

    Reply To This Comment

    Thankful Visitor   —   October 9, 2009
  20. Thanks for the very informative tip. I have been meaning to get serious in mastering CSS. I have an Oreilley book on CSS. I wonder whether reading the whole book on CSS would be too overwhelming and daunting.

    Reply To This Comment

    bohol news   —   October 9, 2009
  21. Nice tips! I really need to work on my CSS skills and these will definitely point me in the right direction.

    Reply To This Comment

    godaddy.coupons   —   October 3, 2009
  22. Thanks for the useful CSS Tips.

    Reply To This Comment

    BuyNeopoints   —   September 12, 2009
  23. Great tips, CSS is one of the area’s I struggle with, thanks.

    Reply To This Comment

    Will   —   September 10, 2009

Leave a Reply





Featured Template

Connect With Me

Check out my about.me profile!

Latest Testimonial

"As the project began, Jon led me through process, and the final product is better than I could have imagined!" [ ...read more... ]

Shawna — 1/25/12