如果使用ID和类,CSS Tilda不起作用

发布于 2025-02-09 00:36:27 字数 793 浏览 3 评论 0原文

#header__menu~.header__button {
  background-color: red;
}
<div class="header__menu">
  <div class="header__button">
    <div class="header__button-1 header__line"></div>
    <div class="header__button-2 header__line"></div>
    <div class="header__button-3 header__line"></div>
  </div>
  <input type="checkbox" name="" id="header__menu">
</div>

此代码只是行不通的。我不知道为什么。我花了30分钟的时间来修复它,但是我真的不知道为什么。

#header__menu~.header__button {
  background-color: red;
}
<div class="header__menu">
  <div class="header__button">
    <div class="header__button-1 header__line"></div>
    <div class="header__button-2 header__line"></div>
    <div class="header__button-3 header__line"></div>
  </div>
  <input type="checkbox" name="" id="header__menu">
</div>

this code just doesn't work. I have no idea why. I spent like 30 minutes trying to fix it, but I have literally no idea why.
screenshot

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

吃不饱 2025-02-16 00:36:27

tilde是a

.header__button-1 ~ .header__button-2 {
  background-color: red;
}

。但是,如果您做到了,

.header__button-3 ~ .header__button-2 {
  background-color: green
}

那将不会发生任何事情。没有标头__ button-2是在.header__ button-3之后进行的。

The tilde is a General Sibling Combinator which means it works on siblings, not parents and children. Specifically, it works on siblings B that comes after sibling A and has the same parent, where you have A ~ B. So, in your code, if you have

.header__button-1 ~ .header__button-2 {
  background-color: red;
}

then .header__button-2 would have a red background. But if you did

.header__button-3 ~ .header__button-2 {
  background-color: green
}

there wouldn't be anything that really happens. There is no header__button-2 that comes after a .header__button-3.

蓝眼泪 2025-02-16 00:36:27

相关选择器的行为类似于相邻的选择器,但与样式规则不同,附近的所有元素都适用。

.header__button-1 ~ div {
    background-color: red;
}

Related selectors behave similar to neighboring selectors, but unlike the, style rules apply to all nearby elements.

.header__button-1 ~ div {
    background-color: red;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文