CSS

CSS Overview Selectors Declarations Rule Sets Comments Reset/Normalize Origin Importance Order Specificity Inheritance Cascade Layers Inline Styles Internal Styles External Styles @import Absolute Units Relative Units Dynamic Viewport Angle Units Time Units Frequency Units: Resolution Units Keywords Color Formats Color Functions & Spaces Background Color Background Image Background Repeat Background Position Background Size Background Attachment Background Shorthand Multiple Backgrounds Linear Gradients Radial Gradients Conic Gradients Font Family Font Size Font Weight Font Style Line Height Text Align Text Decoration Text Transform Letter Spacing Word Spacing Text Shadow Text Wrap Variable Fonts Content Box Padding Box Border Box Margin Box Box Sizing Property Margin Padding Border Outline Box Shadow Block Display Inline Display Inline Block Display Static Position Relative Position Absolute Position Fixed Position Sticky Position Top Element Position Right Element Position Bottom Element Position Left Element Position Z Index Flexbox Box Layout Display Flex Flex Direction Flex Wrap Justify Content Align Items Align Content Gap, Row Gap & Column Gap Flex Grow Flex Shrink Flex Basis Flex Shorthand Order Property Align Self Practical Applications CSS Grid Layout Grid Display Grid Template Columns Grid Template Rows Grid Template Areas Justify Items Grid Auto Columns Justify Self Nested Grids Responsive Web Design Min Width Max Width Orientation Logical Operators Pointer Hover Prefers Color Scheme Fluid Images Flexible Typography Viewport Width Advanced CSS Features Defining variables Using variables Scope & Inheritance of CSS Variables Property Value Fallback Practical Applications :Hover :Active :Focus :Focus Visible :Visited :Link :First Child :Last Child :Nth Child :Nth of Type :Empty :Checked :Disabled :Enabled :Valid :Invalid :Required :Has :Not :Is :Where ::Before Pseudo Element ::After Pseudo Element ::First Letter ::First Line ::Selection ::Marker CSS 2D Transformations CSS 3D Transformations Transform Origin Transform Style Backface Visibility Transition Property Transition Duration Transition Timing Function Transition Delay Transition Shorthand Ease Timing Function Linear Timing Function Ease In Timing Function Ease Out Timing Function Ease In Out Timing Function Cubic Bezier Function Animations (@keyframes) Defining @keyframes Animation Name CSS Animation Duration Animation Timing Function CSS Animation Delay CSS Animation Iteration Count Animations Direction CSS Animation Fill Mode CSS Animation Play State CSS Filter Blur CSS Filter Brightness CSS Filter Contrast() CSS Drop Shadow CSS Grayscale CSS Hue Rotate CSS Invert CSS Opacity CSS Saturate CSS Sepia Mix Blend Mode Background Blend Mode Object Fit & Object Position Scroll Snap Type Scroll Snap Align Scroll Padding Scroll Margin Scroll Triggered Animations JS Variables

:Valid


The :valid pseudo-class selects form elements whose content validates according to their type and other constraints, such as the pattern or required attributes. It is useful for providing immediate feedback on correct user input.


Example 1: Green Border for Valid Email Input

/* This CSS rule targets an email input with valid content. */
input[type="email"]:valid {
  /* The border-color is set to green. */
  border-color: green;
}

Explanation

When a user enters a correctly formatted email address into an input with type="email", this CSS will apply a green border, indicating the input is valid.


Example 2: Showing a "Valid" Icon

/* This CSS rule positions a pseudo-element after a valid input. */
input:valid + .valid-icon::after {
  /* The content property adds a checkmark character. */
  content: '✓';
  /* The color property sets the checkmark color to green. */
  color: green;
}

Explanation

This example uses a pseudo-element to display a green checkmark next to a form field as soon as its content becomes valid, offering clear, positive reinforcement.


Example 3: Styling the Label of a Valid Input

/* This CSS rule targets the label of a valid input. */
input:valid + label {
  /* The color property is set to green. */
  color: green;
}

Explanation

This code changes the color of a label to green when the associated input field contains valid data. This provides another layer of visual feedback for the user.


Example 4: Valid Password Confirmation

/* This CSS rule targets a password input that meets a pattern requirement. */
input[type="password"]:valid {
  /* A subtle green box-shadow is applied. */
  box-shadow: 0 0 5px 1px rgba(0, 255, 0, 0.5);
}

Explanation

If a password field has a pattern attribute (e.g., requiring a certain length and character types), this CSS will apply a green glow when the entered password is valid.


Example 5: Valid URL Input Highlighting

/* This CSS rule targets a URL input with valid content. */
input[type="url"]:valid {
  /* The background-color is changed to a very light green. */
  background-color: #e8f5e9;
}

Explanation

This CSS provides a subtle background color change for a URL input field once a valid URL format is entered.