Revision 26239 of :invalid

  • Revision slug: CSS/:invalid
  • Revision title: :invalid
  • Revision id: 26239
  • Created:
  • Creator: FreakCERS
  • Is current revision? No
  • Comment Add compatibility support for IE; 1 words added, 4 words removed

Revision Content

{{ CSSRef() }}

Summary

The :invalid pseudo-class represents any {{ HTMLElement("input") }} or {{ HTMLElement("form") }} element whose content fails to validate according to the input's type setting. This allows you to easily have invalid fields adopt an appearance that helps the user identify and correct errors.

By default, Gecko does not apply a style to the :invalid pseudo-class. However it does apply a style (a red "glow" using the {{ Cssxref("box-shadow") }} property) to the {{ Cssxref(":-moz-ui-invalid") }} pseudo-class, which applies in a subset of cases for :invalid.

You can disable the glow using the following CSS, or completely override it to alter the appearance of invalid fields.

:invalid {
  box-shadow: none;
}

:-moz-submit-invalid {
  box-shadow: none;
}

:-moz-ui-invalid {
  box-shadow:none;
}

Notes

Radio buttons

If any of the radio buttons in a group (that is, with the same value for their name attribute) is required, the :invalid pseudo-class is applied to all of them if none of the buttons in the group are selected.

Examples

This example presents a simple form that colors elements green when they validate and red when they don't.

<style type="text/css">
  :invalid {
    background-color: #ffdddd;
  }
  
  :valid {
    background-color: #ddffdd;
  }
  
  :required {
    border-color: #800000;
    border-width: 3px;
  }
</style>

<form>
  <label>Enter a URL:</label>
  <input type="url" />
  <br />
  <br />
  <label>Enter an email address:</label>
  <input type="email" required/>
</form>

View this sample live.

Specifications

Specification Status Comment
CSS Basic User Interface Module Level 3 {{ Spec2('CSS3 UI') }}  

Browser compatibility

{{ CompatibilityTable() }}

Feature Chrome Firefox (Gecko) Internet Explorer Opera Safari
Basic support ({{ HTMLElement("input") }}) 10.0 {{ CompatGeckoDesktop("2") }} 10 10.0 5.0
Apply it to {{ HTMLElement("form") }} {{ CompatUnknown() }} {{ CompatGeckoDesktop("13") }} {{ CompatUnknown() }} {{ CompatUnknown() }} {{ CompatUnknown() }}
Feature Android Firefox Mobile (Gecko) IE Mobile Opera Mobile Safari Mobile
Basic support {{ CompatUnknown() }} {{ CompatGeckoMobile("2") }} {{ CompatNo() }} 10.0 5.0
Apply it to {{ HTMLElement("form") }} {{ CompatUnknown() }} {{ CompatGeckoMobile("13") }} {{ CompatUnknown() }} {{ CompatUnknown() }} {{ CompatUnknown() }}

See also

  • {{ cssxref(":valid") }}
  • {{ cssxref(":-moz-submit-invalid") }}
  • {{ cssxref(":required") }}
  • {{ cssxref(":optional") }}

{{ languages( { "fr": "fr/CSS/:invalid" }) }}

Revision Source

<p>{{ CSSRef() }}</p>
<h2 id="Summary">Summary</h2>
<p>The <code>:invalid</code> <a href="/en/CSS/Pseudo-classes" title="Pseudo-classes">pseudo-class</a> represents any {{ HTMLElement("input") }} or {{ HTMLElement("form") }} element whose content fails to <a href="/en/HTML/HTML5/Constraint_validation" title="en/HTML/HTML5/Constraint_validation">validate</a> according to the input's type setting. This allows you to easily have invalid fields adopt an appearance that helps the user identify and correct errors.</p>
<p>By default, Gecko does not apply a style to the <code>:invalid</code> pseudo-class. However it does apply a style (a red "glow" using the {{ Cssxref("box-shadow") }} property) to the {{ Cssxref(":-moz-ui-invalid") }} pseudo-class, which applies in a subset of cases for <code>:invalid</code>.</p>
<p>You can disable the glow using the following CSS, or completely override it to alter the appearance of invalid fields.</p>
<pre class="brush: css">:invalid {
  box-shadow: none;
}

<span id="the-code">:-moz-submit-invalid</span> {
  box-shadow: none;
}

:-moz-ui-invalid {
  box-shadow:none;
}
</pre>
<h2 id="Notes">Notes</h2>
<h4 id="Radio_buttons">Radio buttons</h4>
<p>If any of the radio buttons in a group (that is, with the same value for their <code>name</code> attribute) is <code>required</code>, the <code>:invalid</code> pseudo-class is applied to all of them if none of the buttons in the group are selected.</p>
<h2 id="Examples">Examples</h2>
<p>This example presents a simple form that colors elements green when they validate and red when they don't.</p>
<pre class="brush: html">&lt;style type="text/css"&gt;
  :invalid {
    background-color: #ffdddd;
  }
  
  :valid {
    background-color: #ddffdd;
  }
  
  :required {
    border-color: #800000;
    border-width: 3px;
  }
&lt;/style&gt;

&lt;form&gt;
  &lt;label&gt;Enter a URL:&lt;/label&gt;
  &lt;input type="url" /&gt;
  &lt;br /&gt;
  &lt;br /&gt;
  &lt;label&gt;Enter an email address:&lt;/label&gt;
  &lt;input type="email" required/&gt;
&lt;/form&gt;
</pre>
<p><a href="/samples/cssref/input-validation.html" title="https://developer.mozilla.org/samples/cssref/input-validation.html">View this sample live</a>.</p>
<h2 id="Specifications" name="Specifications">Specifications</h2>
<table class="standard-table"> <thead> <tr style="background-color: rgb(255, 204, 255);"> <th scope="col">Specification</th> <th scope="col">Status</th> <th scope="col">Comment</th> </tr> </thead> <tbody> <tr> <td><a class="external" href="http://dev.w3.org/csswg/css3-ui/#pseudo-validity" title="http://dev.w3.org/csswg/css3-ui/#pseudo-validity">CSS Basic User Interface Module Level 3</a></td> <td>{{ Spec2('CSS3 UI') }}</td> <td> </td> </tr> </tbody>
</table>
<h2 id="Browser_compatibility">Browser compatibility</h2>
<p>{{ CompatibilityTable() }}</p>
<div id="compat-desktop"> <table class="compat-table"> <tbody> <tr> <th>Feature</th> <th>Chrome</th> <th>Firefox (Gecko)</th> <th>Internet Explorer</th> <th>Opera</th> <th>Safari</th> </tr> <tr> <td>Basic support ({{ HTMLElement("input") }})</td> <td>10.0</td> <td>{{ CompatGeckoDesktop("2") }}</td> <td>10</td> <td>10.0</td> <td>5.0</td> </tr> <tr> <td>Apply it to {{ HTMLElement("form") }}</td> <td>{{ CompatUnknown() }}</td> <td>{{ CompatGeckoDesktop("13") }}</td> <td>{{ CompatUnknown() }}</td> <td>{{ CompatUnknown() }}</td> <td>{{ CompatUnknown() }}</td> </tr> </tbody> </table>
</div>
<div id="compat-mobile"> <table class="compat-table"> <tbody> <tr> <th>Feature</th> <th>Android</th> <th>Firefox Mobile (Gecko)</th> <th>IE Mobile</th> <th>Opera Mobile</th> <th>Safari Mobile</th> </tr> <tr> <td>Basic support</td> <td>{{ CompatUnknown() }}</td> <td>{{ CompatGeckoMobile("2") }}</td> <td>{{ CompatNo() }}</td> <td>10.0</td> <td>5.0</td> </tr> <tr> <td>Apply it to {{ HTMLElement("form") }}</td> <td>{{ CompatUnknown() }}</td> <td>{{ CompatGeckoMobile("13") }}</td> <td>{{ CompatUnknown() }}</td> <td>{{ CompatUnknown() }}</td> <td>{{ CompatUnknown() }}</td> </tr> </tbody> </table>
</div>
<h2 id="See_also">See also</h2>
<ul> <li>{{ cssxref(":valid") }}</li> <li>{{ cssxref(":-moz-submit-invalid") }}</li> <li>{{ cssxref(":required") }}</li> <li>{{ cssxref(":optional") }}</li>
</ul>
<p>{{ languages( { "fr": "fr/CSS/:invalid" }) }}</p>
Revert to this revision