4

The label effect will work if required is there in input tag. But the effect will not work if i remove the required attribute Click to see working fiddle Is there any solution for this.

html

<div class="group ">
<input type="text" required="" class="module-input" />
<label >Name</label>
</div>

Snippet:

.module-input {
  font-size: 14px;
  padding-top: 12px;
  display: block;
  width: 97%;
  border: none;
  border-bottom: 1px solid #94a3a9;
  background-color: transparent;
  color: #5a686d;
  margin-bottom: 2%;
}

input:focus {
  outline: none;
}

.group {
  position: relative;
  margin-bottom: 25px;
}


/* LABEL */

label {
  color: #94a3a9;
  font-size: 14px;
  font-weight: normal;
  position: absolute;
  pointer-events: none;
  left: 0.5%;
  top: 10px;
  transition: 0.2s ease all;
  -moz-transition: 0.2s ease all;
  -webkit-transition: 0.2s ease all;
}


/* active state */

input:focus+label,
input:valid+label {
  top: -8px;
  font-size: 12px;
  color: #94a3a9;
}
<div class="group ">
  <input type="text" class="module-input" />
  <label>Name</label>
</div>
Jones Joseph
  • 4,703
  • 3
  • 22
  • 40
rahul patel
  • 262
  • 1
  • 2
  • 15

4 Answers4

6

If you update the rule with the attribute selector, input[required]:valid ~ label, this it will work, both with and without the required attribute

What happens is when an input does not have the reqiured attribute it is considered valid, hence the rule kick in immediately.

Updated

To be able to handle this properly, since :empty won't work on input elements, I added a small script and an attribute selector, which being set on a change and then the CSS rule check if it's empty

window.addEventListener('load', function() {
  var inp = document.querySelectorAll('input');
  for (var i = 0; i < inp.length; i++) {
    inp[i].addEventListener('change', function() {
      this.setAttribute("data-value", this.value);
    })
  }
})
.module-input {
  font-size: 14px;
  padding-top: 12px;
  display: block;
  width: 97%;
  border: none;
  border-bottom: 1px solid #94a3a9;
  background-color: transparent;
  color: #5a686d;
  margin-bottom: 2%;
}

input:focus {
  outline: none;
}

.group {
  position: relative;
  margin-bottom: 25px;
}


/* LABEL */

label {
  color: #94a3a9;
  font-size: 14px;
  font-weight: normal;
  position: absolute;
  pointer-events: none;
  left: 0.5%;
  top: 10px;
  transition: 0.2s ease all;
  -moz-transition: 0.2s ease all;
  -webkit-transition: 0.2s ease all;
}


/* active state */

input[data-value]:not([data-value=""]) ~ label,
input:focus ~ label {
  top: -8px;
  font-size: 12px;
  color: #94a3a9;
}
<div class="group ">
  <input type="text" class="module-input" required="" />
  <label>Name</label>
</div>
<div class="group ">
  <input type="text" class="module-input" />
  <label>Address</label>
</div>
Asons
  • 84,923
  • 12
  • 110
  • 165
5

You have the CSS as input:valid ~ label Which means: put the label on top if the input is valid.

When you remove the required attribute, the input becomes valid by default. So without the required attribute, the label effect wont work by default.

You should use something like input[required]:valid ~ label

Jones Joseph
  • 4,703
  • 3
  • 22
  • 40
  • 1
    A better way would be to use CSS3 selectors. Define your styles on `input:required:valid + label, input:focus + label {...`. i.e. use the `:required` selector along with `:valid`. This allows you to fluidly chain the style on different types of input constraints. e.g. `input:in-range` or `input:out-of-range` or `input:optional` etc. – Abhitalks Apr 07 '17 at 07:42
  • check this https://jsfiddle.net/Rahul_Patel_S_G/sy83j3q2/24/ if i type a value and if i focus it to another text box the css will not work. – rahul patel Apr 07 '17 at 11:49
  • @rahulpatel: That is not possible with CSS. You will JS for that. Reason = `:empty` selector does not work with `input`s. – Abhitalks Apr 07 '17 at 11:51
  • ya i debugged the focusout problem through JS, but when i assign a value to a input from angular the label effect will not work. any solution for that?? – rahul patel Apr 07 '17 at 12:07
  • @rahulpatel would you post your final code so people like me happy cause i got bugs with someone solution in bellow – Freddy Sidauruk Dec 05 '18 at 13:23
1

When you don't place required, the input:valid ~ label properties in css are used because your input is not required, so it is valid

Theo
  • 157
  • 12
1

Just remove tilde ~ sign from valid

input:focus~label,
input:valid label {
top: -8px;
font-size: 12px;
color: #94a3a9;
}

after this doesn't matter using Required or not.

Shakil Ahmad
  • 143
  • 1
  • 2
  • 13