Customizing Hover and Focus States

Tutorial 4 of 5

Customizing Hover and Focus States

1. Introduction

Goal of the Tutorial

This tutorial will guide you through customizing the hover and focus states of HTML elements using CSS. This is an essential skill for creating interactive and user-friendly websites.

Learning Outcomes

  • Understand what hover and focus states are
  • Learn to customize hover and focus states using CSS
  • Understand how to apply different styles to the hover and focus states

Prerequisites

Basic understanding of HTML and CSS is required.

2. Step-by-Step Guide

The Hover State

The hover state is triggered when a user places the cursor over an HTML element without clicking it. We can customize this state using the :hover pseudo-class in CSS.

element:hover {
    /* styles to be applied on hover */
}

The Focus State

The focus state is triggered when an HTML element receives focus, either from the user selecting it or via use of the keyboard. We can customize this state using the :focus pseudo-class in CSS.

element:focus {
    /* styles to be applied on focus */
}

3. Code Examples

Example 1: Changing Background Color on Hover

In this example, we'll change the background color of a button when it is hovered over.

<!-- HTML -->
<button class="myButton">Hover Over Me</button>
/* CSS */
.myButton:hover {
    background-color: lightblue; /* When the button is hovered over, the background color changes to light blue */
}

Example 2: Changing Border on Focus

In this example, we'll change the border of an input field when it is in focus.

<!-- HTML -->
<input type="text" class="myInput" placeholder="Focus on Me">
/* CSS */
.myInput:focus {
    border: 2px solid green; /* When the input field is in focus, the border changes to a 2px solid green line */
}

4. Summary

  • Hover and focus states are triggered when a user interacts with an HTML element.
  • These states can be customized using the :hover and :focus pseudo-classes in CSS.
  • By customizing these states, we can improve the interactivity and user-friendliness of a website.

Next Steps

Practice applying different styles to various elements on hover and focus. Try to combine both for a more interactive experience. You could also learn about other CSS pseudo-classes like :active and :visited.

5. Practice Exercises

Exercise 1:

Create a navigation bar with four items. When each item is hovered over, it should change color.

Exercise 2:

Create a form with two input fields. When each field is in focus, it should have a thick red border.

Exercise 3:

Combine hover and focus states. Create a button that changes color when hovered over and grows in size when in focus.

Solutions:

  1. The solution to Exercise 1 would involve creating a list of items and applying a :hover pseudo-class to change the color when each item is hovered over.
  2. The solution to Exercise 2 would involve creating two input fields and applying a :focus pseudo-class to change the border when each field is in focus.
  3. The solution to Exercise 3 would involve creating a button and applying both :hover and :focus pseudo-classes to change the color and size respectively.

Remember to try different styles and see how they affect the user experience. The key to mastering CSS is practice and experimentation.