We can still select more than one radio button. We need to fix this next.
The magic behind getting radio buttons to work is assigning the same name attribute to each radio field. Remember, this is how our data is getting to the server.
Add the name attribute with the same value to each <input> element.
The name attribute carries the value over to the server.
The <fieldset> & <legend> Elements
Because our radio buttons are a group of elements, it's important to use the appropriate semantic element to indicate they belong together.
Use the <fieldset> element to group related form elements.
The <legend> element provides a title for the grouped elements.
Wrap our radio buttons in the <fieldset> element. Notice that the browser styles this in a gray border by default.
Add the <legend> element as the first child in the <fieldset>. Give it the content: Choose a pizza crust.
<fieldset><legend>Choose a pizza crust</legend><inputtype="radio"id="thin"value="thin"name="crust"><labelfor="thin">Thin-crust<label><inputtype="radio"id="hand"value="hand"name="crust"><labelfor="hand">Hand-tossed<label><inputtype="radio"id="deep"value="deep"name="crust"><labelfor="deep">Deep dish<label></fieldset>
Checkboxes
Checkboxes are like radio buttons, but they allow the user to select more than one choice. Therefore, we need a different value for each name attribute.
You create a checkbox with the <input type="checkbox"> element and attribute.
Each checkbox must have a differentname attribute.
Add the name and value attributes. Each checkbox needs a differentname attribute value. Otherwise, only the first checkbox selected will go to the server.
Just like checkboxes and radio buttons, we need a name and value attribute. The name attribute goes in the <select> element. The value attribute is in every <option> element.
If no value attribute is in the <option> element, the content within the <option> element goes to the server. Although this works, it's best to provide a value attribute. This is because the <option> content could contain special characters and spaces, which could make processing on the server more difficult.
Styling Forms
By default, forms don't look very stylish. Understanding which elements are inline and block-level will help understand how best to style a form.
Centering the form
Place a border around the form element. Since this is a block-level element, it expands the full width of its container.
form{border:4pxsolid#630c15;}
Give the form a width of about 50%
form{border:4pxsolid#630c15;width:50%;}
To center a block-level element, we use the margin: 0 auto; property/value pair to apply an equal margin to the right and left.
Labels for most fields are positioned immediately before the field, that is, for left-to-right languages, either to the left of the field or above it, and for right-to-left languages, to the right of the field or above it.
Labels for radio buttons and checkboxes are positioned after the field.
Labels above text fields
Labels to the left of text fields
Password: Email
Labels to the right of radio buttons
CatDogChinchilla
Aligning Text Boxes
There are many options when it comes to arranging your input boxes. I will show you two popular ways of aligning text boxes, so they are readable. Legibility is important when it comes to forms, so your user doesn't make errors when filling out the form.
The <label> and the <input> element are both inline elements, so they don't automatically go on their own line. However, remember the display CSS property? We can change this behavior on any element!
Option 1: Input on their own line
Add the display: block to the <input> element.
input{display:block;}
This makes each <input> element go on its own line. However, this doesn't look good for our radio buttons and checkboxes. We can use the attribute selector to target just the <input> elements that are for type="text"
Change the previous rule to use the attribute selector to target <input> element that are for type="text", type="password", and the <textarea> element. Use a grouped selector:
Since the <label> element is an inline element, the next element will start where the ends. This can result in a jagged look on the form.
Add a class attribute to each <label> element associated with an input, radio button, or checkbox. I gave my class attribute a value of fixedWidth.
Style this class with the display property. This time we will give it the value of inline-block. This value gives the <label> characteristics of both and inline and block-level elements. It stays on the same line, like an inline element, but can now take a width and height property.
.fixedWidth{display:inline-block;}
No changes are visible on the page, but now we can also add a width to the <label> element.
Add a width of about 130px.
.fixedWidth{display:inline-block;width:130px;}
This expanded the width of those <label> elements to be 130px, therefore making the <input> element start at the same position.
I added a border so you can see the width of the <label> element
Fieldset & Legend Styles
We can also change how the <fieldset> and <legend> appears on the form.
Override the default <fieldset> styles to anything you like: I used the following:
The default submit and reset buttons are rather plain - let's improve them!
The trick here is to either use the attribute selector again to target input[type="submit"] and input[type="reset"] or add class attributes to both <input> elements.
To avoid extra HTML markup, use the attribute selectors.
Add the following styles to the submit and reset buttons.