Using Flexible Widths Sensibly
Believe it or not, flexible tables are often harder to create than fixed-width tables. This is because although you want the overall table to have a flexible width, you probably don't want all columns to resize. Generally, flexible tables are built with one flexible column and one or more fixed-width columns. Getting this column to flex while that one doesn't takes a few extra steps.
Flexible tables that mix fixed and flexible columns almost always do better with a control row to help them. For each fixed-width column in the control row, insert a spacer graphic as described in the previous section, sized to the desired column width. For the flexible-width column, insert a spacer graphic sized to whatever minimum width you want for this column; then assign the cell itself a width of 100%. That means the cell should take up 100% of the space available to it, which is the entire width of the table, minus the fixed widths being propped open by the spacer graphics in your other columns. Figure 8.16 shows the control row set up for a flexible table.
Figure 8.16 A flexible-width table using a control row for stabilization.
If you want to make sure the right columns are flexing in your flexible-width table, try temporarily assigning each cell a different background color. When you preview in the browser, you'll see clearly what's flexing and what isn't.
Making Your Tables Accessible
According to some accessibility pundits, tables are inaccessible tools of the devil and should never be used. But really, there's nothing wrong with using tables for displaying data or organizing page layout, as long as you follow the rules for making them accessible. It's not even that hard!
Accessibility for Data Tables
Section 508 has very little to say about tables, and what it does say applies only to data tables. Section 508, § 1194.22(g) and 1194.22(h), state:
Row and column headers shall be identified for data tables.
Markup shall be used to associate data cells and header cells for data tables that have two or more logical levels of row or column headers.
The first of these rules is applicable to any simply structured data table and can be accomplished using the th (table header) tag with the id attribute and the td (table data) tag with the headers attribute:
<table> <tr> <th id="State">State</th> <th id="Capital City">Capital City</th> </tr> <tr> <td headers="State">New Mexico</td> <td headers="Capital City">Santa Fe</td> </tr> </table>
The second rule applies to more complex data tables, where more than one axis of information is presented. It can be taken care of by using the scope attribute to specify whether the header cell controls a row or column:
<table> <tr> <th> </th> <th id="Store #1" scope="row">Store #1</th> <th id="Store #2" scope="row">Store #2</th> </tr> <tr> <th id="Chocolate Bars" scope="col">Chocolate Bars</th> <td headers="Store #1 Chocolate Bars">355</td> <td headers="Store #2 Chocolate Bars">20</td> </tr> <tr> <th id="Popsicles" scope="col">Popsicles</th> <td headers="Store #1 Popsicles">25</td> <td headers="Store #2 Popsicles">500</td> </tr> </table>
Caption and Summary
Section 508 says nothing about using captions or summaries for tables, although the W3C's Web Accessibility Initiative (WAI) suggests a priority 3 guideline to provide summaries for tables. However, many accessibility experts suggest using these to enhance your table's accessibility. A table's caption is a short sentence summarizing the contents of the table, which will appear in all browsers and can be visually styled using CSS. It looks like this in the code:
<table> <caption>States and their capital cities</caption> [etc] </table>
A summary is similar in functionality, but will not appear in a graphical browser. It looks like this in the code:
<table summary="States and their capital cities"> [etc] </table>
Because a caption and summary perform the same task, you don't need both.
Accessibility for Layout Tables
Section 508 says nothing about layout tables. Although the W3C generally frowns on using tables for layout purposes (believing you should be using CSS instead), it offers the following guidelines for ensuring that they're accessible:
Do not use tables for layout unless the table makes sense when linearized. Otherwise, if the table does not make sense, provide an alternative equivalent (which may be a linearized version). [priority 1]
If a table is used for layout, do not use any structural markup for the purpose of visual formatting. [priority 2]
Keeping in mind that screen readers read tables in a linear fashion, from left to right and top to bottom, make sure that whatever content is in your layout table makes sense when read that way. If it doesn't, either restructure the table or provide a link to another page that contains the same material presented in a linear fashion (that is, without a table). Furthermore, don't use any of the markup usually associated with data tablestable headers, captions, summaries, and so onfor layout tables. Structurally (as well as graphically), layout tables should be invisible.
Making Tables Accessible in Dreamweaver
The Dreamweaver Table object makes it easy to create accessible data and layout tables. Just think carefully about how you fill out the options in the Insert Table dialog box, and the job is pretty much done. After you've created a table, it's harder to add, edit, or remove the accessibility attributes because most of them don't appear in the Property Inspector.
Header Cells and the scope Attribute
When you insert a table that will be used to present tabular data, be sure to choose one of the Headers options in the Insert Table dialog box to place your header cells in the first row, first column, or both. The table will be created with th tags for these cells, with the scope attribute already applied to each (see Figure 8.17). Pretty nice!
If you're creating a layout table, be sure to choose the None option for Headers, and no th tags will be added to the table.
After you've created the table, if you change your mind about what should and shouldn't be a header, select each cell in question and use the Header option in the Property Inspector to turn cells into headers or data cells. If you do this, however, be sure to keep track of the scope attribute! If you change existing header cells into data cells (by deselecting the Header option), the scope attribute remains in place, which it shouldn't because data cells don't have a scope. Because scope doesn't appear in the Property Inspector, you need to edit the code directly or use the Selection Inspector or the Tag Editor dialog box (Edit > Tag) to remove it. If you turn data cells into header cells (by selecting the Header option), no scope attribute is added. According to Section 508, you don't need the scope unless you have more than one level of header; but if you do have a complex table that requires scoping, you'll have to add the correct scope attribute by editing the code or using the Selection Inspector. Dreamweaver also won't stop you from doing something illegal, such as having too many header cells or having headers in illogical positions.
Figure 8.17 Creating header cells with the Table object.
Captions and Summaries
To add a caption or summary to a table while you're inserting it, just fill in the appropriate fields in the Insert Table dialog box. The caption displays in Design view (as well as in the browser window), so it's easy to see, select, and edit its text after it's in place. To edit the caption properties (such as alignment), use the Selection Inspector. The summary won't display, so you'll need to use the Selection Inspector or Tag Editor to work with itor even to remind yourself that it's there.
What if your table already exists and you want to add one of these items? The summary is coded as an attribute to the table tag, so you can select the table and use the Selection Inspector to add it.
It's a trickier proposition to add a caption to an already existing table. Because the caption is coded in its own tag and not as a table tag attribute, you can't use the Edit Tag command or Selection Inspector to create it. But you can do it in Code view:
In Design view, select the table. (This is just to help you find the code for the table after you switch views.)
Switch to Code view or Code and Design view, and find the opening table tag. Put the insertion point immediately after the tag.
In the HTML Insert bar, from the Table object group, choose the Table Caption object (see Figure 8.18). This inserts an empty caption tag in the code.
Between the opening and closing caption tags, enter the text you want for your caption.
Figure 8.18 Inserting a caption with the Table Caption object.
For more in-depth information on table accessibility, see one of the books written specifically on web accessibility, such as Joe Clark's excellent Building Accessible Websites (New Riders, 2001).
CSS and Your Tables
Cascading Style Sheets make it possible to do some wonderful things to table display, all without cluttering up your table code with extra attributes. As always with CSS, be sure to preview in your target browsers to make sure your CSS formatting is supported. Some older browsers (such as Netscape 4) don't interpret all aspects of CSS table formatting properly.
Applying CSS to Tables
You can apply CSS to tables by redefining the table, tr, th, or td tags in your style sheet; or, you can define custom classes or IDs and apply those to any of the table tags.
To apply a custom class or ID to a table, select the table and use the Class drop-down menu in the Table Property Inspector. Or, right-click on the table tag in the Tag Selector and choose Set Class or Set ID from the contextual menu (see Figure 8.19).
Figure 8.19 Using the Tag Selector to apply a custom class to a table.
To apply a custom class or ID to a table cell, select the cell and use the Class drop-down menu in the top portion of the Property Inspector, or right-click the td, th, or tr tags in the Tag Selector and choose Set Class or Set ID from the menu.
CSS Properties and Table Formatting
Applying CSS to your tables can replace traditional HTML formatting for borders, backgrounds, widths, alignment, and even cell padding in tables. No matter which of the following properties you choose to style with CSS, remember to remove those properties from your table's HTML so you don't have any formatting conflicts.
To create a border as part of your table's style, create a new style and, in the CSS Style Definition dialog box, go to the Border category (see Figure 8.20). Here you can choose to assign a border to all four sides of a table element or only certain sides. You can specify solid, dashed, dotted, and other border styles (although not all are equally well supported in browsers, so be sure to check your results). Your border can be a pixel-based or other width. And it can be any color. You can also mix and match so that maybe the top border is black and fat, while the bottom border is gray and thin.
You can apply a border effect to any table element, but the tr tag doesn't always display borders properly, so you might want to apply it to tables and cells only.
Figure 8.20 Applying a border to a table style.
Background Colors and Images
Tables, rows, and cells can also have background colors and images applied to them using CSS styles. Create a new style and, in the CSS Style Definition dialog box, choose the Background category (see Figure 8.21). If you choose a background image, you can also control whether it tiles and how it positions itself within the element by using the Repeat, Horizontal Position, and Vertical Position options.
Figure 8.21 Applying a background image and color to a table style.
Width and Cell Padding
Table and cell width, and table cell padding can be controlled by CSS box properties. To access these properties in the CSS Style Definition dialog box, choose the Box category. Set the width field to any pixel, percent, or other type of value you desire. To adjust cell padding, set the Padding options. Warning, though: Not all browsers treat the CSS padding property well, so either preview carefully in your target browsers or don't use CSS to control cell padding. Cell spacing is also better coded without CSS.
Aligning Cell Contents
You can center cell contents horizontally and vertically by using the CSS block properties. To access these properties in the CSS Style Definition dialog box, choose the Block category. Use the Text Align field (for horizontal alignment) and the Vertical Alignment field (for vertical alignment).
Exercise 8.1 Creating a CSS-Styled Data Table
In this exercise, you'll build a data table and create CSS styles for table formatting. Along the way, you'll make sure it's accessible. Before you start, download the chapter_8 folder from the book's website at http://www.peachpit.com to your hard drive. Define a site called Chapter 8, using the chapter_8 folder as the local root folder.
Create a new file. Save it in your site folder as holidays.html. Figure 8.22 shows what you're going to create in this file.
Working in either Standard or Expanded Tables modes, insert a table with the following settings:
Enter the following headings into the header cells across the top of the table: Holiday, Date, Images, Activities. In the next row, enter your favorite holiday and its information. Keep adding rows and holiday entries until you have four or five entries. (Remember, you can press Tab while in the last cell of the table to quickly add a new row.)
Now that you have data in the table, select it and remove the width. The data will hold it open.
Open the CSS Styles panel and click the New Style button to create a new style. Create a new custom class called .holiday for this document only (see Figure 8.23). Click OK to open the CSS Styles Definition dialog box.
This custom class will be applied to the entire table. It's going to hold only two items: text-formatting options and a width indicator. In the Type category of the CSS Style Definition dialog box, set whatever type formatting you likefont, size, color, and so on. When the style is applied, this will govern all type within your table. Go to the Box category and set the Text Align option to Left. This will left-align all text within the table.
In the Document window, place the insertion point inside the table you created. In the Tag Selector, find the table tag and right-click on it. From the contextual menu, choose Set Class > Holiday (see Figure 8.24). You just used the Tag Selector to help you select a table and apply a CSS class. Very nice! The text in your table changes its formatting, and the table width stretches across the page.
You want to apply a certain set of style formatting to all header cells in the table, but not necessarily to all header cells in all tables throughout the document. So, you'll define a contextual selector that applies only to header cells that occur within elements governed by the holiday class.
Now you'll do the same thing to format all data cells within the holiday table. In the CSS Styles panel, click the New Style button. Create a new CSS Selector with the name .holiday td. In the CSS Style Definition dialog box, go to the Border category. Set a solid, black, 1-pixel border across the top only (see Figure 8.26). Click OK to close the dialog box and create the style.
To control the appearance of the caption, you'll redefine the caption tag. (This will affect all captions, regardless of whether they're part of a holiday table.) In the CSS Styles panel, click the New Style button and redefine caption. In the CSS Style Definition dialog box, to increase the space between the caption and the table, go to the Box category and set a bottom padding of 10 (see Figure 8.27). When you're done, click OK to close the dialog box.
Finally, experiment with a table background. In the CSS Styles panel, select the holiday style and click the Edit Style button. When the dialog box opens, go to the Background category and click the Browse button to choose a background image. From the category_08 folder, choose the brown_ribbon.gif file. Move the dialog box so you can see the table, and click the Apply button. Experiment with different settings to see how they look. To re-create the example shown in Figure 8.22, set the Repeat to No-Repeat and the Vertical and Horizontal positions to Center and Bottom, respectively. When you're done, click OK to close the dialog box.
Take your CSS-styled table for a test drive in as many different browsers as you can. How does it fare?
Figure 8.22 The holidays.html table as it will look when completed.
For headings, choose to create a row of header cells across the top of the table (a look at Figure 8.22 will tell you why). For the caption, enter Holiday Checklist. Leave the summary blank. Clock OK to close the dialog box and insert the table.
Figure 8.23 Creating the holidays custom class.
To set the table width, go to the Box category. In the width field, type 100%.
Click OK to close the dialog box and create the custom class.
Figure 8.24 Using the Tag Selector to select a table and apply a CSS class to it.
In the CSS Styles panel, click the New Style button. Create a new CSS Selector with the name .holiday th (see Figure 8.25). In the CSS Style Definition dialog box, go to the Background category and assign a background color. Click the Apply button to see the new style take effect. Adjust the background color, if you like. Then click OK to close the dialog box and create the style.
Figure 8.25 Creating a contextual selector to format all headers within a holidays-styled element.
Figure 8.26 Creating a top border for each table cell in the holidays table.
Figure 8.27 Using padding to separate a table caption from its table.