:not(:empty) CSS selector is not working?

后端 未结 11 1035
我寻月下人不归
我寻月下人不归 2020-11-28 06:22

I\'m having a heck of a time with this particular CSS selector which does not want to work when I add :not(:empty) to it. It seems to work fine with any combina

相关标签:
11条回答
  • 2020-11-28 06:25

    .floating-label-input {
      position: relative;
      height:60px;
    }
    .floating-label-input input {
      width: 100%;
      height: 100%;
      position: relative;
      background: transparent;
      border: 0 none;
      outline: none;
      vertical-align: middle;
      font-size: 20px;
      font-weight: bold;
      padding-top: 10px;
    }
    .floating-label-input label {
      position: absolute;
      top: calc(50% - 5px);
      font-size: 22px;
      left: 0;
      color: #000;
      transition: all 0.3s;
    }
    .floating-label-input input:focus ~ label, .floating-label-input input:focus ~ label, .floating-label-input input:valid ~ label {
      top: 0;
      font-size: 15px;
      color: #33bb55;
    }
    .floating-label-input .line {
      position: absolute;
      height: 1px;
      width: 100%;
      bottom: 0;
      background: #000;
      left: 0;
    }
    .floating-label-input .line:after {
      content: "";
      display: block;
      width: 0;
      background: #33bb55;
      height: 1px;
      transition: all 0.5s;
    }
    .floating-label-input input:focus ~ .line:after, .floating-label-input input:focus ~ .line:after, .floating-label-input input:valid ~ .line:after {
      width: 100%;
    }
    <div class="floating-label-input">
          <input type="text" id="id" required/>
          <label for="id" >User ID</label>
          <span class="line"></span>
    </div>

    0 讨论(0)
  • 2020-11-28 06:26
    input:not([value=""])
    

    This works because we are selecting the input only when there isn't an empty string.

    0 讨论(0)
  • 2020-11-28 06:30
    input:not(:invalid){
     border: 1px red solid;
    }
    
    // or 
    
    input:not(:focus):not(:invalid){
     border: 1px red solid;
    }
    
    0 讨论(0)
  • 2020-11-28 06:32

    Being a void element, an <input> element is considered empty by the HTML definition of "empty", since the content model of all void elements is always empty. So they will always match the :empty pseudo-class, whether or not they have a value. This is also why their value is represented by an attribute in the start tag, rather than text content within start and end tags.

    Also, from the Selectors spec:

    The :empty pseudo-class represents an element that has no children at all. In terms of the document tree, only element nodes and content nodes (such as DOM text nodes, CDATA nodes, and entity references) whose data has a non-zero length must be considered as affecting emptiness;

    Consequently, input:not(:empty) will never match anything in a proper HTML document. (It would still work in a hypothetical XML document that defines an <input> element that can accept text or child elements.)

    I don't think you can style empty <input> fields dynamically using just CSS (i.e. rules that apply whenever a field is empty, and don't once text is entered). You can select initially empty fields if they have an empty value attribute (input[value=""]) or lack the attribute altogether (input:not([value])), but that's about it.

    0 讨论(0)
  • 2020-11-28 06:32

    You could try using :placeholder-shown...

    input {
      padding: 10px 15px;
      font-size: 16px;
      border-radius: 5px;
      border: 2px solid lightblue;
      outline: 0;
      font-weight:bold;
      transition: border-color 200ms;
      font-family: sans-serif;
    }
    
    .validation {
      opacity: 0;
      font-size: 12px;
      font-family: sans-serif;
      color: crimson;
      transition: opacity;
    }
    
    input:required:valid {
      border-color: forestgreen;
    }
    
    input:required:invalid:not(:placeholder-shown) {
      border-color: crimson;
    }
    
    input:required:invalid:not(:placeholder-shown) + .validation {
      opacity: 1;
    }
    
      
    <input type="email" placeholder="e-mail" required>
    <div class="validation">Not valid</span>

    no great support though... caniuse

    0 讨论(0)
  • 2020-11-28 06:32

    You may approach this differently; omit the use of the :empty pseudo-class and utilize input events to detect a significant value in the <input> field and style it accordingly:

    var inputs = document.getElementsByTagName('input');
    
    for (var i = 0; i < inputs.length; i++) {
      var input = inputs[i];
      input.addEventListener('input', function() {
        var bg = this.value ? 'green' : 'red';
        this.style.backgroundColor = bg;
      });
    }
    body {
      padding: 40px;
    }
    #inputList li {
      list-style-type: none;
      padding-bottom: 1.5em;
    }
    #inputList li input,
    #inputList li label {
      float: left;
      width: 10em;
    }
    #inputList li input {
      color: white;
      background-color: red;
    }
    #inputList li label {
      text-align: right;
      padding-right: 1em;
    }
    <ul id="inputList">
      <li>
        <label for="username">Enter User Name:</label>
        <input type="text" id="username" />
      </li>
      <li>
        <label for="password">Enter Password:</label>
        <input type="password" id="password" />
      </li>
    </ul>

    Related

    • Another post on DOM Mutation Events, suggesting the use of input Events (DOM Mutation Events are now deprecated in DOM level 4, and have been replaced by DOM Mutation Observers).

    Disclaimer: note that input events are currently experimental, and probably not widely supported.

    0 讨论(0)
提交回复
热议问题