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

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

Continue with the main area. First define a new element div named main enclosing both the sidebar and content elements div. This makes it easier to align both beneath the header. Immediately after the closing header-div, add an opening <div id="main">:

    <img src="img/header-ball.gif" height="24" width="20" alt="" id="ball" />
  </div> <!-- header-bottom -->
</div> <!-- header -->
<div id="main">
  <div id="content">
    <h2>Simon Says</h2>

Close this element div immediately after the sidebar’s closing tag </div>. In the stylesheet, give the element #main ten pixels of top margin to separate content from header visually. The following fragment gives the rule. We will return to #main when building the layout.

#main {
  margin-top: 10px;
}

First define rules for the element div with ID #content, adding these rules to the stylesheet:

#content {
  margin: 0 240px 0 0;
  border: 1px solid #b9d2e3;
  background-color: white;
  color: black;
}

#content should have only a right margin, 240 pixels wide, leaving room for the sidebar later.

Also draw a solid one-pixel border around the box #content, in the same blue as the header borders, and give it a white background.

The lead article

At the top of the main area is a distinctive rectangular panel highlighting the lead article. Let us examine it.

Use an element div to create a container named mainfeature, containing the heading, article opening and full-article link:

<div id="content">
  <div id="mainfeature">
    <h2>Simon Says</h2>
    <p>Simon Mackie tells us how a change of shoes has given him new moves and a new outlook as the new season approaches. </p>
    <p><a href="">Read More</a></p>
  </div> <!-- mainfeature -->
  <h2>Recent Features</h2>

Now style the lead article:

#mainfeature {
  background-image: url(img/mainimg.jpg);
  background-repeat: no-repeat;
  background-color: #112236;
  color: white;
  padding: 2em 2em 1em 200px;
}

These rules insert the background image mainimg.jpg in #mainfeature and assign it the value no-repeat. To prevent white space beside it when the browser window is wider than the image, add the background color #112236, matching the image’s far-right edge so it blends into the solid background. Next make the text white and position it with padding: 2em below the top and from the right edge. Set bottom padding to 1em and left padding to 200 pixels, moving the text right so it does not cover the footbag player.

Next, style the heading and text inside #mainfeature:

#mainfeature h2 {
  margin: 0;
  font-weight: normal;
  font-size: 140%;
}
#mainfeature p {
  font-size: 110%;
}

Finally, style the Read More link to the full article. Define a class more and assign it to the paragraph containing the link, then define its rules:

<div id="mainfeature">
  <h2>Simon Says</h2>
  <p>Simon Mackie tells us how a change of shoes has given him new moves and a new outlook as the new season approaches.</p>
  <p class="more"><a href="">Read More</a></p>
</div>

The first rule for the element p containing the link removes its top margin, reducing the gap between text and link. Then right-align the paragraph using text-align: right:

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

Then style the pseudo-classes a:link and a:visited. Give them white text and the bullet image more-bullet.gif as background. To display it only once, set background-repeat to no-repeat and position the background. The value center centers the bullet vertically, and left puts it to the left of the link text. Add 14 pixels of left margin to prevent the text from overlapping it. The following figure shows the changes:

The page after styling the lead article

Figure: The page after styling the lead article.

In Internet Explorer 6, the peekaboo bug that affected our right-aligned navigation strikes again: the lead article sometimes becomes a white rectangle. An extra declaration of the property height: 1% in the rule #mainfeature easily fixes it.

#mainfeature {
  background-image: url(img/mainimg.jpg);
  background-repeat: no-repeat;
  background-color: #112236;
  color: white;
  padding: 2em 2em 1em 200px;
  height: 1%;
}

After reloading, IE should display the article as reliably as Firefox, Opera or Safari.

The latest-articles list

Our layout is taking shape! Now style the main content: the latest-articles list.

The main text currently touches its box edges. Add some space between content and border. The contents of the element div with ID content form an unordered list. We could add a margin around the list and the heading h2 above it. But other pages may need different main content. To present any content consistently, insert a new element div enclosing heading and list, with class inner.

<div id="content">
  <div id="mainfeature">
    <h2>Simon Says</h2>
    <p>Simon Mackie tells us how a change of shoes has given him new moves and a new outlook as the new season approaches.</p>
    <p class="more"><a href="">Read More</a></p>
  </div> <!-- mainfeature -->
  <div class="inner">
    <h2>Recent Features</h2>
    <ul>
      <li>
        <h3>Head for the Hills: Is Altitude Training the Answer?</h3> 
        <p>Lachlan 'Super Toe' Donald</p> 
        <p>Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia Curae; Praesent hendrerit iaculis arcu.</p>
        <p><a href="">Full Story</a></p>
      </li>
      <li>
        <h3>Hack up the Place: Freestylin' Super Tips</h3>
        <p>Jules 'Pony King' Szemere</p>
        <p>Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia Curae; Praesent hendrerit iaculis arcu.</p>
        <p><a href="">Full Story</a></p>
      </li>
      <li>
        <h3>The Complete Black Hat Hacker's Survival Guide</h3>
        <p>Mark 'Steel Tip' Harbottle</p>
        <p>Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia Curae; Praesent hendrerit iaculis arcu.</p>
        <p><a href="">Full Story</a></p>
      </li>
      <li>
        <h3>Five Tricks You Didn't Even Know You Knew</h3>
        <p>Simon 'Mack Daddy' Mackie</p>
        <p>Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia Curae; Praesent hendrerit iaculis arcu.</p>
        <p><a href="">Full Story</a></p>
      </li>
    </ul>
  </div> <--! inner -->
</div> <!-- content -->

To separate the list from its box edges, give the element #content .inner a property margin in the stylesheet.

#content .inner {
  margin: 10px 20px 10px 40px;
}

View the page to see the spacing created by this rule.

Start styling this area’s content with the heading. The design has a blue underline extending across the area, achieved with the property bottom-border. Also add bottom padding to the element h2 to separate text from that line.

#content .inner h2 {
  color: #245185;
  padding-bottom: 0.2em;
  border-bottom: 1px solid #b9d2e3;
  font-size: 110%;
}

Another rule removes default margins and bullets. We could target #content .inner ul because there is only one list here, but other pages may have differently structured lists. Instead, give the element ul the attribute class="features" to style this and similar special lists without changing ordinary lists in the main area.

<div class="inner">
  <h2>Recent Features</h2>
  <ul class="features">
    <li>

#content .inner ul.features {
  margin: 0;
  padding: 0;
  list-style: none;
}

Each article has a heading h3 that we enlarge:

#content .inner h3 {
  font-size: 130%;
}

Each heading also links to its article on Footbag Freaks, so define styles for the pseudo-classes too:

<li>
  <h3><a href="">Head for the Hills: Is Altitude Training the Answer?</a></h3>
  <p>Lachlan 'Super Toe' Donald</p>
  <p>Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia Curae; Praesent hendrerit iaculis arcu.</p>
  <p><a href="">Full Story</a></p>
</li>
<li>
  <h3><a href="">Hack up the Place: Freestylin' Super Tips</a></h3> 
  <p>Jules 'Pony King' Szemere</p>
  <p>Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia Curae; Praesent hendrerit iaculis arcu.</p>
  <p><a href="">Full Story</a></p>
</li>

#content .inner h3 a:link, #content .inner h3 a:visited {
  color: #245185;
}

Finally, make paragraph text dark gray and reduce its size to 90%.

#content .inner p {
  color: #666666;
  font-size: 90%;
}

Author portraits

Add a small author portrait beside every article in the latest-articles list, inserting it after each heading as shown here:

<li>    
  <h3><a href="">Head for the Hills: Is Altitude Training the Answer?</a></h3>    
  <img src="img/lachlan.jpg" alt="Lachlan Donald" height="48" width="35" />    
  <p>Lachlan 'Super Toe' Donald</p>
  <p>Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia Curae; Praesent hendrerit iaculis arcu.</p>
  <p class="more"><a href="">Full Story</a></p> 
</li>

This HTML produces the next figure.

Author images added to the layout

Figure: Author images added to the layout.

Use the property float: left to float portraits to the text’s left. No image widths are needed, as each already has a defined width.

#content .inner .features li img {
  float: left;
  margin: 0 5px 5px 0;
}

We used a selector targeting only images inside a list item with the attribute class="features". This prevents the rules from affecting other images you might add.

The images float left with float: left and have margins, leaving space so text does not touch them. The next figure shows this.

Author images floated left

Figure: Author images floated left.

The design has bold author names. Give the tag <p> containing each name the attribute class="author" and define a class rule making its content bold. We deliberately avoid tags <strong> and <b> because the names are bold for aesthetic rather than structural reasons. Keeping their formatting out of HTML preserves content/presentation separation. CSS also lets us change every author name by modifying one class rule rather than finding and editing names on every page. Here are the HTML changes, followed by the CSS rule that makes all matching author names bold:

<img src="img/lachlan.jpg" alt="Lachlan Donald" height="48" width="35" />
<p class="author">Lachlan 'Super Toe' Donald</p>
<p>Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia Curae; Praesent hendrerit iaculis arcu.</p>

#content .inner p.author {
  font-weight: bold;
}

The final element is the Full Story link below each article. Create a class more and add it as an attribute to each opening tag <p> for that link.

#content .inner p.more{
  margin-top: 0;
  text-align: right;
}
#content .inner p.more a:link, #content .inner p.more a:visited {
  color: black;
  background-image: url(img/more-bullet.gif);
  background-repeat: no-repeat;
  background-position: center left;
  padding-left: 14px;
  font-size: 90%;
  color: #1e4c82;
}

These rules closely resemble those for the lead article’s Read More links above.

The layout should now resemble the original design. The following figure shows our progress: almost everything is finished except the right sidebar.

The page after styling the main area

Figure: The page after styling the main area.

  

 

 

 

back to top