Flag This Hub

CSS Tutorial - Learn CSS - website layout

By


In this css tutorial i'm going to show how to layout your websites using the simple float technique with css. This will replace any need for tables or absolute positioning.

Today there are very few html'ers who use tables to lay out their websites. There are many different ways to layout your website with css, i'm going to show you the easiest and most browser compatible way of doing so.

Floating

So lets start with some floating.

We want to create a div that will hold our header, content, footer - here's how.

We will call our header #header (original - i know) We want this header to appear at the top of the page and centered in the page. For this the first thing we need to do is create a wrapper for the header, content and footer divs. The wrapper has just 2 declarations. Margin and width. This is how it will look:

#wrapper{margin: auto; width: 900px}

Margin auto will put an equal margin on either side of your div cause it do appear in the middle of your page. You can make the width whatever you like. These are the only 2 declarations you should have on your wrapper div, in the html all your divs will go inside the wrapper div.

Now we'll create our header div.

#header{
float: left;
width: 900px;
background-color: blue;
height: 200px;
}

That's all you need for the header. The float: left effectively aligns the div to the left and anything next to the header div will wrap around the div, however because the header div is 900px wide there is no space for anything to wrap around next to the div, so anything that is next to the div will go onto the next line.

Next is the content and footer divs, these will be virtually identical to the header div but we'll change the background colour for clarity.

#content{
float: left;
width: 900px;
background-color: red;
height: 400px;
}

#footer{
float: left;
width: 900px;
background-color: green;
height: 200px;
}

There you have it. Below is your html code and a picture of what your page should look like thus far.

<div id="wrapper">
	
    <div id="header"></div>
    
	<div id="content"></div>
        
	<div id="footer"></div>

</div>
See all 3 photos

 Not exactly the Sistine Chapel but we'll get there.

Next we're going to split the content into a 2 columns, we'll have some content in the left and some content in the right. So lets start by creating our content left and then the content right.

#content-left{
float: left;
width: 200px;
background-color: black;
height: auto;
}

#content-right{
float: left;
width: 700px;
height: auto;
color: white;
}

Notice how we've made the height of the left div by making it an auto height. This means that when we fill it with content the div will grow to accomodate the amount of content inside. Also notice how in the right div, we've left the background colour out, this means the content div's colour will show through which is red. Because we have floated both divs left they jut up next to each other because there is enough space for both of them.

We've also added some content to the left div to give it some height, we've coloured the text with color: white; so it stands out from the black background. You can do this with by styling your p tag or you can do it within the div.

Below is what your html page will now look like:

 Now we'll add some padding to the text in the left div and also add some content to the right div. Here's is the css, after i'll explain what it all means.

#content-right img{
padding: 3px;
float: left;	
}

.padding{
padding: 10px;	
}

#logo{
float: right;
padding: 20px;
font-size: 18pt;
color: white;
font-weight: bold;
}

 You can see the first line of css #content-right img - This targets all html img's inside content-right. So we've added some padding around the image and we've floated the image left. You'll notice in the image how the text wraps around the floating image.

Next is .padding - This adds padding from the edges of the divs that .padding is inside of. Thus pushing the text away from the edges.

We've also added the css for our logo which goes in the header, we've float the logo to the right of the div and added some padding, font size and font weight. You could have an image in here in which case you wouldn't need to specify font, color, weight.

Below you will see the html code for the page and then an image of what your page should be looking like:

<div id="wrapper">
	
    <div id="header">
    	<div id="logo">Logo here</div>
    
    </div>
    
	<div id="content">
    
    <div id="content-left">
    <div class="padding">
    This is some text. This is some text. This is some text. This is some text. This is some text. This is some text. This is some text. This is some text. This is some text. This is some text. This is some text. 
    </div>
    
    </div>
    
    <div id="content-right">
    	 <div class="padding">
    <img src="image.jpg" width="141" height="95" />This is some text. This is some text. This is some text. This is some text. This is some text. This is some text. This is some text. This is some text. This is some text. This is some text. This is some text. 
     This is some text. This is some text. This is some text. This is some text. This is some text. This is some text. This is some text. This is some text. This is some text. This is some text. This is some text. 
      This is some text. This is some text. This is some text. This is some text. This is some text. This is some text. This is some text. This is some text. This is some text. This is some text. This is some text. 
      This is some text. This is some text. This is some text. This is some text. This is some text. This is some text. This is some text. This is some text. This is some text. This is some text. This is some text. </div>
    
    
    </div>
    
    
    </div>
        
	<div id="footer"></div>

</div>

And that is the simplest way of laying out your website. This is the basic knowledge you need for laying out any element with the float property. I hope you have found this article useful. If you are in need of a website then please visit Artisan Websites.

  • Ebook reader vs paper books

    Today ebook readers are becoming more and more popular but it remains to be seen as to whether they are the preferred medium for reading. Below I will argue for both....Let the debate begin! The... - 11 months ago

  • CSS Tutorial - Learn CSS - CSS3 Drop Shadow

    With CSS3 now in it's 4th year, it's becoming more and more common and compatible with upgrades to the major browsers. In this article i'm going to give you a tutorial on how to implement drop... - 12 months ago

  • Honeymoon - Phuket

    The next best thing after getting married is going away to a world that's perfect in every way! This is what my partner and I did. We went to Thailand for our honeymoon in Phuket, somewhere we had... - 13 months ago

  • Mobile Websites

    These days, it's becoming more and more popular for people to browse the internet from their phone and as soon as a site takes to long to load they give up. This is why businesses are creating mobile... - 13 months ago

CSS: The Missing Manual
Amazon Price: $18.00
List Price: $34.99
The Book of CSS3: A Developer's Guide to the Future of Web Design
Amazon Price: $18.86
List Price: $34.95
HTML and CSS: Design and Build Websites
Amazon Price: $15.08
List Price: $29.99
CSS: The Missing Manual
Amazon Price: $27.99
CSS: The Definitive Guide
Amazon Price: $22.55
List Price: $44.99

Comments

No comments yet.

Submit a Comment
Members and Guests

Sign in or sign up and post using a hubpages account.



    Like this Hub?
    Please wait working