Jump to content

User:Bartlett/20

Ka Wiktionary

./ ADD NAME=CH20.HTML

Hour 20. Page Layout in CSS 
What You'll Learn in This Hour:
  • The different strategies for laying out a page in CSS
  • Why it's a bad idea to use

    for page layout

    • The steps to replacing a table-based layout with a CSS-based structure
    • How to write HTML code for CSS-based layouts
    • How to use positioned content to lay out a page
    • How to use floating columns to lay out a page

    One of the major draws of modern CSSas supported by recent, more compliant browsersis the freedom to replace clunky HTML layout tables with structured HTML markup, styled by CSS rules. In previous hours, you've learned how to place individual portions of your web page in specific locations using absolute positioning or floating content. You can use the same types of style rules to build the visual structure of the page.

./ ADD NAME=CH20LEV1SEC1.HTML

Laying Out the Page

[wax ka badal]
This hour will bring together many of the techniques you've learned in previous hours for using CSS properties to lay out an entire page or even an entire website. You won't need to misuse HTML

elements for page layout now that you have reliable CSS techniques for layout in your repertoire. The examples this hour use a redesigned version of the website for the Dunbar Project (http://www.thedunbarproject.com). Dunbar School in Tucson, Arizona, was a segregated school for African-American students from 1918 until 1951, and was closed in 1978. Since then, a community group has been working to restore the school and make it into a community cultural center. The Dunbar Project has graciously allowed the author to use their website in this book for a sample website redesign in CSS. The Dunbar Project site as it appeared in early 2006 is shown in [file:///C:/Documents%20and%20Settings/dani/Asztal/Sams%20Teach%20Yourself%20CSS%20in%2024%20Hours_2ndEd/0672329069/ch.htm#ch20fig01 Figure 20.1]. It is mostly a dark teal color, and although it's not bad, it could be improved through the use of Cascading Style Sheets, as you'll see this hour.

Figure 20.1. The Dunbar Project's original website.
[file:///C:/Documents%20and%20Settings/dani/Asztal/Sams%20Teach%20Yourself%20CSS%20in%2024%20Hours_2ndEd/0672329069/images/20fig01_alt.jpg [View full size image]]
File:20fig01.jpg
 The Problems with Layout Tables 

[file:///C:/Documents%20and%20Settings/dani/Asztal/Sams%20Teach%20Yourself%20CSS%20in%2024%20Hours_2ndEd/0672329069/ch.htm#ch20fig02 Figure 20.2] shows the source view for the original version of the Dunbar Project web-site, which was not designed with CSS. Instead, multiple nested

tags provide the page layout, and is used extensively.

Figure 20.2. Table-based layout can be very convoluted.
[file:///C:/Documents%20and%20Settings/dani/Asztal/Sams%20Teach%20Yourself%20CSS%20in%2024%20Hours_2ndEd/0672329069/images/20fig02_alt.jpg [View full size image]]
File:20fig02.jpg
Tables for layout are problematic for a number of reasons. HTML purists argue against tables on principle: The

tag is meant to identify data in rows and columns of information, and is not intended for page layout. Accessibility mavens will tell you that screen readers employed by visually impaired users struggle with table layout. Table-based layouts are often harder to maintain than CSS-based layouts, requiring extensive rewriting of HTML tags to make simple changes. Later this hour, you'll see how a few CSS rules can easily move entire sections around without touching the HTML document at all. CSS-based layouts make it easier to maintain your HTML pages without cluttering them up with and

tags, and make for simpler transitions to new layouts by just swapping in a new style sheet. Your web pages laid out with CSS will be smaller (and thus load more quickly) than table-based pages. You can write web pages with the main content first in the HTML source and the navigation and footer information after, making your page more friendly to screen readers and search engines.
 Writing HTML with Structure 
The first step to laying out a page is to start with well-written HTML that is divided into sections for styling. This is commonly done with
tags that have id attributes set on them, corresponding to the different sections of the page.

In [file:///C:/Documents%20and%20Settings/dani/Asztal/Sams%20Teach%20Yourself%20CSS%20in%2024%20Hours_2ndEd/0672329069/ch.htm#ch20ex01 Listing 20.1], you can see a redesign of the Dunbar Project home page, which uses simple markup to store the site navigation, the content, the side navigation links, and the page footer.

Listing 20.1. Using
Tags to Create Sections for Positioning
<!-- dunbar-20.1.html --><br /><html><br /><head><br /><title>The Dunbar Project</title><br /></head><br /><body><br /><div id="header"><br /><h1>The Dunbar Project</h1><br /><h2>In the Shadow of Downtown Tucson</h2><br /><div id="sitenav"><br /><ol><li><a href="index.html">Home</a></li><br /><li><a href="about/">About the Dunbar Project</a></li><br /><li><a href="gallery/">Photo Galleries</a></li><br /><li><a href="donate/">Donate</a></li><br /><li><a href="contact/">Contact</a></li></ol><br /></div> <!-- sitenav --><br /></div> <!-- header --><br /><div id="main"><br /><div id="content"><br /><h3>Welcome to The Dunbar Project Website</h3><br /><img src="DunbarTop.jpg" alt="[Dunbar School]"><br /><p>Dunbar School was completed in January 1918, for the<br />purpose of educating Tucson's African-American students.<br />The school was named after <a href="poet.html">Paul<br />Laurence Dunbar</a>, a renowned African-American poet.<br />African-American children in first through ninth grades<br />attended Dunbar until 1951, when de jure segregation was<br />eliminated from the school systems of Arizona. When<br />segregation in Arizona was eliminated, Dunbar School<br />became the non-segregated John Spring Junior High School,<br />and continued as such until 1978 when the school was<br />closed permanently.</p><br /><!-- ... more content omitted ... --><br /></div> <!-- content --><br /><div id="sidebar"><br /><h3>Dunbar Project</h3><br /><ol><li><a href="plan/">The Dunbar Site Plan</a></li><br /><li><a href="auditorium/">Dunbar Auditorium</a></li><br /><li><a href="history/">School History</a></li><br /><li><a href="proposal/">Project Proposal</a></li><br /><li><a href="donors/">Dunbar Donors</a></li><br /><li><a href="poet.html">About Paul Laurence Dunbar,<br />Poet</a></li><br /><li><a href="links/">Related Links</a></li></ol><br /><h3>Coalition Partners</h3><br /><ol><li>The Tucson Urban League</li><br /><li>The Dunbar Alumni Association</li><br /><li>The Dunbar/Spring Neighborhood Association</li><br /><li>The Juneteenth Festival Committee</li></ol><br /><h3>Individual Members</h3><br /><ol> <!-- ... list of donors omitted ... --> </ol><br /></div> <!-- sidebar --><br /><div id="footer"><br /><p id="note501c3">The Dunbar Project is a 501c(3) organization,<br />and your contributions are tax deductible.</p><br /><p id="copyright">Copyright © 2006 by the Dunbar<br />Project. Questions?<br /><a href="mailto:webmaster@thedunbarproject.com"<br />>Mail the Webmaster.</a></p><br /></div> <!-- footer --><br /></div> <!-- main --><br /></body><br /></html><br />

You can download a full copy of this HTML file from the book's website; the content has been cut for space in this listing.

The structure of this page is defined by the
tags with id attributes. The general skeleton (with content omitted) consists of the following:
<div id="header">
 <div id="sitenav"></div>
</div> <! header >
<div id="main">
 <div id="content"></div>
 <div id="sidebar"></div>
 <div id="footer"></div>
</div> <! main >
Comments are used with the closing
tags as reminders about which
is being closed; it makes the page easier to edit later.

The page is constructed of two sections: a header and a main body. Each of these has one or more subsections. This structure provides what's needed to redesign and lay out the page.

Why this particular structure? There are actually many ways you could structure such the page, inserting
tags appropriately. This skeleton is simply the method chosen for this example, to get the specific styles used later on. During the web development process, you might go back to your HTML and add or remove
tags while styling to give more flexibility when creating page layouts. The number of
tags you use will vary from layout to layout. Some web designers believe strongly in using only a minimum number of
tags, whereas others add them freely whenever needed. The approach for this example is down the middle between those extremes: There are enough to make it easy to illustrate how CSS-based layout works, but not so many that we're adding extraneous
tags just because we can.

[file:///C:/Documents%20and%20Settings/dani/Asztal/Sams%20Teach%20Yourself%20CSS%20in%2024%20Hours_2ndEd/0672329069/ch.htm#ch20fig03 Figure 20.3] shows the new HTML page without any styles applied.

Figure 20.3. An unstyled page, ready for layout.
[file:///C:/Documents%20and%20Settings/dani/Asztal/Sams%20Teach%20Yourself%20CSS%20in%2024%20Hours_2ndEd/0672329069/images/20fig03_alt.jpg [View full size image]]
File:20fig03.jpg
 Writing a Layout Style Sheet 

With an HTML page ready for styling, the next step is to write the style sheet. There are several questions to consider regarding how to lay out the page.

The first is a technical question: Will you use absolute positioning for layout, or will you use floated columns? You can get the same general layout effects from both techniques. Positioning is a little bit easier to grasp, at first, so this example uses absolute positioning. Later this hour, however, you'll learn how to lay out the same HTML page with the float property.

You need to figure out how many columns you want. There's a slight increase in complexity when you have more columns, but the specific techniques remain the same whether you're using two columns, three columns, or more. In this redesign, two columns are used to avoid making the example overly complex.

Finally, you need to determine whether you are using a fixed layout or a liquid layout. A fixed layout is one that defines a specific width for an entire page; for example, it may be always 700 pixels across, and excess space in the browser simply becomes wider margins. A liquid layout is one that grows larger (or smaller) based on the user's screen resolution and browser window size.

There are advantages and disadvantages to both fixed and liquid layouts. A fixed layout may be easier to create and easier to read on larger monitors; a liquid layout is more adaptable but could result in overly long lines, which are harder to read. In this example, the Dunbar Project site will use a liquid design with margin size based on em units.

[file:///C:/Documents%20and%20Settings/dani/Asztal/Sams%20Teach%20Yourself%20CSS%20in%2024%20Hours_2ndEd/0672329069/ch.htm#ch20ex02 Listing 20.2] is a style sheet that starts to set up the layout choices.

Listing 20.2. A Style Sheet for Page Layout
/* dunbar-layout-20.2.css */<br /><br />body { margin: 0; padding: 0;<br />background-color: silver; }<br />#header { background-color: black; color: white; }<br />#sitenav ol { padding: 0; margin: 0;<br />display: inline; }<br />#sitenav li { display: inline; padding-left: 1em;<br />margin-left: 1em; border-left: 1px<br />solid black; }<br />#sitenav li:first-child<br />{ padding-left: 0; border-left: none;<br />margin-left: 0; }<br />#sitenav li a { color: white; }<br />#main { padding: 0 12em 2em 2em;<br />position: relative;<br />background-color: gray; }<br />#content { background-color: white; }<br />#sidebar { position: absolute; width: 10em;<br />right: 1em; top: 1em; }<br />#sidebar h3 { color: white;<br />background-color: black; }<br />#sidebar ol { margin: 0 0 1em 0;<br />background-color: white;<br />border: 2px solid black; }<br />#footer { background-color: white; }<br />

This style sheet is deliberately plain and simple, with colors of black, gray, silver, and white to make it easier for you to identify the various sections of the page.

So what's happening here?

  • The first rule sets the margin and padding of the <body> to 0. This is an important first rule for layout because browsers typically add one or the other (or both) to any web page.
  • The #sitenav rules in [file:///C:/Documents%20and%20Settings/dani/Asztal/Sams%20Teach%20Yourself%20CSS%20in%2024%20Hours_2ndEd/0672329069/ch.htm#ch20ex02 Listing 20.2] are used to turn the ordered list of links into a horizontal navigation bar.
  • The #main section is set to position: relative to become the containing block around the #content, #sidebar, and #footer sections.
  • The #main section is also given a large padding on the right, 12em. This is where the #sidebar will be located.
  • Absolute positioning is used to move the #sidebar into the margin, out of its place in the normal flow of content. It is positioned 1 em to the left of the right edge of its containing block (#main) by right: 1em, and 1 em down from the top edge of the containing block by top: 1em.

[file:///C:/Documents%20and%20Settings/dani/Asztal/Sams%20Teach%20Yourself%20CSS%20in%2024%20Hours_2ndEd/0672329069/ch.htm#ch20fig04 Figure 20.4] shows the results of linking this style sheet to the HTML file from [file:///C:/Documents%20and%20Settings/dani/Asztal/Sams%20Teach%20Yourself%20CSS%20in%2024%20Hours_2ndEd/0672329069/ch.htm#ch20ex01 Listing 20.1].

Figure 20.4. Positioning properties define the rough outline of the page.
[file:///C:/Documents%20and%20Settings/dani/Asztal/Sams%20Teach%20Yourself%20CSS%20in%2024%20Hours_2ndEd/0672329069/images/20fig04_alt.jpg [View full size image]]
File:20fig04.jpg

It's still quite rough, but you can see the different sections moved into place. You should note the silver bars above and below the header. Where did they come from, and why?

The silver bars are the result of the background color set on the <body> showing through. They are formed because of the default margin properties set on the

and