[英]Is it possible to select the nearest css selector, or parent
Sorry for the generically worded title, anyway, I have some simple HTML like so: 对于通用的标题很抱歉,无论如何,我有一些简单的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>
Now, whenver a person clicks the input, I am shrinking the label size like so: 现在,当一个人点击输入时,我缩小了标签大小,如下所示:
With the following css: 使用以下css:
&.label-floating {
&:focus-within {
& > .control-label {
top: 10px;
font-size: 11px;
line-height: 1.07143;
}
}
}
Now, my problem is, if the input is not empty and the user leaves the input, the label will return to the normal size. 现在,我的问题是,如果输入不为空且用户离开输入,标签将返回正常大小。 So I have tried things like so:
所以我尝试过这样的事情:
with the following css: 使用以下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;
}
}
}
With no avail, so, my question is, is there a way to achieve what I want, with pure css? 没有用,所以,我的问题是,有没有办法用纯粹的CSS实现我想要的东西?
edit: can someone please edit the OP to embed the images, please. 编辑:请有人请编辑OP以嵌入图片。
Updated with pure css: 更新了纯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;
}
You cannot target an input that contains a value without checking if the content is valid. 您无法在不检查内容是否有效的情况下定位包含值的输入。 You can, however, use a round-about way to check if the input has no value.
但是,您可以使用循环方式检查输入是否没有值。
Your input has a placeholder
attribute. 您的输入具有
placeholder
属性。 The CSS selector :placeholder-shown
will target inputs which are showing a placeholder; CSS选择器
:placeholder-shown
将定位显示占位符的输入; that is, inputs that have a placeholder attribute specified and are not empty. 也就是说,指定了占位符属性且不为空的输入。
You must therefore style the empty input's label. 因此,您必须设置空输入标签的样式。
.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>
Note that :placeholder-shown
is not implemented in IE/Edge, despite being highly requested ; 请注意
:placeholder-shown
尽管受到高度要求 ,但IE / Edge中未实现:placeholder-shown
; there are, however, polyfills available. 但是,有可用的填充物 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.