-
Currently you can configure styles (classes) and CSS in backend config. But only classes are outputted to frontend. Not CSS. So take for example the image in this repo. You define new style called Category which targets h3 and has class "category". You also create CSS for it as in the image. You apply style category to some heading in CKEditor. In backend it will all look good. Class and CSS acting as it should and styling content. On frontend you will have only class output Manually copying styles every time you change them is not very good. So I though that the defined CSS in backend should have some way to be outputted to frontend. I found something here, but I dont understand it. #29 (comment) Thanks. |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments
-
Ok, after little trying and reading #29 (comment) I found a way.
Well, I think this should be documented somewhere better. |
Beta Was this translation helpful? Give feedback.
-
It is technically possible to output the CSS from the CKEditor config used in a field, but as you've noted, it can easily conflict with other fields, or your global CSS. Assuming you have CSS like this in your configuration… p {
border: 3px solid red;
} …you can output it alongside a field, like this: {# Output CKEditor field value: #}
{{ entry.richText }}
{# Load field definition: #}
{% set field = craft.app.fields.getFieldByHandle('richText') %}
{# Get the corresponding `CkeConfig` object:
{% set config = craft.app.plugins.getPlugin('ckeditor').getCkeConfigs().getByUid(field.ckeConfig) %}
{# Register CSS content for output: #}
{% do view.registerCss(config.css) %} However, this will expose the .ck p {
border: 3px solid red;
} Then, use the {# Same config getter logic: #}
{% set configUid = craft.app.fields.getFieldByHandle('richText').ckeConfig %}
{% set config = craft.app.plugins.getPlugin('ckeditor').getCkeConfigs().getByUid(configUid) %}
{# Define a namespace `rich-text` for this field’s output: #}
{% namespace 'rich-text' withClasses %}
{# Wrap output in the same class we used to scope the config CSS: #}
<div class="ck">
{{ entry.richText }}
</div>
{# Echo styles into a tag so the namespacer can find them: #}
<style>{{ config.css }}</style>
{% endnamespace %} |
Beta Was this translation helpful? Give feedback.
Ok, after little trying and reading #29 (comment) I found a way.
Instead of outputting CSS from backend to frontend it gets defined in frontend and imported to backend.
.ck.ck-content p.note { border-left: 4px solid #4a7cf6; padding-left: 1rem; color: #4a7cf6; }
<link rel="stylesheet" href="/css/ckeditor.css" />
@import "/css/ckeditor.css"