Reputation: 235
I'm trying to move the label to above the input field when I focus, but it gets hidden instead.
label > span::focus {
top: -20px;
font-size: 15px;
color: blue;
}
Complete Html & CSS code.
Update:
I figured it out, I just change some HTML classes and things to this:
.user-input:focus + .user-label > span {
margin-top: -15px;
font-size: 10px;
color: blue;
Upvotes: 1
Views: 1564
Reputation: 16251
As I said in comment:
You can not style previous sibling
selector(label
) on focus
the next(input
)
So you have to set the label
after input
in html and set position:relative
instead of absolute
body {
background-color: #f0f0f0;
}
form {
margin-left: 39%;
margin-top: 9%;
}
label {
position: relative;
margin-left: 0%;
font-family: monospace, 'Montserrat', sans-serif;
font-size: 15px;
padding-top: 3px;
/* border: 5px solid black;*/
}
.head {
margin-left: 43%;
margin-top: 5%;
font-size: 25px;
font-family: monospace;
}
.password {
margin-top: 5%;
}
.input-field {
position: absolute;
background: transparent;
border-width: 2px;
border-top: 0;
border-left: 0;
border-right: 0;
background: transparent;
outline: 0;
height: 23px;
border-color: black;
width: 180px;
}
input:focus {
background: #fae596;
border: 0;
}
input:focus + .label {
top:-20px;
font-size: 15px;
color: blue; /* font color while on top */
}
<p class="head">Sign In</p>
<form>
<div class="username">
<input type="text" name="name" class="input-field" id="user"/> <!--input filed-->
<label for="user" class="label"><span>Username</span></label>
</div>
<br>
<br>
<div class="password">
<input type="text" name="name" class="input-field" id="pass" />
<label for="pass" class="label"><span>Password</span></label>
</div>
</form>
Upvotes: -1
Reputation: 545
I think You Need Something Like This
* {
box-sizing: border-box;
}
.group {
position: relative;
margin-bottom: 45px;
margin-top: 45px;
}
input {
font-size: 18px;
padding: 10px 10px 10px 5px;
display: block;
width: 300px;
border: none;
border-bottom: 1px solid #757575;
}
input:focus {
outline: none;
}
label {
color: #999;
font-size: 18px;
font-weight: normal;
position: absolute;
pointer-events: none;
left: 5px;
top: 10px;
transition: 0.2s ease all;
-moz-transition: 0.2s ease all;
-webkit-transition: 0.2s ease all;
}
input:focus~label,
input:valid~label {
top: -20px;
font-size: 14px;
color: #5264AE;
}
input:focus~.bar:before,
input:focus~.bar:after {
width: 50%;
}
.highlight {
position: absolute;
height: 60%;
width: 100px;
top: 25%;
left: 0;
pointer-events: none;
opacity: 0.5;
}
/* active state */
input:focus~.highlight {
-webkit-animation: inputHighlighter 0.3s ease;
-moz-animation: inputHighlighter 0.3s ease;
animation: inputHighlighter 0.3s ease;
}
/* ANIMATIONS ================ */
@-webkit-keyframes inputHighlighter {
from {
background: #5264AE;
}
to {
width: 0;
background: transparent;
}
}
@-moz-keyframes inputHighlighter {
from {
background: #5264AE;
}
to {
width: 0;
background: transparent;
}
}
@keyframes inputHighlighter {
from {
background: #5264AE;
}
to {
width: 0;
background: transparent;
}
}
<form>
<div class="group">
<input type="text" required>
<span class="highlight"></span>
<span class="bar"></span>
<label>Name</label>
</div>
<div class="group">
<input type="text" required>
<span class="highlight"></span>
<span class="bar"></span>
<label>Email</label>
</div>
</form>
Upvotes: 3