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

:Where


The :where() pseudo-class is very similar to :is() in that it takes a selector list as an argument and matches any element that can be selected by one of the selectors in that list. The key difference is that :where() always has a specificity of 0. This makes it ideal for creating broad, overridable styles.


Example 1: Zero Specificity

/* This rule has zero specificity and can be easily overridden */
:where(h1, h2, h3) {
  color: gray;
}

h2 {
  color: blue; /* This will override the :where() rule */
}

Explanation

Even though the h2 selector is simpler, it will override the color set by :where() because :where() has no specificity. This is the primary advantage of using :where().


Example 2: Theming Defaults

/* Set default link colors with zero specificity */
:where(a:link, a:visited) {
  color: #007bff;
}

Explanation

This is a great way to set a default style for links that can be easily customized later without needing more complex selectors. Any other style rule for links will override this one.


Example 3: Reset Styles

/* A simple CSS reset with no specificity */
:where(body, h1, h2, p, ul, ol) {
  margin: 0;
  padding: 0;
}

Explanation

Using :where() in a CSS reset is a modern best practice. It allows developers to apply baseline styles that can be effortlessly overridden by their own component-specific styles without specificity conflicts.


Example 4: Form Element Defaults

/* Default styling for form buttons */
:where(button, input[type="submit"]) {
  font-family: inherit;
  font-size: 100%;
}

Explanation

This rule provides default typographic properties for buttons and submit inputs. Since it has zero specificity, you can easily style a specific button differently with a simple class or ID selector.


Example 5: Overridable Grid Layouts

/* A default grid layout that can be easily changed */
:where(.grid-container) {
  display: grid;
  gap: 1em;
}

Explanation

Here, a base grid layout is defined using :where(). If you need a different grid layout for a specific container, you can apply a class and define new grid properties, and they will apply without any specificity issues.


Example 6: Unstyled List

/* Creates an unstyled list that can be styled later */
:where(ul.unstyled, ol.unstyled) {
  list-style: none;
  padding-left: 0;
}

Explanation

This creates a utility class unstyled for lists. Because it uses :where(), you can easily apply other list styles to elements with this class, for instance, to add back padding for a specific nested list.


Example 7: Broad Component Styles

/* Default styles for all components with a 'card' class */
:where([class*="card"]) {
  border: 1px solid #ddd;
  border-radius: 8px;
}

Explanation

This rule applies a default border and border-radius to any element whose class attribute contains the word "card". The zero specificity of :where() ensures that more specific styling for different card variations will be applied without hassle.