Responsive Web Dedign(四)
freeCodeCamp —- Responsive Web Dedign
Learn HTML Forms by Building a Registration Form
Welcome to the Registration Form project! Start by adding the declaration at the top of the document so the browser knows what type of document it’s reading.
!DOCTYPE html
Below the
DOCTYPE
, add anhtml
element with alang
attribute set toen
, so that you have a place to start putting some code.Next, add opening and closing
head
andbody
tags within thehtml
element.Add a
title
and ameta
element to thehead
. Give your project a title ofRegistration Form
, and give acharset
attribute with a value ofUTF-8
to yourmeta
element.Nest a self-closing
link
element within thehead
element. Give it arel
attribute with value ofstylesheet
and anhref
attribute with a value ofstyles.css
.Within the
body
, provide a heading context for the content, by adding anh1
with the textRegistration Form
.Below the heading, use the following text within a paragraph element to encourage users to register:
1
Please fill out this form with the required information
The
vh
unit stands for viewport height, and is relative to 1% of theheight
of the viewport.It is time to spruce the project up with some CSS. Begin by giving the
body
awidth
of100%
, and aheight
of100vh
.Now, get rid of the horizontal scroll-bar, by setting the
body
defaultmargin
added by some browsers to0
.That is better. Now, make the background easy on the eyes, by changing the
body
background-color
to#1b1b32
. Then, to see the text, change thecolor
to#f5f6f7
.As suggested by the title, you are creating a form. So, after the
p
element, insert aform
with anaction
attribute targetinghttps://register-demo.freecodecamp.org
.The
method
attribute specifies how to send form-data to the URL specified in theaction
attribute. The form-data can be sent via aGET
request as URL parameters (withmethod="get"
) or via aPOST
request as data in the request body (withmethod="post"
).Set the
method
attribute to send your form data via aPOST
request.As the form will have three distinct sections, add three
fieldset
elements within theform
element.The first
fieldset
will hold name, email, and password fields. Start by adding fourlabel
elements to the firstfieldset
.Add the following text to the
label
elements:Enter Your First Name:
Enter Your Last Name:
Enter Your Email:
Create a New Password:
The
rem
unit stands for rootem
, and is relative to the font size of thehtml
element.As
label
elements are inline by default, they are all displayed side by side on the same line, making their text hard to read. To make them appear on separate lines, adddisplay: block
to thelabel
element, and add amargin
of0.5rem 0
, to separate them from each other.Nest an
input
element within eachlabel
. Be sure to add eachinput
after thelabel
text, and include a space after the colon.Following accessibility best practices, link the
input
elements and thelabel
elements together using thefor
attribute.Use
first-name
,last-name
,email
, andnew-password
as values for the respectiveid
attributes.Specifying the
type
attribute of a form element is important for the browser to know what kind of data it should expect. If thetype
is not specified, the browser will default totext
.Give the first two
input
elements atype
attribute oftext
, the third atype
attribute ofemail
, and the fourth atype
attribute ofpassword
.The
email
type only allows emails with a@
and a.
in the domain. Thepassword
type obscures the input, and warns if the site does not use HTTPS.The first
input
element with atype
ofsubmit
is automatically set to submit its nearest parentform
element.To handle the form submission, after the last
fieldset
element add aninput
element with thetype
attribute set tosubmit
and thevalue
attribute set toSubmit
.At this point, you should be able to submit the form. However, you might notice not much happens.
To make the form more interactive, add the
required
attribute to theinput
elements in the firstfieldset
.Now, if you try to submit the form without filling in the required fields, you will see an error message.
Certain
type
attribute values come with built-in form validation. For example,type="email"
requires that the value be a valid email address.Add custom validation to the password
input
element, by adding aminlength
attribute with a value of8
. Doing so prevents inputs of less than 8 characters being submitted.With
type="password"
you can use thepattern
attribute to define a regular expression that the password must match to be considered valid.Add a
pattern
attribute to the passwordinput
element to require the input match:[a-z0-5]{8,}
The above is a regular expression which matches eight or more lowercase letters or the digits
0
to5
. Then, remove theminlength
attribute, and try it out.Let us go to the next part of the registration form. This section will ask for the type of account the user is opening, and will confirm the user has read the terms and conditions.
Start by adding three
label
elements to the secondfieldset
.Users will be allowed to choose either a
Personal Account
orBusiness Account
.To do this, within each of the first two
label
elements, add oneinput
element withtype="radio"
.For the terms and conditions, add an
input
with atype
ofcheckbox
to the thirdlabel
element. Make thisinput
elementrequired
because users should not sign up without reading the terms and conditions.Within each corresponding
label
element, and immediately after theinput
element, add a space and add the following text:1
2
3Personal Account
Business Account
I accept the terms and conditionsYou only want one radio input to be selectable at a time. However, the form does not know the radio inputs are related.
To relate the radio inputs, give them the same
name
attribute with a value ofaccount-type
. Now, it is not possible to select both radio inputs at the same time.Follow accessibility best practices by linking the
input
elements and thelabel
elements in the secondfieldset
.Use
personal-account
,business-account
, andterms-and-conditions
as values for the respectiveid
attributes.To finish this
fieldset
off, link the textterms and conditions
in the thirdlabel
to the following location:1
https://www.freecodecamp.org/news/terms-of-service/
Moving on to the final
fieldset
. What if you wanted to allow a user to upload a profile picture?Well, the
input
typefile
allows just that. Add alabel
with the textUpload a profile picture:
, and nest aninput
accepting a file upload.Add another
label
after the first, with the textInput your age (years):
. Then, nest aninput
with thetype
ofnumber
.Next, add a
min
attribute to theinput
with a value of13
because users under the age of 13 should not register. Also, users probably will not be over the age of 120; add amax
attribute with a value of120
.Now, if someone tries to submit the form with values outside of the range, a warning will appear, and the form will not submit. Give it a try.
Adding a dropdown to the form is easy with the
select
element. Theselect
element is a container for a group ofoption
elements, and theoption
element acts as a label for each dropdown option. Both elements require closing tags.Start by adding a
select
element below the twolabel
elements. Then nest 5option
elements within theselect
element.Nest the
select
element (with itsoption
elements) within alabel
element with the textHow did you hear about us?
. The text should come before theselect
element.The dropdown options are currently empty. To give them content, add the following text to each subsequent
option
element:1
2
3
4
5(select one)
freeCodeCamp News
freeCodeCamp YouTube Channel
freeCodeCamp Forum
OtherSubmitting the form with an option selected would not send a useful value to the server. As such, each
option
needs to be given avalue
attribute. Without which, the text content of theoption
will be submitted to the server.Give the first
option
avalue
of""
, and the subsequentoption
elementsvalue
attributes from1
to4
.The
textarea
element acts like aninput
element of typetext
, but comes with the added benefit of being able to receive multi-line text, and an initial number of text rows and columns.Users will be able to register with a bio. Add a
label
with the textProvide a bio:
at the end of thefieldset
. Add atextarea
element inside thelabel
element. Note that thetextarea
requires a closing tag.Link the applicable form elements and their
label
elements together.Use
profile-picture
,age
,referrer
, andbio
as values for the respectiveid
attributes.The
textarea
appears too small. To give it an initial size, you can add therows
andcols
attributes.Add an initial size of
3
rows and30
columns.To give Campers an idea of what to put in their bio, the
placeholder
attribute is used. Theplaceholder
accepts a text value, which is displayed until the user starts typing.Give the
textarea
aplaceholder
ofI like coding on the beach...
.With form submissions, it is useful, and good practice, to provide each submittable element with a
name
attribute. This attribute is used to identify the element in the form submission.Give each submittable element a unique
name
attribute of your choosing, except for the tworadio
inputs.The HTML for the registration form is finished. Now, you can spruce it up a bit.
Start by changing the font to
Tahoma
, and the font size to16px
in thebody
.Center the
h1
andp
elements by giving them amargin
of1em auto
. Then, align their text in thecenter
as well.Center the
form
element, by giving it amargin
of0 auto
. Then, fix its size to a maximum width of500px
, and a minimum width of300px
. In between that range, allow it to have awidth
of60vw
.During development, it is useful to see the
fieldset
default borders. However, they make the content appear too separated.Remove the
border
, and add2rem
of padding only to the top and bottom of eachfieldset
. Be sure to remove thepadding
from the left and right.To give the
fieldset
elements a bit of separation, select them and give them aborder-bottom
of3px solid #3b3b4f
.The border of the last
fieldset
element looks a little out of place. You can select the last element of a specific type using thelast-of-type
CSS pseudo-class, like this:1
p:last-of-type { }
That will select the last
p
element. Create a new selector that targets the lastfieldset
element and set itsborder-bottom
tonone
.It would be nicer to have the
label
text appear above the form elements.Select all
input
,textarea
, andselect
elements, and make them take up the full width of their parent elements.Also, add
10px
ofmargin
to the top of the selected elements. Set the other margins to0
.For the second
fieldset
, you want theinput
andlabel
text to appear on the same line.Start, by giving the
input
elements in the secondfieldset
a class ofinline
.Select only the
.inline
elements, and give themwidth
ofunset
. This will remove the earlier rule which set all theinput
elements towidth: 100%
.Add some space between the
.inline
elements and thelabel
text, by giving a rightmargin
of0.5em
. Also, set all the other margin to0
.If you look close enough, you will notice the
.inline
elements are too high on the line.To combat this, set the
vertical-align
property tomiddle
.To make the
input
andtextarea
elements blend in with the background theme, set theirbackground-color
to#0a0a23
. Then, give them a1px
,solid
border with a color of#0a0a23
.Currently, if you type in the
input
ortextarea
elements, you will not be able to see the text. Also, their height is too small to be easy to use.Fix this, by setting the
color
to#ffffff
, and setting theirmin-height
to2em
.You want the
select
element to remain with a white background, but now it is not getting the samemin-height
as theinput
andtextarea
elements.Move the
min-height
property and value so that all three element types have the samemin-height
value, and theselect
element still has a white background.To style the submit button, you can use an attribute selector, which selects an element based on the given attribute value. Here is an example:
1
input[name="password"]
The above selects
input
elements with aname
attribute value ofpassword
.Now, use the attribute selector to style the submit button with a
display
ofblock
, and awidth
of60%
.With a
display
ofblock
the submit button sits flush against the left edge of its parent.Use the same technique used to center the
form
to center the submit button.To make the submit button look more in line with the rest of the form, give it the same
height
as the other fields (2em
). Also, increase thefont-size
to1.1rem
.To make the submit button appear more distinct, give it a
background-color
of#3b3b4f
, and aborder-color
ofwhite
.Lastly, for the submit button, you want to separate it from the
fieldset
above, and adjust its width to never be below300px
.Change the
margin
property to include1em
on the top and bottom, while leaving the right and left margins set toauto
. Then set the width as described above.Most browsers inject their own default CSS properties and values for different elements. If you look closely, you might be able to notice the file
input
is smaller than the other textinput
elements. By default, apadding
of1px 2px
is given toinput
elements you can type in.Using another attribute selector, style the
input
with atype
offile
to be the same padding as the otherinput
elements.Speaking of
padding
, the submit button is sitting at the bottom of theform
element. Add2em
ofpadding
only to the bottom of theform
.Last, but not least, change the text color of the
terms and conditions
link to#dfdfe2
.Well done! You have completed the final part of the Registration Form practice project.
1 |
|
1 | body { |