Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Changing text color on hover using CSS

Tags:

html

css

I've searched but couldn't find anything relating to this problem I'm having.

I have been trying to work this out for ages now but can't seem to do it. I have a div which has text and an image in it. I want all text and background within the div to change color when I hover anywhere within the div. I have made it so that the text at the bottom changes, along with the background color, but can't seem to get the top text (h4) to change color.

It changes color when I hover directly over the h4 element but not when I hover anywhere within the div.

The link below is a rough example of what I want to achieve. There is seperate styling on the CSS of the h4 tag so can't make it a p like the rest. That would be the easiest way to do this but unfortunately they must stay different.

This is my CSS style

.container {
    text-align: center;
}

.container h4 {
    text-align: center;
    color: black;
}

#project1 {
    text-align: center;
    color: white;
    background-color: white;
    background-color: rgba(255,255,255,0.9);
    color: black;
}

#project1:hover {
    background-color: blue;
    color: white;
}

#project1 h4:hover {
    color: white;
}

#project1 h4 {
    text-transform: uppercase;
}

a {
    text-decoration: none;
}

Is there any way to do this using CSS and not jquery/javascript? I'm new to Web Development so only know some HTML/CSS at present.

Thanks.

Tom

JSFIDDLE LINK

like image 379
tomjoweb Avatar asked Jun 21 '14 13:06

tomjoweb


People also ask

How do you make something change color when you hover over it CSS?

Changing link color on hover using CSS To change the color of your link on hover, use the :hover pseudo property on the link's class and give it a different color.

How do you change the text color on a mouseover in HTML?

To change an element's text color on mouseover: Add a mouseover event to the element, changing its text color when the user hovers over it. Add a mouseout event to the element, changing its text color back to the default when the user moves their cursor out.


3 Answers

Change your CSS style from

#project1 h4:hover {
    color: white;
}

To

#project1:hover h4 {
    color: white;
}

JSFIDDLE DEMO

like image 91
Shiju K Babu Avatar answered Sep 21 '22 10:09

Shiju K Babu


You can use

#project1 h4 {
    color: inherit;
}

to make it inherit #project1's color.

Demo

like image 43
Oriol Avatar answered Sep 21 '22 10:09

Oriol


You can nest h4 tag in p tag. no need for #project1 h4:hover in CSS.

Demo Fiddle

like image 45
netfreak30 Avatar answered Sep 25 '22 10:09

netfreak30