Webdesign, chapter four: CSS part three: replacing navigation frames

Everyone wants a navigation frame on the left, but everyone knows that frames have been outlawed…. or frowned upon anyway. Now you could use inline frames, which have not been obsoleted yet, but i think for the navigation of a website CSS (and a bit of php) works better.

Replacing the frames is pretty simple, but it would require us to put the navigation into every single page of the website…. and i will show you how ! I will later show you how to make it so you have a separate file for your navigation, just like you would when using frames.
For now, we just use a bit of CSS to make things appear where we want it to, which is navigation on the left and the main content on the right.

CSS has a few different ways of defining position: absolute, fixed, relative, static, and inherit. Inherit just inherits the position from the parent element. Static would place the element where the browser would ordinarily put it, in the flow of the website. Relative specifies where the element would be relative to the parent (you can move it to the left, right, up, down, relative to the parent element). Fixed positioned elements will stay where you put them, even if the website gets scrolled, even if an element would be covered by scrolling, the fixed element will stay right where you want it to be. Absolute position would put an element at a defined position relative to the first element that has a position other than static, if there aren’t any, the html tag is the one that will be picked.

So with that we’re obviously going to separate our website into 2 different areas with fixed positioning. The left one will be 15% wide and the main one will be 84% wide, with 1% left as a separator.

We’re going to use an external style sheet for that which will look like this (for now (saved in post4.css)):
body {color: #08f; background: #fff; font-size: 100%}
h1 {text-align: center; margin: 0;}
div.sidebar-right {position: fixed; top: 0; left: 0; width: 15%; height: 100%; background-color: #00b;}
div.main {position: fixed; top: 0%; left: 16%; width: 84%; height: 100%; background-color: #000;}

and the html for the page looks like this (viewable here):
<title>Webdesign: chapter four</title>
<link rel=”stylesheet” type=”text/css” href=”post4.css” />
<div class=”sidebar-right”>
<p>link to something</p></div>
<div class=”main”>
<h1>Hi There !</h1>
<p>some text</p>

obviously the colors aren’t all that great yet, but it’s reasonably good for demonstration purposes. The body itself has a background color of fff (white), which is then covered by 00b (reasonably dark blue) on the left and 000 (black) on the right, leaving a 1% separator between the navigation and the main content, which is not covered and thus white.

Leave a Reply