Controlling Line Breaks in a Cell

Controlling Line Breaks in a Cell

Unless you specify otherwise, a browser will divide the lines of text in a cell as it decides on the height and width of each column and row. The nowrap attribute forces the browser to keep all the text in a cell on one line.

Figure 16.65. If one of your cells has a two word entry just a little bit bigger than the others, you may want to avoid the text wrap so the numerical data looks more uniform.

To keep text in a cell on one single line:

In a td or th cell, type nowrap= "nowrap".

Figure 16.66. Just add the nowrap attribute to the TD cell that should not be broken into multiple lines.

Figure 16.67. No matter how narrow the window and the table get, the cell's contents will stay on a single line, even if it means some of the table extends beyond the window.

images/U2713.jpg border=0> Tips

  • In HTML, but not XHTML, you can just type nowrap by itself.

  • Browsers will make the cell (and the table that contains it) as wide as it needs to accommodate the single line of text even if it looks really ugly. I don't recommend using the nowrap tag with tables used for layout. It overrides the width attribute.

  • You can use regular line breaks (br) between words to mark where you do want the text to break.

  • You can also type   instead of a regular space to connect pairs of words or other elements with non-breaking spaces.

  • For more information on line breaks, consult Creating a Line Break on page 66 and Setting White Space Properties on page 164.

Speeding up Table Display

Although tables are extremely powerful, they can be very slow to appear in your visitor's browser. The major factor is that the browser must calculate the width and height of the table before it can begin to display the cells. So, if you can keep the browser's calculations to a minimum, the table will appear more quickly and your visitors may actually wait to see it.

To speed up table display:

  • Keep tables as small as possible. Where you can, divide large tables into smaller ones.

  • Specify the width of the table in pixels (see page 232).

  • Use absolute values (in pixels) or percentages for determining cell width.

  • Only specify proportional widths for cells, columns, and horizontal sections when you've already set a fixed width in pixels for the entire table.

  • Divide your table into column groups.

  • Add table-layout:fixed to your table element's style rule (Figure 16.68). This instructs browsers to look only at the first row of a table in order to determine the widths of the columns, instead of worrying about every cell in every row. While the contents of some cells may not fit (their display is governed by the overflow propertysee page 187), the table renders more quickly.

    Figure 16.68. The table-layout property with a value of fixed helps tables render more quickly. It is useful for tables whose cells are regular in size.

17. Forms

Up to now, all the (X)HTML you have learned has helped you communicate your ideas with your visitors. In this chapter, you'll learn how to create forms which enable your visitors to communicate with you.

There are two basic parts of a form: the collection of fields, labels, and buttons that the visitor sees on a page and hopefully fills out, and the processing script that takes that information and converts it into a format that you can read or tally.

Constructing a form's fields and buttons (pages 254276) is straightforward and similar to creating any other part of the Web page. You can create text boxes, special password boxes, radio buttons, checkboxes, drop-down menus, larger text areas, and even clickable images. You will give each element a name that will serve as a label to identify the data once it is processed. I'll also show you how to format forms with CSS.

Processing the data from a form is only slightly more complicated. While in earlier editions I recommended using Perl to write CGI scripts, I now heartily recommend using PHP. It is easy and straightforward and perfectly suited to making Web pages interactive.

While both PHP and Perl are beyond the scope of this book, and even explaining how to use existing scripts stretches the limits a bit, I have provided some ready-made scripts to help you get started (see pages 256 and 258).