简体   繁体   English

使用 CSS 更改悬停时的文本颜色

[英]Changing text color on hover using 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.我有一个 div,其中包含文本和图像。 I want all text and background within the div to change color when I hover anywhere within the div.当我将鼠标悬停在 div 内的任何位置时,我希望 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.我已经这样做了,以便底部的文本与背景颜色一起更改,但似乎无法让顶部文本 (h4) 更改颜色。

It changes color when I hover directly over the h4 element but not when I hover anywhere within the div.当我直接将鼠标悬停在 h4 元素上时它会改变颜色,但当我将鼠标悬停在 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 ap like the rest. h4 标签的 CSS 有单独的样式,所以不能像其他标签一样使它成为 ap。 That would be the easiest way to do this but unfortunately they must stay different.这将是最简单的方法,但不幸的是,它们必须保持不同。

This is my CSS style这是我的 CSS 样式

.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?有没有办法使用 CSS 而不是 jquery/javascript 来做到这一点? I'm new to Web Development so only know some HTML/CSS at present.我是 Web 开发的新手,所以目前只知道一些 HTML/CSS。

Thanks.谢谢。

Tom汤姆

JSFIDDLE LINK JSFIDDLE 链接

Change your CSS style from更改您的 CSS 样式

#project1 h4:hover {
    color: white;
}

To

#project1:hover h4 {
    color: white;
}

JSFIDDLE DEMO JSFIDDLE 演示

You can use您可以使用

#project1 h4 {
    color: inherit;
}

to make it inherit #project1 's color.使其继承#project1的颜色。

Demo演示

You can nest h4 tag in p tag.您可以在 p 标签中嵌套h4标签。 no need for #project1 h4:hover in CSS .不需要#project1 h4:hoverCSS

Demo Fiddle演示小提琴

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM