css - 同时在多个元素上过渡和应用不同的样式

标签 css

我有一个包含 2 个 span 的 anchor 标签...

<a class="banner-logo" href="/search"><span id="banner-logo-hello">Hello</span><span id="banner-logo-world">World</span></a>

悬停此 anchor 标记时,我想更改范围内文本的颜色,但我希望它们各自为不同的颜色。现在,我一次只能让一个 span 过渡。无论悬停在 anchor 标记内的哪个跨度上,我如何才能让两个转换同时发生?

#banner-logo-hello:hover,
#banner-logo-hello:active,
#banner-logo-hello:focus {
  color: red;
  transition: 0.5s;
}

#banner-logo-world:hover,
#banner-logo-world:active,
#banner-logo-world:focus {
  color: yellow;
  transition: 0.5s;
}

最佳答案

两个跨度的目标 a:hover #span-id-name {}

a:hover #banner-logo-hello {
  color: red;
}

a:hover #banner-logo-world {
  color: yellow;
}
<a class="banner-logo" href="/search"><span id="banner-logo-hello">Hello</span><span id="banner-logo-world">World</span></a>

您还可以通过 :nth-child 或类似 :first-child:last-child 的变体来定位>:nth-of-type

a:hover span:last-child {
  color: red;
}

a:hover span:first-child {
  color: yellow;
}
<a class="banner-logo" href="/search"><span id="banner-logo-hello">Hello</span><span id="banner-logo-world">World</span></a>

关于css - 同时在多个元素上过渡和应用不同的样式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41793451/

相关文章:

javascript - 谷歌如何在主搜索栏中完成半完成的幽灵文本?

javascript - 将 article 替换为带有一个按钮的 div,该按钮将保留并可以在 article 和 div 内容之间切换

css - 重叠绝对定位的内容

html - 在同一行的图像上重叠透明阴影

html - 将图像的不同部分链接到不同的 url

javascript - 使用 css 自定义 bot 框架直线 API

css - 是否有在流体模式下使用 Zurb Foundation 网格的最佳实践?

html - 为什么蓝色框上方有一个空格?

css - 悬停时如何使文本和背景颜色仅出现在图像上

css - 浏览器如何将相邻的div布局到 float 的div