---
title: Examples
slug: wip/4Ibg-examples
createdAt: 2023-07-20T14:45:03.180Z
---

# Examples

## Basic Usage

Even though a Media Preview can be used to only display an image, its full potential is leveraged when additionally providing a video clip.

:::hint{type="info"}
**💡 Pro Tip**

The Media Preview does playback the video only when it is `active` . In the example below the Media Preview's active state is controlled by a user's hover gesture.
:::

:::Iframe{iframeHeight="0" code="<iframe title=&#x22;Embeded content from codepen.io&#x22; class=&#x22;no-border max-full-width full-height flex-grow&#x22; src=&#x22;https://codepen.io/redbulldesign/embed/VwrqYqx/e156b360bb0ad779ac443f8d5b9e553e?default-tab=html%2Cresult&#x26;amp;editable=true&#x26;amp;theme-id=39623&#x22; height=&#x22;458px&#x22; loading=&#x22;lazy&#x22;></iframe>"}

:::

**Best Practices:**

- Images should always be paired with an alternative text to support assistive technologies.

## Mode and Appearance

By default, the Media Preview's background adapts the color to a light environment but can be changed to make it more visible in a dark mode.

:::hint{type="info"}
**💡 Pro Tip**

The appearance of a Story Card can be set using the appearance property or by nesting it into a cosmos-mode component. Read more about the [Mode & Appearance](https://cosmos.redbull.design/wip/mode-and-appearance) concept.
:::

:::Iframe{iframeHeight="0" code="<iframe title=&#x22;Embeded content from codepen.io&#x22; class=&#x22;no-border max-full-width full-height flex-grow&#x22; src=&#x22;https://codepen.io/redbulldesign/embed/zYPXEBG/6526aeeb1b20d81e9a4b73f12eb95991?default-tab=html%2Cresult&#x26;amp;editable=true&#x26;amp;theme-id=39623&#x22; height=&#x22;582px&#x22; loading=&#x22;lazy&#x22;></iframe>"}

:::

## Preview Image

To improve the user experience, a Media Preview can display an image preview while waiting for the high-resolution image to be loaded.

*Note: The example below demonstrates the rendering of the preview image only. In a production environment, a high-resolution* `_image_` *would be set alongside.*

:::Iframe{iframeHeight="0" code="<iframe title=&#x22;Embeded content from codepen.io&#x22; class=&#x22;no-border max-full-width full-height flex-grow&#x22; src=&#x22;https://codepen.io/redbulldesign/embed/ZEaVYPp/5cd09ada2d4774a07c60ed94bb808573?default-tab=html%2Cresult&#x26;amp;editable=true&#x26;amp;theme-id=39623&#x22; height=&#x22;393px&#x22; loading=&#x22;lazy&#x22;></iframe>"}

:::

**Best Practices:**

- Image previews should be a down-scaled version of the image. It's common to provide an image that is 10% of its original size.
- Image previews should have the same aspect ratio as their original
- To ensure meaningful content is shown as fast as possible without any additional network requests preview images should be provided as base64 encoded data URIs

## Responsiveness and Performance

A great way to balance client-side performance and responsive images is by using `srcset` in conjunction with `sizes` and `loading` attributes. Read more about responsive image best practices here: [Image](https://cosmos.redbull.design/wip/image)

:::Iframe{iframeHeight="0" code="<iframe title=&#x22;Embeded content from codepen.io&#x22; class=&#x22;no-border max-full-width full-height flex-grow&#x22; src=&#x22;https://codepen.io/redbulldesign/embed/gOQzZqd/b42629241507f3d66fb95c39ef069e59?default-tab=html%2Cresult&#x26;amp;editable=true&#x26;amp;theme-id=39623&#x22; height=&#x22;458px&#x22; loading=&#x22;lazy&#x22;></iframe>"}

:::

## Adaptive Sizing

By default, the Media Preview always takes the available width and calculates its height based on the image that is displayed.

But it can adapt to any size when a different dimension is specified while ensuring that the media displayed is covering the available space.

:::hint{type="info"}
**💡Did you know**

Grab the handle in the lower-right corner of the container in the example below and see the adaptive sizing behavior of the Media Preview in action.
:::

:::Iframe{iframeHeight="0" code="<iframe title=&#x22;Embeded content from codepen.io&#x22; class=&#x22;no-border max-full-width full-height flex-grow&#x22; src=&#x22;https://codepen.io/redbulldesign/embed/oNoJgOq/0107b427ad78e56ec300a339f01fa322?default-tab=html%2Cresult&#x26;amp;editable=true&#x26;amp;theme-id=39623&#x22; height=&#x22;720px&#x22; loading=&#x22;lazy&#x22;></iframe>"}

:::

**Best Practices:**

- Specify a specific size for the Media Preview directly or make it adapt to a container's dimension by setting its height to 100%

## Listen to Resize Events

When initially mounting to the DOM and whenever the Media Preview changes its dimensions, it emits a `resize` the event including the current width and height.

A common use case is to update the image to make it perfectly fit the available space.

:::Iframe{iframeHeight="0" code="<iframe title=&#x22;Embeded content from codepen.io&#x22; class=&#x22;no-border max-full-width full-height flex-grow&#x22; src=&#x22;https://codepen.io/redbulldesign/embed/RwjEENV/d855b701a672c0ebb9de11a80f834ef6?default-tab=html%2Cresult&#x26;amp;editable=true&#x26;amp;theme-id=39623&#x22; height=&#x22;414px&#x22; loading=&#x22;lazy&#x22;></iframe>"}

:::

**Best Practices:**

- `observe-resize` has to be enabled in order for the `resize` event to be emitted
- Only enable the `observe-resize` when needed

