css - 如何将多个选择器与相邻兄弟选择器组合?

标签 css css-selectors

所以我有以下样式组合,可以按我的意愿工作。如果按钮处于 focusactive 状态,则按钮的 visibility: visible;

  .new-comment-button {
    margin: 0.25em 0 0.5em 0;
    float: right;
    visibility: hidden;
  }

  .new-comment-input:focus + .new-comment-button, 
  .new-comment-button:focus {
    visibility: visible;
  }

 .new-comment-input:active + .new-comment-button,
  .new-comment-button:active {
    visibility: visible;
  }

我想做的是将最后两种样式组合成一个,当输入处于 activefocus 状态时可以使用。但是,我尝试的每个组合都失败了,例如:

  .new-comment-input:focus + .new-comment-button,
  .new-comment-input:active + .new-comment-button, 
  .new-comment-button:focus {
    visibility: visible;
  }

我也试过:

  .new-comment-input:focus:active + .new-comment-button,
  .new-comment-button:focus {
    visibility: visible;
  }

都没用。我可以坚持使用单独的样式,但我很好奇是否有一种方法可以将它们正确组合并考虑 input 的两种状态?

最佳答案

你唯一能做的就是删除无关的 { visibility: visible; } 并将其替换为逗号:

.new-comment-input:focus + .new-comment-button, 
.new-comment-button:focus,
.new-comment-input:active + .new-comment-button,
.new-comment-button:active {
  visibility: visible;
}

您有四个选择器,分为两个规则集,但您不能将它们中的任何一个组合起来,因此您必须简单地将它们的两组合并为一个。 :focus:active 表示同时存在这两种状态 (AND),而不是其中任何一种 (OR),因此您不能以这种方式组合它们。

关于css - 如何将多个选择器与相邻兄弟选择器组合?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35053030/

相关文章:

html - CSS 边框显示问题,不适用于 Chrome 和 Safari

jquery - 使用 jQuery 拉伸(stretch)图像以填充整个 div,它在窗口调整大小时调整大小

我页面中的 CSS 背景错误

html - 在 IE 7 和 8 中加载后完整页面移至右侧

css - 第 n 个和第 m 个兄弟元素的选择器?

javascript - 如何使用 JS 选择 input::placeholder?

来自 Sharepoint 的 HTML 电子邮件看起来与网站上的不同

CSS 从具有特定属性的元素中选择第一个子元素

html - 带有 CSS 选择器的表格中的剧透

jquery第n个子问题