label to stay on top after input field is filled - html

I want to keep my label to stay on top of the input field after we fill the input data in html
I have tried the valid function in css but couldn't achieve the functionality.
.txt_field input {
width: 100%;
padding: 0 5px;
height: 40px;
font-size: 16px;
border: none;
background: none;
outline: none;
}
.txt_field label {
position: absolute;
top: 65%;
left: 5px;
color: #adadad;
transform: translateY(-50%);
font-size: 16px;
pointer-events: none;
transition: .3s;
}
.txt_field input:focus~label {
top: 0px;
color: #0170C1;
}
<div class="txt_field">
<i class="fa fa-user"></i>
<input type="text" class="textbox" id="username" placeholder="">
<label>PhoneNumber/Email</label>
</div>
I have tried .txt_field input:focus ~valid but the label keeps overlapping with the input data.
What can I do to make the label stay on top after the input field is filled.

Use the :placeholder-shown pseudo-class to detect does input is filled and use ::placeholder pseudo-elements selectors to hide unwanted placeholder.
Also you need position:relative; for the .txt_field for better view.
And it's better to use + except ~.
Important: Input tag must have a non-empty placeholder attribute.
.txt_field{
position:relative;
}
.txt_field input {
width: 100%;
padding: 0 5px;
height: 40px;
font-size: 16px;
border: none;
background: none;
outline: none;
}
.txt_field label {
position: absolute;
top: 65%;
left: 5px;
color: #adadad;
transform: translateY(-50%);
font-size: 16px;
pointer-events: none;
transition: .3s;
}
.txt_field input:focus+label, .txt_field input:not(:placeholder-shown)+label {
top: 0px;
color: #0170C1;
}
.txt_field input::placeholder {
color: transparent;
}
<div class="txt_field">
<i class="fa fa-user"></i>
<input type="text" class="textbox" id="username" placeholder="if empty will not work">
<label>PhoneNumber/Email</label>
</div>

.txt_field input {
width: 100%;
padding: 0 5px;
height: 40px;
font-size: 16px;
border: none;
background: none;
outline: none;
background-color: rgba(0,0,0,.1);
}
.txt_field label {
position: absolute;
top: 65%;
left: 5px;
color: #adadad;
transform: translateY(-50%);
font-size: 16px;
pointer-events: none;
transition: .3s;
}
.txt_field input:focus~label,
.txt_field input:not(:placeholder-shown)~label{
top: 0px;
color: #0170C1;
}
<div class="txt_field">
<i class="fa fa-user"></i>
<input type="text" class="textbox" id="username" placeholder="">
<label>PhoneNumber/Email</label>
</div>

Two important points:
1- you must learn about HTML structure and understand its importance.
2- you must learn about display and positioning element. learning this will help you to put elements properly where you want them.
don't use position: absolute; for this case. and why is your input 100% width? that's not proper.
.txt_field input {
display: flex;
flex-direction: column;
position: relative;
width: 300px;
padding: 0 5px;
height: 40px;
font-size: 16px;
border: none;
background: none;
outline: none;
border: 1px solid red;
}
.txt_field label {
/* position: absolute; */
color: #adadad;
transform: translateY(-50%);
font-size: 16px;
pointer-events: none;
transition: .3s;
border: 1px solid orange;
}
.txt_field input:focus~label {
top: 0px;
color: #0170C1;
}
<div class="txt_field">
<i class="fa fa-user"></i>
<label>PhoneNumber/Email</label>
<input type="text" class="textbox" id="username" placeholder="">
</div>

You can do it with the translate effect like this:
.input:focus ~ .placeholder,
.input:not(:placeholder-shown) ~ .placeholder {
transform: translateY(-30px) translateX(10px) scale(0.75);
}
and of course you better give it a transition to make it cool
// transition: transform 200ms

Related

CSS only animated login form

I have this animated login form in which when you focus on input or fill up the input the label animates to the top. It works well for the most part when the type of input is text, but when the type of input is email it crashes:
form .register-txt_field{
position: relative;
border-bottom: 2px solid var(--color-light);
margin: 30px 0;
}
.register-txt_field input{
width: 100%;
padding: 0 5px;
height: 40px;
font-size: 16px;
border: none;
background: none;
outline: none;
color: var(--color-light);
}
.register-txt_field label{
position: absolute;
top: 50%;
left: 5px;
color: var(--color-light);
transform: translateY(-50%);
font-size: 16px;
pointer-events: none;
transition: .5s;
}
.register-txt_field span::before{
content: '';
position: absolute;
top: 40px;
left: 0;
width: 0%;
height: 2px;
background: var(--violet);
transition: .5s;
}
.register-txt_field input:focus ~ label,
.register-txt_field input:valid ~ label{
top: -5px;
color: var(--violet);
}
.register-txt_field input:focus ~ span::before,
.register-txt_field input:valid ~ span::before{
width: 100%;
}
input[type="submit"]{
width: 100%;
height: 50px;
background: var(--violet);
border-radius: 10px;
font-size: 18px;
color: var(--color-light);
font-weight: 700;
cursor: pointer;
outline: none;
border: none;
}
input[type="submit"]:hover{
border-color: var(--violet);
transition: .5s;
background-color: inherit;
outline: none;
}
.register-login_link{
margin: 30px 0;
text-align: center;
font-size: 16px;
color: var(--color-light);
}
.register-login_link a{
color: var(--violet);
text-decoration: none;
}
.register-login_link a:hover{
text-decoration: underline;
}
<form method="post">
<div className="register-txt_field">
<input type="text" required />
<span></span>
<label>Name</label>
</div>
<div className="register-txt_field">
<input type="text" required />
<span></span>
<label>Email</label>
</div>
<div class="register-txt_field">
<input type="password" required />
<span></span>
<label>Password</label>
</div>
<Link to={"/login"}>
<input type="submit" value="Sign Up" />
</Link>
<div class="register-login_link">
Already have an account?{" "}
<Link to={"/login"}>
Login
</Link>
</div>
</form>
The animation is happening with this part right here:
.register-txt_field input:focus ~ label,
.register-txt_field input:valid ~ label{
top: -5px;
color: var(--violet);
}
Whenever the input is focused and the content is valid which is text in this case it works but when the type is email it doesn't until the user puts the correct format of email with #gmail and all. If the user puts something random when the type is email it breaks and the label comes down and merges with the input.
My solution to this problem is to combine the :placeholder-shown and :not() pseudo classes as shown in the snippet below.
The problem is that :valid relies upon the user input being valid rather than simply being present. For the name and password fields that doesn't matter, whereas the email field is more of an issue here.
This would seem like an easy fix with a pseudo class like :blank or :user-invalid however these have surprisingly terrible browser support.
Most browsers support :placeholder-shown and :not() though, so we can define a placeholder with whitespace on each input and style inputs that aren't showing their placeholder (have user input):
.register-txt_field input:not(:placeholder-shown)~label {}
form .register-txt_field {
position: relative;
border-bottom: 2px solid var(--color-light);
margin: 30px 0;
}
.register-txt_field input {
width: 100%;
padding: 0 5px;
height: 40px;
font-size: 16px;
border: none;
background: none;
outline: none;
color: var(--color-light);
}
.register-txt_field label {
position: absolute;
top: 50%;
left: 5px;
color: var(--color-light);
transform: translateY(-50%);
font-size: 16px;
pointer-events: none;
transition: .5s;
}
.register-txt_field span::before {
content: '';
position: absolute;
top: 40px;
left: 0;
width: 0%;
height: 2px;
background: var(--violet);
transition: .5s;
}
.register-txt_field input:focus~label,
.register-txt_field input:not(:placeholder-shown)~label {
top: -5px;
color: var(--violet);
}
.register-txt_field input:focus~span::before,
.register-txt_field input:not(:placeholder-shown)~span::before {
width: 100%;
}
<form method="post">
<div class="register-txt_field">
<input type="text" placeholder=" " required />
<span></span>
<label>Name</label>
</div>
<div class="register-txt_field">
<input type="email" placeholder=" " required />
<span></span>
<label>Email</label>
</div>
<div class="register-txt_field">
<input type="password" placeholder=" " required />
<span></span>
<label>Password</label>
</div>
</form>

label is not floating to the top

I want to have the textfield just like gmail has right now. I mean when we focus on the text field, the label or placeholder goes to the top exactly at the top border line of its text field with no border line on the background of the label only. I tried but could not make it happen and i found the code in internet which did not work either. Here is what the code is
.FieldWrapper input[type="text"] {
box-sizing: border-box;
width: 100%;
height: calc(3em + 2px);
margin: 0 0 1em;
padding: 1em;
border: 1px solid #ccc;
background: #fff;
resize: none;
outline: none;
}
.FieldWrapper input[type="text"]:focus {
border-color: #00bafa;
}
.FieldWrapper input[type="text"]:focus+label[placeholder]:before {
color: #00bafa;
}
.FieldWrapper input[type="text"]:focus+label[placeholder]:before,
.FieldWrapper input[type="text"]:valid+label[placeholder]:before {
transition-duration: 0.2s;
-webkit-transform: translate(0, -1.5em) scale(0.9, 0.9);
transform: translate(0, -1.5em) scale(0.9, 0.9);
}
.FieldWrapper input[type="text"]:invalid+label[placeholder][alt]:before {
content: attr(alt);
}
.FieldWrapper input[type="text"]+label[placeholder] {
display: block;
pointer-events: none;
line-height: 1.25em;
margin-top: calc(-1em - 2px);
margin-bottom: calc((3em - 1em) + 2px);
}
.FieldWrapper input[type="text"]+label[placeholder]:before {
content: attr(placeholder);
display: inline-block;
margin: 0 calc(1em + 2px);
padding: 0 2px;
color: #898989;
white-space: nowrap;
transition: 0.3s ease-in-out;
background-image: linear-gradient(to bottom, #fff, #fff);
background-size: 100% 5px;
background-repeat: no-repeat;
background-position: center;
}
<div class="FieldWrapper">
<input type='text' name="name" />
<label alt="field name" placeholder="field name" />
</div>
This is what exactly i was expecting
Try this:
.input-container {
position: relative;
font-family: Arial;
}
.input-container input {
height: 32px;
line-height: 24px;
border-radius: 8px;
border: 1px solid #dcdcdc;
min-width: 300px;
padding: 4px 8px;
transition: all .2s ease;
}
.input-container label {
position: absolute;
top: 12px;
left: 4px;
background-color: #fff;
padding: 0 4px;
font-size: 14px;
pointer-events: none;
transition: all .2s ease;
}
.input-container input:focus {
outline: none;
border: 1px solid #3f51b5;
}
.input-container input:valid + label,
.input-container input:focus + label {
top: -8px;
color: #3f51b5;
}
<div class="input-container">
<input id="my-input" type="text" required>
<label for="my-input">My input label</label>
</div>

How to design an Html input with floating label using only CSS and HTML?

How to design an input text like in material design with a label that floats up and it doesn't overlap the content when the input is not focused, how can i implement that without using Materialise.CSS or any other library
I prefer this technique for its markup simplicity, accessibility, and keyboard-friendliness.
It uses relative and absolute positioning (an absolute label inside a relative container) and never "replaces" the label with another element (such as with the placeholder pseudo-element) because replacement techniques cause flickering in my experience. It also manages the pointer events so that the label never gets in the way of clicking to select the input (but once it's out of the way, its text is selectable).
I've used ems, since we're dealing with animated text. This should allow the technique to scale to different font sizes without modifying the offsets used for padding and margins.
This uses the required attribute and the :valid selector to check whether an input was left blank (in which case, the label drops back down). If this doesn't work for your use case, it can be worked around by adding and removing an empty class to the input using JavaScript (that's what the last CSS ruleset is for). Another option would be to use :placeholder-shown with a dummy placeholder, if you're okay with not supporting Microsoft Edge. Remember, CSS's :empty selector doesn't work the way you'd think it might on HTML inputs.
#import url('https://fonts.googleapis.com/css?family=Roboto');
body
{
/* just to make it feel a little more at home */
font-family: 'Roboto', sans-serif;
}
.floating-label
{
position: relative;
margin-top: 1.75em;
}
.floating-label input[type="text"]
{
border: none;
border-bottom: 1px solid gray;
margin-bottom: 1px;
}
.floating-label input[type="text"]:hover
{
border-bottom: 2px solid black;
margin-bottom: 0;
}
.floating-label input[type="text"]:focus,
.floating-label input[type="text"]:active
{
outline: none;
border-bottom: 2px solid #2196f3;
margin-bottom: 0;
}
.floating-label input[type="text"] + label
{
position: absolute;
pointer-events: none;
bottom: 0.1em;
left: 0.1em;
color: gray;
font-size: 1.0em;
transition: margin 0.2s ease,
color 0.2s ease,
font-size 0.2s ease;
}
.floating-label input[type="text"]:focus + label,
.floating-label input[type="text"]:active + label,
.floating-label input[type="text"]:valid + label
{
pointer-events: auto;
margin-bottom: 1.75em;
font-size: 0.7em;
}
.floating-label input[type="text"]:focus + label,
.floating-label input[type="text"]:active + label
{
color: #2196f3;
}
.floating-label input[type="text"].empty:not(:focus) + label,
.floating-label input[type="text"].empty:not(:active) + label
{
pointer-events: none;
margin-bottom: 0;
color: gray;
font-size: 1.0em;
}
<div class="floating-label">
<input id="first" type="text" required>
<label for="first">First Name</label>
</div>
<div class="floating-label">
<input id="last" type="text" required>
<label for="last">Last Name</label>
</div>
This is how I've done it previously.
The CSS has be processed from SCSS which is why it's so specific.
.container {
width: 15%;
}
.container .form-group {
margin: 0 0 16px;
}
.container .form-group.field--invalid > .field__input-container .field__decoration:before {
border-color: #e4134f;
width: 100%;
}
.container .form-group .field__input-container {
position: relative;
margin-bottom: 8px;
padding-top: 16px;
}
.container .form-group .field__input-container .field__input {
font-size: 18px;
line-height: 28px;
font-weight: 700;
letter-spacing: -1px;
background: transparent;
border: 0;
outline: 0;
padding-top: 20px;
padding-bottom: 2px;
width: 100%;
}
.container .form-group .field__input-container .field__input:focus + .field__label, .container .form-group .field__input-container .field__input:valid + .field__label {
font-size: 14px;
line-height: 20px;
bottom: 50%;
}
.container .form-group .field__input-container .field__input:focus ~ .field__decoration:before, .container .form-group .field__input-container .field__input:valid ~ .field__decoration:before {
width: 100%;
}
.container .form-group .field__input-container .field__input:focus ~ .arrow-down {
border-color: transparent;
}
.container .form-group .field__input-container .field__input--dropdown {
border-color: #000;
-webkit-appearance: none;
-moz-appearance: none;
appearance: none;
}
.container .form-group .field__input-container .field__input--dropdown + .field__label {
color: #000;
}
.container .form-group .field__input-container select::-ms-expand {
display: none;
}
.container .form-group .field__input-container .field__label {
color: #848484;
font-size: 19px;
line-height: 28px;
bottom: 2px;
left: 0;
pointer-events: none;
position: absolute;
top: auto;
transition: bottom 0.3s ease, font-size 0.3s ease, line-height 0.3s ease;
margin: 0;
padding: 0;
border: 0;
vertical-align: baseline;
}
.container .form-group .field__input-container .field__label span {
margin: 0;
padding: 0;
border: 0;
vertical-align: baseline;
}
.container .form-group .field__input-container .field__decoration {
border-bottom: 1px solid;
border-color: #000;
}
.container .form-group .field__input-container .field__decoration:before {
display: block;
position: absolute;
content: "";
left: 0;
bottom: 0;
width: 0;
border-bottom: 2px solid;
border-color: #000;
transition: width 0.3s ease, border-color 0.3s ease;
}
.container .form-group .field__input-container .arrow-down {
position: absolute;
right: 0;
bottom: 12px;
width: 0;
height: 0;
border-left: 4px solid transparent;
border-right: 4px solid transparent;
border-top: 6px solid #000;
transition: border-color 0.3s ease;
z-index: -1;
}
<div class="container">
<div id="user-first-name" class="form-group"><!-- field--invalid -->
<div class="field__input-container">
<input type="text" class="field__input" id="input-first-name" name="input-first-name" required value="" autocomplete="off">
<label class="field__label" id="username-label" for="input-first-name"><span>First name</span></label>
<div class="field__decoration"></div>
</div>
</div>
<div id="user-last-name" class="form-group"><!-- field--invalid -->
<div class="field__input-container">
<input type="text" class="field__input" id="input-last-name" name="input-last-name" required value="" autocomplete="off">
<label class="field__label" id="username-label" for="input-last-name"><span>Last name</span></label>
<div class="field__decoration"></div>
</div>
</div>
<div id="user-title" class="form-group"><!-- field--invalid -->
<div class="field__input-container">
<select class="field__input field__input--dropdown" id="input-title" name="input-title" required autocomplete="off">
<option selected value="" disabled hidden></option>
<option value="" disabled>Please select</option>
<option value="mr">Mr</option>
<option value="mrs">Mrs</option>
<option value="ms">Ms</option>
<option value="dr">Dr</option>
<option value="other">Other</option>
<option value="prefer not to say">Prefer not to say</option>
</select>
<label class="field__label" id="username-label" for="input-title"><span>Title</span></label>
<div class="field__decoration"></div>
<div class="arrow-down"></div>
</div>
</div>
</div>

Align label with css 3 fancy radio button

I have the following radio button, I need the bigger circle to be 38px
input[type=radio] {
visibility: hidden;
}
.label {
font-weight: normal;
color: #333;
}
.label::before {
content: '';
position: absolute;
left: 0;
width: 38px;
height: 38px;
border: 1px solid #727272;
border-radius: 50%;
}
input[type=radio]:checked+label:after {
content: '';
position: absolute;
width: 38px;
height: 38px;
left: 0;
background: #0065bd;
border: none;
transform: scale(0.5);
border-radius: 50%;
}
<div class="container">
<input type="radio" id="radio1" value="on">
<label for="radio1" class="label">Yes</label>
</div>
Here is a fiddle, how can I align the label so it is aligned to the centered and pushed to the right of the circle?
Add .container{ line-height:38px} to have it centered (it seems that it was to the right already)
https://jsfiddle.net/8gubpzhq/
to move it to the right add this to the
.label {
font-weight: normal;
color: #333;
padding-left:5px;//add this line
}
https://jsfiddle.net/vszuu535/
You can add line-height:40px; to your .label to center it vertically. To move it over to the right more you can add padding-left:20px; (You can change the line-height and padding-left to fit your needs).
input[type=radio] {
visibility: hidden;
}
.label {
font-weight: normal;
color: #333;
line-height:40px;
padding-left:20px;
}
.label::before {
content: '';
position: absolute;
left: 0;
width: 38px;
height: 38px;
border: 1px solid #727272;
border-radius: 50%;
}
input[type=radio]:checked + label:after {
content: '';
position: absolute;
width: 38px;
height: 38px;
left: 0;
background: #0065bd;
border: none;
transform: scale(0.5);
border-radius: 50%;
}
<div class="container">
<input type="radio" id="radio1" value="on">
<label for="radio1" class="label">Yes</label>
</div>
Perhaps your code is over-complicating matters; as you want the input to be bigger, maybe you should focus the sizing etc on the input rather than the label?
See the snippet (the radio turns blue now since edit, adapted from this codepen. It's grey before click, blue after, centered, and indented from the edge).
Just a note: If you are going to use a default value (and only have one option) maybe a custom checkbox would be a more suitable choice? (Radios button are usually used in instances where the user would have 2 or more choices, but can only select one).. just a thought.
input[type="radio"] {
display: none;
width: 38px;
height: 38px;
border: 1px solid #727272;
}
input[type="radio"]+label span {
display: inline-block;
width: 38px;
height: 38px;
margin: 9px;
vertical-align: middle;
cursor: pointer;
border-radius: 50%;
background-color: grey;
}
input[type="radio"]:checked+label span {
content="";
background: #0065bd;
}
input[type="radio"]+label span,
input[type="radio"]:checked+label span {
-webkit-transition: background-color 0.4s linear;
-o-transition: background-color 0.4s linear;
-moz-transition: background-color 0.4s linear;
transition: background-color 0.4s linear;
}
<div class="container">
<input type="radio" id="radio1" name="radio">
<label for="radio1" class="label"><span></span>Yes</label>
<input type="radio" id="radio2" name="radio">
<label for="radio2" class="label"><span></span>No</label>
</div>

How to move placeholder to top on focus AND while typing?

I want the placeholder to move to the top when the textbox is on focus and also while the user is typing.
I'm not sure if this is just html/css or any javascript too.
My current css looks like this, and I have no js code yet:
input:focus::-webkit-input-placeholder {
font-size: .75em;
position: relative;
top: -15px;
transition: 0.2s ease-out;
}
input::-webkit-input-placeholder {
transition: 0.2s ease-in;
}
input[type="text"]:focus, input[type="password"]:focus {
height: 50px;
padding-bottom: 0px;
transition: 0.2s ease-in;
}
input[type="text"], input[type="password"] {
height: 50px;
transition: 0.2s ease-in;
}
It almost does the work but the placeholder disappears when I start typing. I'm using twitter-bootstrap, if that makes anything easier!
Thanks.
You could do it like this
HTML:
<div>
<input type="text" class="inputText" />
<span class="floating-label">Your email address</span>
</div>
CSS:
input:focus ~ .floating-label,
input:not(:focus):valid ~ .floating-label{
top: 8px;
bottom: 10px;
left: 20px;
font-size: 11px;
opacity: 1;
}
.inputText {
font-size: 14px;
width: 200px;
height: 35px;
}
.floating-label {
position: absolute;
pointer-events: none;
left: 20px;
top: 18px;
transition: 0.2s ease all;
}
Working JSFiddle here https://jsfiddle.net/273ntk5s/2/
.user-input-wrp {
position: relative;
width: 50%;
}
.user-input-wrp .inputText{
width: 100%;
outline: none;
border:none;
border-bottom: 1px solid #777;
box-shadow: none !important;
}
.user-input-wrp .inputText:focus{
border-color: blue;
border-width: medium medium 2px;
}
.user-input-wrp .floating-label {
position: absolute;
pointer-events: none;
top: 18px;
left: 10px;
transition: 0.2s ease all;
}
.user-input-wrp input:focus ~ .floating-label,
.user-input-wrp input:not(:focus):valid ~ .floating-label{
top: 0px;
left: 10px;
font-size: 13px;
opacity: 1;
}
<h1>The floating label</h1>
<div class="user-input-wrp">
<br/>
<input type="text" class="inputText" required/>
<span class="floating-label">Your email address</span>
</div>
Modified the code from #user1846747 a little.
Only using HTML and css
.searchformfld{
position: relative;
margin: 5px 0px;
}
.searchformfld label{
position: absolute;
padding-left: 10px;
top:15px;
cursor: text;
}
.searchformfld input:focus + label,.searchformfld input:not(:placeholder-shown) + label{
opacity:1;
transform: scale(.9) translateY(-100%) translateX(-10px);
color:#000;
}
.searchformfld input:focus{
border:1px solid #000;
outline-color: #000;
}
.searchformfld{
padding: 15px;
margin:15px 0px;
}
.searchformfld input{
width:100%;
padding-left: 10px;
}
.searchformfld label,.searchformfld input{
transition: all 0.2s;
transition-timing-function: ease;
transition-timing-function: cubic-bezier(0.25, 0.1, 0.25, 1);
opacity:0.5;
}
<div class="searchformfld">
<input type="text" class="candidateName" id="candidateName" name="candidateName" placeholder=" "/>
<label for="candidateName">Candidate name</label>
</div>
You can try the below code. It only uses HTML and CSS and does not reply on Javascript or component libraries:
.text-field {
position: relative;
margin: 10px 2.5px 20px 2.5px;
}
input {
display: inline-block;
border: thin solid #fafafa;
border-bottom: solid medium #999;
color: #444;
background-color: #fafafa;
padding: 10px 10px 10px 10px;
border-top-left-radius: 10px;
border-top-right-radius: 10px;
}
input:focus {
border: thin solid #32cd32;
border-bottom: solid medium #32cd32;
background-color:#fff;
}
label {
color: #999;
position: absolute;
pointer-events: none;
left: 10px;
top: 10px;
transition: 0.2s;
}
input:focus ~ label, input:valid ~ label {
top: -10px;
left: 15px;
font-size: small;
color: #32cd32;
background-color:#fff;
padding:0 5px 0 5px;
}
<div class="text-field">
<input type="text" required>
<label>Input field 1</label>
</div>
<div class="text-field">
<input type="text" required>
<label>Input field 2</label>
</div>
span{
display:block;
}
input:focus::-webkit-input-placeholder { color:transparent; }
input:focus:-moz-placeholder { color:transparent; } /* FF 4-18 */
input:focus::-moz-placeholder { color:transparent; } /* FF 19+ */
input:focus:-ms-input-placeholder { color:transparent; } /* IE 10+ */
<script src="http://code.jquery.com/jquery-3.1.1.min.js"></script>
<script>
$(document).ready(function() {
$('input').focus(function(){
placeholder = $(this).attr('placeholder');
if(placeholder != undefined){
$(this).parent().prepend('<span class="input-placeholder">'+placeholder+'</span>');
}
});
$('input').blur(function(){
$(this).parent().find('.input-placeholder').remove();
});
});
</script>
<div>
<input type="text" class="inputText" placeholder="Email adress" required/>
</div>
The solution I want to propose deals with an input with the movement of the placeholder without the need for the required attribute
.inputField {
position: relative;
}
.inputField input {
padding: 8px 20px;
padding-top: 18px;
border: 1.8px solid rgba(107, 107, 107, 0.4);
border-radius: 3px;
width: 50%;
color: black;
}
.inputField input:focus {
border: 1.8px solid #6b6b6b;
outline: none;
}
.inputField span {
pointer-events: none;
opacity: 0.5;
position: absolute;
padding-left: 20px;
left: 0;
top: 50%;
-webkit-transform: translateY(-50%);
transform: translateY(-50%);
cursor: text;
}
.inputField input:focus+span,
.inputField input:not(:placeholder-shown)+span {
top: 7px;
-webkit-transform: scale(0.7) translateY(-10%) translateX(-8.5px);
transform: scale(0.7) translateY(-10%) translateX(-8.5px);
}
.inputField input,
.inputField span {
-webkit-user-select: none;
-moz-user-select: none;
-ms-user-select: none;
user-select: none;
-webkit-transition: all 0.2s;
transition: all 0.2s;
-webkit-transition-timing-function: cubic-bezier(0.25, 0.1, 0.25, 1);
transition-timing-function: cubic-bezier(0.25, 0.1, 0.25, 1);
}
<div class="inputField">
<input type="text" placeholder=" " />
<span>Placeholder</span>
</div>
That site isn't moving the placeholder, but placing a div (.floating-label) over the input, so when the input is focused the div just animates to be over the input. The key part here is using pointer-events: none; in the floating div, so when you click it the event goes through it to the input box behind it.
if your Floating label is not working when required attribute is removed, try this: using input:not(:placeholder-shown)
input:focus ~ .floating-label,
input:not(:placeholder-shown) ~ .floating-label{
top: 8px;
bottom: 10px;
left: 20px;
font-size: 11px;
opacity: 1;
}
.inputText {
font-size: 14px;
width: 200px;
height: 35px;
}
.floating-label {
position: absolute;
pointer-events: none;
left: 20px;
top: 18px;
transition: 0.2s ease all;
}
<div>
<input placeholder="" type="text" class="inputText" />
<span class="floating-label">Your email address</span>
</div>