2

I am looking to find an example of how to make a label / placeholder transition move up and out of placeholder position into a label position and vice versa..

Example: https://www.xero.com/us/signup/

Tylaw38
  • 65
  • 1
  • 2
  • 10

2 Answers2

5

I found a good Codepen showing an example of how to do it in CSS.

HTML:

<div class="row">
    <input id="name" type="text" name="name">
    <label for="name">Full Name</label>
</div>

CSS:

.row {
  position: relative;
  margin-top: 35px;
}

input {
    display: block;
    padding: 8px 12px;
    width: 100%;
    max-width: 300px;
    border-radius: 5px;
    border: 0;
}

label {
    position: absolute;
    font-weight: 600;
    color: #777777;
    top: 50%;
    left: 12px;
    transform: translateY(-50%);
    -ms-transform: translateY(-50%);
    -webkit-transform: translateY(-50%);
    cursor: text;
    user-select: none;
    transition: 0.15s ease-in-out;
}

input[data-empty="false"] + label,
input:valid + label,
input:focus + label {
    top: -10px;
    left: 0px;
    font-size: 10px;
    color: #ffffff;
}

Example: https://codepen.io/sivan/pen/alKwf

jnstn
  • 191
  • 1
  • 2
  • 10
4

General sibling selectors & :focus does the trick in a very simple way ;)

input{
 position:absolute;
 top:20px;
}
input ~ span{
 transition:top .7s ease;
 position:absolute;
 top:20px;
}
input:focus ~ span{
 top:0px;
}
<label>
<input>
<span>Text</span>
</label>

here is an example with multiple fields

https://jsfiddle.net/shLe3107/1/

hope this is enough else just ask

cocco
  • 16,442
  • 7
  • 62
  • 77
  • 1
    What about when you add text and then the label moves back down? How do you keep it above after entering text? – Tylaw38 Apr 28 '16 at 23:46
  • you need to add javascript to check if it's empty ... there is no simple solution. http://stackoverflow.com/questions/3617020/matching-an-empty-input-box-using-css http://stackoverflow.com/questions/16952526/detect-if-an-input-has-text-in-it-using-css – cocco Apr 28 '16 at 23:48
  • there is nothing such input:empty or input[value=""]....btw in chrome input[value=""] should work... but the others? – cocco Apr 28 '16 at 23:49
  • 1
    basically with js check if there is text set the span fixed to top:0px; – cocco Apr 28 '16 at 23:54