Skip to content

CdsResponsive

New in v4.0

A frame with constant proportions. Set aspect-ratio and the height of the block is calculated from its width on any screen. Anything that needs a fixed shape goes inside: a video, a map, an iframe, a category tile. The space for the content is taken right away, so the layout does not jump while it loads.

Storybook GitLab
Test coverage: 100% — good coverage
API in specs
11 / 11 props, events, slots and exposed members with a test
Test cases
11 unit · 0 browser
Code coverage
100% lines · 100% branches measured in v4.0.0-alpha.13
A11y (axe)
1 recommendations in 9 stories measured in v4.0.0-alpha.11

For photos take CdsImage — it is built on CdsResponsive and can also do lazy loading, a placeholder and cropping. If you need the columns of a page rather than the shape of a single block, take CdsGrid.

Variations ​

Aspect ratio ​

The aspect-ratio prop accepts a fraction (16/9) or a number (1.7778). The width comes from the parent, the height is calculated by the component itself — and recalculated on every change of the width. The calculated height works as a minimum: content that does not fit into it stretches the frame and breaks the proportions.

Sizes ​

By default the frame takes the whole width of its parent. The width, max-width and min-width props limit it, and height, max-height and min-height limit its height; numbers turn into pixels, strings go into CSS as they are.

Layout ​

The inline prop turns the frame into inline-flex — it stops stretching and stands in a row with its neighbors. Set the width yourself in this case: it will not take it from the content.

Slots ​

The height of the frame is held by an invisible spacer, and the content lies on top of it. Hence two slots with different jobs.

SlotDescription
defaultThe main content inside a wrapper — it takes the whole frame
additionalContent without a wrapper, on top of the spacer — for backdrops and absolute layers

The content-class prop puts classes on the wrapper of the default slot — that is how the inner paddings and the layout are set. The root of the component is positioned (position: relative), so the elements from additional can be placed absolutely relative to the frame.

Guidelines ​

Scenarios ​

An embedded map or video ​

Maps and video players arrive in an iframe with proportions of their own. Wrap the iframe into a CdsResponsive with the right aspect-ratio and stretch it with the h-full w-full classes — the player will look the same on a wide monitor and on a phone.

A grid of tiles ​

In a grid of categories the tiles have to be square regardless of the length of the caption. The aspect-ratio="1" value gives a square, and the width of every tile comes from the grid column — the number of columns changes the size, not the shape.

Space for a widget while it loads ​

A report, a chart or a map arrives from the server a second or two later. A frame with proportions takes their place right away: while the loader spins inside, the caption and the buttons below the widget stay exactly where they will end up after the loading.

Switching the proportions ​

The aspect-ratio value is reactive and the change of the height is animated — a crop switcher does not jerk the neighboring blocks.

API ​

Props

NameTypeDefaultDescription
aspectRatiostring | number | undefined—Aspect ratio of the content. For example 1.7778 (16/9) or '4/3'.
contentClassany—CSS class applied to the content wrapper.
heightstring | number | undefined—Component height
inlineboolean | undefined—Renders the component as an inline block (inline-flex) instead of a block one.
maxHeightstring | number | undefined—Maximum component height
maxWidthstring | number | undefined—Maximum component width
minHeightstring | number | undefined—Minimum component height
minWidthstring | number | undefined—Minimum component width
widthstring | number | undefined—Component width

Slots

NameTypeDescription
additionalanyExtra content rendered on top of the sizer, outside the content wrapper.
defaultanyThe main content of the component, wrapped so the aspect ratio is kept.