Skip to content
Advertisement

How to add red required text to an input when the field has not been filled out

I would like to add a red required text on the right side of a text input. I would want it to look similar to this. An email field with the required tag.

I apologize for not have code to add, the only things ive tried have been messing with placeholders as i have no idea which direction to go for this. Any help is much appreciated.

Advertisement

Answer

You may want to consider something like this.

input {
  padding: 1em;
  width: 250px;
}
/* Position label on top of input field */
label {
  margin-left: -80px;
  position: relative;
  z-index: 2;
}
/* Show red label when input invalid */
input:required:invalid+label {
  color: red;
}
/* Don't display label when input is valid */
input:required:valid+label {
  display: none;
}
<input type="email" id="email_input" placeholder="Email" required>
<label for="email_input">Required</label>
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement