Reputation: 867
I'm embedding a HubSpot
form on a page. On this form, I'm trying to achieve functionality where, on focus
, the label moves up. Something like this:
form {
display: inline-block;
}
.field {
padding-top: 10px;
display: flex;
flex-direction: column;
}
label {
order: -1;
padding-left: 5px;
transition: all 0.3s ease-in;
transform: translateY(20px);
pointer-events: none;
}
input:focus + label {
transform: translateY(-2px);
}
<form class="form">
<div class="field">
<input type="text">
<label>Name</label>
</div>
</form>
However, HubSpot
form markup is uneditable and all over the place, so I'm trying to target a label
that is outside of the container
of the input
. See below for demo:
.hs-form-field {
position: relative;
}
.hs-form-field input, .hs-form-field textarea, .hs-form-field select {
outline: 0;
padding: 15px;
width: 50%;
}
.hs-form-field > label {
position: absolute;
top: 50%;
transform: translate(15px, -50%);
}
.hs-form-field input:focus {
color: red;
}
.hs-form-field input:focus + ~ .hs-form-field > label {
transform: translate(10px, -30px);
}
<div class="hs-form-field">
<label>
<span>First name</span>
</label>
<div class="input">
<input type="text" />
</div>
</div>
As you can see, on focus
, I'm trying to target the above selector, but upon inspect, nothing shows, so something isn't working?
Upvotes: 0
Views: 33
Reputation: 995
You can control lable
by placeholder attribute.
.floating-form {
width:320px;
margin-top:2rem;
}
/**** floating-Lable style start ****/
.floating-label {
position:relative;
margin-bottom:20px;
}
.floating-input {
font-size:14px;
padding:4px 4px;
display:block;
width:100%;
height:30px;
background-color: transparent;
border:none;
border-bottom:1px solid #757575;
}
.floating-input:focus {
outline:none;
border-bottom:2px solid #5264AE;
}
label {
color:#999;
font-size:14px;
font-weight:normal;
position:absolute;
pointer-events:none;
left:5px;
top:5px;
transition:0.2s ease all;
-moz-transition:0.2s ease all;
-webkit-transition:0.2s ease all;
}
.floating-input:focus ~ label, .floating-input:not(:placeholder-shown) ~ label {
top:-18px;
font-size:14px;
color:#5264AE;
}
/* active state */
.floating-input:focus ~ .bar:before, .floating-input:focus ~ .bar:after {
width:50%;
}
*, *:before, *:after {
-webkit-box-sizing: border-box;
-moz-box-sizing: border-box;
box-sizing: border-box;
}
/* highlighter */
.highlight {
position:absolute;
height:50%;
width:100%;
top:15%;
left:0;
pointer-events:none;
opacity:0.5;
}
/* active state */
.floating-input:focus ~ .highlight {
-webkit-animation:inputHighlighter 0.3s ease;
-moz-animation:inputHighlighter 0.3s ease;
animation:inputHighlighter 0.3s ease;
}
/* animation */
@-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; }
}
<div class="floating-form">
<div class="floating-label">
<input class="floating-input" type="text" placeholder=" ">
<span class="highlight"></span>
<label>Text</label>
</div>
</div>
Upvotes: 0
Reputation: 1705
:focus-within
might be what you're looking for. https://css-tricks.com/almanac/selectors/f/focus-within/
.hs-form-field:focus-within label {
transform: translate(10px, -50px);
}
.hs-form-field {
position: relative;
margin-top: 2rem;
}
.hs-form-field input, .hs-form-field textarea, .hs-form-field select {
outline: 0;
padding: 15px;
width: 50%;
}
.hs-form-field > label {
position: absolute;
top: 50%;
transform: translate(15px, -50%);
transition: 0.3s;
}
.hs-form-field input:focus {
color: red;
}
.hs-form-field:focus-within label {
transform: translate(10px, -50px);
}
.hs-form-field input:focus + ~ .hs-form-field > label {
transform: translate(10px, -30px);
}
<div class="hs-form-field">
<label>
<span>First name</span>
</label>
<div class="input">
<input type="text" />
</div>
</div>
Upvotes: 1