Chrome-Only CSS: How to Target Chrome
In this article
Applying CSS rules to Chrome only is useful when you need to fix a layout quirk or style a feature that renders differently in Chrome compared to Firefox, Safari, or Edge. Chrome is based on the Blink rendering engine, and you can target it using a combination of CSS feature queries and browser-specific hacks. Below is a practical guide to doing this cleanly and safely.
What You'll Need
- A basic understanding of CSS (selectors, properties, and media queries).
- A text editor or your browser's developer tools (F12) to test changes.
- Access to Chrome and at least one other browser (like Firefox or Safari) to verify that your rules only affect Chrome.
Step-by-Step
- Identify the exact CSS rule you need to apply. For example, you might want to adjust a padding or fix a flexbox gap that only misbehaves in Chrome. Write the rule as you normally would, but plan to wrap it in a Chrome-only condition.
- Choose a targeting method. The most reliable method is to use a CSS feature query that checks for a property Chrome supports but other browsers do not. For example,
@supports (-webkit-appearance: none)is true in Chrome and Safari (both use WebKit/Blink), but you can narrow it further by adding a check for a property that only Chrome supports, such as@supports (not (-moz-appearance: none)) and (-webkit-appearance: none). - Write the Chrome-only block. Place your specific CSS inside the
@supportsrule. For example:
This works because Firefox supports@supports (-webkit-appearance: none) and (not (-moz-appearance: none)) { .my-element { padding: 10px; /* Only applies in Chrome */ } }-moz-appearance, so thenotcondition fails, while Safari also supports-webkit-appearancebut does not support-moz-appearance, so this rule would also apply to Safari. To exclude Safari, you can add another condition such as@supports (-webkit-touch-callout: none)which is true in Safari but not Chrome, and usenot. - Test in Chrome and other browsers. Open your page in Chrome and verify the rule applies. Then open the same page in Firefox and Safari to confirm the rule does not apply there. If it does apply in Safari, adjust your condition (see Common Mistakes).
- Use a fallback for non-Chrome browsers. Always write your default CSS outside the Chrome-only block, so that other browsers get the standard styling. For example, set the default padding to 5px, then override to 10px inside the Chrome-only block.
- Consider using a JavaScript class (alternative). If CSS-only targeting is too fragile, you can add a class to the
<html>element via JavaScript:if (navigator.userAgent.indexOf('Chrome') !== -1) { document.documentElement.classList.add('chrome'); }Then write your rules as.chrome .my-element { ... }. This is more robust but requires JavaScript.
Common Mistakes
- Using only
@supports (-webkit-appearance: none)– this also targets Safari, so your rule will leak to Safari. Always combine with a check for a Firefox-only property to exclude it. - Assuming Chrome is the only Blink browser – Edge (modern) is also Chromium-based, so your Chrome-only rules will apply to Edge as well. If that's acceptable, fine; if not, you need to exclude Edge using a user-agent check in JavaScript.
- Using old hacks like
_property: value– these are obsolete and may not work reliably in modern Chrome. - Not providing a fallback – if you only write the rule inside the Chrome-only block, other browsers will have no styling. Always have a base rule.
Tips
- Keep Chrome-only rules minimal and documented with a comment like
/* Chrome-only fix */so future developers understand why the rule exists. - Use the browser's developer tools to test quickly: in Chrome, open DevTools (F12) and toggle the device toolbar to simulate different browsers? Actually, that doesn't simulate other browsers, so use actual browsers or tools like BrowserStack for testing.
- If you're using a CSS preprocessor like Sass, you can create a mixin for Chrome-only rules to keep your code DRY.
- Consider whether you really need Chrome-only CSS. Often, a cross-browser solution exists. Search for the specific issue before resorting to hacks.
Troubleshooting
- Rule not applying in Chrome: Check the syntax of your
@supportscondition. Ensure you have no typos and that the property you check is actually supported. Also verify that the rule is not overridden by a later rule with higher specificity. - Rule applies in Safari too: Your condition is too broad. Add a condition that excludes Safari, such as
@supports (-webkit-touch-callout: none)and usenotbefore it. - Rule applies in Firefox: Your condition is not excluding Firefox. Make sure you include
not (-moz-appearance: none)in the condition. - Rule applies in Edge (Chromium): As mentioned, Edge is also Chromium. If you need to exclude Edge, you'll have to use JavaScript to detect the exact browser, because CSS cannot distinguish between Chromium browsers.
- CSS validation errors: Some validators may flag
@supportswith vendor prefixes. That's okay; it's valid CSS, but if you're worried, use the JavaScript approach instead.
Frequently Asked Questions
Can I target Chrome only without affecting Safari?
Yes, by using a combination of @supports conditions that include a property Chrome supports but Safari does not (or vice versa). For example, @supports (-webkit-appearance: none) and (not (-webkit-touch-callout: none)) works because Safari supports -webkit-touch-callout and Chrome does not.
Does this work for Chrome on mobile (Android)?
Yes, Chrome for Android uses the same Blink engine, so the same CSS conditions apply. However, be aware that some properties may behave differently on mobile due to viewport settings.
Is it better to use JavaScript or CSS for browser targeting?
CSS is preferred when possible because it's faster and doesn't require JavaScript to be enabled. However, if you need to distinguish between Chromium browsers (Chrome vs Edge), JavaScript is the only reliable way.
Will these Chrome-only rules break in future Chrome versions?
They might, if Chrome changes its support for the properties you used in the @supports condition. To minimize risk, use a property that is unlikely to be removed, and keep your rules simple.
Can I use the same technique for other browsers like Firefox?
Yes, you can adapt the same approach for Firefox by checking for -moz-appearance and excluding WebKit/Blink properties. For example, @supports (-moz-appearance: none) and (not (-webkit-appearance: none)).
What if my Chrome-only rule is not applied because of specificity?
Make sure your Chrome-only rule has a higher specificity than any conflicting rule. You can increase specificity by adding an ID or using !important (but use !important sparingly).