TechBuzzPro

Safari-Only CSS: How to Target Safari

· 5 min read · Apps & Browsers

In this article
  1. Why Safari Has No Official Targeting Mechanism
  2. The @supports Feature Query Method
  3. Vendor-Prefix and Hack Approaches
  4. JavaScript-Based Detection
  5. Common Problems and Misconceptions
  6. Alternatives to Safari-Only Styling
  7. Frequently Asked Questions

No, there is no official CSS selector or media query that targets Safari specifically. Safari does not expose a unique, standards-based identifier that lets you apply styles only to it. However, several reliable workarounds exist: feature queries (@supports), vendor-prefix hacks, and JavaScript-based browser detection. Each has trade-offs in reliability and maintainability, and none is officially endorsed by Apple or the W3C.

Why Safari Has No Official Targeting Mechanism

CSS was designed around capability detection, not browser detection. The W3C has long discouraged browser-specific styling because it fragments the web and breaks when browsers update. Instead, the standard approach is to write CSS that works everywhere and use feature queries to handle differences in support.

Safari shares the WebKit rendering engine with many other browsers on iOS (all iOS browsers must use WebKit), and on macOS it uses WebKit while Chrome and Edge use Blink. That shared engine means Safari is not as isolated as it once was, but it still has quirks: its handling of position: sticky, backdrop-filter, form controls, and scrollbar styling differs from Chromium browsers. These differences are why developers sometimes want Safari-only rules.

Apple does not publish a Safari-specific CSS identifier, and user-agent sniffing on the server side is fragile because Safari's user agent string can be spoofed or changed. The methods below are the practical options that remain.

The @supports Feature Query Method

The most standards-friendly approach is to target a CSS feature that only Safari supports, or that Safari supports differently. For example, Safari supports -webkit-backdrop-filter but Chromium browsers also support the unprefixed backdrop-filter. A more reliable example is the @supports (-webkit-hyphens: auto) query, which matches Safari and some older WebKit browsers but not Firefox or Chromium.

You can write:

  • @supports (-webkit-hyphens: auto) { .selector { /* Safari-only rules */ } }
  • @supports (font: -apple-system-body) { /* targets Safari on Apple platforms */ }

These queries work because they test for a property-value pair that only WebKit-based browsers recognize. The downside is that they are not guaranteed to remain unique to Safari forever, and they may also match other WebKit browsers such as older versions of Chrome on iOS. Always test after browser updates.

Vendor-Prefix and Hack Approaches

Before feature queries were widely supported, developers used CSS hacks that exploited parsing differences. One well-known example is the @media not all and (min-resolution:.001dpcm) hack, which historically matched Safari on macOS but not Chrome. Another is the ::-webkit-any-link pseudo-element combined with a selector that Chromium ignores.

These hacks are brittle. Browser vendors patch parsing behavior, and a hack that works today can silently break after an update. They also fail in Safari on iOS in some versions because the mobile WebKit build differs from the desktop build. Use them only as a last resort and document them clearly in your codebase so future maintainers know why they exist.

A safer variant is to combine a feature query with a property that Safari handles uniquely, such as -webkit-touch-callout, which is recognized by WebKit but ignored by Blink and Gecko. Again, verify in the current Safari version before shipping.

JavaScript-Based Detection

If CSS-only methods are too fragile, JavaScript can detect Safari and add a class to the <html> or <body> element. A common approach checks the user agent string for Safari while excluding Chrome and CriOS:

  1. Read navigator.userAgent.
  2. Check that it contains Safari.
  3. Check that it does not contain Chrome, Chromium, CriOS, Edg, or FxiOS.
  4. If both conditions pass, add a class such as is-safari to the document root.
  5. Write CSS rules scoped to .is-safari .selector.

This method is more explicit but still relies on a string that Apple can change. It also fails for users who spoof their user agent. Feature detection with @supports remains preferable because it tests what the browser can actually do rather than what it claims to be.

Common Problems and Misconceptions

A frequent misconception is that Safari on iOS and Safari on macOS behave identically. They do not. iOS Safari has different viewport behavior, touch event handling, and support for some CSS properties such as position: fixed during scrolling. A rule that targets desktop Safari may not apply correctly on iPhone or iPad.

Another common issue is that all browsers on iOS must use WebKit, so Chrome on iOS will match many Safari-specific feature queries. If you truly need to exclude Chrome on iOS, you must combine feature detection with user-agent checks, which increases complexity.

Finally, Safari updates frequently and silently. A hack that worked in one version may stop working after an automatic update. Always include a fallback style so the page remains usable if the Safari-only rule fails to apply.

Alternatives to Safari-Only Styling

In most cases, the better solution is to fix the underlying cross-browser issue rather than target Safari. Options include:

  • Using @supports to apply progressive enhancement only where a feature is supported, rather than targeting a browser by name.
  • Normalizing styles with a reset or a tool like Normalize.css to reduce rendering differences.
  • Using vendor prefixes for properties that require them, such as -webkit-backdrop-filter, alongside the standard property.
  • Testing in Safari's Responsive Design Mode and on real iOS devices to catch quirks early.

If the goal is to fix a specific Safari bug, document the bug and the workaround in a comment. Future maintainers will thank you, and the workaround can be removed once Apple fixes the issue.

Frequently Asked Questions

Can I use a CSS selector that only Safari understands?

No official selector exists. Some vendor-prefixed pseudo-elements and properties are recognized only by WebKit, but they are not guaranteed to remain Safari-exclusive and may match other WebKit-based browsers.

Does @supports (-webkit-hyphens: auto) target only Safari?

It targets WebKit-based browsers, which includes Safari and some versions of Chrome on iOS. It does not target Firefox or desktop Chromium browsers, but it is not strictly Safari-only.

Why do my Safari-only styles break after an update?

Browser updates change parsing and feature support. Hacks that rely on parsing quirks are especially vulnerable. Feature queries are more stable but still require testing after major updates.

Is user-agent sniffing reliable for Safari?

It is more explicit than CSS hacks but still fragile. User agents can be spoofed, and Apple has changed the Safari user agent string over time. Combine it with feature detection for best results.

Should I target Safari at all?

Only when a genuine rendering difference cannot be fixed with standard CSS. In most cases, progressive enhancement and cross-browser testing are better than browser-specific rules.