Role of CSS: valid Selector

The CSS :valid selector is used to style form input elements that contain valid values according to their specified type or validation constraints. This pseudo-class selector automatically applies styles when the input meets the validation criteria.

Syntax

input:valid {
    /* styles for valid inputs */
}

Example: Email Validation Styling

The following example demonstrates how to style email inputs with valid email addresses −

<!DOCTYPE html>
<html>
<head>
<style>
    input[type="email"] {
        padding: 10px;
        border: 2px solid #ccc;
        border-radius: 4px;
        font-size: 16px;
        width: 300px;
    }
    
    input:valid {
        background-color: #d4edda;
        border-color: #28a745;
        color: #155724;
    }
    
    input:invalid {
        background-color: #f8d7da;
        border-color: #dc3545;
        color: #721c24;
    }
</style>
</head>
<body>
    <h2>Email Validation Example</h2>
    <form>
        <label for="email">Enter your email:</label><br>
        <input type="email" id="email" name="email" value="user@example.com">
        <p>Try typing different email addresses to see the validation styling.</p>
    </form>
</body>
</html>
An email input field appears with a green background and border, indicating the email "user@example.com" is valid. When you type an invalid email format, the field turns red.

Example: Multiple Input Types

The :valid selector works with various input types that have built-in validation −

<!DOCTYPE html>
<html>
<head>
<style>
    .form-group {
        margin: 15px 0;
    }
    
    input {
        padding: 8px;
        border: 2px solid #ddd;
        border-radius: 4px;
        width: 200px;
    }
    
    input:valid {
        border-color: #28a745;
        box-shadow: 0 0 5px rgba(40, 167, 69, 0.3);
    }
    
    input:invalid {
        border-color: #dc3545;
        box-shadow: 0 0 5px rgba(220, 53, 69, 0.3);
    }
</style>
</head>
<body>
    <h2>Form Validation Styling</h2>
    <form>
        <div class="form-group">
            <label>Email:</label><br>
            <input type="email" value="test@domain.com" required>
        </div>
        
        <div class="form-group">
            <label>URL:</label><br>
            <input type="url" value="https://example.com" required>
        </div>
        
        <div class="form-group">
            <label>Number (1-100):</label><br>
            <input type="number" min="1" max="100" value="50" required>
        </div>
    </form>
</body>
</html>
Three input fields appear with green borders and subtle green shadows, indicating all values are valid. The email shows a valid email format, URL shows a valid web address, and number shows a value within the specified range.

Conclusion

The :valid selector provides an excellent way to give immediate visual feedback for valid form inputs. It works automatically with HTML5 input types and validation attributes, making forms more user-friendly and accessible.

Updated on: 2026-03-15T12:25:34+05:30

227 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements