This course will be retired on January 6, 2020. We recommend "HTML Basics" for up-to-date content.
You've already started watching Fieldsets and Labels
In this video, we'll learn how to use the fieldset, legend, and label elements. These three elements will allow us to organize our forms more clearly.
-
0:00
[MUSIC]
-
0:06
We have a lot of ways for users to input data but
-
0:08
we could certainly do more to organize our forms.
-
0:12
In this video we'll learn about fieldsets, legends and labels.
-
0:17
In the previous two videos we started creating a form and
-
0:20
we're gonna build upon that here.
-
0:23
So, let's switch over to our text editor.
-
0:27
And one thing you may have noticed about our form
-
0:30
is that we don't really have labels on a whole lot of things.
-
0:34
For example, the username and password are completely unlabeled.
-
0:38
So if you came to that form without knowing about the form,
-
0:43
it would be pretty confusing.
-
0:44
So it's good to always have labels assigned to your elements.
-
0:50
So, to start out, we're going to actually just break up our form
-
0:56
into two separate sections so that it's a little bit easier for the users to digest.
-
1:02
So to do that were going to use the fieldset element.
-
1:07
So we'll open that there and lets go ahead and
-
1:10
close it just after our favorite foods here.
-
1:15
So, close the fieldset there and then indent all of our elements there.
-
1:22
And then just after the opening of our fieldset, we'll go ahead and
-
1:26
create a legend tag.
-
1:29
And we'll close the legend here.
-
1:33
And, inside of our legend, we'll say Important Information.
-
1:40
We'll save that out.
-
1:42
Switch back and refresh, and
-
1:45
we now have this really nifty box around our Important Information.
-
1:50
So, we should probably create another fieldset down here for the optional items.
-
1:55
So, we'll go ahead and
-
1:57
just copy a little bit of the beginning of our fieldset here with the legend.
-
2:03
And just after our text area here, we'll throw in our fieldset.
-
2:09
And then we want to close this right before the submit button.
-
2:12
So we'll close our fieldset here.
-
2:15
And then let's indent everything so it's in line with the legend, and
-
2:21
in line with the fieldset.
-
2:23
We'll save that and let's actually go ahead and
-
2:26
change this label to say Optional Items.
-
2:31
And if we refresh the browser that's what we've got.
-
2:35
Now, we can go ahead and
-
2:36
actually center these legends to the center of our fieldsets if we want.
-
2:41
To do that, you can go ahead and use the align attribute, and
-
2:47
set it to center on the legend.
-
2:49
So if we refresh the page,
-
2:51
you can see that our Optional Item's label is now centered.
-
2:57
Next, we need to add labels to each one of our form elements.
-
3:02
So let's go ahead and do that, starting with the username text input.
-
3:08
To add a label, we just need to add a label tag, just like that.
-
3:13
And we'll go ahead and close the label tag.
-
3:16
And inside of it we'll say, Username.
-
3:20
Now, how are we going to know which form element this label is associated with?
-
3:25
Well, we're going to add a for attribute, and we're gonna call it username.
-
3:31
Now, you would think that this would be associated with a name, but
-
3:35
it's actually not.
-
3:37
We need to add an id attribute to this input.
-
3:41
And we'll just set it to the same name of username.
-
3:45
And that will associate this label with this form element.
-
3:50
So, if we save that, switch back and refresh.
-
3:53
You can see that we now have a Username next to this form element.
-
3:58
The neat thing about this is that when we associate it with an ID we can actually
-
4:03
click on the label Username and it will focus into the form field automatically.
-
4:12
So let's go ahead and add the rest of these labels.
-
4:15
I'll copy this here and paste it just above our Password.
-
4:19
[BLANK_AUDIO]
-
4:22
And of course we need to add the id of password.
-
4:28
And then we need to add a few labels for our radio buttons and for our check boxes.
-
4:35
So let's just paste this right in here, and that said, Male.
-
4:42
And then, of course we need to add id.
-
4:47
And we'll change the for attribute just like that.
-
4:50
Change the id of female.
-
4:53
And then add a label around it.
-
4:55
[BLANK_AUDIO]
-
5:01
And then change the for label
-
5:04
[BLANK_AUDIO]
-
5:07
And I'm going to just cheat a little bit and actually copy and
-
5:12
paste from off screen to get the rest of these check boxes in.
-
5:16
[BLANK_AUDIO]
-
5:20
So you get the idea, the for attribute needs to match up with the id
-
5:24
attribute of these check boxes to match up with this label.
-
5:31
And then we should probably add a label for our text area and for our select here.
-
5:38
So let's do that, add a label for our essay and we'll add the id essay.
-
5:45
[BLANK_AUDIO]
-
5:49
And we need to go ahead and close this, of course.
-
5:54
And we'll say something more friendly like,
-
5:57
We want to know more about you, with a line break.
-
6:04
Write a short essay about yourself.
-
6:08
[BLANK_AUDIO]
-
6:10
So we'll save that.
-
6:12
And then finally we'll add a label for our select.
-
6:17
So add the label, and we'll say for continents,
-
6:23
close the label, and say, Where are you from?
-
6:31
And, we'll add a line break after that.
-
6:35
And then on our select, we should probably have a name on there so,
-
6:41
let's go ahead and add that in now.
-
6:43
And then we need to have an id for it to match up with the label.
-
6:47
So we'll add that in.
-
6:49
And when we save and refresh the page, it looks like we have
-
6:53
a couple of errors going on here, but we can go ahead and fix those.
-
6:58
For the most part, when you click on these labels,
-
7:01
they should actually select the item you would expect them to.
-
7:06
So let's go ahead and see what's going on with this markup here.
-
7:10
We'll scroll up and
-
7:13
it looks like we actually may have pasted in something twice here.
-
7:19
So we'll just get rid of this here.
-
7:22
[BLANK_AUDIO]
-
7:26
Save that out and refresh.
-
7:28
And there we go.
-
7:30
Now, here we have another issue with the text area.
-
7:34
And we need to actually just add in a line break there, right after our label.
-
7:40
So after our label, add in a line break.
-
7:44
Refresh the page.
-
7:45
And now we just have that nice label at the top.
-
7:48
And our text area.
-
7:50
So again, you can click on the label and
-
7:53
it will focus on the appropriate form element.
-
7:58
Pretty nifty.
-
8:01
Now, it looks like our select element wasn't focusing.
-
8:04
That's because our for attribute and id were not the same.
-
8:09
It was continent instead of continents, so if we refresh, that looks correct now.
-
8:15
We can click on that label and the select will be selected.
-
8:19
And one last thing I would like to emphasize before we close this out
-
8:24
is that, your name attribute in your id attribute,
-
8:29
or your name attribute and for attribute don't actually need to be the same.
-
8:35
However, it's a good idea to keep them the same for the sake of clarity.
-
8:39
However, what is important is that your for
-
8:42
attribute and your id attribute are the same.
-
8:46
[MUSIC]
-
8:48
That covers the basics of forms.
-
8:50
Later on, if you start to learn about server side programming,
-
8:53
you'll learn how to process the data from your forms.
-
8:56
[SOUND]
You need to sign up for Treehouse in order to download course files.
Sign up