【问题标题】:Does :focus work with specificity as :hover does?:focus 是否像 :hover 那样具有特异性?
【发布时间】:2016-04-02 21:51:56
【问题描述】:

这是我的问题。我希望拥有一个图标(搜索图标、日历图标等)的input-group-addon 通过特异性继承其输入字段的悬停、焦点和活动状态。我让它为悬停工作,但由于某种原因,特异性忽略了焦点状态。我认为这是我的代码中的一个冲突的 css 指令,但我在 CODEPEN 中隔离了这个问题,它也在那里。

总之,我希望输入组插件边框在我关注它(选项卡或单击)时与其输入字段无缝更改为黄色,就像我将鼠标悬停在它上面一样。

我的 HTML

<div class="input-group controls">
    <input type="text" class="form-control" placeholder="Search by Name" />
    <span class="input-group-addon right search"></span>
</div>

我的 CSS(如果可能,请使用 Chrome。我没有在此处包含 corssbrowser 的内容以使其更易于阅读)此外,这最初是基于 SCSS 构建的:

.mar40 {
    margin: 40px auto;
}

.form-control {
    border-width: 2px;
    border-color: #8f8f8f;
    -webkit-box-shadow: none;
    box-shadow: none;
    color: #bbbbbb;
    -webkit-border-radius: 34px;
    -moz-border-radius: 34px;
    -ms-border-radius: 34px;
    border-radius: 34px;
    background: #211E1E;
}

.form-control:focus,
.form-control:hover {
    border-color: rgba(248, 151, 29, 0.77);
    -webkit-box-shadow: none;
    box-shadow: none;
    outline: none;
    transition: all 1s ease;
}

.form-control .input-group-addon {
    background: #8f8f8f;
    border-color: #555555;
    border-width: 2px;
}

.controls .input-group-addon.right.search {
    background: #30373e;
    border: 2px solid #8f8f8f;
    color: #bbbbbb;
    border-left: none;
    border-radius: 0px 20px 20px 0;
    padding: 4px 10px;
    min-width: 0;
}

.controls .input-group-addon.right.search:before {
    content: "\f4a4";
    font-family: 'Ionicons';
    font-size: 16px;
}

.controls:focus .input-group-addon.right,
.controls:hover .input-group-addon.right,
.controls:active .input-group-addon.right {
    border: 2px solid rgba(248, 151, 29, 0.77) !important;
    border-left: none !important;
    transition: all 1s ease;
}

.controls:focus .input-group-addon.right:before,
.controls:hover .input-group-addon.right:before,
.controls:active .input-group-addon.right:before {
    color: rgba(248, 151, 29, 0.77);
    transition: all 1s ease;
}

悬停/焦点/活动所需的效果图

这就是我要关注的内容

还有方便的花花公子CODEPEN

谢谢!

【问题讨论】:

  • :focus:hover 具有相同的特异性。我无法重现您的现场演示的问题。
  • 嗯。真的吗?这很奇怪。你用的是铬。也许这是一个浏览器的事情。而且我总是虽然焦点和悬停也具有相同的特异性,但我想不出任何其他原因为什么它不会为我做这件事。顺便说一句,我添加了一张我正在做的事情的照片

标签: html css


【解决方案1】:

:focus 适用于 input 而不是父容器,因此您的选择器组应如下所示。 (请注意第一行中更改的选择器

.form-control:focus + .input-group-addon.right,
.controls:hover .input-group-addon.right,
.controls:active .input-group-addon.right {
  border: 2px solid rgba(248, 151, 29, 0.77) !important;
  border-left: none !important;
  transition: all 1s ease;
}

据我所知,当您 :hover 孩子时,您也间接地悬停在父母身上,因此当 .form-control:hover 适用时,.controls:hover .input-group-addon.right 也适用。因此.form-control.input-group-addon.right 都获得了边框。

但是当您关注.form-control 时,焦点选择器仅适用于input 而不会应用于其容器。因此只有.form-control 获得边框而不是.input-group-addon。因此,必须将选择器更改为基于input 而不是容器的焦点设置.input-group-addon 的样式。

CodePen Demo

【讨论】:

  • 这太棒了。它确实解决了问题,我理解。这实际上是一个特异性问题,而是一个人为错误,而不是选择器。好东西!不过,我确实有一点补充。在某些情况下,我在左侧有输入组插件,这会稍微改变特异性。复制使用 .left 类提供的代码并不能解决问题,因为这是编写 HTML 的方式。换句话说,保存输入组插件的跨度标签放置在输入字段上方
  • 无论如何我都会接受这个答案,我只是觉得您可能想参与该调整。我会将 HTML 添加到 codepen
  • @LOTUSMS:是的,我使用了兄弟选择器 (+)。 CSS 兄弟选择器仅选择 DOM 中低于参考元素的元素(子元素、后代或更高的兄弟元素)。因此,如果 .input-group-addon 元素在 .form-control 之前,它将不起作用。也许您可以调整浮动(即设置float: right)或设置position: absolute 等以将.input-group-addon 保留在.form-control 下方,但仍使其显示在.form-control 之前。
  • 我想到了。这是可行的,但一个丑陋的解决方案。也许是唯一的解决方案。我会解决它,看看我能带来什么。谢谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-05-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多