Support responsive iframes that maintain aspect ratio

Created on 10 June 2022, over 2 years ago
Updated 1 September 2024, 4 months ago

Problem/Motivation

Iframes are notably not responsive. When you set a height and width on an iframe, you have to jump through hoops to make the iframe maintain its aspect ratio properly.

There are various of CSS-only options, but fundamentally unless all of your embedded iframes are guaranteed to have the same height/width ratio, you cannot faithfully create a responsive iframe embed without javascript.

Proposed resolution

One way to achieve responsive iframes is to use jquery.fitvids.js or vanilla fluidvids.js and provision it to target specific iframes with css classname and domain name.

However there is a major problem: fitvids/fluidvids only reacts to iframes that contain specific allow-listed domain names in the iframe src attribute. As soon as you embed an iframe from a third-party website that is not in the allow-list, it is not responsive.

Therefore the proposed solution architecture would be to:

  • Expose a new field configuration setting to allow responsive iframes.
  • Create a custom Drupal Javascript behavior that implements the responsive iframe solution presented in this gist by the author of fitvids.js. The implementation must be slightly modified to be based on a data attribute we provide on the iframe, instead of by domain name.
  • While the aspect ratio calculation could easily be done server-side and provided in the rendered HTML from Drupal, we should instead do the calculation on client side, for greater portability, and to let other devs improve upon the feature in the future, for example reacting to dynamic height changes from within the iframe as viewport changes (see #360549: Auto-Detect iframe height based on target url height β†’ ).

User interface changes

In admin UI: provide a new "Responsive" configuration option exposed in admin UI for the iframe form field widget.
In frontend UI: any iframe with responsive feature enabled and a height and width attribute specified will automatically scale to 100% width of its container while maintaining the correct height via an aspect ratio calculation.

API changes

TBD

Data model changes

TBD

✨ Feature request
Status

Fixed

Version

2.0

Component

Code

Created by

πŸ‡ͺπŸ‡¨Ecuador jwilson3

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Merge Requests

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

Production build 0.71.5 2024