1

I want to use CSS only to create a floating label on an input but have it as a modifier class so that it doesn’t affect the original input, and also showing and hiding the placeholder text.

So, I need to keep the same HTML structure.

I have found examples, but they all seem to say that the input has to be before the label?

Using my markup is it possible to create this using css only?

<div class="test-field test-field--light">
  <label for="a" class="test-field__label">Input label</label>
  <input class="test-field__input" id="a" placeholder="Placeholder text" type="text" value="">
</div>

Here’s my pen: https://codepen.io/anon/pen/XwVrwY

Here is an example of what I need: https://codepen.io/peiche/pen/xOVpPo

Thanks

John
  • 1,737
  • 8
  • 37
  • 61
  • I will close the question as duplicate of parent and previous selector because all the trick to achieve what you want belong there and you issue is basically a selector one and here is the most suitable solution for you with the duplicates: https://stackoverflow.com/a/46406959/8620333 – Temani Afif May 22 '19 at 09:22
  • Do you need both the `label` and the `placeholder` ? Can we remove the placeholder ? – Takit Isy May 22 '19 at 09:33

1 Answers1

1

You should change at least one line of your HTML because there is no "previous sibling" selector. So we can't select the test-field__label on test-field__input event.

Please check the code:

body {
  padding: 15px 35px;
  background: #f5f5f5;
}

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

.test-field--light .test-field__input {
    background-color: #ffffff;
    border-color: #eee;
}

.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--light">
  <input class="test-field__input" id="a" type="text" value="">
  <label for="a" class="test-field__label">Input label</label>
</div>

<div class="test-field test-field--floating">
  <input class="test-field__input" id="b"  type="text" value="" required>
  <label for="a" class="test-field__label">Input label</label>
</div>
Ali
  • 1,180
  • 1
  • 16
  • 34