CSS Best Practices

Cross-Browser Compatibility

Ensure consistent experiences across all browsers with progressive enhancement and smart fallbacks.

How do I make CSS work in all browsers?

Check support on caniuse.com or MDN before using a new feature, let Autoprefixer add vendor prefixes, and use @supports to give older browsers a simpler fallback. Test in Chrome, Firefox and Safari, especially iOS Safari, which has the most quirks.

.grid { display: flex; flex-wrap: wrap; }         /* fallback */
@supports (display: grid) {
  .grid { display: grid; grid-template-columns: repeat(3, 1fr); }
}

Cross-Browser Compatible Code

Code Editor

<div class="grid">
  <div>One</div><div>Two</div><div>Three</div>
</div>
<p class="note"></p>

Preview

Browser Support Strategy

Browser Support Matrix

FeatureChromeFirefoxSafariEdgeIE11
CSS Grid✅ 57+✅ 52+✅ 10.1+✅ 16+⚠️ Partial
Flexbox✅ 29+✅ 28+✅ 9+✅ 12+✅ 11+
CSS Variables✅ 49+✅ 31+✅ 9.1+✅ 15+❌ No
:focus-visible✅ 86+✅ 85+✅ 15.4+✅ 86+❌ No

Always check Can I Use for current browser support data.

Progressive Enhancement

/* Layer 1: Base styles (all browsers) */
.container {
  max-width: 1200px;
  margin: 0 auto;
  padding: 0 1rem;
}

/* Layer 2: Enhanced layout (modern browsers) */
@supports (display: grid) {
  .container {
    display: grid;
    grid-template-columns: 1fr 3fr;
    gap: 2rem;
  }
}

/* Layer 3: Advanced features */
@supports (backdrop-filter: blur(10px)) {
  .modal {
    backdrop-filter: blur(10px);
  }
}

Start with basic functionality, enhance for capable browsers

Feature Detection

/* CSS Feature Queries */
@supports (display: grid) {
  /* Grid-capable browsers */
}

@supports not (display: grid) {
  /* Fallback for older browsers */
}

/* JavaScript Detection */
if (CSS.supports('display', 'grid')) {
  document.body.classList.add('has-grid');
} else {
  document.body.classList.add('no-grid');
}

/* CSS Custom Properties Test */
if (!CSS.supports('color', 'var(--test)')) {
  // Load polyfill or use fallbacks
}

Detect features before using them, provide alternatives

Common Compatibility Issues

CSS Prefix Management

/* Vendor Prefixes (use Autoprefixer instead) */
.element {
  /* Old WebKit */
  -webkit-transform: translateX(100px);
  /* Old Firefox */
  -moz-transform: translateX(100px);
  /* Old IE */
  -ms-transform: translateX(100px);
  /* Standard */
  transform: translateX(100px);
}

/* Flexbox Prefixes */
.flex-container {
  display: -webkit-box; /* Old syntax */
  display: -ms-flexbox; /* IE 10 */
  display: flex; /* Modern */
  
  -webkit-box-pack: justify;
  -ms-flex-pack: justify;
  justify-content: space-between;
}

/* Use Autoprefixer in build process */
/* Input: transform: translateX(100px); */
/* Output: All prefixes added automatically */

Use Autoprefixer to automatically add vendor prefixes based on your browser support targets.

Layout Fallbacks

/* Grid with Flexbox fallback */
.grid {
  /* Flexbox fallback */
  display: flex;
  flex-wrap: wrap;
  margin: -1rem;
}

.grid-item {
  flex: 1 1 300px;
  margin: 1rem;
}

@supports (display: grid) {
  .grid {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(300px, 1fr));
    gap: 2rem;
    margin: 0;
  }

  .grid-item {
    margin: 0;
  }
}

Provide working layouts for all browsers with modern enhancements

CSS Variables Fallbacks

/* CSS Variables with fallbacks */
:root {
  --primary-color: #3b82f6;
  --spacing: 1rem;
}

.element {
  /* Fallback first */
  color: #3b82f6;
  /* Then variable */
  color: var(--primary-color);

  padding: 1rem;
  padding: var(--spacing);
}

/* PostCSS plugin for automatic fallbacks */
/* postcss-css-variables generates fallbacks */

/* JavaScript polyfill detection */
if (!CSS.supports('color', 'var(--test)')) {
  // Load polyfill
}

Always provide fallback values before using CSS variables

Testing & Tools

Testing Tools

  • • BrowserStack: Cross-browser testing service
  • • Sauce Labs: Automated browser testing
  • • CrossBrowserTesting: Live testing platform
  • • Chrome DevTools: Device simulation
  • • Firefox Developer Tools: Responsive mode
  • • Safari Web Inspector: iOS testing
  • • Edge DevTools: Compatibility testing
  • • Can I Use: Feature support lookup

Build Tools

  • • Autoprefixer: Add vendor prefixes automatically
  • • PostCSS: Transform CSS with plugins
  • • cssnext: Use future CSS today
  • • Browserslist: Define target browsers
  • • css-variables-polyfill: IE11 support
  • • flexibility: Flexbox polyfill
  • • CSS Grid polyfill: For older browsers
  • • Babel: JavaScript compatibility

Cross-Browser Checklist

CSS Compatibility

  • □ Vendor prefixes added
  • □ Feature queries used
  • □ Fallbacks provided
  • □ CSS reset/normalize applied
  • □ Box-sizing set consistently
  • □ Font stacks defined

Testing Coverage

  • □ Chrome/Edge tested
  • □ Firefox tested
  • □ Safari tested
  • □ Mobile browsers tested
  • □ Accessibility verified
  • □ Performance checked

Cross-Browser Compatibility — step by step

1 / 4
1

Use CSS resets or normalize

Browsers have different default styles. Apply a CSS reset or normalize.css at the start to give every browser a consistent baseline.

1 of 4
⚠️Safari Flexbox Quirks
Safari has historically had issues with flexbox and CSS Grid. Always test layouts in Safari and use prefixes or fallbacks for newer flex properties like gap when targeting older Safari versions.

🎯 Quick Check

Which CSS rule lets you apply styles only when a browser supports a specific feature?

Cross-browser CSS: summary

Key points

  • A modern reset (such as modern-normalize) smooths out default style differences between browsers.
  • Browsers ignore declarations they don't understand, so put the fallback first and the modern value after it.
  • Baseline (shown on MDN) tells you when a feature works in all major browsers.

Frequently asked questions

›What does @supports do?

@supports (property: value) applies the rules inside only if the browser supports that declaration. It's CSS feature detection.

›Do I still need vendor prefixes?

Rarely by hand. Autoprefixer adds the few that are still needed, such as -webkit-background-clip: text, based on the browsers you target.