Beginner CSS Layout

February 5th, 2007 | Design & Development |

Firstly, if you don't know what CSS is then I recommend you go to W3Schools and have a look at their simple tutorials. They do a very good job of explaining a lot of the simple stuff.

When you're ready to abandon tables for layouts and embrace CSS then you here is where you want to be.

CSS has loads of little pitfalls and things you end up finding out on your own that you wish someone had told you about. I'm going to try and address some of those in my posts on CSS. First however the basics. I know you can find tutorials like this all over the web, it's just a basic layout. It's important however that you get the simple stuff right before you venture on any further.

You can copy the source for the html and css files below or download the zip here.

When you understand everything that's going on here, have a look at my more advanced CSS tutorial.

I've commented the css as much as possible, if you have any trouble please let me know.

The HTML

HTML:
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  2. <html xmlns="http://www.w3.org/1999/xhtml">
  3. <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
  4. <title>CSS Example</title>
  5. <link href="style.css" rel="stylesheet" type="text/css" />
  6. </head>
  7. <div id="border">
  8.     <div id="header">header</div>
  9.     <div id="left">left</div>
  10.     <div id="center">center</div>
  11.     <div id="right">right</div>
  12.     <div id="footer">footer</div>
  13. </div>
  14. </body>
  15. </html>

The CSS

CSS:
  1. body {
  2.     background-color: #e5e5e5;
  3.     color: #FFFFFF;
  4.     font-family: Verdana, Arial, Helvetica, sans-serif;
  5.  
  6.     font-size: 0.7em;
  7.     /*I'm using em here because it defines the base font size for my site.
  8.     all other font sizes will be defined relative to this.
  9.     i.e a h2 might be defined as twice the size of normal body text so that would be 1.4em
  10.     */
  11.  
  12.     line-height: 1.4em;
  13.     /*this sets double line spacing (0.7 * 2 = 1.4)*/
  14.  
  15.     margin-top: 0px;
  16.     /* Some browsers give the body a default top margin of about 10px, if you
  17.     want to get rid of that, set it to 0*/
  18. }
  19.  
  20. /*this style is very important when you start to float blocks around your layout.
  21. A clear element is not visible in your site, it is just used to ensure that blocks
  22. containing floating objects expand to encapsulate them properly.
  23. */
  24. .clear {
  25.     margin: 0px;
  26.     padding: 0px;
  27.     clear: both;
  28.     float: none;
  29. }
  30.  
  31. /*now to set up the elements*/
  32. #border{
  33.     width:780px;
  34.     /*
  35.     Our site is going to have a fixed with of 780px.
  36.     When setting heights and widths don't forget the "px" or they wont work.
  37.     */
  38.  
  39.     margin-left:auto;
  40.     margin-right:auto;
  41.     /*setting the left and right margin to auto will force the browser to centre the border block*/
  42.  
  43.     background-color:#FFFFFF;
  44. }
  45. #header{
  46.     height:120px;
  47.     background-color:#6699CC;
  48. }
  49. #left{
  50.     width:420px;
  51.  
  52.     height:500px;
  53.     /*
  54.     i'm setting the height just for testing purposes.
  55.     the height of this element would normally be set
  56.     by the content within.
  57.     it is possible to set the min-height for an element
  58.     but it is not supported by Internet Explorer
  59.     */
  60.  
  61.     background-color:#336699;
  62.  
  63.     float:left;
  64.     /*
  65.     A float tells an element to move as far left or right as possible until
  66.     its boundaries meet the sides of the element that contains it, or another
  67.     floating element.
  68.     This block is going to float to the far left until it meets the sides
  69.     of the border element which keeps it from going any further.
  70.     */
  71.     clear:left;
  72.     /* this makes sure nothing else can float to the left of the element */
  73. }
  74. #center{
  75.     width:180px;
  76.  
  77.     height:500px;
  78.     /*
  79.     i'm setting the height just for testing purposes.
  80.     the height of this element would normally be set
  81.     by the content within.
  82.     it is possible to set the min-height for an element
  83.     but it is not supported by Internet Explorer
  84.     */
  85.  
  86.     background-color:#3366bb;
  87.  
  88.     float:left;
  89.     clear:none;
  90.  
  91. }
  92. #right{
  93.     width:180px;
  94.  
  95.     height:500px;
  96.     /*
  97.     i'm setting the height just for testing purposes.
  98.     the height of this element would normally be set
  99.     by the content within.
  100.     it is possible to set the min-height for an element
  101.     but it is not supported by Internet Explorer
  102.     */
  103.  
  104.     float:right;
  105.     clear:right;
  106.     background-color:#3366dd;
  107. }
  108. #footer {
  109.     height: 60px;
  110.     background-color: #CCCCCC;
  111.     color: #333333;
  112.     /*
  113.     the following float and clear make the footer block shift to below
  114.     the previous floating elements.
  115.     */
  116.     clear: both;
  117.     float: none;
  118. }


One Response to “Beginner CSS Layout”

  1. Advanced CSS Layout | 1.21 Gigawatts! Says:

    [...] Beginner CSS Layout [...]

Leave a Reply