Images
The @ckeditor/ckeditor5-image
package contains multiple plugins that implement various image-related features:
Image
implements basic support for images.ImageToolbar
adds the image feature’s contextual toolbar.ImageCaption
adds support for captions.ImageStyle
adds support for image styles.ImageTextAlternative
adds support for adding text alternative.ImageUpload
adds support for uploading dropped or pasted images (see: Image upload).ImageResize
adds support for resizing images.
All features listed above except the image resize are enabled by default in all WYSIWYG editor builds.
Check the documentation of each subfeature to learn more about it.
# Base image support
The Image
feature adds support for plain images with just the alt
attribute set. This translates to the following HTML:
<figure class="image">
<img src="..." alt="...">
</figure>
This feature follows the markup proposed by the Editor Recommendations project.
You can see the demo of a WYSIWYG editor with the base image feature enabled below:
The base image feature, unlike in CKEditor 4, does not support any user interface for inserting or managing images. Its sole purpose is to lay ground for other plugins (mentioned above) to build the target user experience. This pattern (composition of atomic features) is common for CKEditor 5 and allows the developers to build their own customized experience by implementing specific subfeatures differently.
# Image contextual toolbar
The ImageToolbar
plugin introduces a contextual toolbar for images. The toolbar appears when an image is selected and can be configured to contain any buttons you want. Usually, these will be image-related options such as the text alternative (which is introduced by the base image plugin) button and image styles buttons.
See a demo of a WYSIWYG editor with the contextual toolbar enabled:
The image toolbar is configured to contain the image text alternative button:
ClassicEditor
.create( document.querySelector( '#editor' ), {
image: {
toolbar: [ 'imageTextAlternative' ]
}
} )
# Image captions
The ImageCaption
plugin adds support for image captions:
<figure class="image">
<img src="..." alt="...">
<figcaption>Caption goes here...</figcaption>
</figure>
By default, if the image caption is empty, the <figcaption>
element is not visible to the user. You can click the image to reveal the caption. See the demo below:
# Image styles
In simple integrations it is enough to let the user insert images, set their text alternative and the editor’s job is done. An example of such a simple solution are e.g. GitHub comments. The styling of the images (for example, their maximum width and margins) is controlled by GitHub through stylesheets.
In more advanced scenarios, the user may need to be able to decide whether the image should take the whole width (if it is the article’s main photo) or it should take, for example, 50% of the width and be pulled out of the content (so called “pulled images”). Various integration scenarios require different types of images to be used.
This is what the ImageStyle
feature is designed for.
However, unlike in CKEditor 4, in CKEditor 5 the end user does not set the image border, alignment, margins, width, etc. separately. Instead, they can pick one of the styles defined by the developer who prepared the WYSIWYG editor integration. This gives the developer control over how the users style images and makes the user’s life easier by setting multiple properties at once.
A style is applied to the image in form of a class. By default, CKEditor 5 is configured to support two styles: “full width” (which does not apply any class — it is the default style) and “side image” (which applies the image-style-side
class).
A normal (full width) image:
<figure class="image"><img src="..." alt="..."></figure>
A side image:
<figure class="image image-style-side"><img src="..." alt="..."></figure>
The actual styling of the images is the developer’s job. CKEditor 5 WYSIWYG editor comes with some default styles, but they will only be applied to images inside the editor. The developer needs to style them appropriately on the target pages.
You can find the source of the default styles applied by the editor here: ckeditor5-image/theme/imagestyle.css
.
Below you can see a demo of the WYSIWYG editor with the image styles feature enabled. The default configuration is used. You can change the styles of images through the image’s contextual toolbar.
# Configuring image styles
The available image styles can be configured using the image.styles
option.
The following WYSIWYG editor supports the default full image style plus left- and right-aligned images:
ClassicEditor
.create( document.querySelector( '#editor' ), {
image: {
// You need to configure the image toolbar, too, so it uses the new style buttons.
toolbar: [ 'imageTextAlternative', '|', 'imageStyle:alignLeft', 'imageStyle:full', 'imageStyle:alignRight' ],
styles: [
// This option is equal to a situation where no style is applied.
'full',
// This represents an image aligned to the left.
'alignLeft',
// This represents an image aligned to the right.
'alignRight'
]
}
} )
.then( ... )
.catch( ... );
The code sample above uses predefined image styles: 'full'
, 'alignLeft'
and 'alignRight'
. The latter two apply, respectively, the .image-style-align-left
and .image-style-align-right
classes to the <figure>
element.
See the result below:
In the example above the options represent simple “align left” and “align right” styles. Most text editors support left, center and right alignments, however, it is better not to think about CKEditor 5’s image styles in this way. Try to understand what use cases the system needs to support and define semantic options accordingly. Defining useful and clear styles is one of the steps towards a good user experience and clear, portable output. For example, the “side image” style can be displayed as a floated image on wide screens and as a normal image on low resolution screens.
# Defining custom styles
Besides using the five predefined styles:
'full'
,'side'
,'alignLeft'
,'alignCenter'
,'alignRight'
you can also define your own styles or modify the existing ones.
Reusing (or modifying) predefined styles has the following advantage: CKEditor 5 will use its official translations for the defined button titles.
You can find advanced examples in the image.styles
configuration option documentation.
# Image upload
See the Image upload guide.
# Responsive images
Support for responsive images in CKEditor 5 is brought by the Easy Image feature without any additional configuration. Learn more how to use the feature in your project in the Easy Image integration guide.
# Resizing images
The image styles feature is meant to give the user the choice between a set of styling options provided by the system (so by the developer or administrator who created it). There are also scenarios where the user should be able to freely set the width of an image. And that is where the image resize feature comes to play.
It is implemented by the ImageResize
plugin and enables four “resize handles” displayed over the selected image. The user can freely resize the image by dragging them. The feature can be configured to use either percentage (default) or pixel values.
# Enabling image resizing
The image resize feature is not enabled by default in any of the editor builds. In order to enable it, you need to load the ImageResize
plugin. Read more in the Installation section.
# Markup and styling
When you resize an image, the inline width
style is used and the <figure>
is assigned the image_resized
class:
<figure class="image image_resized" style="width: 75%;">
<img src="..." alt="...">
</figure>
The image_resized
class is used to disable max-width
assigned by the image styles if one is applied to this image. For instance, the “side image” style is defined like this:
.ck-content .image-style-side {
max-width: 50%;
float: right;
margin-left: var(--ck-image-style-spacing);
}
And the max-width
gets overridden by the following rule:
.ck-content .image.image_resized {
max-width: 100%;
}
Another concern when styling resized images is that by default, CKEditor 5 uses display: table
on <figure class="image">
to make it take the size of the <img>
element inside it. Unfortunately, browsers do not yet support using max-width
and width
on the same element if it is styled with display: table
. Therefore, display: block
needs to be used when the image is resized:
.ck-content .image.image_resized {
display: block;
box-sizing: border-box;
}
.ck-content .image.image_resized img {
width: 100%;
}
.ck-content .image.image_resized > figcaption {
display: block;
}
# Using pixels instead of percentage width
Using percentage widths ensures that content stays responsive when displayed in different places than in the WYSIWYG editor. If the user made an image take 60% of the content’s width in the editor, if you ever change the width of the target page (where this content is displayed), the image will still take 60% of that space. The same is true if the page is responsive and adjusts to the viewport’s width.
If you configured the editor to use pixel values, the image could take, for example, too much space after you introduced a new layout for your website.
However, there are cases where pixel values may be preferred. You can thus configure the editor to use them by setting the config.image.resizeUnit
option:
ClassicEditor
.create( editorElement, {
image: {
resizeUnit: 'px'
}
} )
.then( ... )
.catch( ... );
# Future development
Resizing by dragging handles displayed over the image is the first option provided, but we consider implementing more with time. Some of the possible next steps include:
- Buttons such as “50%”, “75%” and “100%” in the image toolbar, allowing the user to choose only from predefined widths.
- A traditional “width” input. Or an option to set both width and height separately.
- An option to restore the original image size.
- Limiting image resize with min and max values.
We count on your feedback. React with 👍 under the respective tickets or report new ones if you have different ideas.
# Installation
To add image features to your rich-text editor, install the @ckeditor/ckeditor5-image
package:
npm install --save @ckeditor/ckeditor5-image
And add the plugins that you need to your plugin list. You also need to set the image toolbar items.
import Image from '@ckeditor/ckeditor5-image/src/image';
import ImageToolbar from '@ckeditor/ckeditor5-image/src/imagetoolbar';
import ImageCaption from '@ckeditor/ckeditor5-image/src/imagecaption';
import ImageStyle from '@ckeditor/ckeditor5-image/src/imagestyle';
import ImageResize from '@ckeditor/ckeditor5-image/src/imageresize';
ClassicEditor
.create( document.querySelector( '#editor' ), {
plugins: [ Image, ImageToolbar, ImageCaption, ImageStyle, ImageResize ],
image: {
toolbar: [ 'imageTextAlternative', '|', 'imageStyle:full', 'imageStyle:side' ]
}
} )
.then( ... )
.catch( ... );
Read more about installing plugins.
# Common API
The Image
plugin registers:
- The
'imageTextAlternative'
button. - The
'imageTextAlternative'
command - The
'imageInsert'
command which accepts a source (e.g. an URL) of an image to insert.
The ImageStyle
plugin registers:
-
A button for each defined style — e.g.
'imageStyle:full'
and'imageStyle:side'
. -
The
'imageStyle'
command which accepts a value based on theimage.styles
configuration option (e.g.'full'
and'side'
):editor.execute( 'imageStyle', { value: 'side' } );
The ImageUpload
plugin registers:
- The
'imageUpload'
button which opens the native file browser to let you upload a file directly from your disk. - The
'imageUpload'
command which accepts the file to upload.
The ImageResize
plugin registers:
- The
'imageResize'
command which accepts the target width.
We recommend using the official CKEditor 5 inspector for development and debugging. It will give you tons of useful information about the state of the editor such as internal data structures, selection, commands, and many more.
# Contribute
The source code of the feature is available on GitHub in https://github.com/ckeditor/ckeditor5-image.