css - 在 SCSS 中更改输入焦点上的标签颜色

标签 css sass

鉴于此代码,无论我使用什么 - focus、:checked、hover 等,我都无法更改标签的颜色。

我已经在 stackoverflow 上阅读了多个线程,但不知何故我似乎无法处理这个问题。

我知道为了使用+选择器,标签必须在输入之后。

如果我把标签放在输入后,对 SEO 不利吗?

纯 CSS(SCSS) 是否可行,还是我需要 jQuery?

.widg-input {
  .input {
    &:focus {
      outline: none;
      border: none;
      border-bottom: .1rem solid green;
    }
    &:checked {
      +.input-label {
        color: red;
      }
    }
  }
}
<section class="widg-input">
  <label for="input1" class="input-label">firstname</label>
  <input type="text" class="input" name="input1" placeholder="John">
</section>

最佳答案

在输入后有一个标签对 SEO 来说不是坏事。 for 属性将这两个元素关联起来。机器人会尊重这一点。

您可以将标签放在输入后,然后使用flexorder 以可视化方式重新排列它们。

你的 SCSS 看起来像

.widg-input {
  display: flex;
  .input-label {
    order: -1;
  }
  .input {
    &:focus {
      outline: none;
      border: none;
      border-bottom: .1rem solid green;
      + .input-label {
        color: red;
      }
    }
  }
}

这是渲染结果

.widg-input {
  display: flex;
}
.widg-input .input-label {
  order: -1;
}
.widg-input .input:focus {
  outline: none;
  border: none;
  border-bottom: .1rem solid green;
}
.widg-input .input:focus + .input-label {
  color: red;
}
<section class="widg-input">
  <input type="text" class="input" name="input1" placeholder="John">
  <label for="input1" class="input-label">firstname</label>
</section>

关于css - 在 SCSS 中更改输入焦点上的标签颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43968127/

相关文章:

css - MiniCssExtractPlugin 没有链接到我的 html 文件

css - 生成的 css 文件应该与我的组件的源代码放在一起吗?

css - SASS @extend 指令不适用于输入占位符

css - HTML/CSS 页面中的布局挑战

css - 对齐 block 元素的顶部

html - 将图标图像添加到 html 元素的 css 类

html - CSS:将 Bootstrap 输入的标签放在上面?

css - 造型 Quasar q-select

css - 使用@import编译sass时报错 "Invalid UTF-8"

html - 如何在 <button> 元素中放置一个彩色方 block 而不是一些文本?