|
The most difficult thing in CSS to get right is the layout of your site. Here are a couple of tips dealing just with that.
Tip 1 : Clear out the default padding and margin settings before you start working. Different browsers have different default margins so you want to start with a clean slate, so to speak. Use this command:
*
{
margin: 0;
padding: 0;
border: 0;
}
to clear all margins. 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 clickable images, although some people argue that the purple border is neccessary 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.
Tip 2 : To center your layout, use a container div to contain all your content. Declare it as follows:
#container
{
margin: 0 auto;
width: xyzpx;
}
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.
Tip 3 : Work from the top down, and 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 neccessary. 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. 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 thefact is a nightmare.
Tip 6 : Here is an embarrasing 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 seewhy certain elements do not fit into the space available. It is an embarrasing 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. A good page with lots of links to relevant other pages is:
http://www.smashingmagazine.com/2007/05/01/css-float-theory-things-you-should-know/
or
http://www.positioniseverything.net/
Tip 8 : Faux columns. One of the frustrating things about using DIVs for layout is that if you are trying to create a page with different coloured columns you will end up with the situation where the one column might be longer than the other since a div is only as big as the content in it, and you might have a short left-hand menu column with a longer column on the right containing your content. This could result in something like this:
To fix this so that the blue column extends all the way to the bottom is to tile a thin slice of image vertically in the body.
The image that you would tile would be 1 or 2 pixles high, and would look like this (excluding the grey backgound - and also note, this is not to scale!):
The command would then be something like this:
body
{
background: #ffffff url(../images/background.gif) repeat-y 0 0;
}
where you are specifying that the image should repeat downwards. This wil make the left-most column (blue) float downwards to fit in with the longest content. You can then float a left column div over the image.
|