Home  >  Article  >  Web Front-end  >  Why doesn\'t my div change color on hover, only the link inside it?

Why doesn\'t my div change color on hover, only the link inside it?

DDD
DDDOriginal
2024-11-03 18:43:02349browse

Why doesn't my div change color on hover, only the link inside it?

Changing Div Background Color on Hover

Q: I'm trying to make a div's background color change when the mouse hovers over it. I've tried using the CSS code provided below, but only the link inside the div changes color. How can I make the entire div change color on hover?

CSS Code:

the div {background: white;}
the div a:hover {background: grey; width: 100%; display: block; text-decoration: none;}

A: The "a:hover" in your CSS code specifically targets the tag, causing only the link to change color on hover. To change the entire div's background color, you need to use "the div:hover" instead. Here's the corrected code:

the div {background: white;}
the div:hover {background: grey;}

Additionally, if you want the entire div to act as a link, you can add an tag around it with the desired link address:

<a href="link_address">
  <div style="background: white;">
    ...
  </div>
</a>

Note that if you want to change only a specific div, you can give it an ID or class in the HTML and target it using that identifier in your CSS.

The above is the detailed content of Why doesn\'t my div change color on hover, only the link inside it?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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