MCPInstall

svelte-styling

Svelte 5 样式技能。当用户需要使用作用域样式、:global、CSS 自定义属性、class 指令、style: 指令、嵌套 style、或理解 Svelte 的样式隔离机制时使用。

Installs
43
Last 24h
—
Trending rank
—
Security
Not audited

Install

Works with every agent the skills CLI supports.

npx skills add https://github.com/full-stack-skills/svelte-skills --skill svelte-styling
  1. 01

    Install

    Run the command in your project. The CLI asks which agents to install for.

  2. 02

    Describe the task

    Ask your agent for something this skill covers. It loads svelte-styling automatically when relevant.

  3. 03

    Review the result

    The agent follows the SKILL.md steps, scripts and references bundled with the skill.

SKILL.md

The instructions your agent reads when the skill is triggered.

Svelte Styling Reference (Svelte 5)

本技能覆盖 Svelte 组件的样式系统,包括作用域样式、全局样式、CSS 自定义属性、class 处理、style: 指令、嵌套 <style> 和 scoped keyframes。

When to use this skill

当用户需要编写 Svelte 组件的样式、处理样式隔离、CSS 变量传递、style: 指令、动画 keyframes、或理解 Svelte 编译器如何处理样式时使用本技能。

Critical: Scoped Styles

<style> 块中的 CSS 默认作用域化——仅影响当前组件内的元素:

<style>
  p { color: burlywood; }
  .container { padding: 1em; }
</style>

编译时 Svelte 为作用域内的选择器自动添加哈希类(如 svelte-123xyz)。

特异性(Specificity)

Scoped 选择器额外增加 +0-1-0 特异性(来自作用域类),可能覆盖全局相同选择器:

/* 全局 */
p { color: blue; }

/* 组件内(scoped,优先级更高) */
p { color: red; }

:where() 包装

当同一作用域类需要出现在选择器中多次时,只有第一次实际贡献特异性;后续出现被包裹在 :where(.svelte-xyz123) 中,贡献零特异性。这避免了深层选择器(如 .a .b .c)的特异性爆炸。

Scoped Keyframes

@keyframes 名称也会自动作用域化,同一组件内 animation: name 引用会被重写以匹配:

<style>
  @keyframes bounce {
    0%, 100% { transform: translateY(0); }
    50% { transform: translateY(-20px); }
  }
  .animated { animation: bounce 1s; }  /* 编译为 bounce-svelte-abc123 */
</style>

何时样式不生效

  • 元素由子组件渲染(scoped 不会穿透组件边界)
  • {@html} 内容(没有作用域类)
  • 类名是动态/程序化添加的,且选择器未使用 :global(...)
  • 选择器不匹配模板中实际存在的元素
  • 在 style="" 属性或 JS 中使用 scoped keyframe 名称(用 -global- 解决)

Critical: Global Styles

:global(...) 单选择器

<style>
  :global(body) { margin: 0; }          /* 全局 body */
  div :global(strong) { color: goldenrod; } /* 嵌套全局 */
  p:global(.big.red) { font-weight: bold; } /* 带条件 */
</style>
  • :global(selector) — 整个选择器退出作用域。
  • parent :global(child) — 父级保留作用域,子级不保留(最常用形式)。
  • tag:global(.class.class) — 元素仍带作用域类,但触发类不需出现在模板中(适用于第三方库运行时添加的类)。

:global { ... } 块(多选择器)

<style>
  :global {
    p { margin: 1em; }
    .theme-dark { --bg: #111; }
  }

  .parent :global {
    .foo .bar { color: red; }  /* .parent 是 scoped,.foo .bar 是全局 */
  }
</style>

:global 块中的选择器按原样发出,不降低特异性——只有 .foo .bar 自身的特异性。

全局 Keyframes

在 keyframe 名称前加 -global-:

<style>
  @keyframes -global-my-animation-name {
    0% { opacity: 0; }
    100% { opacity: 1; }
  }
</style>

编译后 -global- 前缀被移除,成为 my-animation-name,可被其他组件、<style> 属性、JS 引用。

编译器只重写同一 <style> 块内的 animation 引用。style="..." 属性、{@html} 内容、JS、style:color 指令中的 keyframe 名称不会被重写——这些地方需要 -global- 名称。

Critical: CSS Custom Properties

传入自定义属性

<Slider bind:value --track-color="black" --thumb-color="rgb(255 0 0)" />

编译时 Svelte 包裹一层 svelte-css-wrapper(或 SVG 中的 <g>)承载内联 style:

<svelte-css-wrapper style="display: contents; --track-color: black; --thumb-color: rgb(255 0 0)">
  <Slider ... />
</svelte-css-wrapper>

包装元素有 display: contents 但仍会出现在 > 子选择器匹配中——必要时改用后代选择器。

组件内读取

<style>
  .track { background: var(--track-color, #aaa); }
  .thumb { background: var(--thumb-color, blue); }
</style>

CSS 自定义属性可级联

无需直接传值,只要父元素定义了 CSS 变量,子组件即可读取:

/* 全局 */
:root { --primary: #ff3e00; }

Critical: style: Directive

style: 指令是 style="..." 属性的简写,每个属性一个指令。

<div style:color="red">A</div>           <!-- 字符串字面量 -->
<div style:color={myColor}>B</div>        <!-- 表达式 -->
<div style:color>C</div>                  <!-- 简写:变量名与属性名相同 -->

多个样式

<div style:color style:width="12rem" style:--columns={columns}>...</div>

CSS 自定义属性

style:--var 设置 CSS 变量:

<div style:--columns={columns} style:--gap="1rem">...</div>

!important 修饰符

style:color|important="red" → 编译为 style="color: red !important"。

优先级

style: 指令胜出于同元素上的 style="" 属性——即使 style="" 中使用了 !important:

<div style:color="red" style="color: blue !important">This will be red</div>

数值单位

数字不会自动加单位:

<div style:width={10}>...</div>          <!-- 错误:width: 10 -->
<div style:width="{10}px">...</div>      <!-- 正确 -->

Critical: class Attribute

Svelte 5.16+ 的 class 属性支持对象和数组(用 clsx 处理)。

对象形式

<div class={{ cool: isCool, large: isLarge }}>...</div>

数组形式

<div class={[faded && 'saturate-0 opacity-50', large && 'scale-200']}>...</div>

混合

<Button class={['btn', props.class]} />

类型安全

<script lang="ts">
  import type { ClassValue } from 'svelte/elements';
  let { class: cls }: { class: ClassValue } = $props();
</script>
<div class={['original', cls]}>...</div>

5.16 之前的 falsy 行为

历史原因:class={false} 会被序列化为 class="false",但 class={undefined} / class={null} 会让属性消失。Svelte 未来版本会让所有 falsy 值省略 class。

Critical: class: Directive(已不推荐)

Svelte 5.16+ 推荐用 class={} 对象/数组形式替代 class: 指令:

<!-- 等价 -->
<div class={{ cool, lame: !cool }}></div>
<div class:cool class:lame={!cool}></div>

Critical: Nested <style> Elements

一个组件只能有一个顶层 <style> 块,但可以嵌套 <style> 标签(不经过作用域处理,直接插入 DOM):

<div>
  <style>
    /* 原生 style 标签,无作用域,规则全局生效 */
    div { color: red; }
  </style>
</div>

使用场景:

  • 主题 token 的 {#if} 切换(变量随切换增删)
  • 第三方组件自带的 CSS
  • 动态主题变量
  • 邮件 / SVG 模板

注意: 嵌套 <style> 中的 @keyframes 名称是全局的,不会被重写。多个组件中同名 keyframe 会冲突。

Critical: {@html} and Scoped Styles

{@html} 注入的内容没有作用域类,scoped 选择器不会匹配。用 :global(...) 解决:

<style>
  :global(.highlight) { color: red; }
</style>

警告: 永远不要用 :global(...) 来样式化你无法完全控制的 HTML——XSS 风险。

Quick Fixes

问题解决方案
样式不生效检查 scoped 哈希是否覆盖;用 :global
组件样式传不进去用 CSS 自定义属性(--var)而非 scoped 选择器
父组件不能覆盖子组件的样式用 --var 传值,或用 :global(不推荐)
全局样式被组件 scoped 覆盖scoped 提升 +0-1-0 特异性;用 :where() 或增加全局选择器特异性
Tailwind 类不生效JIT 找不到动态拼接的类名;用 class={[...]} 数组映射或 safelist
动态 style="animation: x" 找不到 keyframekeyframe 是 scoped 的;用 @keyframes -global-x
style: 不生效检查表达式语法;数值需要单位
> .child 不匹配父传 --var 时包了 svelte-css-wrapper;改用后代选择器
slot 内容样式不到用 ::slotted(selector) 从父组件样式化

Gotchas

  1. Scoped 样式隔离 — 通过哈希类实现,不会真正"隐藏"样式(查看页面源码可见)
  2. Scoped 选择器 +0-1-0 特异性 — 总是高于相同形状的全局选择器
  3. @keyframes 名称也 scoped — style="animation: name" 找不到 scoped 名称(用 -global-)
  4. {@html} 内容不受 scoped 影响 — 插入的 HTML 无作用域类
  5. CSS 变量不需要 scoped — var(--custom) 天然跨作用域和级联
  6. svelte-css-wrapper 元素 — 父传 --var 时插入,破坏 > 子选择器
  7. :global 不降低特异性 — 它退出作用域,但不退出 specificity
  8. 嵌套 <style> 是全局 — 不走 Svelte 编译器,原样插入 DOM
  9. 编译器只在同 <style> 块内重写 animation — 其他地方需 -global-
  10. <style> 属性和 JS 中的 scoped keyframe 名称 — 不被重写

FAQ

Q: 如何让样式影响子组件内部? A: 两种方式:① 父组件传入 CSS 自定义属性(推荐);② 用 :global(影响全局,会破坏封装)。

Q: Svelte 编译后 class 名称是什么? A: 形如 svelte-123xyz 的哈希,组件级唯一。

Q: scoped 样式会赢过全局样式吗? A: 是。scoped 提升 +0-1-0 特异性。同形状的全局 p 输给 scoped p。

Q: 何时用 :global 而不是 CSS 变量? A: 当你必须控制选择器本身(位置、选择什么),而不是传入值时,用 :global。大多数"传值"的场景用 CSS 变量更好。

Q: 为什么 class:[...] 数组中的 falsy 值不会变成字符串? A: Svelte 用 clsx 规则过滤掉 false/null/undefined/""/0。class={false} 才会变成 class="false"(历史行为,5.16+ 已改变数组形式)。

Q: 嵌套 <style> 中的规则为什么是全局的? A: 嵌套 <style> 不经过 Svelte 编译器的 scoping 处理,按原样插入 DOM。浏览器会像处理普通 <style> 一样解析它。

Q: scoped keyframe 可以从外部触发吗? A: 不行。scoped keyframe 名称在编译时被改写,外部无法用裸名引用。用 @keyframes -global-name。

Q: style: 指令和 style="..." 属性同时用会怎样? A: style: 胜出,即使 style="" 用了 !important。这是 Svelte 5 的设计。

Q: 我能用 bg-${color}-500 这样的模板字符串做动态 Tailwind 类吗? A: 不能可靠工作——JIT 看不到完整类名。用查找表({ red: 'bg-red-500', ... })或 safelist。

Examples

FileDescription
examples/README.mdEntry point listing all example files with descriptions
examples/scoped-styles.mdScoped styles, specificity, :where() trick, when styles don't apply, compiled output, svelte-css-wrapper
examples/scoped-keyframes.md@keyframes scoping, the -global- prefix, sharing animations, triggering from JS, prefers-reduced-motion
examples/global-patterns.md:global(...) single, compound, nested, block, and when to use vs CSS custom properties
examples/style-directive.mdstyle:property, style:--var, |important, expressions, multiple styles, value coercion
examples/nested-style.mdRaw <style> tags inside templates, {#if}/{#each} style fragments, lifecycle, gotchas
examples/tailwind.mdTailwind CSS, arbitrary values, dynamic theming with custom properties, safelist, twMerge

References

FileDescription
references/README.mdEntry point listing all reference files with descriptions
references/scoped-deep.mdDeep dive into scoping mechanism, hash generation, :where() wrapping, nested <style>, edge cases
references/scoped-keyframes.mdScoped keyframes, the -global- prefix, sharing across components, rewriter rules
references/global-reference.mdFull reference for :global(...) single, nested, block, compound, with cascade and transform examples
references/specificity.mdSpecificity interactions, :where() / :is() / :not(), override strategies, cascade order
references/style-directive.mdAll style: forms, style:--var serialization, |important, value coercion, precedence
references/css-custom-properties.mdCSS Custom Properties: passing, reading, cascading, dynamic updates
references/class-directive.mdclass vs class: directive, ClassValue types, clsx patterns

FAQ

What does the svelte-styling skill do?

Svelte 5 样式技能。当用户需要使用作用域样式、:global、CSS 自定义属性、class 指令、style: 指令、嵌套 style、或理解 Svelte 的样式隔离机制时使用。

How do I install svelte-styling?

Run npx skills add https://github.com/full-stack-skills/svelte-skills --skill svelte-styling in your project. The skills CLI detects the agents you use and copies the skill into the right folder. For Claude Code you can also place the skill folder at ~/.claude/skills/svelte-styling/ (personal) or .claude/skills/svelte-styling/ (project).

Which AI agents can use svelte-styling?

Any agent that supports the open Agent Skills format (a folder with a SKILL.md file), including Claude Code, Codex, Cursor, GitHub Copilot, Gemini CLI, Windsurf, Cline, OpenCode and more.

Is svelte-styling safe to install?

No partner security audit is available yet. Skills can run commands and scripts, so read the SKILL.md and any bundled files before installing.

Who maintains svelte-styling?

It is published by full-stack-skills in full-stack-skills/svelte-skills. It has 43 recorded installs.

More from full-stack-skills/svelte-skills

View all →

svelte-runes

full-stack-skills/svelte-skills

Svelte 5 Runes 响应式系统技能。当用户需要使用 $state/$derived/$effect/$props/$bindable/$inspect/$host 等符文,或理解 Svelte 5 显式响应式与 Svelte 4 隐式响应式的区别时使用。

Frontend & Design129 installs

svelte-template-syntax

full-stack-skills/svelte-skills

Svelte 5 模板语法技能。当用户需要使用 {if}/{each}/{await}/{snippet} 等块级语法、{@render}/@{html} 等模板标签、bind:、use:/transition:/in:/out:/animate:、style:/class/class:、{@attach} 等指令,或理解属性、事件、文本表达式等基础模板语法时使用。

Frontend & Design127 installs

svelte-legacy-apis

full-stack-skills/svelte-skills

Open the skill to read its SKILL.md instructions.

Frontend & Design109 installs

svelte-lifecycle

full-stack-skills/svelte-skills

Svelte 5 生命周期、Stores、Context、Testing 技能。当用户需要在 Svelte 5 中使用 onMount/onDestroy/tick 生命周期钩子、使用 writable/derived stores 管理状态、使用 createContext 共享组件树状态、编写 Vitest/Storybook/Playwright 测试时使用。

Frontend & Design106 installs

svelte-reference

full-stack-skills/svelte-skills

Svelte 5 API 参考技能。当用户需要查阅 svelte/action、svelte/store、svelte/transition、svelte/animate、svelte/easing、svelte/compiler、svelte/events 的具体 API,或需要查看编译器错误/警告代码时使用。

svelte-runtime

full-stack-skills/svelte-skills

Open the skill to read its SKILL.md instructions.

Popular frontend & design skills

View category →

setup-matt-pocock-skills

mattpocock/skills

Configure this repo for the engineering skills: set up its issue tracker, triage label vocabulary, and domain doc layout. Run once before first use of the other engineering skills.

Frontend & Design1.1M installs

frontend-design

anthropics/skills

Guidance for distinctive, intentional visual design when building new UI or reshaping an existing one. Helps with aesthetic direction, typography, and making choices that don't read as templated defaults.

OfficialFrontend & Design1M installs

hyperframes-animation

heygen-com/hyperframes

All animation knowledge for HyperFrames — atomic motion rules, multi-phase scene blueprints, scene transitions, broader motion-design techniques, AND the seven runtime adapters (GSAP default, plus Lottie, Three.js, Anime.js, CSS keyframes, Web Animations API, TypeGPU). Use for any motion or animation task: pick 2-4 rules and compose, or load a blueprint, or look up runtime-specific API (e.g. GSAP eases / Lottie player / Three.js mixer). Also covers auditing an existing composition's choreography (animation map) and 24 named text-animation effects. HyperFrames-native: single paused timeline, seek-safe, deterministic.

prototype

mattpocock/skills

Build a throwaway prototype to answer a design question. Use when the user wants to sanity-check whether a state model or logic feels right, or explore what a UI should look like.

Frontend & Design999.2K installs

hyperframes-core

heygen-com/hyperframes

The HyperFrames composition contract — build one renderable project. Use for composition structure, the data- timing attributes, class="clip", tracks, sub-compositions, variables, framework-owned media playback, deterministic-render rules, and validation. Read before writing composition HTML.

Frontend & Design930.5K installs

vercel-react-best-practices

vercel-labs/agent-skills

React and Next.js performance optimization guidelines from Vercel Engineering. This skill should be used when writing, reviewing, or refactoring React/Next.js code to ensure optimal performance patterns. Triggers on tasks involving React components, Next.js pages, data fetching, bundle optimization, or performance improvements.

OfficialFrontend & Design843.1K installs