Skip to content
StylingCustom CSS

Custom CSS for SvelteKit

Markdown
Loading…

Change the width, spacing or any single element with a CSS rule on its part name.

What it does

Every element the SDK renders carries a data-cy-part attribute: banner, accept-all, dialog, toggle and so on. Target it in your own stylesheet to change anything theme does not cover: width, spacing, shadows, one button, the toggle.

src/app.css
[data-cy-part="banner"] {
  max-width: 34rem;
}

[data-cy-part="actions"] {
  gap: 0.75rem;
}

[data-cy-part="accept-all"] {
  font-weight: 600;
}

Import it in the root layout. cookieyesHandle writes the SDK's critical CSS and stylesheet link right after <meta charset>, ahead of every other tag in <head>, so your file always comes after it and your rules win:

src/routes/+layout.svelte
<script lang="ts">
  import "../app.css";
  import { CookieYesRoot } from "@cookieyes/sveltekit";
  import { config } from "../lib/cookieyes";

  let { data, children } = $props();
</script>

<CookieYesRoot {config} state={data.consent}>
  {@render children()}
</CookieYesRoot>

The full list of part names is on Parts.

The toggle

The switch in the preferences dialog is the one element that needs a specific selector. Its colour is painted by a child, .cy-toggle-track, and the SDK's own rule for the checked state is stronger than a single part selector, so repeat the attribute once to outrank it:

src/app.css
[data-cy-part="toggle"][data-cy-part][data-cy-state="on"] .cy-toggle-track {
  background: #15803d;
}

Good to know

Global CSS only. The banner and recall button render into <body>, so a rule scoped to your app, such as .app-shell [data-cy-part="banner"], matches nothing. A <style> block in one of your components is scoped to that component's own elements, so it does not reach any CookieYes element. Use a global stylesheet such as src/app.css, or wrap the selector in :global(...).

Shared names. title, close, accept-all, reject-all and branding exist on more than one surface. To style one, add its parent: [data-cy-part="banner"] [data-cy-part="title"].

Fonts. The components set their own font and do not inherit yours. Use theme.fontFamily, or set --cy-font in CSS.

Variables versus rules. A rule on a part (font-weight, max-width) always works. A --cy-* variable set in CSS works only while the same value is not set in theme; see Theme tokens.

Common mistakes

A rule does nothing. The stylesheet is not imported anywhere, or the selector is weaker than the SDK's own rule. Import it in src/routes/+layout.svelte, or add the parent part to the selector.

A scoped selector matches nothing. The components are under <body>, outside your app's element. Drop the ancestor.

The checked toggle kept the default colour. Use the selector from The toggle above.

A banner rule also changed the dialog heading. title is shared. Add [data-cy-part="banner"] in front.

Next steps

  • Parts: every part name, per component
  • Tailwind: the same with utility classes
  • Theme tokens: colours, radius and font without CSS

On this page