creating acessible floating labels

138
Accessible Floating Labels Accessible Floating Labels Creating

Upload: russ-weakley

Post on 23-Jan-2018

1.163 views

Category:

Education


0 download

TRANSCRIPT

Page 1: Creating Acessible floating labels

AccessibleFloatingLabels

AccessibleFloatingLabels

Creating

Page 2: Creating Acessible floating labels

Demo

Page 3: Creating Acessible floating labels

Git Repo: https://github.com/russmaxdesign/floating-label

Demo: https://russmaxdesign.github.io/floating-label/

Page 4: Creating Acessible floating labels

What are floating labels?

Page 5: Creating Acessible floating labels

Floating labels are labels that are associated with form controls such as input and textarea elements.

Page 6: Creating Acessible floating labels

These floating labels are initially displayed on top of the form control,

and then “floated” up and out of the form control when it receives

focus.

Page 7: Creating Acessible floating labels

Label

Example of a Floating Label in static state

Page 8: Creating Acessible floating labels

Label

Example of a Floating Label in focus state

Page 9: Creating Acessible floating labels

This often includes a minimalistic approach to the design of the form

controls.

Page 10: Creating Acessible floating labels

This means that input and textarea elements are often

displayed with a border-bottom only, so that they appear more like lines to write on rather than inputs

to enter data into.

Page 11: Creating Acessible floating labels

Traditional Input

Page 12: Creating Acessible floating labels

Floating Input

Page 13: Creating Acessible floating labels

Why use them?

Page 14: Creating Acessible floating labels

Generally speaking, I am not a fan of floating labels - except in some

specific circumstances.

Page 15: Creating Acessible floating labels

The solution has some advantages but also has potential User

Experience and Accessibility issues.

Page 16: Creating Acessible floating labels

The obvious question is: if you’re not a fan, why give a presentation

about them?

Page 17: Creating Acessible floating labels

Recently, the team I work with suggested looking into floating

labels as an option. So, I decided to try to create a non-JavaScript version that was usable and

accessible.

Page 18: Creating Acessible floating labels

Hopefully this presentation will provide you with some things to consider before using floating

labels, or help you make them more usable and accessible.

Page 19: Creating Acessible floating labels

What are the concerns?

Page 20: Creating Acessible floating labels

Concern 1. Is it a form control?

Page 21: Creating Acessible floating labels

My main concern is that if a minimalist design approach is used, it may not be clear to users that the element is actually a form control

at all.

Page 22: Creating Acessible floating labels

First name

Last Name

Email

Example of form using floating labels - in static state

Page 23: Creating Acessible floating labels

This lack of clear identification could be problematic for a range of different audiences including

cognitive impaired and those that are “technology challenged”.

Page 24: Creating Acessible floating labels

Concern 2. Low contrast labels

Page 25: Creating Acessible floating labels

The initial labels often have very low contrast - trying to replicate the

appearance of a placeholder.

Page 26: Creating Acessible floating labels

First name

Last Name

Email

Example of form using floating labels showing low contrast labels

Page 27: Creating Acessible floating labels

This means that critical information - the form labels themselves - may not be readable for some types of

vision impaired users.

Page 28: Creating Acessible floating labels

Concern 3. Is it already filled in?

Page 29: Creating Acessible floating labels

At the other end of the spectrum, if the label has a strong contrast it

could then be mistaken for a value - as if the form control has already

been filled in.

Page 30: Creating Acessible floating labels

First name

Last Name

Email

Example of form using floating labels showing high contrast labels

Page 31: Creating Acessible floating labels

Again, this could be problematic for cognitive impaired and/or

“technology challenged” users.

Page 32: Creating Acessible floating labels

Concern 4. Labels are too small

Page 33: Creating Acessible floating labels

In some cases, the labels are displayed in a very small font-

size.

Page 34: Creating Acessible floating labels

First name

Last Name

Email

Example of form using floating labels with a very small font-size

Page 35: Creating Acessible floating labels

This can be problematic for anyone with diminished eye-sight - especially when it is critical information like a form label.

Page 36: Creating Acessible floating labels

Concern 5. Lack of focus state

Page 37: Creating Acessible floating labels

In many cases, the form controls and labels have no focus state.

Page 38: Creating Acessible floating labels

This can cause major issues for keyboard-only users as they may not be able to identify the elements

that are currently in focus.

Page 39: Creating Acessible floating labels

Test, test, test

Page 40: Creating Acessible floating labels

Bottom line, if you are thinking about using a floating label solution, make sure you test with real users

to make sure it is suitable in the proposed environment before going

too far!

Page 41: Creating Acessible floating labels

And of course, make sure that any solution is accessible to the widest

possible range of audiences and assistive technologies.

Page 42: Creating Acessible floating labels

Two main methods

Page 43: Creating Acessible floating labels

While there are a range of different methods that can be used to create

floating labels, the two most common methods are:

Page 44: Creating Acessible floating labels

Method 1: Label only

Page 45: Creating Acessible floating labels

In the static state, the label is displayed on top of the input.

When the input receives focus, the label is animated upwards so that it

sits above the input.

Page 46: Creating Acessible floating labels

Label

Example of a Floating Label showing label in static state

Page 47: Creating Acessible floating labels

Label

Example of a Floating Label showing label in focus state

Page 48: Creating Acessible floating labels

Method 2: Placeholder and label

Page 49: Creating Acessible floating labels

In the static state, the placeholder is displayed inside the input. When

the input receives focus, the placeholder becomes invisible, and

the label is animated into view, above the input.

Page 50: Creating Acessible floating labels

Placeholder

Example of a Floating Label showing placeholder in static state

Page 51: Creating Acessible floating labels

PlaceholderLabel

Example of a Floating Label showing placeholder fading and label appearing

Page 52: Creating Acessible floating labels

Label

Example of a Floating Label showing label above input

Page 53: Creating Acessible floating labels

Which one to use?

Page 54: Creating Acessible floating labels

We will be looking at how to achieve a usable and accessible version of

the second method because it allows us to display a separate

placeholder and label value.

Page 55: Creating Acessible floating labels

This means we can display more detailed instructions via the placeholder, and then simpler

information via the label.

Page 56: Creating Acessible floating labels

Add your first name

Example of a Floating Label showing placeholder

Page 57: Creating Acessible floating labels

First name

Example of a Floating Label showing label

Page 58: Creating Acessible floating labels

Desired outcomes

Page 59: Creating Acessible floating labels

A lot of the outcomes listed below are to help these floating label

controls to be more discoverable.

Page 60: Creating Acessible floating labels

Static state

Page 61: Creating Acessible floating labels

Before any user interaction with a form control, the placeholder

should be visible and the label should be hidden

Page 62: Creating Acessible floating labels

Placeholder

Example of a Floating Label showing placeholder

Page 63: Creating Acessible floating labels

The placeholder should be of sufficient color contrast.

Page 64: Creating Acessible floating labels

Placeholder

Placeholder text set to #666 - AA Compliant

Page 65: Creating Acessible floating labels

Hover state

Page 66: Creating Acessible floating labels

If a user hovers over the form control, the placeholder and form

control should change in appearance to indicate that the

form control is interactive.

Page 67: Creating Acessible floating labels

Ideally, the form control could change in hover state so that the border appears on all sides of the element - to help make it more

discoverable.

Page 68: Creating Acessible floating labels

Placeholder

Example of a Floating Label in static state

Page 69: Creating Acessible floating labels

Placeholder

Example of a Floating Label in hover state

Page 70: Creating Acessible floating labels

Focus state

Page 71: Creating Acessible floating labels

If a user focuses on the form control (via mouse click or by tabbing to the

element), four things should happen:

Page 72: Creating Acessible floating labels

The placeholder should gradually fade to invisible.

Page 73: Creating Acessible floating labels

The label should animate into view above the form control.

Page 74: Creating Acessible floating labels

A border should appear on all sides of the form control.

Page 75: Creating Acessible floating labels

Additionally, the form control's bottom border should change in color - to help indicate that is is

now in “edit mode”.

Page 76: Creating Acessible floating labels

Placeholder

Example of a Floating Label in static state

Page 77: Creating Acessible floating labels

LabelPlaceholder

Example of a Floating Label showing placeholder fading and label appearing

Page 78: Creating Acessible floating labels

Label

Example of a Floating Label in focus state

Page 79: Creating Acessible floating labels

Post focus state

Page 80: Creating Acessible floating labels

When focus leaves the form control, two things should happen:

Page 81: Creating Acessible floating labels

The label should remain in view but change color to indicate that it is no longer in “edit mode” and has

been completed.

Page 82: Creating Acessible floating labels

The form control’s border-bottom color should return to the original

border-bottom color.

Page 83: Creating Acessible floating labels

Placeholder

Example of a Floating Label in static state

Page 84: Creating Acessible floating labels

LabelPlaceholder

Example of a Floating Label showing placeholder fading and label appearing

Page 85: Creating Acessible floating labels

Label

Example of a Floating Label in focus state

Page 86: Creating Acessible floating labels

User value here

Label

Example of a Floating Label in post-focus state

Page 87: Creating Acessible floating labels

No JavaScript

Page 88: Creating Acessible floating labels

My final desired outcome was to try to make a version that did not use any JavaScript - unlike most of the

other examples I’d seen.

Page 89: Creating Acessible floating labels

I think JavaScript plays an important role in modern web development,

but it concerns me that developers are relying on JavaSCript for

simple things like positioning and animated form labels.

Page 90: Creating Acessible floating labels

In order to meet all of the desired outcomes, I’ve had to use one

experimental CSS selector. This means that this one aspect of the

solution will only work in more modern browsers.

Page 91: Creating Acessible floating labels

However, the solution does use basic progressive enhancement,

so almost all of the functionality should be stable for older browsers.

Page 92: Creating Acessible floating labels

The markup

Page 93: Creating Acessible floating labels

Label placement

Page 94: Creating Acessible floating labels

For this method, I needed the label information to appear after the form control in source order.

Page 95: Creating Acessible floating labels

This allowed me to style the label based on the state of the input. For example, I was then able to

change the style of the label when the control was in focus.

Page 96: Creating Acessible floating labels

//  adjacent  sibling  selector  

.floating-­‐label__control:focus  +  .floating-­‐label__label  {  }

Page 97: Creating Acessible floating labels

In an ideal world, label content should be placed before all form controls in source order - except for radio buttons and checkboxes.

Page 98: Creating Acessible floating labels

However, I used the “for” and “id” attributes to explicitly associate

the label to the form control.

Page 99: Creating Acessible floating labels

<div  class="floating-­‐label">          <label  for="input1">  

           <input  class="floating-­‐label__control"  type="text"  placeholder="Add  your  first  name"  id="input1"  required>  

           <span  class="floating-­‐label__label">First  Name</span>          </label>  

</div>

Page 100: Creating Acessible floating labels

I also wrapped the label around the form control and label content.

Page 101: Creating Acessible floating labels

<div  class="floating-­‐label">          <label  for="input1">  

           <input  class="floating-­‐label__control"  type="text"  placeholder="Add  your  first  name"  id="input1"  required>  

           <span  class="floating-­‐label__label">First  Name</span>          </label>  

</div>

Page 102: Creating Acessible floating labels

Finally, I placed the label content inside a span element, so that I

could style the label content separately to the overall label and

the form control.

Page 103: Creating Acessible floating labels

<div  class="floating-­‐label">          <label  for="input1">  

           <input  class="floating-­‐label__control"  type="text"  placeholder="Add  your  first  name"  id="input1"  required>  

           <span  class="floating-­‐label__label">First  Name</span>          </label>  

</div>

Page 104: Creating Acessible floating labels

The class names

Page 105: Creating Acessible floating labels

For this solution I used a BEM-like naming conversion to make the

solution day to understand for other developers. This meant writing the

selectors as modules, sub-modules and modifiers.

BEM = Block, Element, Module

Page 106: Creating Acessible floating labels

//  module  -­‐  or  parent  element  .floating-­‐label  {  }  

//  sub-­‐module  -­‐  or  child  elements  

.floating-­‐label__control  {  }  

.floating-­‐label__label  {  }  

//  modifier  .floating-­‐label__control-­‐-­‐disabled  {  }

Page 107: Creating Acessible floating labels

<div  class="floating-­‐label">          <label  for="input1">  

           <input  class="floating-­‐label__input"  type="text"  placeholder="Add  your  first  name"  id="input1"  required>  

           <span  class="floating-­‐label__label">First  Name</span>          </label>  

</div>

Page 108: Creating Acessible floating labels

The SCSS

Page 109: Creating Acessible floating labels

I’m going to quickly show some of the SCSS used to create the final

floating label solution. I won’t show all of it, but you can check out

the repo for the finer details if interested.

Page 110: Creating Acessible floating labels

Also, I am using SCSS rather than CSS as it gives me a little bit more control - allowing me to set some

variables that can be used multiple times across the various rules.

Page 111: Creating Acessible floating labels

Variables

Page 112: Creating Acessible floating labels

All of the variables can be defined once at the top of the SCSS file.

Page 113: Creating Acessible floating labels

//  variables  

$color-­‐input-­‐text:  #000;  $color-­‐placeholder:  #666;  

$color-­‐placeholder-­‐hover:  #000;  $color-­‐placeholder-­‐disabled:  #ccc;  

$color-­‐label:  #1f7c8a;  $color-­‐label-­‐focus:  #3655b1;  $color-­‐border:  #ddd;  

$color-­‐border-­‐focus:  #3655b1;  $color-­‐disabled-­‐background:  #f5f5f5;  

$label-­‐size:  14px;

Page 114: Creating Acessible floating labels

Parent

Page 115: Creating Acessible floating labels

I need to set the parent to position:  relative as I’m going

to position the label content relative to this parent.

Page 116: Creating Acessible floating labels

//  floating-­‐label  

.floating-­‐label  {      position:  relative;  

   margin:  30px  0  40px;  }

Page 117: Creating Acessible floating labels

Control

Page 118: Creating Acessible floating labels

For the controls, I am going to set the border to transparent, then

reset the border-­‐bottom to a soft gray.

Page 119: Creating Acessible floating labels

//  control  

.floating-­‐label__control  {      width:  100%;  

   border:  none;      border:  1px  solid  transparent;  

   border-­‐bottom:  1px  solid  $color-­‐border;      padding:  .5em;      resize:  none;  

   color:  $color-­‐input-­‐text;      background:  #fff;  

}  

Page 120: Creating Acessible floating labels

I then need to style the various states of the control - in this case

hover and focus.

Page 121: Creating Acessible floating labels

//  control  when  in  hover  

.floating-­‐label__control:hover  {      border:  1px  solid  $color-­‐border;  

}

Page 122: Creating Acessible floating labels

//  control  when  in  focus  

.floating-­‐label__control:focus  {      border:  1px  solid  $color-­‐border;  

   border-­‐bottom:  1px  solid  $color-­‐border-­‐focus;      outline:  none;  

}  

Page 123: Creating Acessible floating labels

I can also style the input once it has been filled in using the valid

pseudo-class. Keep in mind that this is only supported by the latest

browsers.

Page 124: Creating Acessible floating labels

//  control  when  in  focus  and  valid  -­‐  filled  in  

.floating-­‐label__control:valid  {      border:  1px  solid  transparent;  

   border-­‐bottom:  1px  solid  $color-­‐border;      padding-­‐left:  0;  

}  

Page 125: Creating Acessible floating labels

Label

Page 126: Creating Acessible floating labels

Next, I need to set  position:  absolute on the label content and initially position it down over the

top of the input. It also has an initial opacity of “0”.

Page 127: Creating Acessible floating labels

//  label  

.floating-­‐label__label  {      position:  absolute;  

   top:  -­‐15px;      left:  0;  

   opacity:  0;      color:  $color-­‐label-­‐focus;      font-­‐size:  $label-­‐size;  

}

Page 128: Creating Acessible floating labels

When the input comes into focus, I will then style the label content so that the opacity transitions from

“0” to “1”.

Page 129: Creating Acessible floating labels

//  label  when  in  focus  

.floating-­‐label__control:focus  +  .floating-­‐label__label  {      opacity:  1;  

   transition:  .5s  ease;      -­‐webkit-­‐transform:  translateY(-­‐5px);  

transform:  translateY(-­‐5px);  }

Page 130: Creating Acessible floating labels

placeholder

Page 131: Creating Acessible floating labels

Finally, I am styling the placeholders in their various

states. I’ve only included some of the rules used in the final product as

they are quite extensive.

Page 132: Creating Acessible floating labels

//  placeholder  

::-­‐webkit-­‐input-­‐placeholder  {      opacity:  1;  

   color:  $color-­‐placeholder;  }

Page 133: Creating Acessible floating labels

//  placeholder  hover  

.floating-­‐label__control:hover::-­‐webkit-­‐input-­‐placeholder  {      color:  $color-­‐placeholder-­‐hover;  

}  

Page 134: Creating Acessible floating labels

//  placeholder  -­‐  become  hidden  when  in  focus  

.floating-­‐label__control:focus::-­‐webkit-­‐input-­‐placeholder  {      opacity:  0;  

   transition:  1s  ease;  }  

Page 135: Creating Acessible floating labels

More…

Page 136: Creating Acessible floating labels

Floating labels are problematic Adam Silver

https://medium.com/simple-human/floating-labels-are-a-bad-idea-82edb64220f6

Page 137: Creating Acessible floating labels

Are Float Labels Really That Problematic After All?

Matt D. Smithhttps://blog.prototypr.io/are-float-labels-really-that-

problematic-after-all-da7ffe7d5417

Page 138: Creating Acessible floating labels

Russ Weakley Max Design

Site: maxdesign.com.au Twitter: twitter.com/russmaxdesign Slideshare: slideshare.net/maxdesign Linkedin: linkedin.com/in/russweakley