35
loading...
This website collects cookies to deliver better user experience
peer-
utility that we will use is working with JIT mode.tailwind.config.js
file should be like thismodule.exports = {
mode: 'jit',
// other configs...
}
<div class="input-with-placeholder">
<input type="text" id="username" />
<label for="username">Username</label>
</div>
+
selector to select label comes after input. But before we do that, let's write some css to make sure that example looks pretty :)/*
some reset css..
*/
.input-with-placeholder {
width: 250px;
position: relative;
}
.input-with-placeholder label {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
display: flex;
align-items: center;
padding-left: 10px;
transition: 300ms all;
}
.input-with-placeholder input {
width: 100%;
height: 40px;
padding: 0 15px;
background: #f8f8f8;
}
.input-with-placeholder input:focus + label {
height: 50%;
padding-left: 0;
transform: translateY(-100%);
font-size: 12px;
color: #777;
}
required
attribute to input, and then we can control with :valid
in css. Let's do that.required
attribute to input<input type="text" id="username" required />
.input-with-placeholder input:focus + label,
.input-with-placeholder input:valid + label {
height: 50%;
padding-left: 0;
transform: translateY(-100%);
font-size: 12px;
color: #777;
}
group
and peer
utilities.<div class="w-56 relative group">
<input type="text" id="username" required class="w-full h-10 px-4 text-sm peer bg-gray-100 outline-none">
<label for="username" class="transform transition-all absolute top-0 left-0 h-full flex items-center pl-2 text-sm group-focus-within:text-xs peer-valid:text-xs group-focus-within:h-1/2 peer-valid:h-1/2 group-focus-within:-translate-y-full peer-valid:-translate-y-full group-focus-within:pl-0 peer-valid:pl-0">Username</label>
</div>
.w-56.relative.group
= we set width, relative for absolute label, group for using group-focus-within
.w-full.h-10.px-4.text-sm.peer.bg-gray-100.outline-none
= we set width full, height 2.5 rem, padding left and right 1rem, font-size 0.875rem, peer for using peer-valid
, background gray and no outline..transform.transition-all.absolute.top-0.left-0.h-full.flex.items-center.pl-2.text-sm
= we positioned label into input, and we add transition for the animation and transform for using the translate.group-focus-within
group-focus-within:text-xs.group-focus-within:h-1/2.group-focus-within:-translate-y-full.group-focus-within:pl-0
= so basically, we set font-size smaller, half height, -100% translate y and padding-left 0.peer
and peer-valid
utilities.group-focus-within
. And here's the demo.novalidate
attribute for <form>
tag. So, css will still work, but you will not see any error message at all.