23 March 2009
Basic knowledge of Dreamweaver.
Beginning
Everyone is urging you to make the switch from table-based layouts to tableless, CSS-positioned layouts. You've certainly been thinking about it, but CSS layouts seem so unintuitive. You've established a workflow with tables that, well, works. Why should you change? After all, browsers will always render tables, and you understand their methods and issues. All you have to do to get a design grid is fill out the Dreamweaver table dialog box. Would that it were so easy with CSS! Well, it can be—Dreamweaver includes CSS starter pages that provide a kind of CSS alternative to the table dialog box.
Part 1 of this tutorial gently guides you through the transition from tables to CSS, and demonstrates how in many ways CSS layouts are easier. You'll learn the basics of CSS layouts and see that there are many equivalencies between table and CSS concepts.
In Part 2 you'll learn to work with CSS starter pages as you convert a table-based design to a pure CSS design. You'll even get to add some "wow factor" to the design by using a Spry Accordion widget in the page.
Before you build the Yacht Club design shown in Figure 1, however, you'll learn about the advantages of the CSS approach. You'll also learn a few techniques that you can use to replace old school methods. Once you master a few core concepts, you will understand what goes on underneath the hood of most of the CSS starter pages.
Tables seem so easy because we're familiar with the concept quite apart from web page design. Most of us used Excel spreadsheets or Word tables before we tried to put together our first web pages. The idea of putting content in cells seems intuitive. Still, though tables work well for data, there are disadvantages to using them for layout, a purpose they were never intended to fulfill.
I remember a few years ago when I was called in to advise a company that purchased a Dreamweaver template that had been designed with tables. There were many row and column spans to this carefully constructed house of cards. The problem started when the webmaster tried to make edits to various cells. If the new content made the heights of the cells vary too much from the originals, the construction started to fall apart. Size adjustments in one cell affected surrounding cells. Gaps appeared between images that were supposed to be precisely aligned. Perhaps table designs are not really that easy after all. In CSS layouts, the building blocks have more independence. There are many other advantages as well:
It is always best to start with what you know, and surprisingly, there are quite a few commonalities in the table and CSS approaches to layout. Tables and CSS layouts start with the same raw material. Consider the simple page shown in Figure 2, which could provide the basis for either a table-based or CSS-based layout.
The raw material of this simple page includes features that will be common to both kinds of layouts:
This simple page represents what the web looked like a long time ago before designers figured out ways to manipulate the natural flow of an HTML document. In order to give pages more visual interest, designers first thought inside the box by using tables as grid structures for their content. With CSS positioning techniques, however, they finally got to think outside the box. CSS layouts still use building blocks, but these boxes enjoy more independence from one another. The structure is less rigid, and the creative possibilities are positively liberating!
Before beginning work with any design, you should define a new site in Dreamweaver. Many people skip this step and then lose out on much of the power of Dreamweaver (such as updating links when you change a file name in the Files panel).
Dreamweaver will process the site and display the files in the Files panel.
In this exercise, you'll learn how to replace HTML attributes for aligning, clearing, and spacing images with the CSS properties float, clear, and margin.
Images are, by default, inline elements and flow next to other adjacent inline content. You're now going to use the float property to wrap the text around the image.
leftimage class from the Class popup menu in the Property inspector (see Figure 7)
Now you know how to wrap text around an image by using the float property. If you wanted to place the image on the right and the text on the left, you could create a class and set the float property to right.
But what do you do when you want to stop the wrapping effect? What if, for example, you wanted the "Sail to the Bahamas" heading and its paragraph to display under the sailboat image? This is a job for the clear property.
By using both instead of left, the same class can be used for right or left floated images. Now you need to apply this new class.
clearfloat class as the Class (see Figure 8).
In the first exercise, you learned how to replace the image align HTML attributes with the CSS float property. Before you explore how to use the float property to create side-by-side columns, you'll learn another replacement technique: replacing table cells with CSS boxes, more technically known as DIVs. The DIV element is a generic box in which you can insert content, just like you did in cells! The difference is that the DIV has some semblance of independence, unlike the more tightly interrelated table cells. In this next exercise you will create boxes to divide the page into discrete areas. You will give each of these boxes a name that describes its function and meaning in the document. For example, the top DIV is named "header" because it includes header information. Giving each area of the page a descriptive name conforms to the web standards concept of the semantic web.
Dreamweaver adds a dashed line around the area that now falls within the DIV or division of the page. The line is just a visual effect to help you see where each DIV on the page lies. You will not see this line in your browser. (If you do not see the dashed line, you may have turned off the CSS Layout Outlines under the Eye icon in the Document toolbar.)
If you were to look at the code for the new DIV, you would see that the heading is nested inside a "box" much like a table cell:
<div id="header">
<h1> Luxury Yachts</h1>
</div>
p element in the tag selector at the bottom of the page.
Next you will switch to Code view so that you can select multiple elements. The header and sidebar were easily selectable in the tag selector because there was one parent element in each, an h1 and a p. The content region will have an image, an h2, and multiple paragraphs. The most reliable way to select everything from the opening tag of the first element to the closing tag of the last element is to make the selection in Code view.
p tag to the closing p tag right before the footer text. Refer to Figure 13 to help you make your selection.
Finally, you'll add a DIV around the footer paragraph.
p element in the tag selector at the bottom of the Document window.Your page is now divided into four separate boxes (the header, sidebar, mainContent, and footer DIVs) that you will use with CSS to manipulate the layout. Dreamweaver gives you visual cues for recognizing where each DIV is located (see Figure 14).
This tutorial covers two models for creating side-by-side columns, and the advantages and disadvantages of each. The first model will use absolute positioning on the sidebar DIV. You have used absolute positioning if you ever created a design with the Draw Layer or Draw AP box features in Dreamweaver.
Make sure your CSS Styles panel is open. If you do not see it on screen, choose Window > CSS Styles.
Start by putting each link in the sidebar in its own paragraph:
Note: ID names always begin with the number sign, and must be unique on the page.
You will probably be surprised at the result that appears in Design view in the Dreamweaver document window (see Figure 18). The content area now covers the navigation list. An absolutely positioned DIV is taken out of the natural flow of the document and placed at precise coordinates. The other content no longer "sees" it. You will fix this in the next part of the exercise.
The result should look much better to you (see Figure 20). You carved out a 175-pixel margin for the 150-pixel-wide sidebar DIV to slip into, with 25 pixels extra for a gutter between the DIVs.
This is a fairly effective method for creating the look of side-by-side columns. One of its advantages is that the absolutely positioned DIV can be anywhere in the HTML source code, thus allowing you to increase the accessibility of the page for those using screen readers.
There are, however, some problems. You may have encountered one of them if you ever used the Draw Layer/Draw AP DIV feature in Dreamweaver. Absolutely positioned elements have a certain rigidity about them. If the user increases the text size, the contents might spill out of the box (see Figure 21). If all the DIVs in a design are absolutely positioned, such as happens when the designer draws layers for each area, the user may see overlapping elements that make the page nearly unusable.
There is another potential problem that limits what you can do with the design. As long as the content column is longer than the sidebar column, the footer will appear below both columns and all will be well. If the sidebar column is longer—as it might be if some text were added underneath the links—then the footer will overlap it (see Figure 22). Just as the mainContent DIV did not see the absolutely positioned sidebar DIV, so too will the footer DIV be unaware of its existence.
There are certainly instances where absolutely positioning elements is a good strategy, but you should consider other approaches for most of your work.
Floats are the second method for creating side-by-side columns, and they provide the flexibility to make either column longer than the other. Floated elements, however, must come before the element they are going to float next to in the source order. So in this example, because the sidebar DIV is going to be floated, you must make sure it is in front of the mainContent DIV.
Now the sidebar DIV is floated to the left of the content DIV. However, you'll notice that once the navigation list reaches its end, the mainContent DIV wraps around it (see Figure 24). The default behavior for floats is that the elements that follow wrap around them. The mainContent DIV sees and makes space for the sidebar DIV, but there isn't really a true two-column look. You can fix this by again carving out a left margin space on the content DIV.
The look of two columns is now back (see Figure 25).
You will remember that you achieved the same results by using absolute positioning in the previous exercise. By using a float instead, however, you can allow either column to be longest without overlapping the footer DIV. In the next steps you will put that into action.
The file from the previous exercise should still be open.
The extra text in the left column is still breaking through the footer DIV, though not through the footer text (see Figure 26). Think back to the image floating exercise. The image was taller than the paragraphs next to it, and the h2 crept up to wrap around the image. Elements wrap around floats unless they are cleared. And that is exactly what you need to do to solve this problem.
Note: There is a potential problem you might encounter when floating columns. Remember the h2 element with the clearfloat class on it? It was added to prevent the h2 text from wrapping around the image. If it existed in this exercise page, it would clear the floated sidebar column in addition to the h2 text. The h2 text would then drop down below the last content in the sidebar! There are ways to code around this problem, but they involve more advanced techniques that control the block formatting context. You can read more about this topic and solutions to the problem in an article by Zoe Gillenwater.
mainContent DIV.<div#mainContent> in the tag selector at the bottom of the document window. This selects the entire content area.<br class="clearfloat" />. You can use the Dreamweaver Code Hint feature to help you quickly type out the code (see Figure 27).
Note: Since you are using the default XHTML Transitional 1.0 doctype in Dreamweaver, it is important to add the space and forward slash before closing the element. For example: <br class="clearfloat" />. You can read more about doctypes at Community MX.
You already created the clearfloat class in the image floating exercise, so as soon as you return to Design view, the effect should be immediately apparent (see Figure 28). The footer clears the sidebar column!
You're now going to add a few more properties and values to the clearfloat class. You may remember reading at the beginning of this tutorial that all pages are subject to a browser style sheet before you add even one rule of your own. Many browser style sheets add dimensions to a br element, thus adding space to the page that you may not want. Since the br element is there solely to provide a hook for clearing a float, you do not want it to have any height. Let's now add some values to prevent that potential problem.
By making sure that the browser doesn't add any height, margin space, or line-height, and by setting the size of fonts to only 1 pixel—giving it that tiny size to prevent the br from collapsing all together—you ensure that the break does the job of clearing the float without distorting the design with any unexpected dimension.
There are various methods you could have used to clear the image, but this way gives you a reusable widget that you can apply whenever you need to clear an element. It is also the method that is used in the CSS starter pages.
Just as with table cells, you can add background colors to DIVs. In this step, you'll add background colors to the header, sidebar, and footer. Start with the header and footer:
The new header and footer colors are shown in Figure 30.
Adding background color gets trickier with the sidebar DIV, however. Early on designers identified a problem with CSS layouts. Because of how DIVs work, it seemed impossible to get the look of equal-height column layouts—especially equal-height columns with background colors or images—that are so easy to achieve in table-based designs. To see what I mean, let's add a background color to the sidebar.
If you look at the greenish background color behind the left sidebar column in Figure 31, you'll notice that as soon as the last link displays, the background color ends. A DIV's height extends only as long as there is content inside it.
The time-honored way to address this is to use the faux column technique. This involves nesting the existing DIVs inside a parent DIV. (You may have used nesting techniques to accomplish design goals when you worked with tables.) You can then tile an image on the vertical axis of this parent DIV. It won't matter which column is longest, because whenever one of them grows, the entire parent container DIV grows to contain them both. The background image on the container DIV gives the illusion that the sidebar column has a solid block of color that extends all the way down to the footer.
You'll nest all the divisions into a container DIV. This is a bit like carrying all your travel items in a suitcase rather than trying to juggle them all in your hands. Why, it is even a bit like using a table with multiple rows and columns inside!
By putting all the DIVs into one container DIV, you gain more control over many aspects of your layout as you'll soon see.
<body> in the tag selector at the bottom of the Document window. This selects everything on the page.
HTML has only one background option, and that is to tile both horizontally and vertically on the page. CSS has four repeat options—you can tile on the x-axis or y-axis, not repeat at all, or tile in all directions like the HTML model.
It is a "best practice" to try to avoid horizontal scrollbars. If you assume your "lowest common denominator" is a screen resolution of 800 by 600, you can set the width of a layout somewhere between 720 to 780. This will leave room for the browser chrome, or sides of the browser viewport.
You now have the shell of the layout completed. Your result should look like Figure 36.
You're probably wondering why there are gaps between the header and the top of the container and the footer and the bottom of the container in Dreamweaver Design view. If you are using a browser like Safari or Firefox, you will see these same oddities in the interaction between the container DIV and the header and footer.
You're seeing the effect of margin collapsing. Remember that before you add even one rule to your style sheet, the browser has provided a basic default style sheet that adds things like margin, padding, border, font, and font size to some elements, such as headings and paragraphs. Most browsers, for example, add about 20 pixels of top margin and 20 pixels of bottom margin space for paragraphs. If there are two adjacent paragraphs, the 20 pixels of bottom margin for the first and the 20 pixels of the top margin for the next would add up to 40 pixels—way too much! The W3C instead states that the 40 pixels should collapse into just 20 pixels, with one paragraph's margin escaping into the other.
In the case of the h1 and p text in the header and footer, default browser margins are escaping outside their DIVs into the container DIV. There are a number of ways to fix the escaping margin effect. One is to zero out the top or bottom margin of the element. Adding a small amount of padding or a border are other ways to correct the problem.
Note: Many designers zero out some of the default styling that the browser adds to the layout at the beginning of their style sheets. Different browsers add different values for the various elements, so zeroing them out also gives the design a level starting point. You can then add your own values with the expectation that they will look consistent everywhere. A common practice is to begin a style sheet with a rule such as the following:
body, form,
h1, h2, h3, h4, h5, h6, p, ul, li {
margin: 0;
padding: 0;
}
This fixes the gap between the header and the container. Fixing the gap between the footer and the container will require a slightly more complex selector. Because you don't want to remove the bottom margin from every paragraph on the page, you will target only the paragraph in the footer DIV.
When you redefine a tag (like you just did with the h1 tag), you are making global changes, changes that would affect every instance of that element on the page. Because you have only one h1 on the page, this is not a problem.
But what if you wanted paragraphs to look one way in the mainContent DIV and another way in the footer DIV? Or what if you wanted links to be one color in the mainContent DIV and a different color in the sidebar DIV? This would be a good job for the descendant selector. As the name implies, this creates a selector that is a descendant of another page element. Returning to the paragraph example, you could create a paragraph style that only applies to paragraphs when they are in the footer DIV of the page. You would start with the footer ID name, add one space, and then type p. For example:
#footer p
You could also create a separate paragraph style for the mainContent DIV:
#mainContent p
In the following steps, you will add a descendant selector for the paragraph in the footer DIV.
If you look at the page now, you will see that the gap between the footer DIV and the bottom of the container DIV is now gone (see Figure 39).
Before you conclude Part 1 of this tutorial, add a finishing touch—after all, the devil is in the design details! As the design stands, the text is very close to the left edge of the container. The h1 text in the header, the links in the sidebar, and the text in the footer could all use a little space on the left side. Because the design will look more professional if all three text elements have the same amount of space, it makes sense to make a CSS rule that applies to all three at once. In the next steps, you will use the compound option to create a group selector.
The results are shown in Figure 41.
Note: In Part 2 of this tutorial, you'll learn more about the use of padding, margins, and borders as you explore the box model.
In this part of the tutorial about making the transition from tables to CSS layouts, you learned quite a bit about the basics of CSS, including how to:
It may seem like you went through a lot of steps just to get the basic shell of the Yacht layout, but Adobe doesn't expect you to reinvent the wheel every time you start a new layout. Dreamweaver provides CSS starter pages for all the common layout permutations in its New Document dialog box. These CSS starter pages use the basic methods you learned in this tutorial. The starter pages can help you save time, but they lack images, navigation schemes, and other finishing touches. In Part 2 of this tutorial, you will use a CSS starter page to design the Yacht Club layout.

This work is licensed under a Creative Commons Attribution-Noncommercial-Share Alike 3.0 Unported License
Tutorials and samples |
Dreamweaver user forum |
More |
| 04/23/2012 | Resolution/Compatibility/liquid layout |
|---|---|
| 04/20/2012 | using local/testing server with cs5 inserting images look fine in the split screen but do not show |
| 04/18/2012 | Ap Div help |
| 04/23/2012 | Updating |
Dreamweaver Cookbook |
More |