Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change color of a tag when i hover over a div

Tags:

html

css

I have an HTML structure like this

There's a div with an a tag inside of it. I want it that when I hover over the div, the background color of the div changes and the color of its a tag changes to black.

Is it possible to do it in CSS or do I have to use js?

<div class="example"><a href="#">TEXT</a></div>

Nothing really worked out for me...

I tried these weird, a:hover + .div, but it didn't work.

like image 635
heaven Avatar asked Dec 06 '25 20:12

heaven


2 Answers

This works with your link. With the a:link you can change the links default color and with the a:visited you can make sure it keeps the color after you have visited the link.

With the a:hover color: you can change the color of the text itself when hovering and the background-color of course changes the color of just that. Let me know if you have any more questions.

.example a:link, .example a:visited {
    color: green;
}
.example a:hover {
    color: black;
    background-color: red;
}
<html>
    <body>
        <div class="example"><a href="#">TEXT</a></div>
     </body>
</html>
like image 156
Ietu Avatar answered Dec 08 '25 11:12

Ietu


 .example:hover a{
    color:black;
 }
 .example:hover {
     background:red;
 }

like image 24
Hitansh Thakur Avatar answered Dec 08 '25 12:12

Hitansh Thakur



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!