Free Web 2.0 Code Generator! Generate data entry and reporting .NET Web apps in minutes. Quickly create visually stunning, feature-rich apps that are easy to customize and ready to deploy. Download Now!
Web Forms - Use fieldset to Group Form Sections
(Page 8 of 9 )
Use <fieldset> to group form sections
Using the <fieldset> element is a handy way of grouping form controls into sections. Additionally, adding a descriptive
First though, let’s take a look at what the markup looks like when creating field sets. We’ll add one to our example form:
Figure 5-12 shows us how our form appears in a typical browser with the <fieldset> and <legend> tags added, along with the CSS that we’re applying to the elements. You’ll notice the stylish border that surrounds the form controls that fall within the <fieldset> tags, with the <legend> breaking the border at the top left of the box.
Figure 5-12. Our form example with the <fieldset> and <legend> added.
The reason I call it “stylish” is because, for a default rendering, with no CSS added at all, it’s rather impressive. And it can get even more interesting when we choose to add a bit more customization, which we’ll do next.
You may also begin to see how useful <fieldset> could be for grouping different sections of a form together. For instance, if our example form was the first part of a larger form that had other groups contained in it, using <fieldset> to visually box those sections off is a semantically rich way to make our forms more organized and readable.
Adding style to <fieldset> and <legend>
We can customize the appearance of the default <fieldset> border and <legend> text with CSS just as easily as with any other element. First, let’s change the border’s color and width, and then we’ll modify the text itself.
To stylize <fieldset>'s border,making it a bit more subtle, we’ll use the following CSS:
We also specify a 20-pixel margin on both the right and left, with zero margins on both top and bottom. Why zero margins? Because our form labels and controls are wrapped in <p> tags, there’s already enough padding at both the top and bottom.
Figure 5-13. Our form example with <fieldset> styled.
This chapter is from Web Standards Solutions: The Markup and Style Handbook, by Dan Cederholm (Apress, 2004, ISBN: 1590593812). Check it out at your favorite bookstore today.