Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member
>> In the previous movie we began cleaning up the form's appearance in CSS, but there are still some improvements that we can make to the alignment of the labels and their fields. Remember that we floated the items in the mainNAV bar to get them to line up nicely side by side. We'll use a similar technique here. We'll float the label elements so that they'll align better with their matching fields. If you're following along with the exercise files, the page we're working on is named departments.html and can be found in the 08_06 folder of the chapter eight exercise files.
In the CSS styles panel click the new CSS rule button at the bottom, choose tag for the selector type. In the Tag menu, scroll through the lists and select label. Keep define in styles.css selected then click okay. In the CSS rule definition dialogue box click on the block category. Click on the arrow of the Text Align and select right. Next click on the box category.
We want the width of each of the labels to be consistent so that their text fields will cleanly line up on their left side, so click in the width field, type six and in the adjacent menu choose Ms. We can't choose a pixel measurement for the label width because we have no idea how large or small the user's font size is. If we set the width in Ms though, it will adjust based on that font size. Remember that one M is roughly equal to two characters. So a width of six M's would give us about 12 characters before the text would start wrapping.
This is fine for the labels in our particular form. When you're making your own forms, simply adjust this width to fit the length of your longest label. Next, uncheck the same for all box under padding. Click in the right field and enter .5 and then choose Ms from the adjacent menu. This gives us a little bit of space between the right side of the text label and its form field. Finally, click on the float menu and choose left. Now click okay.
Let's preview the page in a browser and see how it's doing. Click on the globe icon in the document tool bar and choose preview in Firefox. A dialogue box will open saying that additional files need to be saved for the preview to work. What this is referring to is the fact that when we made a change to the style sheet, Dreamweaver opened it up but did not yet save it in case we wanted to undo the styles that we added. Before we can preview departments.html Dreamweaver needs to save styles.css, so click yes for it to save that file.
Now our preview opens in Firefox. You'll see that all of the labels are nicely aligned on the right hand side and all of the fields are nicely aligned on the left. There's plenty of space between each field for it to remain readable. Now let's scroll down the page and look at the other field sets. Unfortunately, the labels and radio buttons are very misaligned now in the second field set. The third field set looks okay. But this long label looks a little bit awkward off to the left of its field. What we can do is create special classes for these special cases.
The styles that we just added will work with a majority of the fields in most forms, since most forms are heavily made up of text fields. But for the cases where we want labels to be to the right of its field, or for the cases where the label is long and we want it to be above its field, we'll need to create special classes. Close Firefox. Click on the new CSS rule button at the bottom of the CSS styles panel. We're going to create a special class any time we have a question with multiple options using radio buttons or checkboxes.
We'll name this class options. So delete the default text in the selector field and type .options, but we don't need to style this class itself, instead we need to style the label within this class. That's because what we're trying to do here is override the styles on the label tag that we just set. So now let's specifically target labels that we within items with the class of options. So leave advanced chosen for the selector type. And as a selector, after you've typed .options, type space label.
Leave define in styles.css selected and click okay. We want to remove the floating and the right text alignment, so first click on the block category. Click on the Text Align menu and choose left then click on the box category. Place your cursor inside the width field and type auto. This overrides the width that we previously set on the label element. Auto simply means let the element be as wide as it needs to be.
This will allow the options beside radio buttons and checkboxes to be as long or short as they need to be. Since they're aligned with their radio buttons and checkboxes cleanly on the left side of the text by default, we don't need to worry about setting the width for them to get them to nicely line up like we did with labels that were to the left of text fields. Next, select the float menu and choose none. Again, we're simply overriding the float property that was set on the label element earlier.
Now click okay. We haven't yet applied this class in the page, so there won't be anything to see in Dreamweaver or in a browser. Before we apply the options class, let's create the second class that we'll need for labels that are long and need to be aligned over their fields instead of to the left. Click on the new CSS rule button, leave advanced chosen as the selector type and delete the text in the selector field. These types of long labels are usually rather explanatory in nature so I'm going to call the class explanatory.
Type .explanatory label. Leave define in styles.css selected and click okay. What we want to happen here is again remove the float width and right text alignment. But we also want to tell the label to sit on its own line above its field. We can do that using the display block property. Remember that block elements stack on top of each other. Click on the block category in the CSS rule definition dialogue box then click on the Display menu and choose block from the list of options.
Next click on the Text Align menu above and choose left. Now click on the box category, type auto in the width field, and select none in the Float menu, now click okay. We now have our classes ready to apply. Scroll down the page in design view. Click on the email label of the first radio button, then click on the LI tag shown in the tag selector. Right click or control click on this tag and select quick tag editor, then type space, and a list appears of possible attributes you can add to this tag.
Class is as the top of the list, so double click on class and you'll then be shown a new menu with classes defined in styles.css that we can choose. Choose the options class. Double click on the options class to choose it. You can see that immediately the label jumps back up by the radio button. Repeat this for the phone label of the second radio button. Click inside that label then click on the LI tag in the tag selector. Click inside the phone label then right click or control click on the LI tag in the tag selector.
In the menu that appears choose quick tag editor, type a space and double click on class from the menu that appears. In the second menu then, double click on options. We're done with our radio buttons, so scroll the rest of the way down the page and click inside the last label on the page for the large text area field. Right click or control click on the LI tag in the tag selector and choose quick tag editor. Type space, double click on class from the menu and then this time, double click on explanatory from the second menu that shows.
Again, the label immediately changes its alignment. This time above the form field. Go to file > save to save your page. Then click over to styles.css to save this file as well. Go to file > save. Click back in departments.html. We're now ready to preview in the browser. Click on the globe icon in the document tool bar and select preview in Firefox. Our first field set hasn't changed in appearance.
Our second and third field sets are much improved. Now the radio button items are nicely aligned with their labels. And the long label in the third field set sits above the text area. This is just the beginning of the styles that you can apply to forms using CSS. One thing to note about styling forms, however, is that the operating system has some control over the appearance of form elements, so you can't greatly alter the appearance of the actual form controls themselves. Although this may be a negative for branding purposes, it can be a positive for accessibility because it keeps the form controls from looking radically different than they do on other sites do that users know how to interact with your form correctly.
Get unlimited access to all courses for just $25/month.Become a member
Access exercise files from a button right under the course name.
Search within course videos and transcripts, and jump right to the results.
Remove icons showing you already watched videos if you want to start over.
Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.
Click on text in the transcript to jump to that spot in the video. As the video plays, the relevant spot in the transcript will be highlighted.
Your file was successfully uploaded.