简体   繁体   English

如何对 CSS 中的每个直接子级分别应用 hover 效果?

[英]How to apply a hover effect on each direct child individually in CSS?

I am trying to create a tab bar with a hover effects on its direct children, therefore i have the following code with the intent of applying an effect on each element inside the list individually, but idoesn't work.我正在尝试创建一个对其直接子项具有 hover 效果的选项卡栏,因此我有以下代码,目的是分别对列表中的每个元素应用效果,但它不起作用。

HTML code: HTML 代码:

   <!-- navigation -->
        <div>
          <nav>
            <ul class="primary-navigation underline-indicators flex">
              <li class="active">
                <a class="uppercase text-white letter-spacing-2" href="#"
                  ><span>01</span>Active</a
                >
              </li>
              <li>
                <a class="uppercase text-white letter-spacing-2" href="#"
                  ><span>02</span>Hovered</a
                >
              </li>
              <li>
                <a class="uppercase text-white letter-spacing-2" href="#"
                  ><span>03</span>Idle</a
                >
              </li>
            </ul>
          </nav>
        </div>

CSS code: CSS 代码:

.underline-indicators > * {
  padding: var(--underline-gap, 1rem) 0;
  border-bottom: 0.2rem solid hsl(var(--clr-white) / 0);
}

.underline-indicators > *:hover,
.underline-indicators > *:focus {
  border-color: hsl(var(--clr-white) / 0.5);
}

.underline-indicators > .active {
  border-color: hsl(var(--clr-white) / 1);
}

is it possible to apply this effect and if yes what is the optimal way to do it?是否可以应用这种效果,如果可以,最佳方法是什么?

Providing that when i replace *:hover with li:hover it works just fine, but i want to use * so i can reuse the same style on different parts of my code.假设当我用li:hover替换*:hover时它工作得很好,但我想使用*这样我就可以在代码的不同部分重用相同的样式。

<style>
.underline-indicators {
    list-style:none;
}

.underline-indicators li > *{
  padding: var(--underline-gap, 1rem) 0;
  border-bottom: 0.2rem solid hsl(var(--clr-white) / 0);
  color:green;
}

.underline-indicators *:hover,
.underline-indicators *:focus {
  border-color: hsl(var(--clr-white) / 0.5);
}
.underline-indicators .active {
    border-color: hsl(var(--clr-white) / 1);
}
</style>

Try This CSS CODE试试这个 CSS 代码

Why you don't set a unique ID for each element that you want to put hover on it?为什么不为每个要放置 hover 的元素设置唯一 ID?

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

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