These 6 keys are commonly used on the web:
- Tab
- Arrow keys
- Spacebar
- Enter
- Escape
- Backspace
Weโll talk about the behavior of each key and the things you need to know about them.
Tab
Tab is the most important key on the web. It lets you cycle through elements you can focus on. These elements include:
- Links
- Buttons
- Form elements
Note: For some weird reason, Safari doesnโt allow you to cycle through links and buttons by default. You need to enable it by going to Settings > Advanced. Under Accessibility, check โPress tab to highlight each item on a webpageโ.
When you Tab into an element, the element gets a focus state. The default style for focus is the blue glow around the element.
Shift + Tab lets you move to the previous focusable element.
You can change the focus style with :focus.
*:focus { outline: 2px solid green;}
Arrow keys
You can use arrow keys to browse a website:
- Down arrow key (
โ) nudges the scrollbar downwards - Up arrow key (
โ) nudges the scrollbar upwards
option+โscrolls down a page on a Mac. ThePageDnkey does the same thing on Windows.option+โscrolls up a page on a Mac. ThePageUpkey does the same thing on Windows.
command+โscrolls to the end of the website on Mac. TheEndkey does the same thing on Windows.command+โscrolls to the top of the website on Mac. TheHomekey does the same thing on Windows.
If the website has a horizontal scrollbar, you can also use left (โ ) and right (โ) keys to nudge the scrollbar in their respective directions.
When youโre interacting with form elements, arrow keys lets you:
- choose a radio option
- choose an option in a select element
Space
Space can be used to interact with elements. It can be used to select a checkbox.
It can be used to open a <select> menu. It can also be used to choose an option from the menu.
If you have focus on a button element, pressing space on the button activates the button. This triggers the click event.
<button>Button</button>const button = document.querySelector('button')
button.addEventListener('click', evt => { console.log('Clicked!')})
Finally, space can be used to scroll down the website by one page. shift + spacebar can be used to scroll up one page.
Enter
Enter can interact with some elements, but it is usually used to confirm or submit things.
First, if youโre on a button, you can use Enter to activate the button. This triggers a click event as well.
Second, if youโre on a link, Enter can be used to navigate to the href of the link.
Third, Enter can also be used to submit a form. This works only if:
- Your focus is INSIDE a form
- Your form has a submit button
- Focus is not in
<textarea> - Focus is not in an opened
<select>menu
const form = document.querySelector('form')form.addEventListener('submit', evt => { evt.preventDefault() // Prevents default behavior so we can console log console.log('Submit event triggered!')})
If the focus is on a <textarea>, enter creates a new line in the <textarea>. It does not submit the form.
If you are in a select element, you can use enter to confirm an option. (You cannot use enter to open the select menu).
Escape
The escape key is used to quit a feature. For example, if you start a search with command + f (ctrl + f on PC), you can quit the search with Escape.
Backspace
Backspace is used for deleting things. You can use it to delete text in a form element like <input type="text"> and <textarea>.
In Firefox, backspace also triggers the browserโs back button. shift + backspace triggers the forward button.
The โback buttonโ behavior was a norm among browsers in the past. This functionality has been removed from Webkit-based browsers. If you want to navigate backwards or forwards, you can use the following:
- Safari and Chrome on Mac:
- Back button:
Command+โ - Forward button:
Command+โ
- Back button:
- Edge and Chrome on Windows:
- Back button:
Alt+โ - Forward button:
Alt+โ
- Back button:
Welcome! Unfortunately, you donโt have access to this lesson. To get access, please purchase the course or enroll in Magical Dev School.
Unlock this lesson