Placeholder position when input has text - css

How to do placeholder styling when Input Text are focus, the placeholder are still visible and the font-size become smaller. For example like this:
I only need webkit support. Does webkit support such thing?

.input {
width: 200px;
height: 35px;
outline:0;
padding:0 10px;
}
.label { position: absolute;
pointer-events: none;
left: 20px;
top: 18px;
transition: 0.2s ease all;
}
input:focus ~ .label,
input:not(:focus):valid ~ .label{
top: 10px;
bottom: 5px;
left: 20px;
font-size: 12px;
}
<div>
<input type="text" class="input" required/>
<span class="label">Company Name</span>
</div>

You need to use a label element and modify it accordingly. Try this:
function fillClass() {
if ($(this).val() != '')
$(this).parent().addClass('input--filled');
else
$(this).parent().removeClass('input--filled');
}
$(".sign-in-input").focusout(fillClass);
.sign-in-input-ctr {
width: 100%;
display: inline-block;
vertical-align: top;
margin-bottom: 25px;
position: relative;
margin-top: 50px;
}
.sign-in-input {
width: 100%;
border: 2px solid transparent;
-webkit-transition: all 0.3s;
transition: all 0.3s;
height: 38px;
background-color: #fff;
outline: none;
border: 1px solid rgba(73, 73, 73, 0.2);
border-radius: 2px;
padding-left: 10px;
}
.sign-in-input:focus,
.input--filled .sign-in-input {
background-color: transparent;
border: 1px solid #a09e9e;
}
.sign-in-label {
width: 100%;
font-size: 12px;
position: absolute;
top: -18px;
pointer-events: none;
overflow: hidden;
padding: 0 15px;
left: 0;
-webkit-transform: translate3d(0, 30px, 0);
transform: translate3d(0, 30px, 0);
-webkit-transition: -webkit-transform 0.3s;
transition: transform 0.3s;
}
.sign-in-input:focus+.sign-in-label,
.input--filled .sign-in-label {
-webkit-transform: translate3d(-15px, 0, 0);
transform: translate3d(-15px, 0, 0);
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="sign-in-input-ctr">
<input class="sign-in-input" type="text" name="email">
<label class="sign-in-label" for="signin-email"><span class="input-label-content">Email</span>
</label>
</div>

//------ contact us dropdown list
$(document).ready(function(){
$( ".form-group .form-control" ).focus(function() {
$(this).parent().addClass('inputFous');
}).blur(function() {
if($.trim($(this).val()) == '')
{
$(this).parent().removeClass('inputFous');
}
});
});
I think there is no webkit support for this, please check this code which will help you:- Jsfiddle

Related

how to make a outlined input with floating label with custom css?

I want to make an input with the floating label. Exactly like the material-ui for react.
Material UI Code :
<TextField
id="outlined-basic"
label="E-Mail"
variant="outlined"
placeholder="Enter E-Mail"
/>
Link for seeing output for the above code : Code
What I want is, the label must be inside the input field. When the user is clicking the input field, the label should float and the placeholder should be shown. If the user doesn't enter anything and if the focus changes, again the placeholder must be hidden and the label must comeback to initial position
What I have tried so far is :
.container {
height: 100%;
display: flex;
justify-content: center;
align-items: center;
}
.material-textfield {
position: relative;
}
label {
position: absolute;
font-size: 1rem;
left: 0;
top: 50%;
transform: translateY(-50%);
background-color: white;
color: gray;
padding: 0 0.3rem;
margin: 0 0.5rem;
transition: 0.1s ease-out;
transform-origin: left top;
pointer-events: none;
}
input {
font-size: 1rem;
outline: none;
border: 1px solid gray;
border-radius: 5px;
padding: 1rem 0.7rem;
color: gray;
transition: 0.1s ease-out;
}
input:focus {
border-color: #6200ee;
}
input:focus + label {
color: #6200ee;
top: 0;
transform: translateY(-10%) scale(0.9) !important;
}
input:not(:placeholder-shown) + label {
top: 0;
transform: translateY(-50%) scale(0.9) !important;
}
input:empty + label {
transform: translateY(75%);
font-weight: bold;
}
input:empty:not(:placeholder-shown) + label {
top: 0;
transform: translateY(-50%) scale(0.9) !important;
}
input:empty::-webkit-input-placeholder {
opacity: 0;
}
<div class="container">
<div class="material-textfield">
<input placeholder="placeholder" type="text" />
<label> label</label>
</div>
</div>
You were mostly there with your code. The most important thing to note, :empty means nothing for inputs. It will always be applied because inputs can't have children.
Though the :placeholder-shown is pretty much the main workaround to styling an empty input.
I removed most of the :empty styles and replaced some with :placeholder-shown.
The other thing I did was I set up the focus and not-empty transitions to be the same.
input:not(:focus)::placeholder {
opacity: 0;
}
This makes it so the placeholder is hidden only when it's not focused.
Here's the full results:
.container {
height: 100%;
display: flex;
justify-content: center;
align-items: center;
}
.material-textfield {
position: relative;
}
label {
position: absolute;
font-size: 1rem;
left: 0;
top: 50%;
transform: translateY(-50%);
background-color: white;
color: gray;
padding: 0 0.3rem;
margin: 0 0.5rem;
transition: 0.1s ease-out;
transform-origin: left top;
pointer-events: none;
}
input {
font-size: 1rem;
outline: none;
border: 1px solid gray;
border-radius: 5px;
padding: 1rem 0.7rem;
color: gray;
transition: 0.1s ease-out;
}
input:focus {
border-color: #6200ee;
}
input:focus+label {
color: #6200ee;
top: 0;
transform: translateY(-50%) scale(0.9) !important;
}
input:not(:placeholder-shown)+label {
top: 0;
transform: translateY(-50%) scale(0.9) !important;
}
input:not(:focus)::placeholder {
opacity: 0;
}
}
<div class="container">
<div class="material-textfield">
<input placeholder="placeholder" type="text" />
<label> label</label>
</div>
</div>
That all being said, there are some accessibility issues with the Material Design shrink labels. So, just make sure you are aware of potential issues.
https://www.matsuko.ca/blog/stop-using-material-design-text-fields/

Floating labels - label above input field

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>
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>

Issues with styling toggle switch in css/sass

I am having some issues I cannot seem to figure out with styling a toggle switch as required. I currently have it like this:
It however needs to look like this:
Here is my HTML:
<label class="switch"><input #handlingUnitAdvancedOptionsCheckBox id="handlingUnitAdvancedOptionsCheckBox" type="checkbox" [checked]="handlingModel.advancedOptions"
(change)="handlingUnitAdvancedOptionsToggle(handlingUnitAdvancedOptionsCheckBox.checked)" />
<span class="slider round"></span>
</label>
Here is my css:
/* The switch - the box around the slider */
.switch {
position: relative;
display: inline-block;
width: 50px!important;
height: 24px!important;
cursor: pointer;
}
/* Hide default HTML checkbox */
.switch input {
opacity: 0;
width: 0;
height: 0;
}
/* The slider */
.slider {
position: absolute;
cursor: pointer;
top: 0;
left: 0;
right: 0;
bottom: 0;
background-color: #ccc;
-webkit-transition: .4s;
transition: .4s;
}
.slider:before {
display: block;
width: 22px!important;
height: 22px!important;
margin: 1px;
background: #fff;
position: absolute;
top: 0;
bottom: 0;
right: 26px;
border-radius: 20px;
transition: margin .3s ease-in 0s;
transition-property: margin;
transition-duration: 0.3s;
transition-timing-function: ease-in;
transition-delay: 0s;
box-shadow: 1px 2px 7px #444;
}
input:checked + .slider {
background-color: #236093;
}
input:focus + .slider {
box-shadow: 0 0 1px #236093;
}
input:checked + .slider:before {
-webkit-transform: translateX(26px);
-ms-transform: translateX(26px);
transform: translateX(26px);
}
/* Rounded sliders */
.slider.round {
border-radius: 20px;
}
.slider.round:before {
border-radius: 50%;
}
Not sure where I am going wrong here or what I need to adjust. I have tried changing the transform and the padding but I cannot seem to get it right.
You can use the following solution:
.switch {
cursor: pointer;
display: inline-block;
height: 24px;
position: relative;
width: 50px;
overflow:hidden;
}
.switch input {
height: 0;
opacity: 0;
width: 0;
}
.slider {
background: #ccc;
bottom: 0;
cursor: pointer;
display: block;
left: 0;
position: absolute;
right: 0;
top: 0;
-webkit-transition: .4s;
transition: .4s;
}
.slider:before {
background: #fff;
border-radius: 50%;
bottom: 1px;
box-shadow: 3px 0px 7px #444;
content: "";
display: block;
height: 22px;
left: 1px;
position: absolute;
right: 50px;
top: 1px;
width: 22px;
transition-property: all;
transition-duration: .6s;
}
input:checked + .slider {
background: #236093;
}
input:checked + .slider:before {
box-shadow: -3px 0px 7px #002551;
left: calc(100% - 23px); /** width of .slider:before + 1px */
}
.slider.round {
border-radius: 24px;
}
<label class="switch">
<input #handlingUnitAdvancedOptionsCheckBox id="handlingUnitAdvancedOptionsCheckBox" type="checkbox" [checked]="handlingModel.advancedOptions"
(change)="handlingUnitAdvancedOptionsToggle(handlingUnitAdvancedOptionsCheckBox.checked)" />
<span class="slider round"></span>
</label>

password input shakes when I focus/unfocus on.username

When I focus/unfocus on password, no shaking happens, but when I focus/unfocus on username then the password shakes...
Why is that happening & how to stop that?
body {
background-color: royalblue; /*#f0f0f0;*/
}
label {
font-family: 'Montserrat', sans-serif;
font-size: 14px;
z-index: -1;
border: 0;
color: white;
position: relative;
}
.head {
color: white;
margin-left: 44%;
font-family: monospace;
font-size: 25px;
}
/*.content {
margin-top: 10%;
margin-left: 41%;
}*/
.password {
margin-top: 5%;
}
form {
position: relative;
}
input {
position: absolute;
font-family: 'Montserrat', sans-serif;
font-size: 15px;
background: transparent;
border: 0; /* BORDER yes/no */
border-bottom: 2px solid beige;
background: transparent;
outline: 0;
height: 25px;
width: 180px;
z-index: 1;
margin-top: 5px;
}
/*input:focus {
outline: 1;
}*/
label::after {
content: '';
position: absolute;
top: 1px;
left: 0;
width: 180px;
height: 25px;
border-radius: 2px;
transition: transform .3s;
}
label::after{
z-index: -1;
background: beige; /*#a86bf;*/
transform: scale3d(1, 0, 1);
transform-origin: 0% 0%;
}
input:focus {
border-radius: 2px;
}
input:focus + label::after,
input:valid + label::after {
transform: scale3d(1, -1.3, 1);
transition-timing-function: linear;
top: -1px;
}
span {
position: relative;
margin-top: -30px;
display: block;
padding: .6em 0;
padding-left: -5px;
transition: top .5s ease, font-size .5s ease;
/* transition: transform 1s 2s;*/
top: 0;
}
input:focus + label > span,
input:valid + label > span {
top: -20px;
font-size: 11px;
padding-bottom: 15px;
}
/* font-family: monospace;*/
/*transform: translate3d(0, -80%, 0); */
/* transition-timing-function: linear;*/
<p class="head">Sign In</p>
<form>
<div class="content">
<div class="username">
<input name="name" class="user-input" id="user" required>
<label class="user-label" for="user"><span>Username</span></label>
</div>
<div class="password">
<input name="name" class="pass-input" id="pass" required>
<label class="pass-label" for="pass"><span>Password</span></label>
</div>
</div>
</form>
Add height to both div containers, so they keep it when clicked on.
body {
background-color: royalblue; /*#f0f0f0;*/
}
label {
font-family: 'Montserrat', sans-serif;
font-size: 14px;
z-index: -1;
border: 0;
color: white;
position: relative;
}
.head {
color: white;
margin-left: 44%;
font-family: monospace;
font-size: 25px;
}
.username {
height:40px;
}
.password {
height:40px;
}
/*.content {
margin-top: 10%;
margin-left: 41%;
}*/
.password {
margin-top: 5%;
}
form {
position: relative;
}
input {
position: absolute;
font-family: 'Montserrat', sans-serif;
font-size: 15px;
background: transparent;
border: 0; /* BORDER yes/no */
border-bottom: 2px solid beige;
background: transparent;
outline: 0;
height: 25px;
width: 180px;
z-index: 1;
margin-top: 5px;
}
/*input:focus {
outline: 1;
}*/
label::after {
content: '';
position: absolute;
top: 1px;
left: 0;
width: 180px;
height: 25px;
border-radius: 2px;
transition: transform .3s;
}
label::after{
z-index: -1;
background: beige; /*#a86bf;*/
transform: scale3d(1, 0, 1);
transform-origin: 0% 0%;
}
input:focus {
border-radius: 2px;
}
input:focus + label::after,
input:valid + label::after {
transform: scale3d(1, -1.3, 1);
transition-timing-function: linear;
top: -1px;
}
span {
position: relative;
margin-top: -30px;
display: block;
padding: .6em 0;
padding-left: -5px;
transition: top .5s ease, font-size .5s ease;
/* transition: transform 1s 2s;*/
top: 0;
}
input:focus + label > span,
input:valid + label > span {
top: -20px;
font-size: 11px;
padding-bottom: 15px;
}
/* font-family: monospace;*/
/*transform: translate3d(0, -80%, 0); */
/* transition-timing-function: linear;*/
<p class="head">Sign In</p>
<form>
<div class="content">
<div class="username" >
<input name="name" class="user-input" id="user" required>
<label class="user-label" for="user"><span>Username</span></label>
</div>
<div class="password" >
<input name="name" class="pass-input" id="pass" required>
<label class="pass-label" for="pass"><span>Password</span></label>
</div>
</div>
</form>
What I think's happening is that your CSS is adding a small margin to the bottom of the <div class="username"> and this is causing your "shaking". The reason it's not happening when you click on the Password is because there's nothing under it.

AngularJS ng-class after :active or :focus

I have a form I am trying to create using CSS to achieve a halfway "material" feel. I achieved a lot of the animations by ditching the stock bootstrap forms entirely and rewriting them to get the maximum amount of flexibility.
EDIT: Forgot to link Codepen - http://codepen.io/h3xc0ntr0l/pen/ONwdLm
Essentially, the "form groups" are set to position:relative and used to position the input and a couple pseudo elements absolutely, yada yada. I am using angular and am not able to incorporate jquery into the project.
my problem is as follows
<div class="-form-group">
<input type="email" class="-form-input" ng-class='{"content":"user.email"}' ng-model="user.email"/>
<span class="in-email"></span>
</div>
this does not have the behavior i need. I guess if I have "ng-model", then whatever is in there is going to evaluate to true.
I also tried the following with scss:
.-form-input{
border: none;
box-shadow: none;
border-radius: 0;
font-size: 1rem;
background: none;
outline: none;
width: 100%;
margin-top: 2em;
position: relative;
z-index:4;
&:active, &:focus{
.in-email &{ //
&:before{
top: -50%;
color: transparent;
border-bottom: transparent;
}
}
}
}
the effects are achieved via
.in-email, .in-pwd{
&:before{
position: absolute;
top: 50%;
left: 0%;
width: 100%;
z-index:3;
color: black;
transition: 500ms ease all;
border-bottom: 1.5px solid rgba(128,128,128, .5);
}
}
I can not think of anything else that would prevent the span from coming back down after i leave the hover regardless of whether or not there is any content. does anyone have any tips?
(function(){
angular.module('app').controller([
'$scope', function($scope){
$scope.user = undefined;
}
]);
})();
form {
width: 100%;
padding: 1em;
border: .25px solid #e9e9e9;
box-shadow: 0 1rem 3rem 0 rgba(0, 0, 0, 0.33);
height: 20rem;
margin: 0 auto;
}
form .-form-group {
position: relative;
width: 100%;
height: auto;
transition: 500ms ease;
margin: 0 1em 2em 0;
}
form .-form-group .-form-input {
border: none;
box-shadow: none;
border-radius: 0;
font-size: 1rem;
background: none;
outline: none;
width: 100%;
margin-top: 2em;
position: relative;
z-index: 4;
}
.in-email form .-form-group .-form-input:active:before, .in-email form .-form-group .-form-input:focus:before {
top: -50%;
color: transparent;
border-bottom: transparent;
}
form .-form-group input:-webkit-autofill {
background-color: #e9e9e9 !important;
}
form .-form-group:after {
content: "";
display: block;
transition: 250ms 500ms ease all;
border-bottom: 1.5px solid #288690;
width: 0;
}
form .-form-group:hover:after, form .-form-group:focus:after, form .-form-group:active:after {
content: "";
width: 100%;
top: 90%;
}
form .-form-group:hover .in-email:before, form .-form-group:hover .in-pwd:before, form .-form-group:focus .in-email:before, form .-form-group:focus .in-pwd:before, form .-form-group:active .in-email:before, form .-form-group:active .in-pwd:before {
top: -50%;
color: transparent;
border-bottom: transparent;
}
form .button {
border-radius: 1px;
border: none;
}
form .in-email:before, form .in-pwd:before {
position: absolute;
top: 50%;
left: 0%;
width: 100%;
z-index: 3;
color: black;
transition: 500ms ease all;
border-bottom: 1.5px solid rgba(128, 128, 128, 0.5);
}
.content:before {
content: "" !important;
}
.in-email:before {
content: "Email Address";
}
.in-pwd:before {
content: "Password";
}
.nav, .pagination, .carousel, .panel-title a {
cursor: pointer;
}
#ui-root {
margin-top: 5rem;
padding: 1rem;
}
.ui-container {
position: relative;
width: 100%;
padding: 0 10% 0 10%;
}
.button {
background-color: #288690 !important;
}
<link rel="stylesheet" href="http://getbootstrap.com/dist/css/bootstrap.css"/>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js"></script>
<div class="container">
<div class="ui-container">
<div class="row" id="ui-root">
<div class="col-sm-12 col-md-4 col-md-offset-4">
<form autocomplete="off" class="text-center">
<!-- chrome autcomplete -->
<div class="-form-group">
<input type="email" class="-form-input" ng-class='{"content": "user.email"}' ng-model="user.email"/>
<span class="in-email"></span>
</div>
<div class="-form-group">
<input type="password" class="-form-input" ng-class='{"content": "user.password"}' ng-model="user.password" />
<span ng-class='{"content": "user.password"}' class="in-pwd"></span>
</div>
<button class="btn btn-primary button">Login</button>
</form>
</div>
</div>
</div>
</div>

Resources