Tabindex

Tabindex

When dealing with element focusing, tabindex has a big role. This attribute indicates if the element can be focusable and reachable with the keyboard navigation. When pressing the Tab key, the browser shifts focus from one element to another. By using tabindex, we can change the tab order flow.

When creating any element, styling should be included.

Values

The tabindex accepts integer values. They can be:

  • Negative (usually tabindex=”-1”) - the element is not reachable by the keyboard navigation.
  • tabindex=”0” - the element is reachable by the keyboard navigation but the order is defined by the documents source order.
  • Positive - the element is reachable by the keyboard navigation, and the order is defined by the value. For example, tabindex=”2” is focusable before tabindex=”3”, but after tabindex=”1”.

Usage

Tabindex can be used on the following elements:

  • input,
  • anchor (a),
  • textarea,
  • select,
  • button.

After creating any of these elements, give the text a proper line-height.

Example

HTML

<body>
  <label>This element will be focused second</label>
  <input name=”field1” type=”email” tabindex=”2” />

   <label>This element will be focused first</label>
  <textarea rows=”4”  cols=”5” tabindex=”1”></textarea>

   <label>The second focused last</label>
  <input name=”field3” type=”text” tabindex=”3”/>
</body>

Here we can see the order of the tab selection, starting from the lowest to the highest number. If an element has tabindex=”0”, it will be excluded from the tab order. If two elements have the same tabindex value, they will be selected in the order which they appear in the element.

If you found this article interesting and useful, check out other subjects such as relative font size, css columns and a little something about SASS and LESS.

Summary

This short article summarizes the usage of the tabindex. By mastering this, you will write more efficient and user-friendly forms.

Thank you for reading and happy coding!

Wow, you've reached the end! Why not subscribe to our newsletter while you are still there? :)