tag with its width property set), percentage width values for the sidebar are based on the fixed width-containing element—it isn’t based on the window size and doesn’t change when the browser window size changes. This is true of any element whose width you specify by using percentage values: The percentage is based on the width of the tag that surrounds the element.
7. Complete the style, and then insert the div. At this point you can continue to style the sidebar. You could add a background color, set a font family, that, thanks to inheritance (see page 390), will apply to all the text inside the div, and so on. When you’re done, click OK in the Style Definition window; you return to the Insert Div Tag window with the class box filled out with your freshly created style’s name. Click OK to insert the div, and then watch the sidebar float. Now it’s time to style the main column. 8. Follow the same steps for the main content div: select the page elements that form that main content on the page, click the Insert Div Tag button, and then create a new class (or ID) style for the page’s main content region. In this instance, you don’t need to float anything. You merely have to add a left margin to the main content so that it won’t try to wrap below the end of the sidebar. If the sidebar is shorter than the other content on the page, the text from the main column wraps underneath the sidebar. It’s much like how the main text interacts with the right-floated photo in Figure 9-4. If the main content wrapped underneath the sidebar, the appearance of two side-by-side columns would be ruined. Adding a left margin that’s equal to or greater than the width of the sidebar indents the main content of the page, creating the illusion of a second column.
By the way, it’s usually a good idea to make the left margin a little bigger than the width of the sidebar: That creates some empty space—a gutter—between the two elements. So if the sidebar is 170 pixels wide, adding a left margin of 185 pixels for the main content div adds an extra 15 pixels of space. If you use percentages to set the width of the sidebar, use a slightly larger percentage value for the left margin.
In addition, avoid setting a width for the main content div. It’s not necessary, since browsers simply expand it to fit the available space. Even if you want a fixed-width design, you don’t need to set a width for the main content div, as described in Figure 9-7. Expanding the two-column design into a three-column design isn’t difficult either (see Figure 9-9). First, add another
between the two columns and float it to the right. Then add a right margin to the middle column, so that if the text in the middle column runs longer than the new right sidebar, it won’t wrap underneath the sidebar.
HTML Source Order
CSS Layout
1
banner
1
2
left sidebar
2
3
right sidebar
Figure 9-9
banner 4
3
L
R
A three-column design uses the same concepts as the two-column design. In this case, you float both the left and right sidebars, and add both left and right margins to the center column. The left-hand diagram shows the order of the HTML; the right side shows what the web page looks like.
4 main content
Tip If you’re concerned about displaying your HTML in a specific order—for example, you’d like the main content region to appear before the sidebar—you can do that simply by floating the main content and the sidebar boxes. You’ll see an example of this in the tutorial at the end of this chapter.
Understanding the Box Model It’s no coincidence that you find the Float property in the Box category of the CSS Rule Definition window (Figure 9-10).
Chapter 9: Page Layout
447
Understanding the Box Model
Figure 9-10
Use the Box category to define the dimensions of a style, to position an object on the page, and to add space between the styled object and the objects around it.
To fully understand CSS layouts and make the most of floats, you need to understand the other CSS properties in this category: width, height, padding, margin, and clear. • Width and height. You can specify the width and height for any styled object using these properties. If you want a paragraph that’s 100 pixels wide, create a class style with the Width property set to 100 pixels, and then apply it to the paragraph. You’ll often use the Width property in conjunction with the Float property (see the following paragraph) to do things like create a box with a set width that floats to either the left or right side of the page—a common format for pull-quotes, message boxes, and sidebars. Be careful with the Height property. Many designers use it for precise control over page elements. Unfortunately, height is tricky to control. If you set a height for a sidebar that contains text, and you later add more text, you can end up with text spilling outside the sidebar—the same thing can happen if a visitor increases the text size in his browser. In other words, set the height of an object only if you’re sure the content inside will never get taller—for example, if the content is an image. • Float. To force an object to the left or right side of a page and have other content wrap around it, use the Float property. Of course, that’s been most of the point of this chapter, so you probably understand this property by now. However, there’s
448
Dreamweaver CS6: The Missing Manual
one important point to keep in mind: Floating an object doesn’t necessarily move it to the side of a page or browser window. A floated object merely goes to the left or right edge of what’s called its “containing block.” If you float a div to the left of a page to create a sidebar, and then insert an image into the sidebar and float that image right, the image goes to the right edge of the sidebar, not to the right edge of the page. In addition, if you float multiple elements, they can often end up sitting beside each other—you use this technique to create fourcolumn layouts, where each column floats next to the other.
Understanding the Box Model
• Clear. Clear prevents an element from wrapping around any object with a right or left Float property. This property comes in handy when you want to force an element to appear below a floated object instead of wrapping around it. The classic example is a page’s footer (the area at the bottom of the page that usually contains contact information and a copyright notice). If a page has a left-floated sidebar that’s longer than the main content, the footer can move up the page and wrap around the sidebar. In this case, the bottom of the sidebar is at the bottom of the page, and the footer is somewhere in the middle. To fix this problem, set the footer’s Clear property to both. That forces the footer to drop below both left- and right-floated elements. (If you merely want something to drop below a left-floated element, but still wrap around anything floated right, choose the left option; to clear a right-floated element, choose right.) In other words, if you ever see page content next to a floated element instead of underneath it, use the clear property to properly position that content. • Padding. Padding is the gap that separates the content of a page element—like a paragraph of text or an image—and its border. If you put a 1-pixel border around an image and want to add 10 pixels of space between the image itself and that border, type 10 into the Top padding box, and then choose “pixels” from the drop-down menu. To set the padding around each edge separately, turn off the “Same for all” box and then type values into each of the four boxes. • Margin. The margin is the amount of space surrounding an element. It surrounds the border and padding properties of the style, and lets you add space between elements. Use any of the values—pixels, percentages, and so on—that CSS supports. Padding, margins, borders, and the content inside the styled tag make up what web designers call the CSS Box Model, seen in Figure 9-11. Margins and padding are invisible. They also have similar effects: 5 pixels of left padding adds 5 pixels of space to the left edge of a style; the same happens when you add a 5-pixel left margin. Most people use margins to put space between elements (for example, between the right edge of one column and left edge of an adjacent one) and padding to add space between an element’s border and its content (like moving text within a column away from a surrounding borderline). Because you can’t see padding or margins (just the empty space they make), it’s often difficult to know if the gap between, say, the banner at the top of your page and the main area of content results from the banner’s style or the main area’s style.
Chapter 9: Page Layout
449
Understanding the Box Model
Figure 9-11
In what web designers call the CSS Box Model, every style is composed of multiple boxes, one inside the other. Each box controls certain display properties of the style. The outermost box is called the margin. It controls the space between the border of the styled object and any other objects around that object, such as images, paragraphs, or tables; padding is the space between the border and the content itself (the innermost box). The area within the border, which includes the content and the padding, may also have a background color. Actually, the background color is drawn underneath the border, so if you assign a dashed or dotted border, the background color appears in the gaps between the dots or dashes.
Note Dreamweaver includes a tool to help you visualize the margins and padding of elements. To learn how to use Inspect mode, see page 494.
You also can’t always tell if the extra space comes from the padding or the margin setting. Dreamweaver includes a helpful diagnostic tool (see Figure 9-12) that lets you see these invisible properties. When you select a
tag with margin or padding properties set, Dreamweaver draws a box around the div and adds slanting lines to indicate the space the margins and padding occupy (Figure 9-12 shows the box and lines in action).
450
Dreamweaver CS6: The Missing Manual
Margins appear outside padding, and Dreamweaver represents them by lines that slant downward from left to right; padding appears inside the margin, and Dreamweaver indicates them with lines that go upward from left to right. When you select a
tag (the Tag Selector discussed on page 26 is a great way to do this), Dreamweaver highlights the margins and padding values defined in that ID style. Visual aids menu
Understanding the Box Model
Figure 9-12
In addition to displaying the space occupied by margins and padding, the CSS Box Model’s visual aids indicate the
tag’s vertical and horizontal center point (circled in this figure). If you mouse over the border, margin, or padding, a yellow box appears listing the values for those properties, in this case a 25 px bottom margin, a 1px bottom border, and 8px of bottom padding.
Margin
Padding
As you can see, there’s a considerable amount of margin on the bottom and a small amount of padding (8 pixels’ worth) applied to the top, left, bottom and right edges. If you find these visual aids confusing, you can turn them off via the Visual Aids menu in the document window (see Figure 9-12) or by choosing View→Visual Aids→Layout Box Model. These same steps turn the margin and padding visual aids back on.
Chapter 9: Page Layout
451
Dreamweaver’s CSS Layouts Frequently Asked Question
When Width Doesn’t Equal Width In my style sheet, the CSS Width property of one of my styles is 150 pixels. But when I preview the page in a web browser, the
tag I applied the style to is much wider than 150 pixels. Is there a bug in my browser? No, you’re browser is fine. The problem lies with the difference between the CSS width property and the final calculated width of an element onscreen. The width you see onscreen is the sum total of several separate CSS properties, not just the Width property. The Width property merely defines the width of the content area of the style—the place where the text, images, or other nested tags sit. The actual width—that is, the amount of screen real estate assigned by the web browser—is the total of the widths of the left and right margins, left and right padding, left and right borders, and the Width property. So say you create a style with a width of 100 pixels, 10 pixels of padding on all four sides, a 2 pixel border, and 20 pixels of space in the left margin. While the space dedicated to the content inside the style is 100 pixels wide, any tag with this style will have an onscreen width of 144 pixels: 100 (width) + 10 (left padding) + 10 (right padding) + 2 (left border) + 2 (right border) + 20 (left margin).
around all other page elements. Its width is 960 pixels, so everything inside that div must fit in that 960-pixel space. If you wanted to create a four-column layout, you might insert four divs and create styles that set the width of each to 240 pixels (960 divided by 4). However, if you added even a 1-pixel border to one of the divs, they’d suddenly take up more than 960 pixels, and you’d find that the last column wouldn’t fit—in fact, it would drop down below the other three columns, creating a very weird-looking layout. So if you find yourself floating lots of elements, and one of them drops below another when it should sit beside it, odds are pretty good that the elements are just too wide to sit side by side. So decrease the width, margins, padding, or borders until the elements fit (breaking out a calculator and adding up the margins, padding, borders, and widths can also help). The CSS Height property and the final height of a style behave the same way. The onscreen height of an element is a combination of the height, top and bottom margins, padding, and borders.
This can cause problems if you’re not careful. For example, in a fixed-width layout, you might create a div that wraps
Dreamweaver’s CSS Layouts You’ll contend with many details when you build CSS-based layouts. For example, you need to understand the intricacies of the CSS Box Model, as well as the sometimes-bizarre behavior of floats. In addition, different browsers handle some CSS properties differently, which sometimes means a design that looks great in Firefox but completely falls apart in Internet Explorer 6. (Remember, even though much of the Windows-loving world has upgraded to IE 8 and IE 9, there are still plenty of folks cruising around the Web in IE 6 jalopies.) Fortunately, Dreamweaver is ready to give you a helping hand with 18 predesigned CSS Layouts.
452
Dreamweaver CS6: The Missing Manual
Dreamweaver’s CSS Layouts aren’t finished web page designs. They don’t have graphics, fancy text, drop-down menus, or any whiz-bang features. They’re basic designs intended to lay the foundation for your design talents. Each layout is a simple HTML file and style sheet, each works with all current browsers, and each design’s handcrafted CSS code irons out the many wrinkles in troublesome browsers (most notably Internet Explorer 6). In other words, instead of spending a day stretching and sizing your own canvas, a Dreamweaver CSS Layout is like going to the art store and buying a ready-made and primed canvas so you can get busy painting.
Dreamweaver’s CSS Layouts
Creating a new CSS layout page takes just a few steps: 1. Choose File➞New. This is the same first step you take when creating any new web page. The New Document window appears (Figure 9-13). You can also use the Ctrl+N (c-N) keyboard shortcut to open this window (however, it’s possible to disable this keyboard shortcut, as described on page 47; you might want to do that if you’d rather skip this clunky window whenever you just want a new, blank web page).
CSS layouts
Selected layout
Preview
Figure 9-13
This Blank Page New Document dialog box lists Dreamweaver’s readymade CSS Layouts. For example, “2 column fixed, right sidebar, header and footer” indicates a design with 2 columns: the main content column on the left, and a thinner sidebar (for supplementary info like links) on the right. The design also sports a fixed width and has header and footer divs. A preview of the selected layout appears in the top-right of the window; a brief description below the preview provides more detail on how the layout works.
2. Choose Blank Page from the left column, and the type of page you wish to create in the Page Type column. Usually, you’ll select HTML from the Page Type category, since most of the time you’ll create regular web pages. However, if you’re creating a page that uses a server-side language like PHP, you’d select it from the list.
Chapter 9: Page Layout
453
Dreamweaver’s CSS Layouts
Tip The Page From Sample category in the left column of the New Document window (see Figure 9-13) is
mostly useless. It includes sample pages with some rudimentary design, but you can write better style sheets on your own. However, Dreamweaver CS6 includes a “Mobile starters” category for creating mobile sites (see on page 537 for a discussion of this feature).
3. From the Layout column, select a page layout. This is where the fun begins. As you’ve read before, choose <none> here to create an empty web page. The other options, however, let you choose one of 18 prefab CSS-based layouts. Basically, you decide how many columns you want (one, two, or three), whether you want a header and footer (like a banner at the top or a copyright notice at the bottom), and the type of web page layout (fixed-width or liquid). A fixed-width design maintains a constant page width no matter the width of a visitor’s browser window. A liquid design lets the overall width of the page change with the size of a browser window. See page 435 for more on these types of layout. Dreamweaver previews each design in the top-right of the New Document window. See Figure 9-14 to decipher the visual