# Put Users in Control With Confirmation Feedback Buttons

This tutorial will teach you how to create a responsive web page with buttons which take on different states depending on the user's interaction. This type of interaction is especially useful on links such as "purchase" or "delete" where it's wise to confirm that the user indeed wants to take a specific action.

This interaction is one that many are familiar with; you can see a similar effect in Apple's app store when purchasing apps.

Apologies for the French, you get the idea..

The page we create in this tutorial will also work on mobile devices, as it's a responsive design.

## Introduction

We'll be creating a sample page based on the Tuts+ network, this page will contain confirmation feedback buttons. Although the example uses "join now" confirmation buttons, you could conceivably use this style of interaction anywhere where you need a user to confirm the action they are about to take.

### When and why to choose this interaction

Good UI design gives users a comprehensible sense of power that consistently helps them feel in control.

An important aspect of human computer interaction is conveying to the user a sense of control. When users are in control they feel comfortable. When they aren't in control they get frustrated. But when you think about it, humans are always in control. Computers never do anything without you first instructing them.

This is where the importance of good software and UI design enters the picture. Good UI design gives users a comprehensible sense of power that consistently helps them feel in control. They'll never ask "wait why did that happen?" or "wait how did I get here?"

You can convey a sense of control to the user by communicating insights to them with each interaction. Help them understand cause and effect in the system. To use a basic example, if you save something, the system will give you a positive message saying "your settings have been saved". Thus, users will never question "I wonder if that was saved?"

### What does this have to do with the tutorial?

The interaction we'll create in this tutorial helps give the user a sense of control. By changing the button state and having the user confirm their decision to purchase, we make sure that the user never does anything unintentionally. This is especially useful when you're asking users to part with their hard earned money; the last thing anyone wants is to accidentally pay for something!

The beauty of this interaction is that, rather than receiving a pop-up prompt saying "are you sure you want to purchase?", users are notified via the button state change they are about to do something important. If they don't want to do it, they can simply continue to browse the site; whereas an alert prompt would explicitly require the user to make a yes/no decision.

### Before you start

Make sure you grab the image file dependencies for this tutorial. Put them in a folder named "images". All your other files (HTML, CSS, JavaScript) will go in the main directory. At the end of the tutorial, your file structure will look like this:

## Step 1: Getting Started with HTML

Let's get started by creating some basic HTML that will define our page structure.

 1 2   3   4   5  Confirmation Feedback Button Styles  6   7   8 
 9 
 10   11  

So here we have our basic HTML5 page structure. We included our DOCTYPE, our page title, and a main <section> element with an ID of #container

## Step 2: Linking to Dependencies

First we will add a link to our CSS stylesheet (which we will soon create). This goes in the <head> element.

 1 2  

Next we'll include a link to Google's hosted version of jQuery, as well as a link to "script.js" which will hold the javascript code we create later on. Let's put these right before the closing </body> tag.

 1 2   3   4 5  

Because we'll be using HTML5 elements like <header> and <section> we'll need to add the HTML5 shiv so our markup works in IE8. Include this in your header:

 1 2  

## Step 3: Designing Flexibly

We'll design this page to be responsive and flexible down to mobile. To ensure mobile browsers render our page correctly we'll have to set the meta viewport property. To learn more about this, check out Ian Yates' article on the viewport meta tag. Otherwise, simply add this snippet of code in your <head> element.

 1 2  

## Step 4: Creating the Header Markup

 1 2 
 3 

 4 

The best way to learn creative and technical skills like design, development, and more!

 5 


Our header (at different sizes) is pretty basic and looks like this:

## Step 5: Creating the List Markup

Now we're going to create an unordered list under our <header> element. We'll use this to wrap our listed items in.

### The unordered list

 1 2 
 3 


As you can see, we gave our unordered list a class of 'list' which we'll use to target its styles with CSS.

### The list item structure:

 1 2 
•  3   6 
 7   8 
 9 
• 

As you can see, inside of each <li>, we have two main elements; a <div> and a <figure>. The figure element is ideally suited to house our list item's icon. The div with a class of 'info' is where we put the information associated with the icon. Again, this is all contained inside an <li> element.

### The icon

Inside of <figure class="icon"> we are going to put the item's icon using the <img /> tag. The figure element's width will be controlled. Then, by stating that the image should have a maximum width equal to the figure, it will resize according to the parent element!

 1 2  

### The info

Inside of <div class="info"> we are going to have three elements:

1. The item's header (using the <h2> element)
2. The action button (using the <a> element)
3. The item's description (using the <p> element)

We're going to give our button (the <a> element) a class of "join" for CSS styling later on.

 1 2 
 3 

Activetuts+

 4  $15.99  5  Lorem ipsum dolor sit amet, consectetur adipisicing elit, tempor incididunt ut labore et dolore magna aliqua.  6   ### Final code for each <li> element  1   2  •  3   6   7  Activetuts+  8 $15.99  9 

Lorem ipsum dolor sit amet, consectetur adipisicing elit, tempor incididunt ut labore et dolore magna aliqua.

 10 
 11 
• 

Here's what we have so far:

## Step 6: Duplicating and Modifying Each <li> Element

Now that we have the basic structure for each <li> element, all we have to do is duplicate it for each Tuts+ entry. We have to change the following for each list item:

1. The <h2> text
2. The <a href> link
3. The <img src /> file name

You can go ahead and copy/paste the list item code for each Tuts+ site.

This is what we have now:

## Step 7: Basic CSS

Now that we have all our markup complete, let's start styling the page. We'll begin by setting a few basic resets:

 1 2 /* quick reset */  3 body, h1, h2, p, ul, li {  4  margin:0;  5  padding:0;  6 }  7 ul {list-style-type:none;} 

Now let's style the main body element.

 1 2 body {  3  background: #eee;  4  font: 16px/1.4em Helvetica, Arial, sans-serif;  5  color:#333;  6 } 

Here we set the background color and some defaults for our typography such as the type size, color, and line height.

## Step 7: Going Responsive (Liquid)

We want our page to be flexible all the way down to mobile. So, the two key components we'll use to achieve this are percentages and the max-width rule.

By looking at our HTML, you'll see <section id="container"> is the main container for our page's content. We'll use percentage values for the width and we'll add some basic styling to separate it visually from the page background.

 1 2 #container {  3  width:100%;  4  margin:0 auto 40px;  5  max-width:600px;  6  background:#fff;  7  border-radius:0 0 3px 3px;  8  border:1px solid #d0d0d0;  9  border-top:0;  10  box-shadow:0 1px 0px #fff;  11 } 

Here we set our container's width to be 100% of the browser's viewport. Our margins center the content on the page. We also added max-width:600px because we don't want our list items to ever be larger than that.

As you can see, at larger sizes our container has some extra space on the sides with a light background. But at smaller sizes, that extra space disappears and we just have a white background for our content.

Let's not forget to make our images responsive:

 1 2 img {  3  max-width:100%;  4 } 

Now let's style our main <header> element and the content inside of it.

 1 2 header {  3  text-align:center;  4  padding:30px 20px;  5 }  6 header h1 {  7  margin-bottom:20px;  8 }  9 header p {  10  color:#413c38;  11  font-size:1.2em;  12  line-height:1.4em;  13 } 

Our header element and its content are now nicely centered and sized.

## Step 9: Structural List Item Styles

Now we're going to start styling our list items and their siblings. First, let's create some basic structural styles:

 1 2 .icon,  3 .info {  4  box-sizing:border-box;  5 }  6 .icon {  7  float:left;  8  width:15%;  9  text-align:right;  10  padding-left:3%;  11 }  12 .info {  13  width: 85%;  14  float:left;  15  padding:0 5%;  16 } 

If you remember correctly, inside each list item we have two main elements: 'icon' and 'info'. The icon figure will be floated left and given a small width. The 'info' div will be floated right and given the majority of the parent width.

As you can see, we used the box-sizing:border-box; rule on these two divs. This allows us to have our total width add up to 100% and still be able to add padding without going over a total width of 100% (To learn more about this property, read Encourage Responsive Form Elements to Play Nice).

## Step 10: List Item Styles

After our structural CSS additions, we now have something that looks like this:

As you can see, this needs to be cleaned up a little bit. First because we are floating our elements 'info' and 'icon', we need to add a clear to each of the list elements. We'll also add a little styling to each list item.

 1 2 .list li {  3  padding: 20px 0;  4  border-top:1px solid #eee;  5  overflow:auto;  6  clear:both;  7 }  8 .list li:hover {  9  background:#f7f7f7;  10 } 

Lastly, we're going to style the information inside each list item:

 1 2 .info h2 {  3  margin-bottom:10px;  4  font-size:1.75em;  5  line-height:1em;  6  display:inline-block;  7 }  8 9 .info p {  10  font-size:14px;  11  color:#777;  12 } 

Now we have something that's starting to look pretty solid.

## Step 11: Button Styles

Now let's apply some styling to our buttons. We'll make them visually prominent:

 1 2 a {  3  text-decoration:none;  4  color:#fff;  5 }  6 .join {  7  background: #57a9eb; /* Old browsers */  8  background: -moz-linear-gradient(top, rgba(87,169,235,1) 0%, rgba(53,156,234,1) 100%); /* FF3.6+ */  9  background: -webkit-gradient(linear, left top, left bottom, color-stop(0%,rgba(87,169,235,1)), color-stop(100%,rgba(53,156,234,1))); /* Chrome,Safari4+ */  10  background: -webkit-linear-gradient(top, rgba(87,169,235,1) 0%,rgba(53,156,234,1) 100%); /* Chrome10+,Safari5.1+ */  11  background: -o-linear-gradient(top, rgba(87,169,235,1) 0%,rgba(53,156,234,1) 100%); /* Opera 11.10+ */  12  background: -ms-linear-gradient(top, rgba(87,169,235,1) 0%,rgba(53,156,234,1) 100%); /* IE10+ */  13  background: linear-gradient(top, rgba(87,169,235,1) 0%,rgba(53,156,234,1) 100%); /* W3C */  14  filter: progid:DXImageTransform.Microsoft.gradient( startColorstr='#57a9eb', endColorstr='#359cea',GradientType=0 ); /* IE6-9 */  15  border:1px solid #326fa9;  16  border-top-color:#3e80b1;  17  border-bottom-color:#1e549d;  18  color:#fff;  19  font-weight:bold;  20  text-transform:uppercase;  21  text-shadow:0 -1px 0 #1e3c5e;  22  font-size:11px;  23  border-radius:5px;  24  box-shadow:0 1px 0 #bbb, 0 1px 0 #9cccf3 inset;  25  white-space:nowrap;  26  -webkit-transition:all .25s ease;  27  -moz-transition:all .25s ease;  28  transition:all .25s ease;  29  float:right;  30  display:inline-block;  31  padding:1px 1em 0; /* make appear center from 1px highlight */  32  line-height:2.25em;  33 } 

What did we do here?

Position
We floated our button to the right. This will make the button appear to the right of the main list header.
We used the line-height property to make the button the height we wanted. We also added 1px of padding on the top, this offsets the 1px highlight we added using the box-shadow property.
Visual Styling

## Step 11: Alternate Button Styles

Everything is looking pretty good. What we want to do now is add styles for a class that will be applied to the button(s) when they are clicked. For this tutorial, we are going to add the text "join now" to the existing button text when the button gets clicked. Essentially, the user has to make the decision to "join" twice.

To accomplish this, we are going to use some CSS styling, including the CSS3 pseudo class ::after

## Step 14: Toggling Classes

Our script is actually pretty simple. All we need to do is toggle the class of "clicked" whenever a button gets clicked.

So let's setup a function:

 1 2 $('.join').on('click', function() {  3  // code here  4 });  Now, what we want to do is check and see if the button that was clicked already has a class of "clicked" (perhaps it was clicked earlier). If it doesn't have the 'clicked' class, we'll add it and prevent the href attribute of the link from being followed.  1 2 // if it doesn't have a class of clicked,  3 // add one and prevent the link from being followed  4 if ( !($(this).hasClass('clicked') ) ) {  5 6  // Remove any 'clicked' classes if there are any  7  $('.clicked').removeClass('clicked');  8   9  // Add 'clicked' class to the button that was clicked  10 $(this).addClass('clicked');  11   12  //prevent the link from being followed  13  return false;  14 } 

If the button that was clicked already has a class of "clicked" the href value will be followed by the browser. This works well because if a user encounters this page and has javascript disabled, they simply won't be able to confirm their decision to "join now". Rather, the link will simply be followed without any confirmation feedback.

## Step 15: Removing the Clicked Class

If a user clicks on the price button, they'll get a changed button state. What if they want to dismiss the button and they click outside the button itself? We're going to register that click and remove the class of "clicked". This is a simple line of jQuery:

 1 2 // if click happens outside .buy button, remove it's class  3 $('body').on('click', function() {  4 $('.clicked').removeClass('clicked');  5 }); 

## Step 16: Adding a Transition

We want to add a little bit more interactivity by making the transition between the 'clicked' and normal button states smoother. We'll just use a CSS3 transition and apply it to the 'join' button class:

 1 2 -webkit-transition:all .25s ease;  3 -moz-transition:all .25s ease;  4 -ms-transition:all .25s ease;  5 -o-transition:all .25s ease;  6 transition:all .25s ease; 

## That's it!

You now have a nice confirmation feedback button. It will help users confirm their choice to make an important decision with your web page/application. Feel free to play around with this concept, make it better and learn more!