# CSS Styling Best Practices Proper CSS styling ensures your plugin respects user themes and provides a native Obsidian experience. ## Table of Contents - [Avoid Inline Styles](#avoid-inline-styles) - [Use Obsidian CSS Variables](#use-obsidian-css-variables) - [Avoid `!important`](#avoid-important) - [Avoid `:has` Selector](#avoid-has-selector) - [Scope Plugin Styles](#scope-plugin-styles) - [Theme Support](#theme-support) - [Spacing and Layout](#spacing-and-layout) - [Complete Examples](#complete-examples) --- ## Avoid Inline Styles ### Avoid Inline Styles Rule: `obsidianmd/no-static-styles-assignment` ❌ **INCORRECT**: ```typescript element.style.color = 'red'; element.style.fontSize = '14px'; element.setAttribute('style', 'margin: 10px;'); element.setCssStyles({ color: 'red' }); // fixed value — reported since v0.5.0 ``` ✅ **CORRECT**: ```typescript // Add class in TypeScript element.addClass('my-custom-element'); // Values that change at runtime: set a CSS variable, consume it in styles.css element.setCssProps({ '--my-width': `${width}px` }); // needs minAppVersion 1.1.7 // Computed values and clearing a style are not reported (v0.5.1) element.setCssStyles({ top: `${y}px` }); element.style.color = ''; // Define styles in styles.css using Obsidian CSS variables .my-custom-element { color: var(--text-error); font-size: var(--font-ui-small); margin: var(--size-4-2); } ``` Rationale: Move all styles to CSS for better theme/snippet adaptability. Use Obsidian's CSS variables for theme consistency. This rule is an `error` and, for new submissions, a hard error in the scanner. v0.5.0 checks `setCssStyles()`/`setCssProps()` too. Since v0.5.1 it reports only properties with a fixed value (a computed value like `` `${y}px` `` is allowed), reports at that property rather than at the call — move an `eslint-disable-next-line` above a multi-line call to the property — and allows `style.setProperty('--name', …)` with any value. `setCssStyles()` and `setCssProps()` need `minAppVersion` 1.1.7, which `no-unsupported-api` checks since v0.5.1. Since v0.6.0, with type information, it reports a `style` object only when its type is an element's `CSSStyleDeclaration` (or can't be told, such as `any`) — a plugin's own `settings.style.theme = 'compact'` is not reported. --- ### Don't Create `` or `