XML technologies / CSS / Advanced websites with CSS / A simple CSS layout / Building the layout / The sidebar

(Excerpt from “CSS − Advanced websites with Cascading Stylesheets” by Rachel Andrew & Dan Shafer)

The sidebar sits below the main area, as the next figure shows. No rules have been applied yet, so it appears in its natural place in the document.

The unstyled sidebar

Figure: The unstyled sidebar.

First, move the sidebar to the space reserved for it: to the right of the main area.

Observe what happens if we position it absolutely from the top and right. Add these rules to the stylesheet:

#sidebar {
  position: absolute;
  top: 0;
  right: 0;
  width: 220px;
  background-color: #256290;
  color: white;
  margin: 0;
  padding: 0;
}

View the page in the browser. The sidebar is anchored to the viewport’s upper-right corner, as in the next figure.

Sidebar positioned absolutely at the top right

Figure: Sidebar positioned absolutely at the top right.

When discussing absolute and relative positioning (see Absolute and relative positioning in context), I said that elements are always positioned relative to their parent’s location. This was called the positioning context. Here the element #sidebar has no positioned parent, so it uses the viewport as its positioning context.

However, we can position an element to provide a useful context for #sidebar: the element div with ID #main.

Find #main in the stylesheet and add these declarations:

#main {
  position: relative;
  top:0;
  left: 0;
  width: 100%;
  margin-top: 10px;
}

Now #sidebar uses the element #main as its parent, positioning #sidebar inside #main, as shown in the next figure.

Sidebar positioned inside a relatively positioned container

Figure: Sidebar positioned inside a relatively positioned container.

With the sidebar in place, we can style its contents. Start with the headings h3 that label its different sections:

#sidebar h3 {
  font-size: 110%;
  background-image: url(img/sidebar-header-bg.jpg);
  background-repeat: no-repeat;
  margin: 0;
  padding: 0.2em 0 0.2em 10px;
  font-weight: normal;
}

Behind the headings, an image produces the gradient from the graphic design.

Attractive background effects:
Background images are an excellent way to create attractive headings. They avoid images containing the actual heading text, which would complicate maintenance: even tiny text changes would require new images.

Look more closely at the sidebar’s content areas beneath the headings. For each, define an element div and a class inner to create some spacing and move the text away from the sidebar edge. Add this element div in each of the three areas:

<div id="sidebar">
  <div class="inner">
    <h3>Site Search</h3>
    <form method="post" action="" id="searchform">
    <div>
      <label for="keywords">Keywords</label>:
      <input type="text" name="keywords" id="keywords" />
    </div>
    <div>
      <input type="submit" name="btnSearch" id="btnSearch" />
    </div>
    </form>
  </div>
  <div class="inner">
    <h3>Coming Events</h3>
    <ul>
      <li>10 Apr 06 -<br /><a href="">Seattle Zone Qualifier</a></li>
      <li>13 Apr 06 -<br /><a href="">World Cup - Round /a></li>
      <li>21 Apr 06 -<br /><a href="">FootbagOOM 05 - NY</a></li>
      <li>28 Apr 06 -<br /><a href="">WFPA AGM - Hong Kong</a></li>
      <li>3 May 06 -<br /><a href="">World Cup - Round 9</a></li>
    </ul>
  </div>
  <div class="inner">
    <h3>Move of the Month</h3>
    <h4>The Outer Stall</h4>
    <p>Eti bibendum mauris nec nulla. Nullam cursus ullamcorper quam. Sed cursus vestibulum leo.</p>
    <p><a href="">more</a></p>
  </div>
</div> <!-- sidebar -->

Give the class inner ten pixels of padding:

#sidebar .inner {
  padding: 10px;
}

Sidebar after styling headings and the inner class

Figure: Sidebar after styling headings and the inner class.

As the figure shows, the sidebar is taking shape. Next comes the upcoming-events list.

#sidebar ul {
  list-style-image: url(img/more-bullet.gif);
  margin-left: 0;
  padding-left: 20px;
}

These rules use the image more-bullet.gif as a bullet, remove margins and add 20 pixels of right padding to align list entries with the headings.

#sidebar p, #sidebar li {
  font-size: 90%;
  line-height: 1.4em;
}

Next, reduce the text and list-entry font sizes to 90% and slightly increase line spacing with the property line-height.

#sidebar ul a:link, #sidebar ul a:visited {
  color: white;
}

The design has white, underlined sidebar links, produced by the rule above.

Finally, define a class "date", assigned as an attribute to a tag <span> to make all listed events bold.

<ul>
  <li><span class="date">10 Apr 06</span> -<br /><a href="">Seattle Zone Qualifier</a></li>
  <li><span class="date">13 Apr 06</span> -<br /><a href="">World Cup - Round 8</a></li>
  <li><span class="date">21 Apr 06</span> -<br /><a href="">FootbagOOM 05 - NY</a></li>
  <li><span class="date">28 Apr 06</span> -<br /><a href="">WFPA AGM - Hong Kong</a></li>
  <li><span class="date">3 May 06</span> -<br /><a href="">World Cup - Round 9</a></li>
</ul>

#sidebar .date {
  font-weight: bold;
}

The next figure shows the resulting events list:

The styled events list in the sidebar

Figure: The styled events list in the sidebar.

The form

Now define rules for the search form at the top of the sidebar. Give the elements input of type "text" the attribute class="text". Then define a rule for #searchform .text. The following rule makes the text box 196 pixels wide and surrounds it with a border.

#searchform .text {
  width: 196px;
  border: 1px solid #45bac0;
}

Give the element div enclosing the submit button the attribute class="searchbutton" and add a rule for that class. It right-aligns the element within the box and adds a top margin to separate the button from the text field.

#searchform .searchbutton {
  text-align: right;
  margin-top: 4px;
}

Then style the button itself: its border matches the text field, its background matches the sidebar, and its label is white. These rules do all of that.

#searchform .btn {
  border: 1px solid #45bac0;
  background-color: #256290;
  color: white;
}

Also give the element input a class attribute with the value btn. The result should match the next figure.

The redesigned search form

Figure: The redesigned search form.

Move of the month

The last section at the sidebar’s bottom is “Move of the Month.” First insert its image into the document below the heading h4 and give it the attribute class="motm-image":

<h3>Move of the Month</h3>
<h4>The Outer Stall</h4>
<img src="img/sidebar-player.gif" alt="player demonstrating the outer stall move" height="110" width="60" class="motm-image" />
<p>Eti bibendum mauris nec nulla. Nullam cursus ullamcorper quam. Sed cursus vestibulum leo.</p>
<p class="more"><a href="">more</a></p>

Use the property float to float the image right, leaving room for text on the left:

#sidebar .motm-image {
  float: right;
  margin: 0 30px 0 20px;
}

As you can see, we added left and right margins to the image. Finally, style the link "more", much like the “Read More” and “Full Story” links. Unlike those, the link "more" would normally appear beside the floated image, but we want it always below. The following code uses the property clear: right to ensure that no floated elements sit to its right. Also apply the class more to the tag <p> containing the link:

#sidebar p.more {
  clear: right;
  margin: 0 30px 0 0;
  text-align: right;
}

In A three-column layout we will examine the property clear more closely. For now, remember its values: left (moves the insertion point below a left-hand element float), right (moves the insertion point below a right-hand element float), and both (moves the insertion point below all elements float). You will appreciate this property when using floats in your layouts.

The final rules should look familiar from the “Read More” and “Full Story” links:

#sidebar p.more a:link, #sidebar p.more a:visited {
  color: white;
  background-image: url(img/more-bullet.gif);
  background-repeat: no-repeat;
  background-position: center left;
  padding-left: 14px;
}

This completes our two-column layout! The following figure shows the finished page.

The completed two-column layout

Figure: The completed two-column layout.

  

 

 

 

back to top