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

:Invalid


The :invalid pseudo-class selects form elements whose content does not validate against their type or constraints. This is crucial for creating user-friendly forms that guide users in correcting their mistakes.


Example 1: Red Border for Invalid Input

/* This CSS rule targets an input with invalid content. */
input:invalid {
  /* The border-color is set to red. */
  border-color: red;
}

Explanation

This is a common and effective way to show that a field is invalid. The red border immediately draws the user's attention to the field that needs correction.


Example 2: Displaying an Error Message

/* This CSS rule targets a span with the .error-message class next to an invalid input. */
input:invalid ~ .error-message {
  /* The display property is set to block to show the message. */
  display: block;
  /* The color is set to red. */
  color: red;
}

Explanation

This example shows an error message (which is initially hidden) when an input field is invalid. The general sibling combinator (~) is used to select the error message element.


Example 3: Shaking Animation for Invalid Fields

/* This defines a keyframe animation for shaking. */
@keyframes shake {
  0%, 100% { transform: translateX(0); }
  25% { transform: translateX(-5px); }
  75% { transform: translateX(5px); }
}

/* This applies the animation to invalid inputs. */
input:invalid {
  animation: shake 0.2s;
}

Explanation

This more advanced example uses a CSS animation to make an invalid input field "shake" when the user input is incorrect, providing a very noticeable cue for correction.


Example 4: Invalid Input with an Icon

/* This CSS positions a pseudo-element after an invalid input. */
input:invalid + .invalid-icon::after {
  /* The content property adds a cross character. */
  content: '✗';
  /* The color is set to red. */
  color: red;
}

Explanation

Similar to the valid icon example, this displays a red "X" next to an input field when its content is invalid, offering immediate and clear feedback.


Example 5: Disabling Submit Button When a Field is Invalid

/* This CSS rule disables the submit button if any required field is invalid. */
form:invalid input[type="submit"] {
  opacity: 0.5;
  pointer-events: none;
}

Explanation

This powerful technique prevents a form from being submitted if any of its required fields are invalid. It works by targeting the submit button within a form that is in an :invalid state.