CSS Feature Queries

CSS Feature Queries, introduced in CSS3, enable developers to apply styles based on the presence of certain CSS features in the browser. This allows for targeted styling for browsers that support specific features, enhancing compatibility and providing fallbacks for older browsers.

Basic Concepts

Syntax

Feature queries start with '@supports' followed by the condition inside parentheses.

				
					@supports (display: grid) {
  /* CSS code for browsers that support CSS Grid */
}

				
			

Usage

Use feature queries to check if a browser supports a particular CSS property or value before applying styles.

				
					@supports (display: flex) {
  /* CSS code for browsers that support Flexbox */
}

				
			

Advanced Techniques

Complex Conditions

Combine multiple conditions within a feature query to target browsers that support a combination of CSS features.

				
					@supports (display: grid) and (grid-template-columns: auto) {
  /* CSS code for browsers that support CSS Grid with auto column sizing */
}

				
			

Negating Conditions

Use the 'not' keyword to apply styles when a certain feature is not supported.

				
					@supports not (display: grid) {
  /* CSS code for browsers that do not support CSS Grid */
}

				
			

Nested Feature Queries

Feature queries can be nested within other feature queries to create more complex conditions.

				
					@supports (display: grid) {
  @supports (grid-template-rows: 1fr) {
    /* CSS code for browsers that support CSS Grid with flexible row sizing */
  }
}

				
			

Logical Operators

Use logical operators (and, or, not) to create more sophisticated conditions within feature queries.

				
					@supports (display: grid) and (not (grid-auto-flow: dense)) {
  /* CSS code for browsers that support CSS Grid without dense auto-placement */
}

				
			

Example

				
					/* Apply styles for browsers that support CSS Grid */
@supports (display: grid) {
  .container {
    display: grid;
    grid-template-columns: 1fr 1fr;
  }
}

/* Fallback for browsers that do not support CSS Grid */
@supports not (display: grid) {
  .container {
    float: left;
    width: 50%;
  }
}

				
			

Considerations

  1. Granular Feature Detection: Be specific when targeting features within feature queries to ensure accurate detection and application of styles.
  2. Progressive Enhancement: Use feature queries to progressively enhance designs by applying advanced styles only when supported, while providing functional fallbacks for unsupported features.

CSS Feature Queries empower developers to create robust and adaptable stylesheets by selectively applying styles based on browser support for specific CSS features. By leveraging advanced techniques, developers can ensure graceful degradation in older browsers while embracing the full potential of modern CSS. Embracing CSS Feature Queries enables developers to craft more resilient and future-proof web experiences that cater to a diverse range of devices and browsers. Happy Coding! ❤️

Table of Contents