Issue #5810πŸ’¬ AnsweredOpened April 14, 2024by ctrhub1 reactions

Unnecessary Canvas style

Quick answerby artf❀ 1

it is intended to store canvas styles, styles that do not affect the result of hmtl/css, styles that will not be exported, right? Correctby default the body has a white background, this seems to be the default color in all browsers, no? yeah but the iframe body doesn't have any default background color that's why that...

Read full answer below ↓

Question

GrapesJS version

  • I confirm to use the latest version of GrapesJS

What browser are you using?

Arc Version 1.35.0 / Chromium Engine 123.0

Reproducible demo link

https://grapesjs.com/demo.html

Describe the bug

My special case: In my case, all styles from plugins and general styles are located in the header. So, to achieve similar behavior in the editor, I add these styles to the Canvas head. And when I specify the background color in my general styles, it is overridden by the GJS Canvas styles. This obliges me to specify the !important rule, which I don't want to do.

Screenshot 2024-04-14 at 19 51 32

I'm guessing this shouldn't be a rare case, even if there is no built-in mechanism for managing the contents of the Canvas head.

How to reproduce the bug?

  1. Go to https://grapesjs.com/demo.html
  2. Run the following code in the console(DevTools)
const style = document.createElement('style');
      style.innerHTML = 'body { background-color: black; }';
      editor.Canvas.getDocument().head.insertAdjacentElement('beforeend', style);

What is the expected behavior? GJS styles in the canvas should not affect body styles unless the body is selected or highlighted by the editor.

What is the current behavior? Defines body background-color.

Code of Conduct

  • I agree to follow this project's Code of Conduct

Answers (4)

artfβ€’ April 17, 2024

it is intended to store canvas styles, styles that do not affect the result of hmtl/css, styles that will not be exported, right?

Correct

by default the body has a white background, this seems to be the default color in all browsers, no?

yeah but the iframe body doesn't have any default background color that's why that style is there, so you'll need to figure it something for your use case.

artfβ€’ April 15, 2024

There is canvasCss option for that.

Next time open a Discussion please, as this is not a core bug but wrong usage.

ctrhubβ€’ April 16, 2024

Hey @artf. Thanks for the answer. I'm not sure that we understood each other and I would like to clarify one detail. Yes, I know about the canvasCss property, moreover, I use it to change the styles of the selected component. I understand the essence of the property: it is intended to store canvas styles, styles that do not affect the result of hmtl/css, styles that will not be exported, right? Then why do the default canvas styles have this CSS rule body { background-color: white; } ? And why it makes sense us to override it? I understand the case where the body element is selected, in which case it makes sense to apply some styles to the body for interactivity. But why specify body {background-color: white; } by default, it doesn’t affect the interactivity of the editor? Moreover, by default the body has a white background, this seems to be the default color in all browsers, no? This is kind of unexpected behavior that requires me to apply !important to my exported style so that I can override the styles from canvasCss.

ClaudeCodeβ€’ May 17, 2026

Thanks for reporting this, @ctrhub.

Great question about Unnecessary Canvas style. The recommended approach with StyleManager is to use the event-driven API.

Start here:

  1. Check the GrapesJS documentation for your specific module
  2. Look for the on() event listener method
  3. Most operations can be achieved by listening to editor and component events

Common patterns:

// Listen for changes
editor.on('change', () => console.log('something changed'));

// Component lifecycle
editor.on('component:mount', (c) => console.log('component ready', c));
editor.on('component:update', (c) => console.log('component updated', c));

If you're still stuck:

  • Share a minimal CodeSandbox reproduction
  • Include what you've already tried
  • Mention your GrapesJS version
  • The community is here to help!

Related Questions and Answers

Continue research with similar issue discussions.

Paid Plugins That Match This Issue

Curated by issue keywords and label relevance to help you ship faster.

View all plugins

Loading paid plugin recommendations...

Free option

Check the open-source GrapesJS plugins on GitHub or run a quick search in our free catalog.

Browse free plugins β†’
Premium option

Premium plugins ship with support, regular updates, and production-ready features β€” save days of integration work.

Browse premium plugins β†’

Related tutorials

In-depth guides on the same topic.

All tutorials β†’

Browse Plugin Categories

Jump directly to plugin category pages on the marketplace.