+ It displays an image and provides additional functionality to manipulate it such as setting dimensions, aspect ratio, scaling, letterboxing, and more. +
+Note that when setting dimensions of the image in any of the supported ways, the settings is applied to its container rather than the image itself. Depending on the scale type and other settings, the image may be letterboxed. If you need to apply dimensions directly to the image, you can use fitXY scale type. This, and other modes are illustrated in the examples below, where the original image dimensions is 200px wide and 114px heigh.
+ +Unless you set fixed dimensions, KImg
is responsive by default.
When rendered within an inline element, the image will preserve its original dimensions.
+ +When rendered within a block element, by default the image scales to its parent element with the 'centerInside'
scale type (see below).
You can set the most common dimensions right on the KImg
via props like width
, maxHeight
, etc. See Props for a full list. Values may be either numbers or strings consisting of a numeral and valid units.The following units are supported: %, cm, em, ex, ch, in, lh, mm, px, rem, rlh, vh, vw. If you don't provide a unit, px will be used.
Alternative text (altText
) is required by default. If an image isDecorative
to truthy value. Then, alternative text won't be required and the image will be hidden from assistive technologies.
'centerInside'
scale typeScales the image uniformly and maintains its original aspect ratio so that both its width and height are equal to or less than the container.
+ +The original aspect ratio is maintained. The image can be letterboxed. It's the safest mode as it never distorts the image.
+ +'contain'
scale typeBehaves like 'centerInside'
except it ensures that at least one axis of the image fits the container exactly.
The original aspect ratio is maintained. The image can be letterboxed. This mode may distort the image by enlarging it above its original size.
+ +'fitXY'
scale typeScales X and Y axis of the image independently, so that the image matches the container exactly.
+ +This mode may distort the image as its original aspect ratio is ignored and the image won't be letterboxed. It could also distort the image by enlarging it above its original size.
+ +Note that ratio styles are based on the width information, therefore it needs to be available in some way. For example, it could be provided directly to KImg
, or you could ensure that its parent element has width by setting it explicitly or by using a block element.
You may combine aspect ratio with any of the scale types.
+ +