w3resource

CSS Properties: How to specify where to navigate when using the arrow keys

Go to Exercise page

Solution:

HTML Code:

<!DOCTYPE html><!-- Declares the document type and version of HTML -->
<html><!-- Begins the HTML document -->
<head><!-- Contains metadata and links to external resources -->
<title>How to specify where to navigate when using the arrow keys</title><!-- Sets the title of the document -->
<style type="text/css"> /* Starts CSS styling */
button { /* Selects all button elements */
    position: absolute; /* Sets the position of the buttons to absolute */
} /* Ends the button styling */

button#w3r1 { /* Selects the button with id "w3r1" */
    top: 20%; left: 25%; /* Sets the position of the button */
    nav-index: 1; /* Specifies the navigation index of the button */
    nav-right: #b2; nav-left: #b4; /* Specifies the element to navigate to when pressing the right or left arrow keys */
    nav-down: #b2; nav-up: #b4; /* Specifies the element to navigate to when pressing the down or up arrow keys */
    color: #3333CC; /* Sets the color of the button */
} /* Ends the styling for button#w3r1 */

button#w3r2 { /* Selects the button with id "w3r2" */
    top: 40%; left: 50%; /* Sets the position of the button */
    nav-index: 2; /* Specifies the navigation index of the button */
    nav-right: #b3; nav-left: #b1; /* Specifies the element to navigate to when pressing the right or left arrow keys */
    nav-down: #b3; nav-up: #b1; /* Specifies the element to navigate to when pressing the down or up arrow keys */
	color: #3333CC; /* Sets the color of the button */
} /* Ends the styling for button#w3r2 */

button#w3r3 { /* Selects the button with id "w3r3" */
    top: 70%; left: 25%; /* Sets the position of the button */
    nav-index: 3; /* Specifies the navigation index of the button */
    nav-right: #b4; nav-left: #b2; /* Specifies the element to navigate to when pressing the right or left arrow keys */
    nav-down: #b4; nav-up: #b2; /* Specifies the element to navigate to when pressing the down or up arrow keys */
	color: #3333CC; /* Sets the color of the button */
} /* Ends the styling for button#w3r3 */

button#w3r4 { /* Selects the button with id "w3r4" */
    top: 40%; left: 0%; /* Sets the position of the button */
    nav-index: 4; /* Specifies the navigation index of the button */
    nav-right: #b1; nav-left: #b3; /* Specifies the element to navigate to when pressing the right or left arrow keys */
    nav-down: #b1; nav-up: #b3; /* Specifies the element to navigate to when pressing the down or up arrow keys */
	color: #3333CC; /* Sets the color of the button */
} /* Ends the styling for button#w3r4 */
</style><!-- Ends CSS styling -->
</head><!-- Ends the head section -->
<body><!-- Begins the body of the document -->
<p>w3resource Tutorial</p><!-- Paragraph element with text content -->
<button id="w3r1">Button 1</button><!-- Button element with id "w3r1" -->
<button id="w3r2">Button 2</button><!-- Button element with id "w3r2" -->
<button id="w3r3">Button 3</button><!-- Button element with id "w3r3" -->
<button id="w3r4">Button 4</button><!-- Button element with id "w3r4" -->
</body><!-- Ends the body section -->
</html><!-- Ends the HTML document -->

Explanation:

  • This HTML document demonstrates how to specify the navigation behavior for arrow keys using CSS properties.
  • CSS comments are added to explain each section of the code.
  • Each button is styled individually using CSS.
  • Navigation properties such as nav-index, nav-right, nav-left, nav-down, and nav-up are used to define the navigation behavior when arrow keys are pressed.
  • The color property sets the color of the buttons.

Live Demo:

See the Pen nav-index-answer by w3resource (@w3resource) on CodePen.


See the solution in the browser

Supported browser

Firefox logo Chrome logo Opera logo Safari logo Internet Explorer logo
No No Yes No No

Go to Exercise page

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.