Lab 12 Forms
Goal
- Create a basic web form
- Learn the pseudo-class focus
- Learn selectors for input elements
Learn
<form>
<label for="name">
<input type="email" id="name">
</form>
<form>
The html form element represents a document section that contains interactive controls for submitting information to a web server. Essentially this is the container that will hold all of the <input>
and <label>
elements.
<input>
The input element is used to create the interactive controls for the web based form. There are a wide variety of input types. The type of input is defined by a type attribute placed inside the opening input tag (just like an alt, class or id). Input tags are self contained and therefore do not have a closing tag.
<input type="value">
Possible values for the type attribute:
- Button - A push button with no default behavior.
- Checkbox - A check box allowing single values to be selected/deselected.
- Color - A control for specifying a color. A color picker's UI has no required features other than accepting simple colors as text
- Date - A control for entering a date (year, month, and day, with no time).
- Datetime-local - A control for entering a date and time, with no time zone.
- Email - A field for editing an e-mail address.
- File - A control that lets the user select a file.
- Hidden - A control that is not displayed but whose value is submitted to the server.
- Image - A graphical submit button. You must use the src attribute to define the source of the image and the alt attribute to define alternative text. You can use the height and width attributes to define the size of the image in pixels.
- Month - A control for entering a month and year, with no time zone.
- Number - A control for entering a number.
- Password - : A single-line text field whose value is obscured.
- Radio - A radio button, allowing a single value to be selected out of multiple choices.
- Range - A control for entering a number whose exact value is not important.
- Reset - A button that resets the contents of the form to default values.
- Search - A single-line text field for entering search strings.
- Submit - A button that submits the form.
- Tel - A control for entering a telephone number.
- Text - A single-line text field.
- Time - A control for entering a time value with no time zone.
- Url - A field for entering a URL.
- Week - A control for entering a date consisting of a week-year number and a week number with no time zone.
<input type="email" name="email-address">
The name attribute is also applied to input tags. The name attribute is used to identify the submitted data from the form. The name value is only seen on the back end and isn't visible to the users.
There are a large amount of other attributes that can be applied to different types of inputs. We will only be covering the basics in this class. If you want to more about creating inputs for forms checkout this website.
<label for="name">
<label>
The HTML label element is used to caption another element. In this lab you will use it to label the input elements.
The for attribute on the <label>
element is used to link a <input>
and <label>
together. A for attribute looks for an id value, so if you have id on a input and a for attribute with the same value, when you click on the label the input element will focus.
Styling Form
Styling form elements can be quite tricky. Each input has many default styles, so it can be difficult to override them while leaving the element functional. We will be utilizing some basic CSS to style the form.
:focus
The :focus CSS pseudo-class represents an element (such as a form input) that has received focus. It is generally triggered when the user clicks or taps on an element.
Selectors for input tags look like this:
input[type=text]{
background-color:white;
}
If you want to add the same styles to multiple inputs it looks like this:
input[type=number], [type=text], [type=color]{
background-color:white;
}
When using input selectors in CSS if you want to add a hover effect or another pseudo-class it would look like this:
input:hover[type=text]{
background-color:blue;
}
Notice how the pseudo-class comes after input and not type. If you want to add the same hover effect to multiple inputs it would look like this:
input:hover[type=number], input:hover[type=text]{
background-color:red;
}
Practice
Experiment Try clicking on the First Name label. Notice how the input next to the label gets a blue outline. That is the focus status' default styles applying to the input. Now add a for attribute to the second label that matches the id value for the second input. Note for attributes always look for id values, so you do not need a hashtag.
HTML
Preview
Experiment In the example below add a :focus effect on the first name and telephone number input that turns the background aqua. Then add a yellow background color to the submit input. Then add a hover effect of orange to the submit input.
HTML
CSS
Preview
Open GitHub Desktop (you may need to download the program again if the computers get wiped.)
Couple of things to check in GitHub Desktop.
- Check to see if it says Pull Origin. If it does, click Pull Origin. Why? A pull occurs if you or a collaborator made a change to the repository and pushed the changes. ALWAYS MAKE SURE TO PULL BEFORE CODING!
- If the computers were wiped you made need to reclone your repository to the computer.
With your cs-318 repository selected, Click open in Visual Studio code.
Create a new folder called lab12
Create a new file called index.html in the lab12 folder
Setup your HTML files
Create your stylesheet
Link your stylesheet to the HTML
Change Document in the <title>
element to "Monster Creator"
Add a header and main element to the body
In the header add a h1 with the content "Monster Creator"
Add a form element in the main
In the form
label "Monster's Name"
input
- type "text"
- id "name"
- name "monster-name"
label "Monster's Email"
input
- type "text"
- id "email"
- name "monster-email"
- value "@monsteremail.com"
- The value attribute is used to define the default value preset when the web page loads.
label "Monster's Birthday"
input
- type "date"
- id "birthday"
- name "monster-birthday"
Add a <fieldset>
element
Inside the fieldset element add:
Add a <legend>
tag with the content "Scary?"
- label "YES!"
- input
- type "radio"
- id "scary-yes"
- name "monster-scary"
- value "Yes!"
- label "No"
- input
- type "radio"
- id "scary-no"
- name "monster-scary"
- value "No"
**Fieldset ends here
label "Color of Monster
input
- type "color"
- id "color"
- name "monster-color"
label "Number of Tentacles"
input
- type "number"
- id "tentacles"
- name "monster-tentacles"
- value "0"
- min "0"
- max "1000000"
- Min and max are attributes used to define the minimum and maximum value that the user can submit. Example:
<input type="number" min="1" max="2">
- Min and max are attributes used to define the minimum and maximum value that the user can submit. Example:
label "Picture of Monster"
input
- type "file"
- id "picture"
- name "monster-picture"
- accept "images/*"
- The accept attribute is used to specify which file types are allowed. images/* allows any kind of image file.
label "Number of Eyeballs"
input
- type "number"
- id "eyes"
- name "monster-eyes"
- min "0"
- max "22"
label "Monster's Creator"
input
- type "text"
- id "creator"
- name "monster-creator"
- value "Your Name Here"
Add a div with the class value of "actions"
In the div add:
input
- type reset
input
- type submit
Add the for="" attribute to all labels. The value for the for="" attribute will be the same value as the id on the corresponding input. For example:
<label for="name">Monster's Name</label>
<input type="text" id="name" name="monster-name">
CSS
body
margin 0
background color #18810f
font color #fff
font family
- Go to Google Fonts
- Search for Montserrat
- Select Bold 700 font
- Search for Eater
- Select the font
- You should have two fonts selected. When you select the fonts a popup will appear on the right. You will be using the
<link>
element created by Google Fonts to link your html file to those fonts. There is no need to download the fonts. - Paste the generated
<link>
into the head tag in your html document - Add
font-family: 'Montserrat', sans-serif;
to body selector in CSS document
header
background color #000
font color yellowgreen
margin 0
padding 1rem
text align center
font size 2rem
font-family: 'Eater', cursive;
form>label
.
We are using this specific selector because we don't want the labels in the fieldset to use the display block property.
display block
font size 1.5rem
margin 1rem 0 .25rem.
Hint: three values for margin top | left & right | bottom
fieldset
font size 1.5rem
margin 1rem 0 .25rem
border 3px solid yellowgreen
border radius .25rem
form
background color rgba(0, 0, 0, .9).
Hint: We are using rgba to obtain a transparent black background. It has a 90% opacity.*
padding 1rem 2rem.
Hint: Two values for padding are top & bottom | left & right
width 50rem
max-width:calc(90% - 4rem);
.
Hint: We are using this max-width to ensure that the form will look good on all screen sizes, even those smaller than 50rem. The calc() is a way to do a calculation. The reason we need to use this is the width we are setting and the padding on the left and right are getting added together to make the width. So without the calculation the width could be 90% + 4rem.
margin 1rem auto.
Hint: Remember with a set width we can use auto on the left and right to center the element.
I am only going to list the type of input for the CSS directions, follow the selector examples for inputs in the Learn section above to correctly format the input selectors. Commas indicate that multiple selectors can be used for one ruleset.
Text, email, date, number, color
font-size 1.25rem
width calc(100% - 1rem)
border 3px solid yellowgreen
padding .5rem
border-radius .25rem
Color
width 4rem
height 4rem
border-radius 2rem
Focus: text, email, date, number, color
background color black
font color white
outline:none;
Hint: This will remove the blue outline caused by the default focus styles on inputs.
Reset, Submit
font-family:Eater
, cursive;
font size 1.5rem
padding 1rem
border-radius 2rem
border 3px solid yellowgreen
margin 1rem 2rem 0 0.
[Hint] Four values for margin: top | right | bottom | left
Submit
background color yellowgreen
Reset
background color black
font color yellowgreen
Hover: submit
background color black
font color yellowgreen
Hover: reset
font color black
background color red
border-color red
Create an images folder and place the following image in the folder
body
background image eyeballs.svg
background size 20%
No need for media queries in this lab because the form in styled to function on all sizes.
Finish Form Setup
In the opening <form>
tag add the following attributes:
action="https://theresahanson.com/monster-creator"
- This is where we are sending the data.
method="post"
- Post is used to send data to a server to create/update a resource.
enctype="multipart/form-data"
- This is needed when your form includes any
<input type="file">
elements.
Final Steps
Validate your HTML.
- Navigate to W3C Validator
- Select the validate by file upload tab at the top of the page
Lint your CSS at Jigsaw.w3.org
Fill out and submit your form. Check your submission here
Now look at GitHub Desktop. You should see all of your files listed out in the left column.
Now you are going to commit your changes.
With all of the files checked. Fill out the Summary field in the bottom left and then click Commit to main.
Committing is staging the files, so they can be pushed to the repository hosted at GitHub.
Now you want to push your changes to the Repository. Now if you access the repository from another computer Lab12 will be there. After you push in the top right of the nav it should say "Fetch Origin" instead of "Push Origin"
Your are Done with Lab12
Are you someone who needs to double check that it worked? If you want to make sure the push was successful click on the "View on GitHub" button from GitHub Desktop. It will open up your repository in the browser. From there you should be able to see all of your files.
...What if I need to make a change after pushing?
So let's say you have already pushed but noticed an error. No worries, this is easy to fix.
In visual studio code make any changes you need to. Once you are done making corrections. Save.
Go back to GitHub Desktop. You should see your changes to the documents listed there.
Fill out the Summary field again. I suggest saying what you fixed. For example: "Fixed paragraph error on Sea-Jellies"
Click "Commit to main"
Click "Push Origin" and you are done.