Set a:hover based on class(设置 a:hover 基于类)
问题描述
我有以下 HTML:
<div class="menu">
<a class="main-nav-item" href="home">home</a>
<a class="main-nav-item-current" href="business">business</a>
<a class="main-nav-item" href="about-me">about me</a>
</div>
在 CSS 中,我想将这些菜单项的 a:hover
设置为特定颜色.所以我写:
In CSS, I want to set the a:hover
for these menu items to a particular color. So I write:
.menu a:hover
{
color:#DDD;
}
但是,我只想为具有类 main-nav-item<a> 标记设置此 a:hover
颜色> 而不是 main-nav-item-current,因为它具有不同的颜色并且在悬停时不应该改变.menu div 中的所有 <a>
标记都应在悬停时更改颜色,但具有 current 类的标记除外.
But, I want to set this a:hover
color only for those <a>
tags with the class main-nav-item and not the main-nav-item-current, because it has a different color and shouldn't change on hover. All <a>
tags within the menu div should change color on hover except the one with the current class.
如何使用 CSS 来实现?
How can I do it using CSS?
我尝试了类似的东西
.menu a:hover .main-nav-item
{
color:#DDD;
}
认为只有具有 main-nav-item 类的才会在悬停时改变颜色,而不是当前的.但它不起作用.
thinking that only ones with main-nav-item class will change color on hover, and not the current one. But it is not working.
推荐答案
试试这个:
.menu a.main-nav-item:hover { }
为了理解它是如何工作的,以浏览器的方式阅读它是很重要的.a
定义元素, .main-nav-item
限定 元素仅适用于具有该类的元素,最后是伪元素:hover
类应用于前面的限定表达式.
In order to understand how this works it is important to read this the way the browser does. The a
defines the element, the .main-nav-item
qualifies the element to only those which have that class, and finally the psuedo-class :hover
is applied to the qualified expression that comes before.
基本上可以归结为:
将此悬停规则应用于所有具有 main-nav-item
类的锚元素,这些锚元素是具有 menu
类的任何元素的后代子元素.
Apply this hover rule to all anchor elements with the class
main-nav-item
that are a descendant child of any element with the classmenu
.
这篇关于设置 a:hover 基于类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:设置 a:hover 基于类
基础教程推荐
- 当用户滚动离开时如何暂停 youtube 嵌入 2022-01-01
- Karma-Jasmine:如何正确监视 Modal? 2022-01-01
- 在 JS 中获取客户端时区(不是 GMT 偏移量) 2022-01-01
- 在for循环中使用setTimeout 2022-01-01
- 角度Apollo设置WatchQuery结果为可用变量 2022-01-01
- 响应更改 div 大小保持纵横比 2022-01-01
- 动态更新多个选择框 2022-01-01
- 有没有办法使用OpenLayers更改OpenStreetMap中某些要素 2022-09-06
- 我什么时候应该在导入时使用方括号 2022-01-01
- 悬停时滑动输入并停留几秒钟 2022-01-01