Module: CSS Styling and Effects

Hover and focus effects

Hover and Focus Effects

Hover and focus effects are crucial for providing visual feedback to users, enhancing interactivity and usability. They indicate which elements are interactive and respond to user actions.

Hover Effects

Hover effects are triggered when the user moves their mouse cursor over an element. They're commonly used for buttons, links, and other interactive components.

CSS hover Pseudo-class:

The :hover pseudo-class is used to apply styles when an element is hovered over.

/* Example: Change button background color on hover */
button {
  background-color: #4CAF50; /* Default background color */
  color: white;
  padding: 10px 20px;
  border: none;
  cursor: pointer;
}

button:hover {
  background-color: #3e8e41; /* Background color on hover */
}

/* Example: Change link color and underline on hover */
a {
  color: blue;
  text-decoration: none;
}

a:hover {
  color: red;
  text-decoration: underline;
}

Common Hover Effects:

  • Color Changes: Changing the background or text color.
  • Background Image Changes: Swapping out a background image.
  • Text Decoration: Adding or removing underlines.
  • Shadows: Adding or modifying box shadows.
  • Transformations: Scaling, rotating, or translating elements.
  • Opacity Changes: Adjusting the transparency of an element.
  • Transitions: Creating smooth animations between states (see below).

Focus Effects

Focus effects are triggered when an element receives keyboard focus, typically through tabbing or clicking. They are essential for accessibility, allowing keyboard users to navigate and interact with the page.

CSS focus Pseudo-class:

The :focus pseudo-class is used to apply styles when an element has focus.

/* Example: Add an outline to an input field on focus */
input[type="text"] {
  padding: 5px;
  border: 1px solid #ccc;
}

input[type="text"]:focus {
  outline: 2px solid blue; /* Add a blue outline on focus */
  border-color: #66afe9; /* Change border color on focus */
}

/* Example: Change button background color on focus */
button:focus {
  background-color: #2e64fe;
}

Important Considerations for Focus Styles:

  • Accessibility: Always provide a visible focus indicator. Removing the default outline without replacing it with a custom style is a major accessibility issue.
  • Contrast: Ensure the focus indicator has sufficient contrast with the surrounding elements.
  • Consistency: Use consistent focus styles throughout your website.
  • Don't remove focus styles entirely: Keyboard users rely on these to navigate.

Transitions

Transitions create smooth animations between different states (e.g., from normal to hover or focus).

transition Property:

The transition property specifies which properties should be animated, the duration of the animation, and the timing function.

/* Example: Smooth color change on hover */
button {
  background-color: #4CAF50;
  color: white;
  padding: 10px 20px;
  border: none;
  cursor: pointer;
  transition: background-color 0.3s ease; /* Transition background-color over 0.3 seconds with an ease timing function */
}

button:hover {
  background-color: #3e8e41;
}

/* Example: Transition multiple properties */
div {
  width: 100px;
  height: 100px;
  background-color: red;
  transition: width 0.5s ease, height 0.5s ease, background-color 0.5s ease;
}

div:hover {
  width: 200px;
  height: 200px;
  background-color: blue;
}

transition Shorthand:

transition: property duration timing-function delay;

  • property: The CSS property to transition (e.g., background-color, width, transform). Use all to transition all properties.
  • duration: The length of time the transition takes (e.g., 0.3s, 1s).
  • timing-function: Defines the speed curve of the transition. Common values:
    • ease (default): Starts slowly, speeds up, then slows down.
    • linear: Constant speed.
    • ease-in: Starts slowly.
    • ease-out: Ends slowly.
    • ease-in-out: Starts and ends slowly.
  • delay: The amount of time to wait before the transition starts (e.g., 0.1s).

Combining Hover, Focus, and Transitions

You can combine these techniques to create sophisticated and user-friendly effects.

button {
  background-color: #4CAF50;
  color: white;
  padding: 10px 20px;
  border: none;
  cursor: pointer;
  transition: background-color 0.3s ease, box-shadow 0.3s ease;
}

button:hover {
  background-color: #3e8e41;
  box-shadow: 0 4px 8px 0 rgba(0, 0, 0, 0.2);
}

button:focus {
  outline: none; /* Remove default outline */
  box-shadow: 0 0 0 3px rgba(0, 123, 255, 0.5); /* Add a custom focus ring */
}

This example demonstrates a button that smoothly changes background color on hover, adds a shadow on hover, and displays a custom focus ring when focused. The outline: none; is used to remove the default browser outline, but a replacement focus indicator (the box-shadow) is always provided for accessibility.