Viewers: in countries Watching now:
This course covers CSS best practices and techniques for styling and structuring forms. Author Ray Villalobos explains how forms work, best practices for structuring HTML, new additions to forms in HTML5, and how to style individual elements. The course discusses how to use CSS to style traditional and custom elements and reviews browser discrepancies, custom elements, and custom fonts and how to style for different types of user feedback. Advanced CSS layouts and design patterns and issues specific to styling for handhelds are also covered.
Some forms can get so complicated that you'll need to find ways of the dividing the information into different sections. In this chapter, I am going to show you how to style a form, so that the different field sets appear as different tabs. This video is part of a series where I'll show you how to use jQuery to handle navigation between field sets as well as how to validate the form and style the validation elements. Let's get started by defining our basic style sheet for our form. So I am going to go to the exercise files and open up the Starting Points folder, find the starting point for this video.
And I am going to copy the Working Folder onto the desktop, holding down the Option key and dragging it, and then I want to take this Working Folder and drag it onto Espresso, so it will open it up as a project. And then I am going to take the codesnippets.txt file and move it into the workspace, and I'm also going to double-click on the index.html file and the mystyle.css, so it also goes into the workspace. So let's take a look at our HTML document and I'm going to hit the Preview button holding down the Option key so that we can get a live preview.
So we have an HTML form with three different field sets; one for login information, another one for other information, and a section for comments. Two of the fieldsets have an additional class called hidden right here, that will hide these fieldsets on the final form. Let me show you what this page is going to look like after we get done with this video. Our page will look like this. So we could do that. We can get started by going into the codesnippets.txt file and scrolling up to the information for our current video.
So we'll copy the links to our normalize.css document and then mystyle.css file and we'll put that in the index document, in the head section. So you could see that some of these styles have already taken effect, we'll go back into codesnippets and grab some additional code for clearing form elements a bit further and to clear any items that were floating. So I am going now to the mystyle.css document and I'm going to paste that and you could see that we are clearing the appearance of most of the input fields to make them all start with similar look.
Back into codesnippets, I am going to get some basic styling for the body section as well as some basic styling for the form, so I am going to copy that and go back into my stylesheet. And here I'm adding some background to the body section, this looks like a lot of code, but it's really just some code for a background texture and a gradient beneath that background texture with different browser prefixes so that it displays in most browsers. Now for the form element we have added a font and some other basic styling plus a border-radius, so that the edges of this form are rounded, as you can see right here, and we have a different texture that we have applied to the form plus some roundness to the edges and a slight drop shadow.
Now we need to fix the fieldset outline and style the legends as well. So we are going to go back into codesnippets, scroll down and I am going to grab this piece of code right here, go back into our stylesheet and just paste that at the bottom. What this is going to do is just style the fieldsets and because the fieldsets display differently in different versions of Internet Explorer and other browsers, we need to do a little trick that we have done before in other videos. And because the legends will display differently in different browsers, especially versions of Internet Explorer, we are going to hide the way the legends are displayed and then display the legends by grabbing the title from the fieldset and styling then that by using a before pseudo tag.
This will work for most versions of Internet Explorer except for versions of Internet Explorer before version 8. So we are going to have to go back into codesnippets and grab another piece of code that will fix this problem in Internet Explorer 7. So we'll grab this little piece of code. This is a conditional stylesheet that applies only if the version of Internet Explorer is less than Internet Explorer 8. So we'll copy that, go back into our index file and paste that in our head section, and then we'll go back into codesnippets to grab some code to handle the list items. By default list items display with numbers right next to them and they're indented quite a bit, so we're going to grab some code to take care of that.
Go back into mystyle.css. I'm going to paste that and you can see that here I took care of some of the margins and the numbers that appear normally on ordered lists, and I also took care of adding a little bit of a border before and after each one of the list items giving the form an embossed look between each one of the fields. And then on the first element and the last element of each list, I deleted the borders so that we don't see these weird kind of borders that will appear at the beginning and at the end of the form.
So if we don't do this, you'll see an extra border appear at the beginning and end of each list item. All right our form is looking pretty good. This field right here needs to be fixed a little bit further. This is a second level ordered list, so we'll go back into codesnippets and we'll grab the code to fix those elements asking them to be displayed as inline elements. So now those appear as inline elements right next to each other, how they should. So back into codesnippets we'll grab a little bit of code to fix our labels.
So back into mystyle.css, and this will style all our labels so that they look really nice now, and notice that I have some basic code for the labels, and then I have a special version of the code for a singleline label, which is a special class that I use for styling this particular label. I don't want this label to be bolded like the other one, and for the labels that are part of a group, so I have this special request type text as a grouphead class because technically it's not a label, although it's a label for these other elements.
Each one of these elements are already a label. So I can't style those. I have to style a headline which I've done with this group class right here. You can see it in the index.html. If I scroll down to that section on the Comments, I have this div class that's acting as our headline, have a class of grouphead so that I can style it like the other labels as well. Back into our codesnippets, now we're going to grab some code to handle the text area and the input field. So these inputs fields right now are just a little bit too short and this text area also needs to be a lot bigger and have a little bit of a better style.
So we'll grab this, back into mystyle.css, I'm going to paste that, and you can see that our fields are now a good width and so is our text area, and they have essentially just a slight gradient so that you can see they are just a bunch of browser prefixes, and so they have a nice little slight gradient for the inside and some other settings here to just give it a little bit of breathing room and width that looks more appropriate to the form.
So back into the codesnippets, now we are going to grab a section for second level inputs. Notice what's happening down here, our second level inputs are displaying kind of right on top of each other, because we've done that to all these other input fields, we've all told them to display right on top of each other so that the labels appear on top. So we need to fix that with this section right here, and we are also going to -- let's go back to mystyle.css. So that kind of fixes most of it.
I am going to refresh the screen because sometimes the live preview in Espresso doesn't refresh appropriately. So you can see now by adding those pieces of code we have fixed our second level input fields, asked them to display as inline, and we've also made sure that on all browsers that the check boxes are also displayed as inline elements that way, this will always be right next to it. Then we have added a little bit of a taller height to our text area to make sure we have plenty of room to write in here.
So back into the codesnippets, so let's now style the select field which looks a little plain, looking white like that, so we're going to grab this piece of code. And this is essentially just some width and height styles plus a gradient that looks really nice. Let's go ahead and apply that so we can see it. Let's see the gradient, it's a slight gradient that just gives it a different color, and we have a bunch of different browser prefixes here as well as some roundness to the edges. So back into codesnippets, just about the only thing that's left is to style the button, so it gets a lot of code, but again most of it is right here in the background gradient that we are applying there.
So I am going to copy that, go back into the mystyle.css area, and I'm going to paste that and you can see that the send button gets some standard width and height. My entire form is using variable width, so you could see that if I resize these forms, the forms will resize up to a certain point. Finally, I am going to go back into my codesnippets to get the hover version of this arrow, and the hover version of the arrow is just going to be a different color background.
So back into the CSS file, and this has the pseudo-class hover here, so all we do here is just change the color of the gradient so that when we roll over this button, we get a different color gradient and these are all just browser prefixes. We've done a lot of styling that you've probably seen in some of the other videos in this series, and this already looks like a great form, but in future videos I am going to show you how to add navigation and interactivity.
There are currently no FAQs about CSS: Styling Forms.
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.