XML technologies / CSS / Advanced websites with CSS / A three-column layout / Adding a third column / The new sidebar in its new position
(Excerpt from “CSS − Advanced websites with Cascading Stylesheets” by Rachel Andrew & Dan Shafer)
Now we can move the new sidebar into position with CSS and style its content. Start with the two-column stylesheet and find the rules for the element #content. First, make room for the sidebar.
Check the first sidebar’s position:
In Repositioning the sidebar, we showed how changing just two CSS properties moves a column from right to left. For our present layout, we assume that sidebar is on the right of the main area. You may first need to undo those stylesheet changes.
#content {
margin: 0 240px 0 160px;
border: 1px solid #b9d2e3;
background-color: white;
color: black;
}Change the left margin width from 0 to 160px, creating a 160-pixel margin for the main area.
Now define a rule for #sidebar2 to move it into the new margin to the main area’s left.
#sidebar2 {
position: absolute;
top: 0;
left: 0;
}This places the new column at the top left of the relatively positioned div-container with ID #main, as shown in the next figure.
Figure: The page after positioning sidebar2.
The content of sidebar2 now overlaps the element div with ID content, as you probably expected. Both sidebars overlay the space created by the margins of the element content.
A sidebar wider than its margin would cover the main content. Prevent this by specifying a width for sidebar2:
#sidebar2 {
position: absolute;
top: 0;
left: 0;
width: 159px;
border-top: 1px solid #b9d2e3;
border-left: 1px solid #b9d2e3;
border-bottom: 1px solid #b9d2e3;
background-color: white;
color: black;
margin: 0;
padding: 0;
}I have already started styling this column: top, left and bottom borders, plus a white background. The following figure shows progress so far.
Figure: Fixed width for sidebar2 prevents overlaps.
Now style the individual elements inside sidebar2.
#sidebar2 .inner {
margin: 10px;
} #sidebar2 .inner provides a container for the column’s contents, all of class inner, leaving ten pixels between content and border.
#sidebar2 p {
font-size: 90%;
color: #666666;
}
#sidebar2 a:link, #sidebar2 a:visited {
color: #245185;
font-weight: bold;
}Also style the paragraphs inside #sidebar2: reduce text to 90% and make it dark gray. Links inside the element div with ID #sidebar2 should be blue and bold.
#sidebar2 h3 {
color: #245185;
padding-bottom: 0.2em;
border-bottom: 1px solid #b9d2e3;
font-size: 110%;
}The weblog and newsletter headings are level-three headings, blue and underlined by a border. The following figure shows these changes.
Figure: The page after styling the text blocks.
Navigation
A list of links appears at the new column’s top. Define rules to make it fit our design better.
Hide the bullets and remove default indentation so entries align with the remaining text:
#nav {
list-style: none;
margin: 0;
padding: 0;
}Give each entry a bottom border and padding to separate entries from one another and from their borders:
#nav li {
border-bottom: 1px solid #b9d2e3;
padding: 0.4em 0 0.2em 0;
font-size: 90%;
}Finally, remove link underlining and set font weight to normal; previously, all links in #sidebar2 were bold. The next figure shows the result.
#nav li a:link, #nav li a:visited {
text-decoration: none;
color: #245185;
font-weight: normal;
}Figure: The styled navigation.
The weblog
Next comes the weblog section. It contains a featured entry inside the element div named bloglatest and a list of the three latest entries. On the finished site, these will link to the complete entries.
To make the featured entry’s date orange, apply a rule to the element h4 inside the element div with ID bloglatest:
#bloglatest h4 {
color: #ff4e00;
font-size: 100%;
font-weight: bold;
}For the entry list, first remove all margins by setting margin to 0, then use padding to define 20 pixels of padding. Replace the ordinary bullets with the image more-bullet.gif, already used elsewhere in our layout:
#blog {
margin: 0;
padding: 0 0 0 20px;
list-style: url(img/more-bullet.gif);
}Finally style the entries: each date is orange, with blue link text beside it. Since links are already defined as blue, we can make the whole entry orange and links stay blue. The next figure shows the finished section.
#blog li {
font-size: 90%;
padding-bottom: 0.5em;
color: #ff4e00;
font-weight: bold;
}Figure: The finished weblog section.
The newsletter
The subscription form is the very last element to add to the layout. First create a style rule for its text field so we can address it by name.
Create a rule with the selector #newsletterform .text and set a width of 150 pixels so the element cannot protrude from the column and cover main content.
#newsletterform .text {
width: 135px;
border: 1px solid #45bac0;
}Next, give the submit button a new appearance:
#newsletterform .searchbutton {
text-align: right;
margin-top: 4px;
}This rule applies to the element div enclosing the submit button and right-aligns it. To do this, right-align the parent element’s contents.
Finally, add a rule for #newsletterform .btn to the stylesheet:
#newsletterform .btn {
border: 1px solid #45bac0;
background-color: #256290;
color: white;
font-size: 80%;
}This makes the button match the search-form button in the right sidebar. The next figure shows the result.
Figure: The newsletter subscription form.
Great! Our three-column layout is complete, shown in Internet Explorer 6 in the next figure. I usually develop in Firefox, which implements standards better, then test whether Internet Explorer displays the layout as expected. This layout is relatively simple and, as you can see, displays correctly in Internet Explorer 6.
Figure: The finished three-column layout in Internet Explorer 6 for Windows.
This is an excellent starting point for projects needing a basic three-column layout, with or without a header. Absolute positioning offers good control of column widths and makes cross-browser consistency relatively easy. However, it is not always the best choice. The following pages examine common problems designers face with three-column layouts.
Copyright © 2006 of the German translation, dpunkt.verlag GmbH
You may print the online version for your private use.
Otherwise, this chapter from “CSS − Anspruchsvolle Websites mit Cascading Stylesheets” is subject to the same conditions as the bound edition: the work, including all its parts, is protected by copyright. All rights reserved, including reproduction, translation, microfilming, and storage and processing in electronic systems.
dpunkt.verlag GmbH, Ringstraße 19, 69115 Heidelberg