1. Web Design
  2. Form Design

Bring Your Forms Up to Date With CSS3 and HTML5 Validation

Scroll to top
Read Time: 15 min

Let's look at how to create a functional form which validates users' data, client-side. With that done, we'll cover prettying it up using CSS, including some CSS3!

Before we get started, you may want to consider using one of our HTML5 Templates or CSS Themes for your next project—that is, if you need a quick, professional solution.

Otherwise, it's time to begin this step by step tutorial.

Step 1: Conceptualization Functionality

First we want to conceptualize what our form is going to look like and how it is going to function. For this example, let's create a simple contact form that asks for the following information from the user:

  • Name
  • Email
  • Website
  • Message

We want to make sure the user is entering the information correctly. To accomplish this, we will use HTML5's new client-side validation techniques. What about users who don't have HTML5 capabilities? You can simply use server-side validation, but that will be beyond the scope of this article.

Step 2: Conceptualization Form

Let's get an idea of what we want our form to look like by creating a rough mockup.

As you can see, the following elements make up our form:

  • Form Title Required fields notification
  • Form labels
  • Form inputs Placeholder text
  • Form field hints
  • Submit Button

Now that we've specified which elements make up our form, we can create the HTML markup.

Step 3: HTML Starter Code

Let's create our basic HTML markup from the form concept we created.

Up to this point, our HTML file will still appear blank in the browser. This is simply starter code for an HTML5 page.

Step 4: HTML Form

Let's create the HTML form (we'll leave the action method blank for now, since server-side validation will not be covered in this tutorial):

Step 5: HTML Form Elements

To keep our form content organized and structured, we'll wrap our form elements (label, input, etc) in a list. So let's start by creating the form header and our first input element:

Form Hints

As seen in our mockup, we're going to have formatting hints for the "email" and "website" fields. So we'll add our hints under the input fields where necessary, and give them a class so we can style them later.

The Remaining Input Elements

Let's go ahead and create our remaining form elements, remembering to wrap each section in a list item.

Step 6: Adding the Placeholder Attribute

One of the first improvements HTML5 brings to web forms (one you're probably already familiar with) is the ability to set the placeholder text. Placeholder text is displayed when the input field is either empty or not in focus.

Let's add the placeholder attribute to our input elements. This will help the user understand what they should enter in each field.

Quick Tip: Style your placeholder Text

Here's a quick tip, if you want to style your placeholder text, there are some browser prefixes to help you:

Support for the placeholder attribute is pretty well established in modern browsers (except IE9, sorry). If you really need to have it supported across all browsers, there are some javascript solutions you could look into.

Step 7: Basic CSS

Let's add some basic CSS to give our form some structure. I'll walk you through the rules:

Remove :focus Style

Webkit automatically adds some styling to input elements when they are in focus. Since we'll be adding our own styles, we want to override these defaults:

Typographic Styles

Let's add some typographic styles to our form elements:

List Styles

Let's style our list elements to give our form some structure:

Also, let's add a slight border to the top and bottom sections of the form. We can accomplish this by using the :first-child and :last-child selectors. These select, as the names imply, the first and last elements in the <ul> list.

This adds some useful visual sectioning to our form. Keep in mind that these CSS selectors are not supported in older browsers. Since this is not vital to key functionality, we're rewarding our those who use current browsers.

Form Header

Let's style the header section of our form. This includes the heading tag and the notification that informs users that the asterisk (*) indicates required fields.

Form Input Elements

Let's style all of our core form elements, the ones used to collect user information.

Now, let's add some extra visual CSS styles. Some of these are CSS3 styles that reward users who use modern browsers.

Step 8: Add Some Interactivity with CSS3

Let's add a little bit of interactivity. We'll make the field that is currently selected expand by adding some padding.

Now for browsers that support it, let's make the expansion of the field a smooth transition using CSS3.

Step 9: The required Attribute in HTML5

Now it's time for what we've all been waiting for: HTML5's form handling tools.

Adding the required attribute to any input/textarea element will tell the browser that a value is required before the form can be submitted. Thus, a form cannot be submitted if a required field has not been filled out.

So, let's go ahead and add the required attribute to all of our form elements (because we want them all to be filled out).

Step 10: Styling required Fields

You'll probably notice that, visually speaking, nothing happened by adding the required attribute. We are going to style required fields using CSS. For this example, we are going to add a red asterisk as a background image in each required field. To accomplish this, we will want to first add some padding on the right side of our input where the background image will be (this will prevent text overlap if the field entry is a long string):

Now we will use the CSS pseudo selector :required to target all the form elements with a required attribute. I made a simple 16x16 pixel red asterisk icon in photoshop that will serve as the visual indicator of a required field.

What happens upon submission?

Right now, different browsers will do different things when a form using HTML5 elements is submitted. When the form is submitted, most browsers will prevent the form from being submitted and will display a "hint" to the user, marking the first field that is required and has no value. Visual styling and support for these 'bubble fields' is quite broad. Hopefully these behaviors will become standardized in the future.

You can see current browser support for the required attribute at quirksmode.

Quick Tip:

You can actually style the bubble message somewhat in webkit using the following:

Step 11: Understanding New HTML5 type Attributes and Client-Side Validation

HTML5 validation works according to the type attribute that is set within the form fields. For years HTML only supported a handful of type attributes, such as type="text" but with HTML5 there are a over a dozen new input types including email and url which we are going to use in our form.

By combining our input type attributes with the new required attribute, the browser can now validate the form's data client-side. If a user's browser does not support the new type attributes, such as type="email", it will simply default to type="text". This is actually pretty amazing. Essentially you have backwards compatibility in all browsers on earth, hooray!

So what if the browser does actually support the new type attributes? For desktop browsers there is no visual difference (unless specified by custom CSS rules). A type="text" field looks the same as a type="email" field. However, for mobile browsers, there is a difference when it comes to the user interface.

An Example: The iPhone

Apple's iPhone detects the form types and dynamically changes the on-screen keyboard by providing context-aware characters. For example, all email addresses require the following symbols: "@" and "." So the iPhone provides those characters when the input type is specified to email.

Step 12: Changing the type Attributes

We already have our form fields set to the default type="text". But now we want to change the type attribute on our email and website fields to their corresponding HTML5 type.

Step 13: HTML5 Validation

As mentioned before, HTML5 validation is based on your type attributes and it is on by default. There is no specific markup required in order to activate form validation. If you wish to turn it off, you can use the novalidate attribute like this:

Name Field

Let's look at our first field that asks the user for his/her name. As described eariler, we've added the type="text" attribute and the required attribute. This informs the web browser that this field is mandatory and it should validate the field as simply text. So as long as the user enters at least one character in that field, it will validate.

Now we will create our own CSS to style field inputs that are considered valid and invalid by the browser. If you remember, we used :required in our CSS to style all input elements with a required attribute. Now, we can style our required fields that are either valid or invalid by adding :valid or :invalid to our CSS rules.

First, let's style fields that are invalid. For this example, we only want to style the form as invalid when it is in focus. We'll add a red border, red shadow, and red icon created in photoshop to indicate the invalid field.

Now, let's create the rules that indicate the field is valid. We'll add a green border, green shadow, and greed checkmark icon made in photoshop. This will be applied to all valid fields whether they are in focus or not.

Now when you focus on a form field, the red invalid styling is shown. As soon as a single character has been entered in the field, it is validated and green CSS styles are shown to indicate that fact.

Email and URL Fields

Our CSS styles and validation rules are already applied to the email field because we set the type and required attributes earlier.

Step 14: Introducing the HTML5 pattern Attribute

Using the type="email" attribute as an example, it appears that most browsers validate that field as *@* (any character + the "@" symbol + any character). This is obviously not very limiting but it does prevent users from entering spaces or values that are entirely wrong.

In the example of the type="url" attribute, it appears as though the minimum requirement for most browsers is any character followed by a colon. So, if you entered "h:" then the field would validate. This is not extremely helpful but it does prevent users from entering irrelevant information, such as their email or home address. Now, you could handle being more specific on with your input values in your server-side validation; however, we're going to talk about how to do that in HTML5.

The pattern Attribute

The pattern attribute accepts a javascript regular expression. This expression is used, rather than the browser default, to validate the field's value. So our HTML now looks like this:

Now our field will only accept values that start with "http://" or "https://" and one additional character. These regular expression patterns can be confusing at first, but once you take the time to learn them, your forms will be open to a whole new world.

Step 15: Form Field Hints (CSS)

Now let's style our form hints that tell the user the format they should use when entering their information.

We set display:none because we are only going to show the hints when the user focuses on the input field. We also set our tooltips to default to our red invalid color, because they are always considered invalid until the proper information is entered in.

Using the ::before Selector

Now we want to add a little triangle to our hint boxes that help direct and guide the eye. This can be done using images, but in our case we are going to do it using pure CSS.

Because it is purely a presentational element that is not vital to the page's functionality, we are going to add a small triangle that points left using the ::before pseudo selector. We can do this by using one of the unicode geometric shapes.

Normally we would use the HTML Unicode format to display these in our HTML (as shown in the image above). However, because we will be using the ::before CSS selector, we have to use the triangle's corresponding escaped unicode when using the content:"" rule. Then we just use positioning to get it where we want it.

Using the + Adjacent Selector

Finally, we are going to use the CSS adjacent selector to show and hide our form field hints. The adjacent selector (x + y) selects the element that is immediately preceded by the former element. Since our field hints come right after our input fields in our HTML, we can use this selector to show/hide the tooltips.

As you can see from the CSS, we also set the form hints to change colors along with the input's border when a field is valid or invalid.

Step 16: Sit Back and Admire Your Beautiful HTML5 Form

Go ahead and take a look at your final product!


As you can see, the new HTML5 form features are pretty neat! Everything is backwards compatible so incorporating these new features into your website won't break anything.

HTML5 validation is coming closer to replacing client-side validation in helping users properly fill out their online forms. However, HTML5 validation still does not replace server-side validation. For the time being, it's best to use both methods when handling user-submitted information. Thanks for reading!

Take a look through our HTML5 Templates or CSS Themes for your next project—if you need a professional, ready-made solution. 

Did you find this post useful?
Want a weekly email summary?
Subscribe below and we’ll send you a weekly email summary of all new Web Design tutorials. Never miss out on learning about the next big thing.
Looking for something to help kick start your next project?
Envato Market has a range of items for sale to help get you started.