html - CSS:如果同级输入没有值,则选择标签并更改颜色
问题描述
如果他们没有,我想让所有人labels
都拥有。color: red;
sibling
input
value
我尝试先选择inputs
没有value
这样的:
.state-input--red > .input__wrapper > .input__input:not([value])
之后我试图选择他sibling
label
添加这部分:
~ .input__label
所以我得到了这个选择和设置color
,red
但它不起作用:
.state-input--red > .input__wrapper > .input__input:not([value]) ~ .input__label {
color: red;
}
我还尝试通过在之前sibling
添加 来指定选择,但它不起作用。这是我的片段:.input__wrapper >
label
.state-input--red > .input__wrapper > .input__input:not([value]) ~ .input__label {
color: red;
}
<div class="input state-input--red">
<div class="input__wrapper">
<label class="input__label" for="input00">I should be black, because my siblin input has a value</label>
<input id="input00" type="text" value="Hello World">
</div>
</div>
<hr>
<div class="input state-input--red">
<div class="input__wrapper">
<label class="input__label" for="input01">I should be red, because my sibling input has no value</label>
<input id="input01" class="input__input" type="text">
</div>
</div>
解决方案
选择器 likea ~ b
匹配b
元素前面的所有兄弟元素a
,但在您的标记中,它是:label
之前的 who input
,这就是它不起作用的原因。
作为一种可能的解决方案,您可以使用 将.input__wrapper
元素转换为flexbox
容器,然后在代码中flex-direction: row-reverse
切换 和 的顺序以保持当前的可视化。label
input
这样做的选择器.input__input:not([value]) ~ .input__label
将按预期工作。
推荐阅读
- java - 每个子数组的所有元素的总和
- path - 尽管已将其添加到 PATH 中,但我无法从 CMD 运行 g++
- oauth - 如何允许 .Net Web API 中的多个域使用 CORS 进行 OAuth 令牌身份验证?
- java - 将一个占位符替换为 Java 字符串中的另一个占位符
- java - 从数据类型列表中选择最合适的数据类型
- csv - 删除 CSV 文件中的最后一行
- python - 为什么poetry安装windows只依赖macOS和linux?
- javascript - 代理 ??= new https.Agent({ ...this.client.options.http.agent, keepAlive: true });
- c# - C# 模式匹配与属性的操作值
- r - 如何将所有因子水平折叠成R中的新水平