是否可以 select 最近的 css selector, or parent
Is it possible to select the nearest css selector, or parent
抱歉标题措辞笼统,无论如何,我有一些简单的 HTML 像这样:
<div class="block flex-1 mx-4 relative w-1/2">
<div class="form-group label-floating">
<label class="control-label">Password</label>
<input class="form-control" placeholder="" type="password">
<span class="material-input"></span>
</div>
</div>
现在,只要有人点击输入,我就会像这样缩小标签大小:
与以下 css:
&.label-floating {
&:focus-within {
& > .control-label {
top: 10px;
font-size: 11px;
line-height: 1.07143;
}
}
}
现在,我的问题是,如果输入不为空并且用户离开输入,标签将 return 变为正常大小。所以我尝试过这样的事情:
与以下 css:
&.label-floating {
&:focus-within {
& > .control-label {
top: 10px;
font-size: 11px;
line-height: 1.07143;
}
}
& input:not(:empty){
& .control-label {
top: 10px;
font-size: 11px;
line-height: 1.07143;
}
& < .control-label {
top: 10px;
font-size: 11px;
line-height: 1.07143;
}
}
}
没有用,所以,我的问题是,有没有办法用纯 css 实现我想要的?
编辑:有人可以编辑 OP 以嵌入图像吗?
更新为纯css:
.form-group {
margin-bottom: 1.5rem;
position: relative;
}
.form-group > .control-label {
color: #888da8;
}
.form-group.label-floating:focus-within > .control-label {
top: 10px;
font-size: 11px;
line-height: 1.07143;
}
.form-group.label-floating input:not(:empty) .control-label {
top: 10px;
font-size: 11px;
line-height: 1.07143;
}
您不能在不检查内容是否有效的情况下定位包含值的输入。但是,您可以使用一种迂回的方式来检查输入是否具有 no 值。
您的输入具有 placeholder
属性。 CSS 选择器 :placeholder-shown
将针对显示占位符的输入;也就是说,指定了占位符属性且不为空的输入。
因此,您必须设置空输入标签的样式。
.form-control:placeholder-shown ~ .control-label {
font-size: 20px;
color: red;
}
.label-floating:focus-within > .control-label,
.form-control ~ .control-label {
font-size: 11px;
color: blue;
}
<div class="form-group label-floating">
<input class="form-control" placeholder="" type="password">
<label class="control-label">Password</label>
</div>
请注意 :placeholder-shown
未在 IE/Edge 中实现,尽管 highly requested; there are, however, polyfills 可用。
抱歉标题措辞笼统,无论如何,我有一些简单的 HTML 像这样:
<div class="block flex-1 mx-4 relative w-1/2">
<div class="form-group label-floating">
<label class="control-label">Password</label>
<input class="form-control" placeholder="" type="password">
<span class="material-input"></span>
</div>
</div>
现在,只要有人点击输入,我就会像这样缩小标签大小:
与以下 css:
&.label-floating {
&:focus-within {
& > .control-label {
top: 10px;
font-size: 11px;
line-height: 1.07143;
}
}
}
现在,我的问题是,如果输入不为空并且用户离开输入,标签将 return 变为正常大小。所以我尝试过这样的事情:
与以下 css:
&.label-floating {
&:focus-within {
& > .control-label {
top: 10px;
font-size: 11px;
line-height: 1.07143;
}
}
& input:not(:empty){
& .control-label {
top: 10px;
font-size: 11px;
line-height: 1.07143;
}
& < .control-label {
top: 10px;
font-size: 11px;
line-height: 1.07143;
}
}
}
没有用,所以,我的问题是,有没有办法用纯 css 实现我想要的?
编辑:有人可以编辑 OP 以嵌入图像吗?
更新为纯css:
.form-group {
margin-bottom: 1.5rem;
position: relative;
}
.form-group > .control-label {
color: #888da8;
}
.form-group.label-floating:focus-within > .control-label {
top: 10px;
font-size: 11px;
line-height: 1.07143;
}
.form-group.label-floating input:not(:empty) .control-label {
top: 10px;
font-size: 11px;
line-height: 1.07143;
}
您不能在不检查内容是否有效的情况下定位包含值的输入。但是,您可以使用一种迂回的方式来检查输入是否具有 no 值。
您的输入具有 placeholder
属性。 CSS 选择器 :placeholder-shown
将针对显示占位符的输入;也就是说,指定了占位符属性且不为空的输入。
因此,您必须设置空输入标签的样式。
.form-control:placeholder-shown ~ .control-label {
font-size: 20px;
color: red;
}
.label-floating:focus-within > .control-label,
.form-control ~ .control-label {
font-size: 11px;
color: blue;
}
<div class="form-group label-floating">
<input class="form-control" placeholder="" type="password">
<label class="control-label">Password</label>
</div>
请注意 :placeholder-shown
未在 IE/Edge 中实现,尽管 highly requested; there are, however, polyfills 可用。