You cannot style the message, but you can create your own "message" element.
Building on the example that you link to, you can add e.preventDefault()
to stop the default action of displaying the validity message, and then make your own element show up.
Update 2023
This is an extended example from the example I did in the first place. Now, there is a "real" message box and when you focus the form again the messages will disappear.
document.forms.form01.addEventListener("invalid", function(e){
e.preventDefault();
e.target.classList.add('invalid');
}, true);
document.forms.form01.addEventListener("focus", function(e){
e.preventDefault();
[...e.target.form.querySelectorAll('.invalid')]
.forEach(elm => elm.classList.remove('invalid'));
}, true);
label {
position: relative;
}
div.feedback {
border: solid thin red;
position: absolute;
left: 10%;
padding: .2em;
background-image: linear-gradient(180deg, #fff, #ddd 40%, #ccc);
display: none;
}
input.invalid~div.feedback {
display: block;
}
input:invalid {
border: thin solid red;
}
<form name="form01">
<label>
<span>Name</span>
<input name="name" placeholder="Enter Your Name" type="text" required autocomplete="off">
<div class="feedback">This needs to be a name</div>
</label>
<label>
<span>Email</span>
<input name="email" placeholder="Enter Your Email" type="email" required autocomplete="off">
<div class="feedback">This needs to be an email</div>
</label>
<button>Submit</button>
</form>