0

I'm working on an existing form, where I want to introduce floating labels.

A lot of the solutions I have seen on stack overflow are great, but require the label tag, to sit directly underneath the <input> tag. For example here.

On the form I'm working on though, my label tag, sits above the <input> tag. And the input tag, sits in a seperate div. (I am not allowed to move these tags around - please don't ask, restriction of the application!).

Is there anyway, I can achieve floating CSS labels, with this structure?

.test-field {
clear: both;
margin-bottom: 20px;
position: relative;
}


.test-field__input {
font-size: 0.875em;
line-height: 1;
-moz-appearance: none;
-webkit-appearance: none;
background-color: #f5f5f5;
border: 2px solid #eee;
border-radius: 4px;
color: grey;
height: calc((40 / 14) * 1em);
line-height: 1;
outline: 0;
padding: 0 8px;
vertical-align: top;
width: 100%;
}

/* // FOCUS */
.test-field__input:focus, 
.test-field__input ~ input:checked:focus {
border-color: #5d7199;
}

/* // DISABLED */
.test-field--disabled .test-field__input {
background-color: #e8e8e3;
cursor: not-allowed;
}

.test-field--disabled .test-field__flag, .test-field--disabled .test-field__label {
color: #d0d0cb;
}

/* // ERROR */
.test-field--error .test-field__input, .test-field--error .test-field__selection .atc-field__input {
border-color: #ff4436;
color: #ff4436;
}

/* // FLOATING LABEL */

.test-field--floating .test-field-input {
  position: relative;
  margin-bottom: 1.5rem;
}
.test-field__label {
  position: absolute;
  top: 0;
  padding: 7px 0 0 13px;
  transition: all 200ms;
  opacity: 0.5;
}

.test-field__input:focus + .test-field__label,
.test-field--floating .test-field__input:valid + .test-field__label {
  font-size: 75%;
  transform: translate3d(0, -100%, 0);
  opacity: 1;
}
<div class="test-field test-field--floating">
<label for="a" class="test-field__label">Input label</label>
  <input class="test-field__input" id="b"  type="text" value="" required>
</div>
Reena Verma
  • 1,617
  • 2
  • 20
  • 47
  • 1
    Does this answer your question? [Is there a CSS parent selector?](https://stackoverflow.com/questions/1014861/is-there-a-css-parent-selector) – CBroe Jun 04 '21 at 07:42
  • 1
    You can not do it with CSS with this structure. The technique is based on that you format the label depending of different pseudo classes applying to the input field - but CSS can only select to the right and/or downwards in the DOM, not upwards or to the left. – CBroe Jun 04 '21 at 07:44
  • Thank you, I think I have found a different way to make this work – Reena Verma Jun 04 '21 at 09:57

1 Answers1

1

This seemed to be a good way to replicate floating labels, with a similar DOM structure:

const floatField = document.querySelectorAll('.ej-form-field-input-textbox');

const floatContainer = document.querySelectorAll('.ej-form-field');


for (let i = 0; i < floatField.length; i++) {
    floatField[i].addEventListener('focus', () => {
     
    floatContainer[i].classList.add('active');
  });
};


for (let i = 0; i < floatField.length; i++) {
    floatField[i].addEventListener('blur', () => {
    floatContainer[i].classList.remove('active');
  });
};
.ej-form-field {
  border: solid 1px #ccc;
  padding: 0 8px;
  position: relative;
}
.ej-form-field input {
  border: 1px solid red;
  font-size: 16px;
  outline: 0;
  height: 30px;
/*   padding: 16px 0 10px; */
}

label {
  font-size: 16px;
  position: absolute;
  transform-origin: top left;
  transform: translate(0, 16px) scale(1);
  transition: all .1s ease-in-out;
  height: 40px;
}

.ej-form-field.active label {
  transform: translate(0, -6px) scale(.95);
  color: red;
  margin-left: 10px;
  background-color: #fff;
  border: 1px solid green;
  height: 20px;
}
<div id="floatContainer" class="ej-form-field">
  <label for="floatField">First name</label>
  <div>
    <input id="floatField" class="ej-form-field-input-textbox" type="text">
  </div>
</div>


<div id="floatContainer" class="ej-form-field">
  <label for="floatField">Last name</label>
  <div>
    <input id="floatField" class="ej-form-field-input-textbox" type="text">
  </div>
</div>
Reena Verma
  • 1,617
  • 2
  • 20
  • 47