Reputation: 3163
I have created a material design style input form with label, here is a JSfiddle demo It seems to be working fine, but it only works when I add required
in input tag, if I remove required
from input it stopped working, not sure why, can somebody please look in to it?
thanks in advance.
.ttl{
font-size:16px;
margin:10px 0;
}
.form-row:nth-child(1){
margin-top:50px;
}
.form-row {
margin-top: 20px;
margin-bottom: 20px;
float: left;
width: 100%;
position: relative;
}
.form-group input {
height: 2.5rem;
}
.form-group .control-label {
position: absolute;
top: 0.25rem;
pointer-events: none;
padding-left: 0.125rem;
z-index: 1;
color: #696969;
font-size: 1.750rem;
font-weight: normal;
-webkit-transition: all 0.28s ease;
transition: all 0.28s ease;
font-family: 'Montserrat-Regular';
text-align: center;
width: 100%;
}
.form-group .bar {
position: relative;
border-bottom: 0.0625rem solid #999;
display: block;
}
.form-group .bar::before {
content: '';
height: 0.125rem;
width: 0;
left: 50%;
bottom: -0.0625rem;
position: absolute;
background: #40b8f1;
-webkit-transition: left 0.28s ease, width 0.28s ease;
transition: left 0.28s ease, width 0.28s ease;
z-index: 2;
}
.form-group input, .form-group textarea {
display: block;
background: none;
padding: 0.125rem 0.125rem 0.0625rem;
font-size: 1.750rem;
border-width: 0;
border-color: transparent;
line-height: 2;
width: 100%;
color: transparent;
-webkit-transition: all 0.28s ease;
transition: all 0.28s ease;
box-shadow: none;
text-align: center;
}
.form-group select ~ .control-label, .form-group input:focus ~ .control-label, .form-group input:valid ~ .control-label, .form-group input.form-file ~ .control-label, .form-group input.has-value ~ .control-label, .form-group textarea:focus ~ .control-label, .form-group textarea:valid ~ .control-label, .form-group textarea.form-file ~ .control-label, .form-group textarea.has-value ~ .control-label {
font-size: 1.750rem;
color: gray;
top: -2.750rem;
left: 0;
}
.form-group select:focus ~ .bar::before, .form-group input:focus ~ .bar::before, .form-group textarea:focus ~ .bar::before {
width: 100%;
left: 0;
}
<p class="ttl">Without required it doesn't work</p>
<div class="form-row">
<div class="form-group">
<input type="text" name="student">
<label for="input" class="control-label">Label Title here</label>
<i class="bar"></i>
</div>
</div>
<p class="ttl">With required it works</p>
<div class="form-row">
<div class="form-group">
<input required type="text" name="student">
<label for="input" class="control-label">Label Title here</label>
<i class="bar"></i>
</div>
</div>
Upvotes: 0
Views: 144
Reputation: 3207
Just add below Css.
.form-group , .form-group input:valid ~ .control-label{top:0;}
.form-group input:focus ~ .control-label{top: -2.750rem;}
.ttl{font-size:16px; margin:10px 0;}
.form-row:nth-child(1){margin-top:50px;}
.form-row {
margin-top: 20px;
margin-bottom: 20px;
float: left;
width: 100%;
position: relative;
}
.form-group input {
height: 2.5rem;
}
.form-group .control-label {
position: absolute;
top: 0.25rem;
pointer-events: none;
padding-left: 0.125rem;
z-index: 1;
color: #696969;
font-size: 1.750rem;
font-weight: normal;
-webkit-transition: all 0.28s ease;
transition: all 0.28s ease;
font-family: 'Montserrat-Regular';
text-align: center;
width: 100%;
}
.form-group .bar {
position: relative;
border-bottom: 0.0625rem solid #999;
display: block;
}
.form-group .bar::before {
content: '';
height: 0.125rem;
width: 0;
left: 50%;
bottom: -0.0625rem;
position: absolute;
background: #40b8f1;
-webkit-transition: left 0.28s ease, width 0.28s ease;
transition: left 0.28s ease, width 0.28s ease;
z-index: 2;
}
.form-group input, .form-group textarea {
display: block;
background: none;
padding: 0.125rem 0.125rem 0.0625rem;
font-size: 1.750rem;
border-width: 0;
border-color: transparent;
line-height: 2;
width: 100%;
color: transparent;
-webkit-transition: all 0.28s ease;
transition: all 0.28s ease;
box-shadow: none;
text-align: center;
}
.form-group select ~ .control-label, .form-group input:focus ~ .control-label, .form-group input:valid ~ .control-label, .form-group input.form-file ~ .control-label, .form-group input.has-value ~ .control-label, .form-group textarea:focus ~ .control-label, .form-group textarea:valid ~ .control-label, .form-group textarea.form-file ~ .control-label, .form-group textarea.has-value ~ .control-label {
font-size: 1.750rem;
color: gray;
top: 0;
left: 0;
}
.form-group select:focus ~ .bar::before, .form-group input:focus ~ .bar::before, .form-group textarea:focus ~ .bar::before {
width: 100%;
left: 0;
}
.form-group , .form-group input:valid ~ .control-label{top:0;}
.form-group input:focus ~ .control-label{top: -2.750rem;}
<p class="ttl">Without required it doesn't work</p>
<div class="form-row">
<div class="form-group">
<input type="text" name="student" required>
<label for="input" class="control-label">Label Title here</label>
<i class="bar"></i> </div>
</div>
<p class="ttl">With required it works</p>
<div class="form-row">
<div class="form-group">
<input required type="text" name="student" required>
<label for="input" class="control-label">Label Title here</label>
<i class="bar"></i> </div>
</div>
Upvotes: 0
Reputation: 4480
remove .form-group input:valid ~ .control-label
from the style of input focused
Change to This
.form-group select ~ .control-label, .form-group input:focus ~ .control-label, .form-group input.form-file ~ .control-label, .form-group input.has-value ~ .control-label, .form-group textarea:focus ~ .control-label, .form-group textarea:valid ~ .control-label, .form-group textarea.form-file ~ .control-label, .form-group textarea.has-value ~ .control-label {
font-size: 1.750rem;
color: gray;
top: -2.750rem;
left: 0;
}
https://jsfiddle.net/fy577w6b/3/
Or add styles for the control label when input is invalid.
Upvotes: 1