title: Images
{@snippet features/build-image-source}
The @ckeditor/ckeditor5-image package contains multiple plugins that implement various image-related features:
All features listed above except the image resize and image linking are enabled by default in all WYSIWYG editor builds.
Check the documentation of each subfeature to learn more about it.
The {@link module:image/image~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](https://ckeditor.github.io/editor-recommendations/features/image.html) project.
You can see the demo of a WYSIWYG editor with the base image feature enabled below:
{@snippet features/image}
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.
The {@link module:image/imagetoolbar~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:
{@snippet features/image-toolbar}
The image toolbar is configured to contain the image text alternative button:
ClassicEditor
.create( document.querySelector( '#editor' ), {
image: {
toolbar: [ 'imageTextAlternative' ]
}
} )
The {@link module:image/imagecaption~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:
{@snippet features/image-caption}
See the {@link features/image-upload Image upload} guide.
Support for responsive images in CKEditor 5 is brought by the {@link features/easy-image Easy Image} feature without any additional configuration. Learn more how to use the feature in your project in the {@link features/easy-image#responsive-images Easy Image integration} guide.
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.
Finally, in certain situations, the user should be able to granularly control how an image is presented so they should be able to set the size and alignment separately.
The {@link module:image/imagestyle~ImageStyle} feature solves the last two scenarios. The former is handled by so-called "semantical styles" and the latter by "presentational styles" in combination with image resize.
The available image styles can be configured using the {@link module:image/image~ImageConfig#styles config.image.styles} option. Respective buttons should also be added to the image toolbar via {@link module:image/image~ImageConfig#toolbar config.image.toolbar}.
A semantical style let the user choose from a predefined "types" of images. The user is not able to 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 default semantical 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 integrator's job. CKEditor 5 WYSIWYG editor comes with some default styles, but they will only be applied to images inside the editor. The integrator 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`](https://github.com/ckeditor/ckeditor5/blob/master/packages/ckeditor5-image/theme/imagestyle.css).
Read more about {@link builds/guides/integration/content-styles styling the content of the editor}.
Below you can see a demo of the WYSIWYG editor with the semantical image styles. The "full" and "side" styles are the default value of {@link module:image/image~ImageConfig#styles config.image.styles} so you do not need to set it.
ClassicEditor
.create( document.querySelector( '#editor' ), {
plugins: [ Image, ImageToolbar, ImageCaption, ImageStyle ],
image: {
toolbar: [
'imageStyle:full',
'imageStyle:side',
'|',
'imageTextAlternative'
],
// The default value,
styles: [
'full',
'side'
]
}
} )
.then( ... )
.catch( ... );
See the result below. You can change the styles of images through the image's contextual toolbar.
{@snippet features/image-style}
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 (e.g. mobile browsers).
While semantical styles can be combined with manual [image resizing](#resizing-images), these features were not designed to be used together.
If you want to enable image resizing, use [presentational image styles](#presentational-styles).
Presentational styles do not add any special meaning to the content. They directly control the visual aspect of an image.
Currently, the available presentational styles are "align center", "align left" and "align right".
ClassicEditor
.create( document.querySelector( '#editor' ), {
image: {
// Configure the available styles.
styles: [
'alignLeft', 'alignCenter', 'alignRight'
],
// You need to configure the image toolbar, too, so it shows the new style buttons.
toolbar: [
'imageStyle:alignLeft', 'imageStyle:alignCenter', 'imageStyle:alignRight',
'|',
'imageTextAlternative'
]
}
} )
.then( ... )
.catch( ... );
The code sample above uses predefined presentational image styles: 'alignLeft', 'alignCenter' and 'alignRight'. They apply, respectively, the .image-style-align-left, .image-style-align-center and .image-style-align-right classes to the <figure> element.
See the result below:
{@snippet features/image-style-presentational}
Besides using the {@link module:image/imagestyle/utils~defaultStyles 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 {@link module:image/image~ImageConfig#styles config.image.styles} configuration option documentation.
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 {@link module:image/imageresize~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.
The plugin also gives you an ability to change the size of the image through the image toolbar. You can set an optional static configuration with {@link module:image/image~ImageConfig#resizeOptions} and choose whether you want to use a dropdown or set of the standalone buttons.
The editor offers different ways to resize images either by using resize handles or by using dedicated UI components.
In this case, the user is able to resize images via dragging square handles displayed in each corner of the image. Once image resizing was enabled, this option does not require any additional configuration.
{@snippet features/image-resize}
You can configure the editor for resizing images by handles in two different ways:
ImageResizeEditing, ImageResizeHandles, ImageResizeButtons).import Image from '@ckeditor/ckeditor5-image/src/image';
import ImageResize from '@ckeditor/ckeditor5-image/src/imageresize';
ClassicEditor
.create( document.querySelector( '#editor' ), {
plugins: [ Image, ImageResize, ... ],
...
} )
.then( ... )
.catch( ... );
import Image from '@ckeditor/ckeditor5-image/src/image';
import ImageResizeEditing from '@ckeditor/ckeditor5-image/src/imageresize/imageresizeediting';
import ImageResizeHandles from '@ckeditor/ckeditor5-image/src/imageresize/imageresizehandles';
ClassicEditor
.create( document.querySelector( '#editor' ), {
plugins: [ Image, ImageResizeEditing, ImageResizeHandles, ... ],
...
} )
.then( ... )
.catch( ... );
Both ways enable resize handles by default.
In this case, the user is able to choose from a set of predefined options. These options can be displayed in the image toolbar in form of a dropdown.
To use this option, you need to enable image resizing and configure the available {@link module:image/image~ImageConfig#resizeOptions resize options}.
const imageConfiguration = {
resizeOptions: [
{
name: 'imageResize:original',
value: null,
label: 'Original'
},
{
name: 'imageResize:50',
value: '50',
label: '50%'
},
{
name: 'imageResize:75',
value: '75',
label: '75%'
}
],
toolbar: [ ..., 'imageResize' ]
}
{@snippet features/image-resize-buttons-dropdown}
In this case, the resize options are displayed in form of separate buttons. The benefit of this solution is the smoothest UX as the user needs just one click to resize an image.
To use this option, you need to enabling image resizing and configure the available {@link module:image/image~ImageConfig#resizeOptions resize options}.
const imageConfiguration = {
resizeOptions: [
{
name: 'imageResize:original',
value: null,
icon: 'original'
},
{
name: 'imageResize:50',
value: '50',
icon: 'medium'
},
{
name: 'imageResize:75',
value: '75',
icon: 'large'
}
],
toolbar: [
...,
'imageResize:50',
'imageResize:75'
'imageResize:original',
]
}
{@snippet features/image-resize-buttons}
If, for some reason, you want to configure the editor in such a way that images can be resized only by buttons you can do so by omitting the {@link module:image/imageresize/imageresizehandles~ImageResizeHandles ImageResizeHandles} plugin. As a result, plugins setup should look like this: plugins: [ 'ImageResizeEditing', 'ImageResizeButtons', ... ] as opposed to plugins: [ 'ImageResize', ... ]. It will enable resizing image feature only by means of the chosen UI (dropdown or standalone buttons) in the image toolbar.
import Image from '@ckeditor/ckeditor5-image/src/image';
import ImageToolbar from '@ckeditor/ckeditor5-image/src/imagetoolbar';
import ImageResizeEditing from '@ckeditor/ckeditor5-image/src/imageresize/imageresizeedititing';
import ImageResizeButtons from '@ckeditor/ckeditor5-image/src/imageresize/imageresizebuttons';
ClassicEditor
.create( document.querySelector( '#editor' ), {
plugins: [ Image, ImageResizeEditing, ImageResizeButtons, ImageToolbar, ... ],
image: {
resizeOptions: [
{
name: 'imageResize:original',
value: null,
icon: 'original'
},
{
name: 'imageResize:50',
value: '50',
icon: 'medium'
},
{
name: 'imageResize:75',
value: '75',
icon: 'large'
}
],
toolbar: [
// ...,
'imageResize:50',
'imageResize:75',
'imageResize:original',
]
}
} )
.then( ... )
.catch( ... );
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 {@link module:image/imageresize~ImageResize} plugin. Read more in the Installation section.
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 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 {@link module:image/image~ImageConfig#resizeUnit config.image.resizeUnit} option:
ClassicEditor
.create( editorElement, {
image: {
resizeUnit: 'px'
}
} )
.then( ... )
.catch( ... );
{@snippet features/image-resize-px}
The {@link module:link/linkimage~LinkImage} plugin adds support for linking images. Some use cases where this is needed are:
<figure class="image">
<a href="...">
<img src="..." alt="...">
</a>
<figcaption>Image caption</figcaption>
</figure>
{@snippet features/image-link}
The image linking feature is not enabled by default in any of the editor builds. In order to enable it, you need to load the {@link module:link/linkimage~LinkImage} plugin. Read more in the Installation section.
The {@link module:link/linkimage~LinkImage} plugin is available in the {@link api/link `@ckeditor/ckeditor5-link`} package.
To add image features to your rich-text editor, install the @ckeditor/ckeditor5-image package:
npm install --save @ckeditor/ckeditor5-image @ckeditor/ckeditor5-link
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';
import LinkImage from '@ckeditor/ckeditor5-link/src/linkimage';
ClassicEditor
.create( document.querySelector( '#editor' ), {
plugins: [ Image, ImageToolbar, ImageCaption, ImageStyle, ImageResize, LinkImage ],
image: {
toolbar: [
'imageStyle:full',
'imageStyle:side',
'|',
'imageTextAlternative',
'|',
'linkImage'
]
}
} )
.then( ... )
.catch( ... );
Read more about {@link builds/guides/integration/installing-plugins installing plugins}.
The {@link module:image/image~Image} plugin registers:
'imageTextAlternative' button.'imageTextAlternative' command}'imageInsert' command} which accepts a source (e.g. an URL) of an image to insert.The {@link module:image/imagestyle~ImageStyle} plugin registers:
'imageStyle:full' and 'imageStyle:side'.The {@link module:image/imagestyle/imagestylecommand~ImageStyleCommand 'imageStyle' command} which accepts a value based on the {@link module:image/image~ImageConfig#styles image.styles} configuration option (e.g. 'full' and 'side'):
editor.execute( 'imageStyle', { value: 'side' } );
The {@link module:image/imageupload~ImageUpload} plugin registers:
'imageUpload' button which opens the native file browser to let you upload a file directly from your disk.'imageUpload' command} which accepts the file to upload.The {@link module:image/imageresize~ImageResize} plugin registers:
'imageResize' command} which accepts the target width.
We recommend using the official {@link framework/guides/development-tools#ckeditor-5-inspector 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.
The source code of the feature is available on GitHub in https://github.com/ckeditor/ckeditor5/tree/master/packages/ckeditor5-image.