Highlight Required Input Fields using CSS

In this tutorial we will see how to Highlight Required Input Fields using CSS. :required CSS Selector is used for this purpose.

:required Selector

The :required selector selects HTML form elements with required attribute. The :required selector only applies to the html form elements like input, select and textarea.

HTML Code

Simple HTML Code is given below, In this code both username and password are required elements while email is optional.

<form>
<label>Email:</label>    
<input type="email">
<label>UserName:</label>    
<input type="text" required>
<label>Password:</label>    
<input type="password" required>
<input type="submit">    
</form>

CSS Code

CSS code is given below, :required selector is used to change the border color of required elements while :optional selector is used for 1 optional input element.

input
{
display: block;
margin: 5px 0px;
}
:required
{
border: 1px solid #FF305D;    
}
:optional
{
border: 1px solid #3075FF;    
}

Demo

Video Tutorial

Watch video tutorial on Highlight Required Input Fields using CSS.

Social Media Menu with Popup Tooltip using HTML and CSS Horizontal Navigation Menu with HTML and CSS Responsive Sidebar Menu with CSS and JavaScript Cool CSS Button with Hover Animation Pure CSS Custom Radio Buttons Custom Emoji Radio Buttons with CSS Color Picker Design with HTML CSS and JavaScript Custom Radio Buttons with CSS Engraved HTML Buttons With CSS Neumorphic Buttons Design with CSS Cool Profile Card Design with HTML CSS Neumorphic Search Bar Design with HTML and CSS Simple Login Form with Placeholder Animation Pure CSS Button with Folded Corner Background Color Change Slider with HTML CSS and JavaScript 404 Page Not Found Template Design with HTML and CSS Movies Blog Card Design with HTML and CSS Simple CSS Border Animation with before and after Selector Custom Social Select Dropdown Menu with CSS and JavaScript Facebook Offline and Online Notification with CSS and JS New Year 2022 Text Animation with CSS and JS Change Placeholder Color using CSS Custom Select Tag DropDown with CSS and JavaScript Simple Price Range Slider With HTML CSS and JavaScript Profile Card With Hover Effect using HTML and CSS Circle With Two Colors using CSS Simple FAQ Accordion With HTML CSS and JavaScript