mozilla

Revision 26231 of :invalid

  • Revision slug: CSS/:invalid
  • Revision title: :invalid
  • Revision id: 26231
  • Created:
  • Creator: teoli
  • Is current revision? No
  • Comment linkification + better wording; 4 words added, 7 words removed

Revision Content

{{ CSSRef() }}{{ gecko_minversion_header("2.0") }}

Summary

The :invalid pseudo-class represents any {{ HTMLElement("input") }} element whose content fail 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.

Browser compatibility

Browser Lowest version
Internet Explorer ---
Firefox (Gecko) 4.0 (2.0)
Opera 9.5

Safari | Chrome | WebKit

5 | 8 | 534.10

Specifications

See also

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

Revision Source

<p>{{ CSSRef() }}{{ gecko_minversion_header("2.0") }}</p>
<h3>Summary</h3>
<p>The <code>:invalid</code> <a href="/en/CSS/Pseudo-classes" title="Pseudo-classes">pseudo-class</a> represents any {{ HTMLElement("input") }} element whose content fail 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>
<h3>Notes</h3>
<h4>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>
<h3>Examples</h3>
<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>
<h3>Browser compatibility</h3>
<table class="standard-table"> <tbody> <tr> <th>Browser</th> <th>Lowest version</th> </tr> <tr> <td>Internet Explorer</td> <td>---</td> </tr> <tr> <td>Firefox (Gecko)</td> <td>4.0 (2.0)</td> </tr> <tr> <td>Opera</td> <td>9.5</td> </tr> <tr> <td> <p>Safari | Chrome | WebKit</p> </td> <td> <p>5 | 8 | 534.10</p> </td> </tr> </tbody>
</table>
<h3>Specifications</h3>
<ul> <li><a class=" external" href="http://www.w3.org/TR/css3-ui/#pseudo-validity" title="http://www.w3.org/TR/css3-ui/#pseudo-validity">CSS 3 Basic User Interface Module</a></li>
</ul>
<h3>See also</h3>
<ul> <li>{{ cssxref(":valid") }}</li> <li>{{ cssxref(":-moz-submit-invalid") }}</li> <li>{{ cssxref(":required") }}</li> <li>{{ cssxref(":optional") }}</li>
</ul>
Revert to this revision