Issue #1228💬 AnsweredOpened June 24, 2018by ronaldaug1 reactions

Can't reset CSS code which has parent or child.

快速解答by artf1

To remove the CSS codes that belong to a specific block. Indeed that block of CSS doesn't belong to any component (I'm talking about how the editor works) so that declaration is always rendered. GrapesJS can associate only class-based selectors, eg. not even nested like .class1 .class2, in that case the rule is import...

Read full answer below ↓

Question

Tested on both npm and cdn GJS Version - 0.14.20 (latest) OS - Mac Browser - Chrome

Problem If CSS class name has a parent or child like #parent .child{ color: red }, user can't reset the style to origin, it still remain inside CodeViewer.

Codepen Demo 1 - Adding CSS from block Codepen Demo 2 - Adding CSS inside #gjs <div>

In demo1, Let's say user drag "Test Block" into the canvas and remove it. CSS style will still remains inside the CodeViewer. Even user clear the canvas, it will not be removed.

I am not sure this is a bug or not. But in most case, we used to declare CSS selectors including parents not for ruining the other sections' styles.

Answers (3)

artfJune 30, 2018

To remove the CSS codes that belong to a specific block.

Indeed that block of CSS doesn't belong to any component (I'm talking about how the editor works) so that declaration is always rendered. GrapesJS can associate only class-based selectors, eg.

.class1 {...}
.class1.class2 {...}
.class1.class2, .class3 {...}

not even nested like .class1 .class2, in that case the rule is imported correctly but do not belong to any component

artfJune 24, 2018

This is because you're just clearing components. You can use the built-in command core:canvas-clear

ronaldaugJune 27, 2018

@artf Thanks for your quick reply.

Sorry for my unclear question.

What I actually want to achieve is

  • To remove the CSS codes that belong to a specific block. For example, when I delete a block in the canvas, the CSS codes of that block will also be deleted. I don't want to clear the entire canvas.

Probably, it will fire on destroy. editor.Commands.add ('tlb-delete', e => { // remove the CSS from this block })

In this case, I can remove the CSS code that has no parent by deleting the components but if the CSS codes have a parent or a child, it does not work.

Codepen demo

And here is a video I've tested. Alt text for your video

According to my components, I added <style> tags inside the block's content, If there is a better option to work with styles, please guide me. Thanks in advance.

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.