CSS Portal

:optional CSS Pseudo Class

If this site has been useful, we’d love your support! Consider buying us a coffee to keep things going strong!

Description

The :optional pseudo-class in CSS is used to select form elements that do not have the required attribute set. Essentially, it targets inputs, textareas, or select elements where the user is not strictly obligated to provide a value. This pseudo-class is particularly useful when styling forms differently based on whether a field is mandatory or optional, improving both usability and visual clarity.

Unlike general form element selectors, :optional only matches elements that are eligible to be filled but are not marked as required. For example, an <input> element without the required attribute will match :optional, while an <input required> will not. This pseudo-class complements :required, allowing developers to easily differentiate between mandatory and optional fields.

When combined with other pseudo-classes or attributes, :optional allows for nuanced form styling. For example, you might want optional fields to have a lighter border color or a subtle background shade to indicate that filling them is not compulsory. It can also be used with pseudo-classes like :focus to style optional fields when the user interacts with them, or with the border-color property to visually guide users through a form.

Here’s a simple example demonstrating its usage:

<form>
  <label for="username">Username (required):</label>
  <input type="text" id="username" name="username" required>

  <label for="nickname">Nickname (optional):</label>
  <input type="text" id="nickname" name="nickname">

  <button type="submit">Submit</button>
</form>
input:optional {
  border: 2px dashed #888;
  background-color: #f9f9f9;
}

input:required {
  border: 2px solid #f00;
}

In this example:

  • The username field does not match :optional because it is required, so it gets a solid red border.
  • The nickname field does match :optional, and it receives a dashed grey border with a light background, signaling it’s optional.

One key point is that :optional only applies to form elements that support being optional. Elements like <input type="hidden"> or buttons will never match this pseudo-class. Additionally, it can be combined with other selectors for more complex styling, such as form input:optional:focus to change the appearance of optional fields when they are active.

This pseudo-class improves form accessibility and visual cues, making it easier for users to understand which inputs are required versus optional. It pairs naturally with validation styling using pseudo-classes like :valid and :invalid, enabling a complete visual feedback system in forms.

Syntax

:optional {
  /* ... */
}

Example

<form>
<div class="field-group">
<label for="user">Username (Required):</label>
<input type="text" id="user" name="user" required>
</div>

<div class="field-group">
<label for="email">Email (Optional):</label>
<input type="email" id="email" name="email">
</div>

<button type="submit">Submit</button>
</form>
/* Styles applied only to optional inputs */
input:optional {
border: 2px dashed #999;
background-color: #fafafa;
padding: 8px;
border-radius: 4px;
}

/* For contrast: styles applied to required inputs */
input:required {
border: 2px solid #2ecc71;
padding: 8px;
border-radius: 4px;
}

/* General styling for the form layout */
.field-group {
margin-bottom: 15px;
display: flex;
flex-direction: column;
width: 300px;
}

label {
margin-bottom: 5px;
font-family: sans-serif;
font-size: 14px;
}

Browser Support

The following information will show you the current browser support for the CSS :optional pseudo class. Hover over a browser icon to see the version that first introduced support for this CSS psuedo class.

This psuedo class is supported by all modern browsers.
Desktop
Chrome
Edge
Firefox
Opera
Safari
Tablets & Mobile
Chrome Android
Firefox Android
Opera Android
Safari iOS
Samsung Internet
Android WebView
-

Last updated by CSSPortal on: 31st December 2025

If this site has been useful, we’d love your support! Consider buying us a coffee to keep things going strong!