Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Apply styles to HTML output of CKEditor 5

I'm using CKEditor 5 to help my customers to publish their articles. But the content generated by the editor is pure HTML piece. How to style the content? Is it possible that the content styled in page's <div/> is the same with styled in ckeditor5?

I know that I can write CSS for the contents, is the best way?

like image 781
lei li Avatar asked Sep 19 '18 08:09

lei li


People also ask

What is the HTML output in CKEditor 5?

Listed below are all official CKEditor 5 packages as well as some partner packages together with their possible HTML output. If a plugin generates a different HTML output depending on its configuration, it is described in the “HTML output” column. The classes, styles or attributes applied to an HTML element are all possible results.

How do I create a custom build for CKEditor 5?

If you are not afraid to get your hands dirty, you can always create a custom CKEditor 5 build from the source code with allthe CSS (both UI and the content) extracted to a separate file. See how to do that in a dedicated guide. #The full list of content styles Below there is a full list of content styles used by the editor features.

What is the source file for ckeditor5 metadata?

Source file: @ckeditor/ckeditor5-html-embed/ckeditor5-metadata.json The plugin can output any arbitrary HTML provided by the user. That HTML is always wrapped with a <div class="raw-html-embed"> element. Source file: @ckeditor/ckeditor5-html-support/ckeditor5-metadata.json

How to use CK-content CSS styles in CKEditor 5?

This narrows their scope when used in CKEditor 5 so they do not affect the rest of the application. To use them in the front–end, you will have toadd the ck-contentCSS class to the container of your content. Otherwise the styles will not be applied.


1 Answers

Your question boils down to this FAQ entry:

There is no such thing as the contents.css file because in CKEditor 5 features bring their own content styles, which are by default included in the JavaScript build and loaded by the style–loader (they can be extracted, too). It optimizes the size of the builds as the styles of unused features are simply excluded.

Although some styles are provided by the features, it is up to the developers to make sure the content created by CKEditor 5 is properly styled, both in the front–end and in the back–end. To style the content in the editor (back–end), use the .ck-content CSS class

There are very few content styles provided by the editor at this moment. That's why it's up to website maintainers to style the edited content in the front/back–end.

For instance, to make sure the <a> elements are properly styled, you'd probably want something like this:

#content-container-in-frontend a,
#backend-container .ck-content a {
   ...
}

P.S. Don't hesitate to have your say in the issue about content styles. We'd love to know more about your use–case (What's your environment? Do you use Webpack? How do you manage styles in the project?), what would you expect and what works best for you.


Update: You can now browse content styles used by the editor and use them in the front-end of your application. Learn more.

like image 182
oleq Avatar answered Oct 18 '22 14:10

oleq