Home > Web Front-end > CSS Tutorial > How to Smoothly Transition Background Colors on Menu Hover?

How to Smoothly Transition Background Colors on Menu Hover?

Susan Sarandon
Release: 2024-12-05 20:06:18
Original
809 people have browsed it

How to Smoothly Transition Background Colors on Menu Hover?

How to Implement Smooth Background-Color Transitions for Menu Hovers

Achieving a transition effect for background color on hover is a common styling requirement, but can sometimes present challenges. Here, we explore a solution to this problem.

Original Code

The provided CSS code attempts to implement background color transitions, but encounters an issue:

#content #nav a:hover {
    color: black;
    background-color: #AD310B;
    /* Transition properties */
}
Copy after login

Solution

To enable transitions, you need to ensure support across various browsers. The following updated code should provide the desired fading effect:

a {
    background-color: #FF0;
}

a:hover {
    background-color: #AD310B;
    -webkit-transition: background-color 1000ms linear;
    -ms-transition: background-color 1000ms linear;
    transition: background-color 1000ms linear;
}
Copy after login

Explanation

  • -webkit-transition and -ms-transition provide browser-specific support for transitions in Safari and Internet Explorer, respectively.
  • transition is the standard transition property that supports modern browsers.

Example

<a>Navigation Link</a>
Copy after login

The above is the detailed content of How to Smoothly Transition Background Colors on Menu Hover?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template