Issue #2541💬 AnsweredOpened January 31, 2020by andy-aware0 reactions

Assign style to class not ID

快速解答by artf

Just create blocks/components with classes and, by default, the Style Manager will apply styles to them

Read full answer below ↓

Question

What I'm trying to achieve is whenever the "block" inside of the canvas is edited the style isn't added to an #id but instead added to a class.

My logic is to take the current ID that is used - the random string and remove it as the ID and make it a class and then assign styles to that class instead of the ID, yes it'll result in lots of classes but it'll mean I won't have issues with ID's on the frontend.

I can't seem to find how to do this though, whenever I do it on:

editor.on('component:update', component => {})

It just doesn't work. The ID is always undefined. component.getAttributes()['id']; and I don't seem to be able to reassign the style set to that ID to a unique class.

I know this sounds silly, but my issue comes when I'm using some Javascript on the frontend of my site and it's throwing a wobbler when it duplicates the HTML for an infinite scroller. Multiple ID's on the page is incorrect and making it into a class would work much better.

Answers (3)

artfFebruary 3, 2020

Just create blocks/components with classes and, by default, the Style Manager will apply styles to them

Fahad-ishfaqJanuary 25, 2024

Just create blocks/components with classes and, by default, the Style Manager will apply styles to them

Hi! Can you please tell if we can switch off this behaviour. I want to editor to setup CSS style to element id instead of class names. As same class can be assigned to different elements, which cause change to styles of all elements.

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 →

Browse Plugin Categories

Jump directly to plugin category pages on the marketplace.