根据类别设置 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
设置为特定颜色。所以我写道:
.menu a:hover
{
color:#DDD;
}
但是,我只想为那些具有 main-nav-item 类的 标签设置此
a:hover
颜色em> 而不是 main-nav-item-current,因为它具有不同的颜色,并且在悬停时不应更改。 menu div 中的所有 标签在悬停时都应更改颜色,但具有 current 类的标签除外。
我怎样才能使用 CSS 做到这一点?
我尝试过类似的
.menu a:hover .main-nav-item
{
color:#DDD;
}
想法,认为只有具有 main-nav-item 类的项目才会在悬停时改变颜色,而不是当前的颜色。但它不起作用。
I have the following 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>
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;
}
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.
How can I do it using CSS?
I tried something like
.menu a:hover .main-nav-item
{
color:#DDD;
}
thinking that only ones with main-nav-item class will change color on hover, and not the current one. But it is not working.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
试试这个:
为了理解它是如何工作的,以浏览器的方式阅读它很重要。
a
定义元素,.main-nav-item
将元素限定为仅具有该类的元素,最后是 psuedo- class:hover
应用于前面的限定表达式。基本上可以归结为:
Try this:
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.Basically it boils down to this:
级联正在咬你。试试这个:
这段代码表示要抓取所有具有 main-nav-item 类并且是类菜单子级的链接,并在它们悬停时应用颜色 #DDD。
Cascading is biting you. Try this:
This code says to grab all the links that have a class of main-nav-item AND are children of the class menu, and apply the color #DDD when they are hovered.
根据类设置 a:hover 您可以简单地尝试:
Set a:hover based on class you can simply try:
怎么样
.main-nav-item:hover
这可以保持较低的特异性
how about
.main-nav-item:hover
this keeps the specificity low
尝试一下,
假设我们的代码中使用了一个锚标记,并且在主程序中调用了“div”类。 a:hover 可以做到这一点,当鼠标移到它上面时,它会给背景提供吸血鬼黑色,给文本提供白色,这就是悬停的意思。
try this
lets say we have a anchor tag used in our code and class"div" is called in the main program. the a:hover will do the thing, it will give a vampire black color to the background and white color to the text when the mouse is moved over it that's what hover means.
我发现如果你添加一个
!important
,它会起作用,而之前它不会起作用。另外,我在某处读到顺序很重要。助记符“LoVe HaTe”可以帮助您记住它:链接 ->访问过 ->悬停->积极的
I found if you add a
!important
, it works when previously it didn't.Also, I've read somewhere that the order is important. The mnemonic "LoVe HaTe" helps you remember it: link -> visited -> hover -> active
一个常见的错误是在类名之前留一个空格。即使这是正确的语法:
它也永远不会起作用。
因此,你不会
写成
One common error is leaving a space before the class names. Even if this was the correct syntax:
it never would have worked.
Therefore, you would not write
it would be