Resizing images
The image styles feature is meant to give the user a choice between a set of styling options provided by the system (i.e. 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 into play. It is implemented by the ImageResize
plugin.
# Enabling image resizing
The image resize feature is enabled by default in the document editor build only. In order to enable it in other builds, you need to load the ImageResize
plugin. Read more in the installation section.
# Methods to resize images
The editor offers different ways to resize images either by using “resize handles” or by using dedicated UI components — either a dropdown or standalone buttons.
The ImageResize
plugin enables the 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 on-click image toolbar. You can set an optional static configuration with resizeOptions
and choose whether you want to use a dropdown or a set of standalone buttons.
# Using resize handles
In this case, the user is able to resize images by dragging square handles displayed in each corner of the image. Once image resizing is enabled, this option does not require any additional configuration.
Use the corner handles to resize the image and adjust it to the text as needed. You can also use the alignment options from the image toolbar to achieve the desired effect.
Images can also be pre-resized using styling, as observed below (the last three images are hard-set to 28% for the visual consistency of the article).
Talking birds
There are many species of birds that can mimic human speech. Among those, parrots are the most widespread in popular culture and associated with the ability of speech, although other birds like crows, mynas or starlings can, too, mimic the human voice. Some of those can only repeat w few words while others can have extensive vocabularies of over a thousand words.
The oldest recorded talking bird in history was probably a parakeet living 2,500 years ago, described by the Greek historian Ctesias. While the birds' ability to repeat human words is undebatable, there is a debate whether they understand the language at all and if so — to what extent.
Well known talking birds species
The sun parakeet (lat. Aratinga solstitialis), also known in aviculture as the sun conure, is a medium-sized, vibrantly colored parrot native to northeastern South America. The species is currently unfortunately endangered due to loss of habitat and hunting for trade.
Sun parakeets are social birds, forming large flocks, communicating both verbally and non-verbally and flying large distances together. Even though they are regarded as talking birds, their speech and ability to learn tricks in captivity are quite moderate.
Listen to this sun concure, who does not want to be stroked.
The cockatiel (lat. Nymphicus hollandicus), also known as the quarrion, is a small parrot originating from Australia. Those cute birds with a characteristic tip on their heads are highly appreciated as household pets all around the world. They should be treated with great care.
Some consider cockatiels to be difficult to train in the arcana of human speech, but in fact, their soft voice requires better skills from the trainer to understand what they mimic.
Listen to Butters, the talking cockatiel.
The budgerigar (lat. Melopsittacus undulatus) is a small, long-tailed, seed-eating parrot usually nicknamed the budgie, and known in American English as the parakeet. They are often kept as pets — even more often than cockatiels. Males of the species are among the top five parrot species with the ability to talk. Their mimicry is impressive: a male parakeet owned by Camille Jordan appeared in the 1995 Guinness World Records book as the bird with the largest vocabulary, consisting of 1,728 words!
Listen to this amazing budgie named Kiwi, using three dozen words.
You can configure resizing images by handles in two different ways in the CKEditor 5 WYSIWYG editor:
-
Either by installing the
ImageResize
plugin, which contains all needed features (ImageResizeEditing
,ImageResizeHandles
,ImageResizeButtons
) as described in the installation of this guide. -
Or by installing the combination of
ImageResizeEditing
andImageResizeHandles
plugins, that won’t load the unnecessaryImageResizeButtons
plugin:
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.
# Using resize dropdown
In this case, the user is able to choose from a set of predefined options. These options can be displayed in form of a dropdown in the image toolbar available after the user clicks the image.
To use this option, you need to enable image resizing and configure the available resize options. Then add the dropdown to the image toolbar configuration.
const imageConfiguration = {
resizeOptions: [
{
name: 'resizeImage:original',
value: null,
label: 'Original'
},
{
name: 'resizeImage:40',
value: '40',
label: '40%'
},
{
name: 'resizeImage:60',
value: '60',
label: '60%'
}
],
toolbar: [ ..., 'resizeImage' ]
}
Try out the live demo of the resize dropdown available in the image toolbar below.
Images in the example below were prepared to match the exact aspect ratios, so they can be displayed together, with equal heights.
If you want to define the possible aspect ratios of the inserted images, for example allow the user to insert 1:1 and 40% width, and 1:2 and 20% width images, you should use the image style feature.
The example of CSS fixing the image aspect ratio is in the last example of this guide.
# Using standalone resize buttons
In this case, the resize options are displayed in the 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 enable image resizing and configure the available resize options. Then add appropriate buttons to the image toolbar configuration.
const imageConfiguration = {
resizeOptions: [
{
name: 'resizeImage:original',
value: null,
icon: 'original'
},
{
name: 'resizeImage:50',
value: '50',
icon: 'medium'
},
{
name: 'resizeImage:75',
value: '75',
icon: 'large'
}
],
toolbar: [
...,
'resizeImage:50',
'resizeImage:75',
'resizeImage:original',
]
}
Try out the live demo of the individual resize buttons available in the image toolbar below :
# Disabling image resize handles
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 ImageResizeHandles
plugin.
As a result, your plugin setup should look like this: plugins: [ 'ImageResizeEditing', 'ImageResizeButtons', ... ]
as opposed to plugins: [ 'ImageResize', ... ]
.
This will enable the image resize feature only by means of the chosen UI: either a 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: 'resizeImage:original',
value: null,
icon: 'original'
},
{
name: 'resizeImage:50',
value: '50',
icon: 'medium'
},
{
name: 'resizeImage:75',
value: '75',
icon: 'large'
}
],
toolbar: [
// ...,
'resizeImage:50',
'resizeImage:75',
'resizeImage:original',
]
}
} )
.then( ... )
.catch( ... );
# Markup and styling
When you resize an image, the inline width
style is used and the <figure>
element 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">
elements to make it take up 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 the content stays responsive when displayed in places other than the WYSIWYG editor. When the user made an image take up, for example, 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 up 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 up, 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',
resizeOptions: [
{
name: 'resizeImage:original',
label: 'Original',
value: null
},
{
name: 'resizeImage:100',
label: '100px',
value: '100'
},
{
name: 'resizeImage:200',
label: '200px',
value: '200'
}
]
}
} )
.then( ... )
.catch( ... );
The following demo uses CSS to set up the fixed image aspect ratio, so a 200px wide image automatically gets the same height.
.ck.ck-content .image {
position: relative;
}
.ck.ck-content .image img {
position: absolute;
left: 0;
top: 0;
width: 100%;
height: 100%;
object-fit: cover;
border-radius: 50%;
}
.ck.ck-content .image::before {
content: '';
padding-top: 100%;
display: block;
}
Check out the difference in the live demo below:
Poppies on the Wheat
Helen Hunt Jackson
Along Ancona's hills the shimmering heat,
A tropic tide of air with ebb and flow
Bathes all the fields of wheat until they glow
Like flashing seas of green, which toss and beat
Around the vines. The poppies lithe and fleet
Seem running, fiery torchmen, to and fro
To mark the shore.
The farmer does not know
That they are there. He walks with heavy feet,
Counting the bread and wine by autumn's gain,
But I,—I smile to think that days remain
Perhaps to me in which, though bread be sweet
No more, and red wine warm my blood in vain,
I shall be glad remembering how the fleet,
Lithe poppies ran like torchmen with the wheat.
# Installation
The image resize feature is enabled by default in the document editor build only. To enable it in other editor builds, you need to install the ImageResize
plugin, which contains all needed features (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( ... );
# Common API
The ImageResize
plugin registers:
- Image resize buttons (to use in the image toolbar).
- The
'resizeImage'
command that 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/tree/master/packages/ckeditor5-image.
Every day, we work hard to keep our documentation complete. Have you spotted an outdated information? Is something missing? Please report it via our issue tracker.