I am working in Google Chrome at the moment, which is important.
I have some CSS and jQuery that create cute 'placeholders' (actually they're labels but they start off looking like placeholders) that animate into labels. Here is the codepen https://codepen.io/humanhickory/pen/KKPjgLe
//HTML
<div class="form-group col-lg-6 field">
<label for="FirstName" class="col-12">Date without Required tag</label>
<input type="date" class="form-control col-12"/>
</div>
<div class="form-group col-lg-6 field">
<label for="FirstName" class="col-12">Date With Required Tag</label>
<input type="date" class="form-control col-12" required/>
</div>
//CSS
.field > input,
.field > label {
position: relative;
display: inline-block;
width: 100%;
transition: all 0.3s ease-in-out; /* adding animation */
}
.field > label {
display: inline-block;
margin: 0 auto;
padding: 0 0 5px 7px;
top: 36px;
z-index: 1; /* placing label behind input */
}
form input[type="text"],
form input[type="email"],
form input[type="date"] {
font-size: 1em;
padding: 0 0 0 7px;
z-index: 10;
background: transparent; /* adding transparency */
}
.filled label {
top: 0;
font-size: 0.8em;
}
.form-group {
margin-bottom: .5rem !important;
}
input[type="date"]::-webkit-datetime-edit {
color: transparent;
}
input[type="date"]:focus::-webkit-datetime-edit {
color: black !important;
}
input[type="date"]:valid::-webkit-datetime-edit {
color: black;
}
//JQUERY
$("form input").on("blur input focus", function() {
var $field = $(this).closest(".field");
if (this.value) {
$field.addClass("filled");
} else {
$field.removeClass("filled");
}
});
$("form input").on("focus", function() {
var $field = $(this).closest(".field");
if (this) {
$field.addClass("filled");
} else {
$field.removeClass("filled");
}
});
$('.field > select').on("focus", function () {
var $field = $(this).closest(".field");
var $select = $(this).closest("select");
if (this) {
$field.addClass("filled");
$select.removeClass("hiddenText");
} else {
$field.removeClass("filled");
}
});
So all of the input types work perfectly, except for 'date'. If I don't put the required tag on a date input, it overlaps and looks awful. However, I can't find anything in my css or jquery that would cause this to happen. Thoughts?
I've changed the targeted classes and that did nothing. I've added !important tags to things and it's made it worse. I really have no idea what could cause this.
Bonus points if you know how to make it work at all on Edge/other browsers. However, I haven't spent much time trying to get this part to work, so I'm less worried about it.