使用 CSS 匹配空输入框

发布于 2024-09-16 19:43:30 字数 105 浏览 6 评论 0原文

如何将样式应用于空输入框?如果用户在输入字段中键入内容,则不应再应用该样式。这在 CSS 中可能吗?我试过这个:

input[value=""]

How do I apply a style to an empty input box? If the user types something in the input field, the style should no longer be applied. Is this possible in CSS? I tried this:

input[value=""]

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(15

两人的回忆 2024-09-23 19:43:31

虽然目前 (2021-10-01) 没有浏览器支持它,但有人建议使用 :blank 伪类。

参考: https://developer.mozilla.org/en-US /docs/Web/CSS/:空白
请注意,这是实验,目前没有浏览器支持它。

While no browser currently (2021-10-01) supports it, there is a proposal for a :blank pseudo-class.

ref: https://developer.mozilla.org/en-US/docs/Web/CSS/:blank.
Do note, this is experimental, and no browser supports it as of now.

江南月 2024-09-23 19:43:31

我对答案感到好奇,我们有明确的属性来获取空输入框,看看这段代码

/*empty input*/
input:empty{
    border-color: red;
}
/*input with value*/
input:not(:empty){
    border-color: black;
}

更新

input, select, textarea {
    border-color: @green;
    &:empty {
        border-color: @red;
    }
}

更多,以便在验证中获得很好的外观

 input, select, textarea {
    &[aria-invalid="true"] {
        border-color: amber !important;
    }

    &[aria-invalid="false"], &.valid {
        border-color: green !important;
    }
}

I'm wondered by answers we have clear attribute to get empty input boxes, take a look at this code

/*empty input*/
input:empty{
    border-color: red;
}
/*input with value*/
input:not(:empty){
    border-color: black;
}

UPDATE

input, select, textarea {
    border-color: @green;
    &:empty {
        border-color: @red;
    }
}

More over for having a great look in the validation

 input, select, textarea {
    &[aria-invalid="true"] {
        border-color: amber !important;
    }

    &[aria-invalid="false"], &.valid {
        border-color: green !important;
    }
}
赠我空喜 2024-09-23 19:43:30

在现代浏览器中,您可以使用 :placeholder-shown< /a> 定位空输入(不要与 < 混淆代码>::占位符)。

input:placeholder-shown {
    border: 1px solid red; /* Red border only if the input is empty */
}

在 HTML 中,您必须设置 placeholder 属性。 Chrome 浏览器至少需要一个空格字符作为值。

<input type="text" placeholder=" "> <!-- Do not remove placeholder -->

In modern browsers you can use :placeholder-shown to target the empty input (not to be confused with ::placeholder).

input:placeholder-shown {
    border: 1px solid red; /* Red border only if the input is empty */
}

In HTML you must set the placeholder attribute. Chrome browser requires at least a space character as value.

<input type="text" placeholder=" "> <!-- Do not remove placeholder -->
岁吢 2024-09-23 19:43:30

如果只有该字段是必填,您可以使用input:valid

#foo-thing:valid + .msg { visibility: visible!important; }      
 <input type="text" id="foo-thing" required="required">
 <span class="msg" style="visibility: hidden;">Yay not empty</span>

请参阅 jsFiddle 上的实时

或使用 #foo-thing:invalid 进行否定(信用致@SamGoody)

If only the field is required you could go with input:valid

#foo-thing:valid + .msg { visibility: visible!important; }      
 <input type="text" id="foo-thing" required="required">
 <span class="msg" style="visibility: hidden;">Yay not empty</span>

See live on jsFiddle

OR negate using #foo-thing:invalid (credit to @SamGoody)

断舍离 2024-09-23 19:43:30

input[value=""], input:not([value])

适用于:

<input type="text" />
<input type="text" value="" />

但是一旦有人开始输入,样式就不会改变(你需要 JS)。

input[value=""], input:not([value])

works with:

<input type="text" />
<input type="text" value="" />

But the style will not change as soon as someone will start typing (you need JS for that).

回忆那么伤 2024-09-23 19:43:30

更新字段的值不会更新其在 DOM 中的 value 属性,因此您的选择器始终匹配字段,即使它实际上不为空。

相反,使用 invalid 伪类 来实现你想要的,像这样:

input:required {
  border: 1px solid green;
}
input:required:invalid {
  border: 1px solid red;
}
<input required type="text" value="">

<input required type="text" value="Value">

Updating the value of a field does not update its value attribute in the DOM so that's why your selector is always matching a field, even when it's not actually empty.

Instead use the invalid pseudo-class to achieve what you want, like so:

input:required {
  border: 1px solid green;
}
input:required:invalid {
  border: 1px solid red;
}
<input required type="text" value="">

<input required type="text" value="Value">

酒与心事 2024-09-23 19:43:30

我意识到这是一个非常古老的线程,但从那以后事情发生了一些变化,它确实帮助我找到了解决问题所需的正确组合。所以我想我应该分享我所做的事情。

问题是,如果可选输入被填充,我需要将相同的 css 应用到可选输入,就像我对填充的必需输入一样。 css 使用了 psuedo 类:valid,它在未填充时也将 css 应用于可选输入。

这就是我修复它的方法;

HTML

<input type="text" required="required">
<input type="text" placeholder="">

CSS

input:required:valid {
    ....
}
input:optional::not(:placeholder-shown){
    ....
}

I realize this is a very old thread, but things have changed a bit since and it did help me find the right combination of things I needed to get my problem fixed. So I thought I'd share what I did.

The problem was I needed to have the same css applied for an optional input if it was filled, as I had for a filled required. The css used the psuedo class :valid which applied the css on the optional input also when not filled.

This is how I fixed it;

HTML

<input type="text" required="required">
<input type="text" placeholder="">

CSS

input:required:valid {
    ....
}
input:optional::not(:placeholder-shown){
    ....
}
幸福不弃 2024-09-23 19:43:30

如果不需要支持旧版浏览器,您可以使用 requiredvalidinvalid 的组合。

使用它的好处是 validinvalid 伪元素可以很好地与输入字段的类型属性配合使用。例如:

input:invalid, textarea:invalid { 
    box-shadow: 0 0 5px #d45252;
    border-color: #b03535
}

input:valid, textarea:valid {
    box-shadow: 0 0 5px #5cd053;
    border-color: #28921f;
}
<input type="email" name="email" placeholder="[email protected]" required />
<input type="url" name="website" placeholder="http://johndoe.com"/>
<input type="text" name="name" placeholder="John Doe" required/>

作为参考,JSFiddle 在这里: http://jsfiddle.net/0sf6m46j/

If supporting legacy browsers is not needed, you could use a combination of required, valid, and invalid.

The good thing about using this is the valid and invalid pseudo-elements work well with the type attributes of input fields. For example:

input:invalid, textarea:invalid { 
    box-shadow: 0 0 5px #d45252;
    border-color: #b03535
}

input:valid, textarea:valid {
    box-shadow: 0 0 5px #5cd053;
    border-color: #28921f;
}
<input type="email" name="email" placeholder="[email protected]" required />
<input type="url" name="website" placeholder="http://johndoe.com"/>
<input type="text" name="name" placeholder="John Doe" required/>

For reference, JSFiddle here: http://jsfiddle.net/0sf6m46j/

小帐篷 2024-09-23 19:43:30

这对我有用:

对于 HTML,将 required 属性添加到输入元素

<input class="my-input-element" type="text" placeholder="" required />

对于 CSS,使用 :invalid 选择器来定位空输入

input.my-input-element:invalid {

}

注释:

  • 关于 w3Schools.com 中的required
    “如果存在,它指定在提交表单之前必须填写输入字段。”

This worked for me:

For the HTML, add the required attribute to the input element

<input class="my-input-element" type="text" placeholder="" required />

For the CSS, use the :invalid selector to target the empty input

input.my-input-element:invalid {

}

Notes:

  • About required from w3Schools.com:
    "When present, it specifies that an input field must be filled out before submitting the form."
于我来说 2024-09-23 19:43:30
$('input#edit-keys-1').blur(function(){
    tmpval = $(this).val();
    if(tmpval == '') {
        $(this).addClass('empty');
        $(this).removeClass('not-empty');
    } else {
        $(this).addClass('not-empty');
        $(this).removeClass('empty');
    }
});

在 jQuery 中。我添加了一个类并使用 css 进行样式设置。

.empty { background:none; }
$('input#edit-keys-1').blur(function(){
    tmpval = $(this).val();
    if(tmpval == '') {
        $(this).addClass('empty');
        $(this).removeClass('not-empty');
    } else {
        $(this).addClass('not-empty');
        $(this).removeClass('empty');
    }
});

in jQuery. I added a class and styled with css.

.empty { background:none; }
戴着白色围巾的女孩 2024-09-23 19:43:30

将类名添加到输入中,然后对其应用 CSS 规则对我来说很有用:

<input type="text" name="product" class="product" />

<style>
input[value=""].product {
    display: none;
}
</style>

It worked for me to add a class name to the input and then apply CSS rules to that:

<input type="text" name="product" class="product" />

<style>
input[value=""].product {
    display: none;
}
</style>
停滞 2024-09-23 19:43:30

这个问题可能已经被问过一段时间了,但是当我最近登陆这个主题寻找客户端表单验证时,并且作为 :placeholder-shown 支持 越来越好,我认为以下内容可能对其他人有帮助。

使用 Berend 使用这个 CSS4 伪类的想法,我能够创建一个仅在用户完成后触发的表单验证填充它。

以下是 CodePen 上的演示和解释:
https://codepen.io/johanmouchet/pen/PKNxKQ

This question might have been asked some time ago, but as I recently landed on this topic looking for client-side form validation, and as the :placeholder-shown support is getting better, I thought the following might help others.

Using Berend idea of using this CSS4 pseudo-class, I was able to create a form validation only triggered after the user is finished filling it.

Here is ademo and explanation on CodePen:
https://codepen.io/johanmouchet/pen/PKNxKQ

夕色琉璃 2024-09-23 19:43:30

如果您很高兴不支持 IE 或 Chromium Edge 之前的版本(如果您使用它进行渐进增强,这可能没问题),您可以像 Berend 所说的那样使用 :placeholder-shown 。请注意,对于 Chrome 和 Safari,您实际上需要一个非空占位符才能正常工作,尽管空格也可以。

*,
 ::after,
 ::before {
  box-sizing: border-box;
}

label.floating-label {
  display: block;
  position: relative;
  height: 2.2em;
  margin-bottom: 1em;
}

label.floating-label input {
  font-size: 1em;
  height: 2.2em;
  padding-top: 0.7em;
  line-height: 1.5;
  color: #495057;
  background-color: #fff;
  background-clip: padding-box;
  border: 1px solid #ced4da;
  border-radius: 0.25rem;
  transition: border-color 0.15s ease-in-out, box-shadow 0.15s ease-in-out;
}

label.floating-label input:focus {
  color: #495057;
  background-color: #fff;
  border-color: #80bdff;
  outline: 0;
  box-shadow: 0 0 0 0.2rem rgba(0, 123, 255, 0.25);
}

label.floating-label input+span {
  position: absolute;
  top: 0em;
  left: 0;
  display: block;
  width: 100%;
  font-size: 0.66em;
  line-height: 1.5;
  color: #495057;
  border: 1px solid transparent;
  border-radius: 0.25rem;
  transition: font-size 0.1s ease-in-out, top 0.1s ease-in-out;
}

label.floating-label input:placeholder-shown {
  padding-top: 0;
  font-size: 1em;
}

label.floating-label input:placeholder-shown+span {
  top: 0.3em;
  font-size: 1em;
}
<fieldset>
  <legend>
    Floating labels example (no-JS)
  </legend>
  <label class="floating-label">
    <input type="text" placeholder=" ">
    <span>Username</span>
  </label>
  <label class="floating-label">
    <input type="Password" placeholder=" ">
    <span>Password</span>
  </label>
</fieldset>
<p>
  Inspired by Bootstrap's <a href="https://getbootstrap.com/docs/4.0/examples/floating-labels/">floating labels</a>.
</p>

If you're happy not not supporting IE or pre-Chromium Edge (which might be fine if you are using this for progressive enhancement), you can use :placeholder-shown as Berend has said. Note that for Chrome and Safari you actually need a non-empty placeholder for this to work, though a space works.

*,
 ::after,
 ::before {
  box-sizing: border-box;
}

label.floating-label {
  display: block;
  position: relative;
  height: 2.2em;
  margin-bottom: 1em;
}

label.floating-label input {
  font-size: 1em;
  height: 2.2em;
  padding-top: 0.7em;
  line-height: 1.5;
  color: #495057;
  background-color: #fff;
  background-clip: padding-box;
  border: 1px solid #ced4da;
  border-radius: 0.25rem;
  transition: border-color 0.15s ease-in-out, box-shadow 0.15s ease-in-out;
}

label.floating-label input:focus {
  color: #495057;
  background-color: #fff;
  border-color: #80bdff;
  outline: 0;
  box-shadow: 0 0 0 0.2rem rgba(0, 123, 255, 0.25);
}

label.floating-label input+span {
  position: absolute;
  top: 0em;
  left: 0;
  display: block;
  width: 100%;
  font-size: 0.66em;
  line-height: 1.5;
  color: #495057;
  border: 1px solid transparent;
  border-radius: 0.25rem;
  transition: font-size 0.1s ease-in-out, top 0.1s ease-in-out;
}

label.floating-label input:placeholder-shown {
  padding-top: 0;
  font-size: 1em;
}

label.floating-label input:placeholder-shown+span {
  top: 0.3em;
  font-size: 1em;
}
<fieldset>
  <legend>
    Floating labels example (no-JS)
  </legend>
  <label class="floating-label">
    <input type="text" placeholder=" ">
    <span>Username</span>
  </label>
  <label class="floating-label">
    <input type="Password" placeholder=" ">
    <span>Password</span>
  </label>
</fieldset>
<p>
  Inspired by Bootstrap's <a href="https://getbootstrap.com/docs/4.0/examples/floating-labels/">floating labels</a>.
</p>

岁月静好 2024-09-23 19:43:30

因此,我之前正在尝试使用新的 :where 和 :is 子句并构想出这一点乐趣,在找到带有 :invalid 和 :placeholder-shown 位的这篇文章后,我想我可能会分享这种可能性以供将来

:required:where( input, textarea ):where( :placeholder-shown, :invalid ) {
    border-color: var(--warning);
}

参考:root { --警告: 橙色; } 任何必需的输入或文本区域的颜色,该输入或文本区域为空或无效。这就是彻头彻尾的性感

So I was playing around earlier with the new :where and :is clauses and conceived of this bit of fun, and after finding this post with the :invalid and :placeholder-shown bits, thought I might share this possibility for future reference

:required:where( input, textarea ):where( :placeholder-shown, :invalid ) {
    border-color: var(--warning);
}

which applies the :root { --warning: orange; } color to any required input or textarea, that is either empty or invalid. And that is just downright sexy

剧终人散尽 2024-09-23 19:43:30

这就是您如何使其成为可能的方法。不要忘记为您的输入设置 placeholder=" "required attr。您可以根据需要更改道具。

body{
    display: flex;
    justify-content: center;
    align-items: center;
}
.input-gp {
    margin-top: 150px;
    position: relative;
    
}
.input-gp input {
    position: relative;
    
}
.input-gp label{
    position: absolute;
    left: 5px;
    bottom: 5px;
    transition: all .4s ease;
}
.input-gp input:placeholder-shown + label{
    left: 10px;
    bottom: 5px;
}
.input-gp input:focus + label,
.input-gp input + label{
    bottom: 30px;
    left: 10px;
}
<body>
 <div class="input-gp ">
<input  type="email" name="" id="email" placeholder=" "       required>
   <label class=" position-absolute" for="email"> Email</label>
  </div>
  
 </body>

This is how you can make it possible. don't forget to set placeholder=" " and required attr to your inputs. you can change the props as you wish.

body{
    display: flex;
    justify-content: center;
    align-items: center;
}
.input-gp {
    margin-top: 150px;
    position: relative;
    
}
.input-gp input {
    position: relative;
    
}
.input-gp label{
    position: absolute;
    left: 5px;
    bottom: 5px;
    transition: all .4s ease;
}
.input-gp input:placeholder-shown + label{
    left: 10px;
    bottom: 5px;
}
.input-gp input:focus + label,
.input-gp input + label{
    bottom: 30px;
    left: 10px;
}
<body>
 <div class="input-gp ">
<input  type="email" name="" id="email" placeholder=" "       required>
   <label class=" position-absolute" for="email"> Email</label>
  </div>
  
 </body>

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文