:invalid

  • Revision slug: CSS/:invalid
  • Revision title: :invalid
  • Revision id: 334491
  • Created:
  • Creator: Inkbug
  • Is current revision? No
  • Comment Fixing live sample

Revision Content

{{ CSSRef() }}

Summary

The :invalid CSS 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.

Example

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

HTML Content

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

CSS Content

:invalid {
  background-color: #ffdddd;
}
  
:valid {
  background-color: #ddffdd;
}
  
:required {
  border-color: #800000;
  border-width: 3px;
}

{{ EmbedLiveSample('Example') }}

Specifications

Specification Status Comment
{{ SpecName('HTML WHATWG', '#selector-invalid', ':invalid') }} {{ Spec2('HTML WHATWG') }} No change.
{{ SpecName('HTML5 W3C', '#selector-invalid', ':invalid') }} {{ Spec2('HTML5 W3C') }} Defines the semantic regarding HTML and constraint validation.
{{ SpecName('CSS4 Selectors', '#validity-pseudos', ':invalid') }} {{ Spec2('CSS4 Selectors') }} No change.
{{ SpecName('CSS3 UI', '#pseudo-validity', ':invalid') }} {{ Spec2('CSS3 UI') }} Defines the pseudo-class, but not the associated semantic.

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> CSS <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 {
&nbsp; box-shadow: none;
}

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

:-moz-ui-invalid&nbsp;{
  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="Example" name="Example">Example</h2>
<p>This example presents a simple form that colors elements green when they validate and red when they don't.</p>
<h3 id="HTML_Content">HTML Content</h3>
<pre class="brush: html">
&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>
<h3 id="CSS_Content">CSS Content</h3>
<pre class="brush: css">
:invalid {
  background-color: #ffdddd;
}
  
:valid {
  background-color: #ddffdd;
}
  
:required {
  border-color: #800000;
  border-width: 3px;
}</pre>
<p>{{ EmbedLiveSample('Example') }}</p>
<h2 id="Specifications">Specifications</h2>
<table class="standard-table">
  <thead>
    <tr>
      <th scope="col">Specification</th>
      <th scope="col">Status</th>
      <th scope="col">Comment</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>{{ SpecName('HTML WHATWG', '#selector-invalid', ':invalid') }}</td>
      <td>{{ Spec2('HTML WHATWG') }}</td>
      <td>No change.</td>
    </tr>
    <tr>
      <td>{{ SpecName('HTML5 W3C', '#selector-invalid', ':invalid') }}</td>
      <td>{{ Spec2('HTML5 W3C') }}</td>
      <td>Defines the semantic regarding HTML and constraint validation.</td>
    </tr>
    <tr>
      <td>{{ SpecName('CSS4 Selectors', '#validity-pseudos', ':invalid') }}</td>
      <td>{{ Spec2('CSS4 Selectors') }}</td>
      <td>No change.</td>
    </tr>
    <tr>
      <td>{{ SpecName('CSS3 UI', '#pseudo-validity', ':invalid') }}</td>
      <td>{{ Spec2('CSS3 UI') }}</td>
      <td>Defines the pseudo-class, but not the associated semantic.</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