- CSS Tutorial
- CSS - Home
- CSS - Introduction
- CSS - Syntax
- CSS - Selectors
- CSS - Inclusion
- CSS - Measurement Units
- CSS - Colors
- CSS - Backgrounds
- CSS - Fonts
- CSS - Text
- CSS - Images
- CSS - Links
- CSS - Tables
- CSS - Borders
- CSS - Border Block
- CSS - Border Inline
- CSS - Margins
- CSS - Lists
- CSS - Padding
- CSS - Cursor
- CSS - Outlines
- CSS - Dimension
- CSS - Scrollbars
- CSS - Inline Block
- CSS - Dropdowns
- CSS - Visibility
- CSS - Overflow
- CSS - Clearfix
- CSS - Float
- CSS - Arrows
- CSS - Resize
- CSS - Quotes
- CSS - Order
- CSS - Position
- CSS - Hyphens
- CSS - Hover
- CSS - Display
- CSS - Focus
- CSS - Zoom
- CSS - Translate
- CSS - Height
- CSS - Hyphenate Character
- CSS - Width
- CSS - Opacity
- CSS - Z-Index
- CSS - Bottom
- CSS - Navbar
- CSS - Overlay
- CSS - Forms
- CSS - Align
- CSS - Icons
- CSS - Image Gallery
- CSS - Comments
- CSS - Loaders
- CSS - Attr Selectors
- CSS - Combinators
- CSS - Root
- CSS - Box Model
- CSS - Counters
- CSS - Clip
- CSS - Writing Mode
- CSS - Unicode-bidi
- CSS - min-content
- CSS - All
- CSS - Inset
- CSS - Isolation
- CSS - Overscroll
- CSS - Justify Items
- CSS - Justify Self
- CSS - Tab Size
- CSS - Pointer Events
- CSS - Place Content
- CSS - Place Items
- CSS - Place Self
- CSS - Max Block Size
- CSS - Min Block Size
- CSS - Mix Blend Mode
- CSS - Max Inline Size
- CSS - Min Inline Size
- CSS - Offset
- CSS - Accent Color
- CSS - User Select
- CSS Advanced
- CSS - Grid
- CSS - Grid Layout
- CSS - Flexbox
- CSS - Visibility
- CSS - Positioning
- CSS - Layers
- CSS - Pseudo Classes
- CSS - Pseudo Elements
- CSS - @ Rules
- CSS - Text Effects
- CSS - Paged Media
- CSS - Printing
- CSS - Layouts
- CSS - Validations
- CSS - Image Sprites
- CSS - Important
- CSS - Data Types
- CSS3 Tutorial
- CSS3 - Tutorial
- CSS - Rounded Corner
- CSS - Border Images
- CSS - Multi Background
- CSS - Color
- CSS - Gradients
- CSS - Box Shadow
- CSS - Box Decoration Break
- CSS - Caret Color
- CSS - Text Shadow
- CSS - Text
- CSS - 2d transform
- CSS - 3d transform
- CSS - Transition
- CSS - Animation
- CSS - Multi columns
- CSS - Box Sizing
- CSS - Tooltips
- CSS - Buttons
- CSS - Pagination
- CSS - Variables
- CSS - Media Queries
- CSS - Functions
- CSS - Math Functions
- CSS - Masking
- CSS - Shapes
- CSS - Style Images
- CSS - Specificity
- CSS - Custom Properties
- CSS Responsive
- CSS RWD - Introduction
- CSS RWD - Viewport
- CSS RWD - Grid View
- CSS RWD - Media Queries
- CSS RWD - Images
- CSS RWD - Videos
- CSS RWD - Frameworks
- CSS References
- CSS - Questions and Answers
- CSS - Quick Guide
- CSS - References
- CSS - Color References
- CSS - Web browser References
- CSS - Web safe fonts
- CSS - Units
- CSS - Animation
- CSS Resources
- CSS - Useful Resources
- CSS - Discussion
CSS - Pseudo-class :invalid
The CSS pseudo-class selector :invalid represents an element (such as <form>, <fieldset>, or <input>) whose contents or value is invalid as per its type specified in the type attribute, or fail to validate.
For instance, email inputs, <input type="email"> whose value does not match a valid email pattern or a number input <input type="number"> having a value with alphabets. Both these cases are invalid.
The pseudo-class :invalid is useful in indicating the field errors to the user.
Any style applied using :out-of-range pseudo-class, overrides the styles applied using :invalid pseudo-class. Works for <input type="number">.
:invalid pseudo-class can be used with other selectors, such as :hover, :focus, etc.
In case of radio buttons, if one of them is required, the :invalid pseudo-class is applied to all the radio buttons.
Gecko does not apply a style to the :invalid pseudo-class. However, Gecko does apply a style to :user-invalid pseudo-class.
Accessibility concerns: For an invalid input, the default color used is red, which might be difficult for people with certain color blindness state. Instead it is advisable to add an icon or decriptive text along with the invalid input, so that it is easier to understand.
Syntax
:invalid { /* ... */ }
CSS:invalid Example
Following example demonstartes use of :invalid pseudo-class used on input field. Here we see when the data in the field is invalid, either not in the specified range or incorrect, the background color changes.
<html> <head> <style> .container { margin: 40px auto; max-width: 700px; } p { font-size: 1.5em; } input { display: block; width: 50%; height: 2em; background-color: lightgray; } input:invalid { background-color: tomato; } input[type="number"]:invalid { background-color: orange; } input:invalid:focus { outline: 0; border: none; box-shadow: 0 0 5px 5px rgba(0, 136, 255, 0.3); } </style> </head> <body> <div class="container"> <p> Input type is number ranging between 1 and 10. </p> <input type="number" min="1" max="10" value="2"> <p> Input type is email, which is required. If empty, its invalid. </p> <input type="email" required> </div> </body> </html>
In the above example:
Two input fields are provided, one of type number and the other of email type.
The input field with number, provided CSS styling, such that if the value is not a number and not in the range of 1 to 10, it is marked as invalid.
The email input field is marked as invalid, if its not following the email pattern or empty.
Another pseudo-class :focus is used along with :invalid, such that when an invalid field has a focus, the CSS style is applied.
Following example demonstartes use of :invalid pseudo-class used on fieldset. The visibility of input field and button is kept hidden, till the input is either empty or incorrect.
<html> <head> <style> /* Set the visibility to hidden for an invalid fieldset */ fieldset:invalid ~ fieldset { visibility: hidden; } /* Disable the button while the fieldsets have invalid value */ form:invalid button { background-color: darkgrey; opacity: 0.2; } input, textarea { box-sizing: border-box; width: 500px; font-family: monospace; padding: 0.25em 0.5em; } button { width: 200px; border: thin solid darkgrey; font-size: 1.25em; background-color: green; color: white; position:relative; } </style> </head> <body> <form> <fieldset> <label for="userid">UserId</label><br /> <input type="text" name="userid" id="userid" required /> </fieldset> <fieldset> <label for="email">Email</label><br /> <input type="email" name="email" id="email" required /> </fieldset> <button type="submit" name="send">Submit</button> </form> </body> </html>
In the above example:
There are two fieldsets, each with one label and input element, one for user id and the other email.
The second fieldset remains hidden, until a valid value is not passed in the first one.
Till both the fieldsets have valid values, the button remains disabled.
The input elements are set to be required, so the user needs to enter a valid input, only then the rest of the form can be seen and acted upon.
To Continue Learning Please Login
Login with Google