In this tutorial we will dive deeper into form building. If you haven't already I encouraged you to first check out How to Create a Simple HTML Form and Understand Each Element as this post is more of a continuation in learning from the original blog post.
Be sure to add the Live Server extension for automatic refresh and use Prettier to format your code.
It can also be useful to learn some Visual Studio Code shortcuts to work faster.
In this assignment we will be learning a new form field the select element and applying css to our form with light styling and centering using Flexbox.
For a better grasp of CSS Flexbox I encourage you to check out my guide Learn CSS the Fun Way: Interactive Games That Teach Flexbox and Grid.
You are tasked with building a form with 4 form fields (name, phone, email, & select dropdown) in HTML, centering it with CSS Flexbox, and styling it with CSS.
Just like other more basic form fields, a form field with select with also consist of two elements: the input area and the label. However instead of an input field you will instead use a special select element that wraps around option elements. The label element does not change and is the same implementation we learned in the prior blog post.
Another unique thing to point out is that the select element does not use a type attribute as the type is already implied by the word select. Normally when we use the input tag we also need to use the type attribute to make it more specific and clear what type we intend for the form field. In this case for select the type is implied so we only need the other three attributes we discussed (id, name, & required).
I'd also like to note that the option tag uses the value attribute the same way that the submit button uses the value attribute. Here the value serves as the text that displays for the option in the dropdown. You could also have server-side logic based on the value selected. This is similar to having multiple submit buttons with different server-side actions. Also note that the name attribute for the select element serves as the key and the value attribute will serve as the value depending on which option is chosen.
To add css to your html form you will need to create an external stylesheet (styles.css) and link it to your html file (<link href="styles.css" rel="stylesheet" />). It will also be useful to add a reset.css file.
After linking your css files be sure to style your form and center it in the browser using flexbox. Remember that a css ruleset begins with the name of the tag followed by curly braces and within the curly braces you add your css rules.
No fluff. Just real projects, real value, and the path from code to cash — one useful build at a time.