CSS

CSS Overview Selectors Declarations Rule Sets Comments Reset/Normalize Origin Importance Order Specificity Inheritance Cascade Layers Inline Styles Internal Styles External Styles @import Absolute Units Relative Units Dynamic Viewport Angle Units Time Units Frequency Units: Resolution Units Keywords Color Formats Color Functions & Spaces Background Color Background Image Background Repeat Background Position Background Size Background Attachment Background Shorthand Multiple Backgrounds Linear Gradients Radial Gradients Conic Gradients Font Family Font Size Font Weight Font Style Line Height Text Align Text Decoration Text Transform Letter Spacing Word Spacing Text Shadow Text Wrap Variable Fonts Content Box Padding Box Border Box Margin Box Box Sizing Property Margin Padding Border Outline Box Shadow Block Display Inline Display Inline Block Display Static Position Relative Position Absolute Position Fixed Position Sticky Position Top Element Position Right Element Position Bottom Element Position Left Element Position Z Index Flexbox Box Layout Display Flex Flex Direction Flex Wrap Justify Content Align Items Align Content Gap, Row Gap & Column Gap Flex Grow Flex Shrink Flex Basis Flex Shorthand Order Property Align Self Practical Applications CSS Grid Layout Grid Display Grid Template Columns Grid Template Rows Grid Template Areas Justify Items Grid Auto Columns Justify Self Nested Grids Responsive Web Design Min Width Max Width Orientation Logical Operators Pointer Hover Prefers Color Scheme Fluid Images Flexible Typography Viewport Width Advanced CSS Features Defining variables Using variables Scope & Inheritance of CSS Variables Property Value Fallback Practical Applications :Hover :Active :Focus :Focus Visible :Visited :Link :First Child :Last Child :Nth Child :Nth of Type :Empty :Checked :Disabled :Enabled :Valid :Invalid :Required :Has :Not :Is :Where ::Before Pseudo Element ::After Pseudo Element ::First Letter ::First Line ::Selection ::Marker CSS 2D Transformations CSS 3D Transformations Transform Origin Transform Style Backface Visibility Transition Property Transition Duration Transition Timing Function Transition Delay Transition Shorthand Ease Timing Function Linear Timing Function Ease In Timing Function Ease Out Timing Function Ease In Out Timing Function Cubic Bezier Function Animations (@keyframes) Defining @keyframes Animation Name CSS Animation Duration Animation Timing Function CSS Animation Delay CSS Animation Iteration Count Animations Direction CSS Animation Fill Mode CSS Animation Play State CSS Filter Blur CSS Filter Brightness CSS Filter Contrast() CSS Drop Shadow CSS Grayscale CSS Hue Rotate CSS Invert CSS Opacity CSS Saturate CSS Sepia Mix Blend Mode Background Blend Mode Object Fit & Object Position Scroll Snap Type Scroll Snap Align Scroll Padding Scroll Margin Scroll Triggered Animations JS Variables

:Required


The :required pseudo-class selects form elements that have the required attribute. It allows you to visually distinguish fields that must be filled out from optional ones.


Example 1: Asterisk for Required Fields

/* This CSS adds a red asterisk after the label of a required input. */
label.required::after {
  content: ' *';
  color: red;
}

Explanation

A common convention is to mark required fields with an asterisk. This CSS automatically adds a red asterisk to the content of any label with the class required.


Example 2: Highlighting Required Empty Fields

/* This CSS rule targets required inputs that are empty. */
input:required:placeholder-shown {
  /* A yellow border indicates a required but unfilled field. */
  border: 1px solid gold;
}

Explanation

This code uses the :placeholder-shown pseudo-class in conjunction with :required to highlight required fields that have not yet been filled out by the user.


Example 3: Required Field Background Color

/* This CSS rule sets a light blue background for all required fields. */
input:required, textarea:required {
  background-color: #eaf6ff;
}

Explanation

This CSS helps users quickly identify all the mandatory fields on a form by giving them a distinct background color.


Example 4: Bold Labels for Required Fields

/* This CSS makes the label text of a required input bold. */
input:required + label {
  font-weight: bold;
}

Explanation

This example makes the labels of required fields bold, providing a clear visual distinction from the labels of optional fields. The adjacent sibling selector targets the label immediately following the required input.


Example 5: Custom Outline on Focused Required Fields

/* This CSS provides a custom outline when a required field is focused. */
input:required:focus {
  /* A blue outline is applied on focus. */
  outline: 2px solid #007bff;
  outline-offset: 2px;
}

Explanation

This CSS enhances the focus state for required fields. When a user tabs into or clicks on a required input, a prominent blue outline appears, guiding their focus.