html - 如何仅在输入聚焦时显示标签
问题描述
标签覆盖了我的输入。单击输入后,我仍然希望看到带有Dupsko
内容的标签,但是在我的输入上。
div {
position: relative;
background-color: rgba(38, 50, 56, 0.31);
transition: all 250ms cubic-bezier(0.35, 0, 0.25, 1);
}
label {
padding-left: 8px;
position: absolute;
text-overflow: ellipsis;
overflow: hidden;
cursor: text;
z-index: -10;
}
input {
outline: none;
border: none;
background-image: none;
background-color: transparent;
width: 100%;
padding: 8px;
border-bottom: 1px solid #263238;;
transition: all 250ms cubic-bezier(0.35, 0, 0.25, 1);
}
input:focus {
background-color: #263238;
border-bottom: 2px solid #3FBA84;
color: #FFFFFF;
}
input:focus ~ label {
position: unset;
color: #3FBA84;
}
<div>
<label>Dupsko</label>
<input>
</div>
仅当输入有焦点时,如何将样式应用于此标签?
解决方案
input:focus ~ label
选择label
前面有一个元素的每个input:focus
元素。因此,您需要更改 html 中两个元素的顺序。另外,我移动了
position: absolute;
from label
toinput
以便input
将堆叠在label
.
div {
position: relative;
background-color: rgba(38, 50, 56, 0.31);
transition: all 250ms cubic-bezier(0.35, 0, 0.25, 1);
}
label {
padding-left: 8px;
overflow: hidden;
cursor: text;
text-overflow: ellipsis;
}
input {
position: absolute;
outline: none;
border: none;
background-image: none;
background-color: transparent;
width: 100%;
padding: 8px;
border-bottom: 1px solid #263238;;
transition: all 250ms cubic-bezier(0.35, 0, 0.25, 1);
}
input:focus {
background-color: #263238;
border-bottom: 2px solid #3FBA84;
color: #FFFFFF;
z-index: -1;
}
input:focus ~ label {
position: unset;
color: #FFFF;
z-index: 1;
}
<div>
<input><label>Dupsko</label>
</div>
推荐阅读
- bash - 使用 grep 从文本文件中提取一行?
- javascript - 使用 javascript 删除 cookie 不会立即完成
- javascript - 从 React.js 应用程序启动一个 .exe
- visual-studio-code - 在 .ts 文件中搜索所有 pascal camel-case 单词
- java - 如何从 set 方法创建不同类型的新对象并将其存储到特定的 Array 列表中?
- kotlin - 为什么 guice requestInjection 对 Kotlin 对象不起作用
- excel - 将值带入范围时,VBA“编译错误:找不到方法或数据成员”
- fish - shell中的PATH获取神秘入口(鱼壳,ubuntu系统)
- perf - 使用 -r 和 -x 运行时 perf-stat 中的列是什么
- python - 如何使用 Python docx 库在 MS Word 文档的左上方添加徽标?