html - 如何通过 CSS 为聚焦输入框上的标签着色?

标签 html css

我想要着色标签对输入焦点的影响。就像我关注输入框时,标签颜色应该变为蓝色。但这不起作用,我想知道为什么以及如何解决这个问题?

.test-label{
			color: purple;
}
.test-box:focus .test-label{
			color: blue !important;
}
<form>
		<label class = "test-label">Label</label>
		<br />
		<input type = "text" class = "test-box">
	</form>

最佳答案

您可以使用 focus-within为此。

Note it has limited support.

.test-label {
  color: purple;
}

form:focus-within .test-label {
  color: blue;
}
<form>
  <label class="test-label">Label</label>
  <br />
  <input type="text" class="test-box">
</form>

关于html - 如何通过 CSS 为聚焦输入框上的标签着色?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46713981/

相关文章:

html - 当我在 Photoshop 中设计图像以作为 CSS 中的标题发布时,分辨率出现问题

php - 如何在数据库php/mysql中存储图片链接

html - 如何在 Bootstrap 列中垂直居中 div?

html - 如何在 Rails 中启用自动代码重新加载

jquery - 如何将div排列为停靠面板?

javascript - D3.JS Y轴标签问题

html - 框阴影在 Microsoft Edge 中不起作用

html - 为什么特殊部门中 HTML 中的单选按钮不起作用?

css - 一般替换 dijit ContentPane 的加载消息

javascript - 哪些文本可以复制但不能搜索?