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 htmlBelow the
DOCTYPE, add anhtmlelement with alangattribute set toen, so that you have a place to start putting some code.Next, add opening and closing
headandbodytags within thehtmlelement.Add a
titleand ametaelement to thehead. Give your project a title ofRegistration Form, and give acharsetattribute with a value ofUTF-8to yourmetaelement.Nest a self-closing
linkelement within theheadelement. Give it arelattribute with value ofstylesheetand anhrefattribute with a value ofstyles.css.Within the
body, provide a heading context for the content, by adding anh1with 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
vhunit stands for viewport height, and is relative to 1% of theheightof the viewport.It is time to spruce the project up with some CSS. Begin by giving the
bodyawidthof100%, and aheightof100vh.Now, get rid of the horizontal scroll-bar, by setting the
bodydefaultmarginadded by some browsers to0.That is better. Now, make the background easy on the eyes, by changing the
bodybackground-colorto#1b1b32. Then, to see the text, change thecolorto#f5f6f7.As suggested by the title, you are creating a form. So, after the
pelement, insert aformwith anactionattribute targetinghttps://register-demo.freecodecamp.org.The
methodattribute specifies how to send form-data to the URL specified in theactionattribute. The form-data can be sent via aGETrequest as URL parameters (withmethod="get") or via aPOSTrequest as data in the request body (withmethod="post").Set the
methodattribute to send your form data via aPOSTrequest.As the form will have three distinct sections, add three
fieldsetelements within theformelement.The first
fieldsetwill hold name, email, and password fields. Start by adding fourlabelelements to the firstfieldset.Add the following text to the
labelelements:Enter Your First Name:Enter Your Last Name:Enter Your Email:Create a New Password:
The
remunit stands for rootem, and is relative to the font size of thehtmlelement.As
labelelements 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: blockto thelabelelement, and add amarginof0.5rem 0, to separate them from each other.Nest an
inputelement within eachlabel. Be sure to add eachinputafter thelabeltext, and include a space after the colon.Following accessibility best practices, link the
inputelements and thelabelelements together using theforattribute.Use
first-name,last-name,email, andnew-passwordas values for the respectiveidattributes.Specifying the
typeattribute of a form element is important for the browser to know what kind of data it should expect. If thetypeis not specified, the browser will default totext.Give the first two
inputelements atypeattribute oftext, the third atypeattribute ofemail, and the fourth atypeattribute ofpassword.The
emailtype only allows emails with a@and a.in the domain. Thepasswordtype obscures the input, and warns if the site does not use HTTPS.The first
inputelement with atypeofsubmitis automatically set to submit its nearest parentformelement.To handle the form submission, after the last
fieldsetelement add aninputelement with thetypeattribute set tosubmitand thevalueattribute 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
requiredattribute to theinputelements in the firstfieldset.Now, if you try to submit the form without filling in the required fields, you will see an error message.
Certain
typeattribute 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
inputelement, by adding aminlengthattribute with a value of8. Doing so prevents inputs of less than 8 characters being submitted.With
type="password"you can use thepatternattribute to define a regular expression that the password must match to be considered valid.Add a
patternattribute to the passwordinputelement 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
0to5. Then, remove theminlengthattribute, 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
labelelements to the secondfieldset.Users will be allowed to choose either a
Personal AccountorBusiness Account.To do this, within each of the first two
labelelements, add oneinputelement withtype="radio".For the terms and conditions, add an
inputwith atypeofcheckboxto the thirdlabelelement. Make thisinputelementrequiredbecause users should not sign up without reading the terms and conditions.Within each corresponding
labelelement, and immediately after theinputelement, 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
nameattribute 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
inputelements and thelabelelements in the secondfieldset.Use
personal-account,business-account, andterms-and-conditionsas values for the respectiveidattributes.To finish this
fieldsetoff, link the textterms and conditionsin the thirdlabelto 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
inputtypefileallows just that. Add alabelwith the textUpload a profile picture:, and nest aninputaccepting a file upload.Add another
labelafter the first, with the textInput your age (years):. Then, nest aninputwith thetypeofnumber.Next, add a
minattribute to theinputwith a value of13because users under the age of 13 should not register. Also, users probably will not be over the age of 120; add amaxattribute 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
selectelement. Theselectelement is a container for a group ofoptionelements, and theoptionelement acts as a label for each dropdown option. Both elements require closing tags.Start by adding a
selectelement below the twolabelelements. Then nest 5optionelements within theselectelement.Nest the
selectelement (with itsoptionelements) within alabelelement with the textHow did you hear about us?. The text should come before theselectelement.The dropdown options are currently empty. To give them content, add the following text to each subsequent
optionelement: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
optionneeds to be given avalueattribute. Without which, the text content of theoptionwill be submitted to the server.Give the first
optionavalueof"", and the subsequentoptionelementsvalueattributes from1to4.The
textareaelement acts like aninputelement 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
labelwith the textProvide a bio:at the end of thefieldset. Add atextareaelement inside thelabelelement. Note that thetextarearequires a closing tag.Link the applicable form elements and their
labelelements together.Use
profile-picture,age,referrer, andbioas values for the respectiveidattributes.The
textareaappears too small. To give it an initial size, you can add therowsandcolsattributes.Add an initial size of
3rows and30columns.To give Campers an idea of what to put in their bio, the
placeholderattribute is used. Theplaceholderaccepts a text value, which is displayed until the user starts typing.Give the
textareaaplaceholderofI like coding on the beach....With form submissions, it is useful, and good practice, to provide each submittable element with a
nameattribute. This attribute is used to identify the element in the form submission.Give each submittable element a unique
nameattribute of your choosing, except for the tworadioinputs.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 to16pxin thebody.Center the
h1andpelements by giving them amarginof1em auto. Then, align their text in thecenteras well.Center the
formelement, by giving it amarginof0 auto. Then, fix its size to a maximum width of500px, and a minimum width of300px. In between that range, allow it to have awidthof60vw.During development, it is useful to see the
fieldsetdefault borders. However, they make the content appear too separated.Remove the
border, and add2remof padding only to the top and bottom of eachfieldset. Be sure to remove thepaddingfrom the left and right.To give the
fieldsetelements a bit of separation, select them and give them aborder-bottomof3px solid #3b3b4f.The border of the last
fieldsetelement looks a little out of place. You can select the last element of a specific type using thelast-of-typeCSS pseudo-class, like this:1
p:last-of-type { }
That will select the last
pelement. Create a new selector that targets the lastfieldsetelement and set itsborder-bottomtonone.It would be nicer to have the
labeltext appear above the form elements.Select all
input,textarea, andselectelements, and make them take up the full width of their parent elements.Also, add
10pxofmarginto the top of the selected elements. Set the other margins to0.For the second
fieldset, you want theinputandlabeltext to appear on the same line.Start, by giving the
inputelements in the secondfieldseta class ofinline.Select only the
.inlineelements, and give themwidthofunset. This will remove the earlier rule which set all theinputelements towidth: 100%.Add some space between the
.inlineelements and thelabeltext, by giving a rightmarginof0.5em. Also, set all the other margin to0.If you look close enough, you will notice the
.inlineelements are too high on the line.To combat this, set the
vertical-alignproperty tomiddle.To make the
inputandtextareaelements blend in with the background theme, set theirbackground-colorto#0a0a23. Then, give them a1px,solidborder with a color of#0a0a23.Currently, if you type in the
inputortextareaelements, 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
colorto#ffffff, and setting theirmin-heightto2em.You want the
selectelement to remain with a white background, but now it is not getting the samemin-heightas theinputandtextareaelements.Move the
min-heightproperty and value so that all three element types have the samemin-heightvalue, and theselectelement 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
inputelements with anameattribute value ofpassword.Now, use the attribute selector to style the submit button with a
displayofblock, and awidthof60%.With a
displayofblockthe submit button sits flush against the left edge of its parent.Use the same technique used to center the
formto center the submit button.To make the submit button look more in line with the rest of the form, give it the same
heightas the other fields (2em). Also, increase thefont-sizeto1.1rem.To make the submit button appear more distinct, give it a
background-colorof#3b3b4f, and aborder-colorofwhite.Lastly, for the submit button, you want to separate it from the
fieldsetabove, and adjust its width to never be below300px.Change the
marginproperty to include1emon 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
inputis smaller than the other textinputelements. By default, apaddingof1px 2pxis given toinputelements you can type in.Using another attribute selector, style the
inputwith atypeoffileto be the same padding as the otherinputelements.Speaking of
padding, the submit button is sitting at the bottom of theformelement. Add2emofpaddingonly to the bottom of theform.Last, but not least, change the text color of the
terms and conditionslink to#dfdfe2.Well done! You have completed the final part of the Registration Form practice project.
1 |
|
1 | body { |