QUESTION: Adding default styling to a component
Question
I have been working on integrating grapesjs into a new development I'm working on. I have had much success with the documentation and wiki forum but am running into a few issues I'm hoping someone out there can help me with. I am working with a mobile view only, I have set the default device to mobile and hidden the option to change.
When I insert an image, I would like the default css to include max-width: 100% as if I load a large image into the mobile view I will get an image much larger than the screen I'm looking at.
I have tried:
blockManager.get('image').set({
style:{
'max-width':'100%'
}
});
But I realize that might be an incorrect use and maybe only width (not max-width) is valid there. I have also tried using the cssComposer with no success.
var sm = editor.SelectorManager;
var cssComposer = editor.CssComposer;
var sel1 = sm.add('img');
var rule = cssComposer.add([sel1]);
rule.set('style', {'max-width': '100%'});
Any help would be greatly appreciated. Thank you.
Answers (3)
Your change on the block doesn't work because you don't change the actual component (just the block's property)
editor.BlockManager.get('image').get('content').style = { 'max-width':'100%' }
With cssComposer instead, by default, selectorManager accepts classes
...
var sel1 = sm.add('img'); // here you'r adding `.img` selector and not `img`
An easier way to add custom CSS might be this one
editor.addComponents('<style> ... </style>');
But be careful, if store your templates and then run this command on any refresh this will continue to create styles. For default CSS I'd recommend:
grapesjs.init({
// CSS that could only be seen (for instance, inside the code viewer)
protectedCss: 'img { ... }',
// If have an external file, like for example, some reset CSS
// you can use this
canvas: {
styles: ['https://...file.css']
}
})
Thank you! I was able to get that working with:
protectedCss: 'img {max-width:100%;}',
I have the same issue with videos and iframes (for youtube) being dropped onto the template larger than the screen size and the protectedCss solution did not work for those. Any suggestions?
protectedCss is loaded so the CSS is here, if it doesn't work probably your CSS technique is not correct
Related Questions and Answers
Continue research with similar issue discussions.
Issue #822
[QUESTION] How to get the html and css code in view code tool?
or how to get all the html code and css so that I could save it in my database. Or there is already a functionality in the documentation? y...
Issue #841
[QUESTION] Add default Trait (attribute) to model for all HTML Components (elements)
Hi, I've read the Traits, Components and API wikis, grepped all the open & closed issues. I've even found the code where you are creating t...
Issue #886
[HELP WANTED]: questions about some customization.
Hey Guys, first thank you very much for your work, this project seems really promising. I have a few question for a website i'm building. I...
Issue #1016
[Question] Button staying pressed after running command
Is there any way to keep a button from staying pressed after the command completes? Been looking through the documentation and didn't see a...
Paid Plugins That Match This Issue
Curated by issue keywords and label relevance to help you ship faster.
Loading paid plugin recommendations...
Browse Plugin Categories
Jump directly to plugin category pages on the marketplace.