With some providers (Youtube from what I could find) we have aspect ratio problems when we render them using the OembedFormatter.
The problem I discovered is primarily with Youtube: the resource returned from Youtube does not contain the requested width/height but the default size (or simply A size) of the video. This is fine if the formatter is configured to use a width and height that corresponds to the aspect ratio of that particular video. But that won't ever happen. When we configure the formatter to a specific max width but a height of 0, there is at least some expectation that the rendered iframe maintains the aspect ratio. At the moment, since the formatter is configured to a *max* width and no *max* height the iframe gets that that width attribute and the default height that comes from the resource. So the result is that the iframe size doesn't match the size of the video. A solution for this is to calculate the aspect ratio of the video using the values in the resource and recalculate the height attributed based on that in case the max height is configured as 0.
Discussing with @phenaproxima we might make a checkbox (default unchecked) for this in order to prevent BC.
Other providers, like Vimeo, may not have this issue. So in these cases this should not apply. But youtube is pretty important.
| Comment | File | Size | Author |
|---|---|---|---|
| #65 | 3060968-65.patch | 9.85 KB | heddn |
Issue fork drupal-3060968
Show commands
Start within a Git clone of the project using the version control instructions.
Or, if you do not have SSH keys set up on git.drupalcode.org:
Comments
Comment #2
upchuk commentedComment #3
upchuk commentedOK, updated the patch to clean up the tests + make the aspect ratio maintenance a formatter option to ensure full BC just in case.
Also updated the issue title and description to split out the responsive part of the container iframe that went into #3061134: oEmbed videos are not fully responsive.
Comment #4
phenaproximaI think, overall, I like this idea and think it would be useful. If we can do it in a backwards-compatible way, then this will improve the experience of adding and using remote videos -- it will bring them closer to the "it just works" holy grail.
Also...you wrote tests. Thank you!!! :) Will do a more detailed review when I have a little more bandwidth.
Comment #5
phenaproximaComment #6
phenaproximaAlso, re-categorizing as a feature request, since this technically is not a bug.
Comment #7
rosinegrean commentedComment #8
yogeshmpawarComment #9
yogeshmpawarResolved some coding standard issues & added an interdiff as well.
Comment #10
phenaproximaComment #12
mrpauldriver commentedConfirm that Vimeo has similar problems.
Comment #13
mrpauldriver commentedTested against 8.8.0-beta1 on simplytest.me
Vimeo
When maximum width value is empty and maintain aspect ratio checkbox is enabled.
oembed iframe does not display the video, but instead shows the test site in an iframe. Screenshot 1
When maximum width value is provided (of 1024px) and maintain aspect ratio checkbox is enabled.
Video scales to fill its container, but white space is seen above and below the video when the viewport is scaled down. Screenshot 2
YouTube
When maximum width value is empty and maintain aspect ratio checkbox is enabled.
As with vimeo the iframe does not display the video and again shows the test site in an iframe. Same as Screenshot 1
When maximum width value is provided (of 1024px) and maintain aspect ratio checkbox is enabled.
Video scales to fill its container, but letterbox effect is seen with black space above and below the video when scaled down. Screenshot 3
Patch does not apply to 8.9.x on simplytest.me
Comment #14
mrpauldriver commentedNot sure how long this simplytest.me link will stay live, but I remember hearing that they were extending the 24 hour thing
https://stm5dd3b08bb00d5-uhwftnqckcswi0euo3vliwyv3e9pzcxb.tugboat.qa/node/1
U: admin
P: admin
Comment #16
jdhaire commentedNew to this space, but we have this issue. Can anyone point me in the direction of a solution? Thanks in advance!
Comment #17
phenaproximaComment #19
karlsheaHow are people handling this in the real world? I'm trying to move some sites off of Video Embed Field, and since this has been an issue in Drupal core for like two years now it's obvious there's some way around it otherwise it would have gotten fixed already.
Comment #20
heddnThis adds a comma in large numbers (1,024) , why are we using number_format?
roundseems like it would be enough.The settingsSummary also is off.

Here we set the aspect ratio, after setting a hight of 1100 (which gets hidden):
But then the summary still says max dimensions since Maximum height was left in the form before selecting the checkbox toggle for aspect ratio:

Comment #21
heddnThis seems to resolve all the feedback from #20 and #13. I don't have the whitespace/blackspace above/below the video in my site.
Comment #22
heddnFixed patch. Interdiff was fine.
Comment #24
heddnSeems like a novice to fix the failing test. Tagging
Comment #25
anmolgoyal74 commentedComment #26
mrpauldriver commentedI am testing the latest patches against Drupal 9.1 and 9.2 but do not see any improvement when compared with my observations made in #13.
I'm slightly reluctant to formally mark as Needs work when I am up against core maintainers/contributors, but I feel like saying this needs work :)
Comment #27
mrpauldriver commentedBy using the css below in my theme, I am able to resolve all the problems. Could something like this be included in the core media module?
The padding-top value is variable, but pretty sure that 16:9 would cover the majority of remote video sources.
Source. https://www.w3schools.com/howto/howto_css_responsive_iframes.asp
Comment #28
playful commented#27 worked perfectly for me without applying any patch.
Comment #29
extect commented@MrPaulDriver: Thank you!! #27 works perfectly!
Comment #30
jonesui commented#27 is also working for me without patching.
Comment #31
kleiton_rodrigues commented@MrPaulDriver: Tks #27 works good!
Comment #32
ranjith_kumar_k_u commented#27 works fine.
Before using css

After using css

Comment #33
kreatil commented#27 solved it in my use case. Sometimes it's the simple solutions! Thanks!
Comment #35
delmarr commented#27 works well when using a media field, however when embedding a remote video in the wysiwyg, the editor window will still display a small thumbnail and which won't reflect the published page that displays the full width video.
I suspect a developer may need to update the CSS provided by ckeditor /core/modules/ckeditor/css/ckeditor-iframe.css and — depending on your setup — possibly update a twig template to provide the field name i.e., .field--name-field-media-oembed-video
Comment #36
wrd commentedI first tried the patch in #25, and got what seems like a peculiar result. The max width on the field formatter was set to 1180px (because on a page with a full-width layout, that is indeed the widest possible element on this site). I then placed a video in a layout with a 2/3-1/3 column layout, in the 2/3 column, with a width of 780px. What I wound up with was a video with a height of 664px, rather than the expected 439px. It appears that the height was based on the max width rather than the actual width.
I then tried the styling solution in #27, and it works very nicely, although I expect we'll need to adjust it should we need to use any videos that aren't 16x9.
Comment #37
yazzbe commented#27 worked for me :)
Comment #38
camhowardMany thanks to MrPaulDriver for the CSS in #27 which is a huge help!
A quick note that the first class name in his example:
.field--name-field-media-oembed-videomay not be the same in all themes.
In my themes, I found either one of the following classes works when adding a video to the body field via the Media Library:
.field-media--field-media-oembed-videoor
.field-name-field-media-oembed-videoThe second class name in his example:
.media-oembed-contentseems to be more consistent across themes.
Hope this helps!
Comment #40
gonssalCSS in #27 works fine unless you set a caption to the video. For some reason core's stable theme sets
display: tableto the.captionclass, which was a pretty bad decision honestly.So if you want the code in #27 to work with captions, you have to add:
.caption { display: block; }Comment #42
chris matthews commentedIn #4 Adam (phenaproxima) said,
Question is, what is the proper path for this feature request as far as potentially getting it committed to core?
(1) continuing to work on the initially proposed solution in the latest patch
or
(2) implementing a new solution using the custom CSS outlined in #27, #38 and #40 ?
Comment #43
stevenx commented#27 works good for me for instagram, twitter, youtube, tiktok, giphy, facebook all embeded with https://www.drupal.org/project/oembed_providers module
Comment #44
phenaproximaOK, so that's a large number of people reporting that #27 is a good fix for this problem. I have no objections to adding that CSS to the core media module. Would someone be willing to create a patch or merge request for it (against Drupal 9.5.x)?
Comment #45
chris matthews commentedPlease note the comment in #35
Comment #46
phenaproximaIn that case, let's still proceed with a patch and rescope this issue to deal only with media fields. We can file a follow-up to deal with this in CKEditor.
Comment #47
camhowardWhen a patch is created using the code in #27, is there a way to account for theme differences as described in #38 and #40? Or does that become too complex?
Comment #48
phenaproximaProbably could use advice from @lauriii or another front-end expert, but my guess is that we'd want to add the CSS to the Media module, then override that if and as needed in core themes.
Comment #49
luke.leberGiven this feature relies on resource metadata, should we postpone on https://www.drupal.org/project/drupal/issues/2983456 to prevent stale aspect ratios from becoming a problem?
We already have a ton of communication issues with our social media teams regarding making changes on the YT side that our content management team aren't aware of.
Comment #50
cgmonroe commentedPatching core is a no-go for me. Also, I needed to deal with multiple aspect ratios. I found the following solution:
Created a module with its own OEmbedFormatter field plugin based on the core version with the patch from #25, with different plugin id and settings file.
I added code to this to include an aspect-x-x (e.g. aspect-16-9) class to the iframe, e.g.:
Plus some code to add this class to the element.
Created a field--media--field-media-oembed-video.html.twig template based on the default field.html.twig template that adds the aspect class from the iframe element to the parent div, e.g.:
Then added CSS for these classes based on: https://www.w3schools.com/howto/howto_css_responsive_iframes.asp
In the media type display tab, I changed the field to use my field widget, set a reasonable max width, and enabled preserve aspect option.
A no patch solution that handles multiple video aspect ratios. Verified with youTube and Vimeo videos. This could also be expanded to handle portrait videos but I did not have that need.
Comment #51
imclean commentedJust adding some information which could be useful to this discussion.
YouTube's oEmbed response contains a width and height.
https://www.youtube.com/oembed?url=https://www.youtube.com/watch?v=nu8KX...
This width and height is the minimum size required to play the video.
When submitting a request for an oEmbed resource, the maximum width and height (maxwidth, maxheight) can be sent. If the minimum response size is larger than the maximum request size then the request should fail.
Currently the the iFrame dimensions are set by the provider to exactly the minimum size. When the maximum height and/or width is entered, Drupal sets the iFrame to exactly these dimensions.
Question: Does Drupal pass along the maximum width and height settings in the request, when entered?
Sending the maxheight and maxwidth with the requests results in a response with at least one dimension matching the max and the other maintaining aspect ratio.
https://www.youtube.com/oembed?url=https://www.youtube.com/watch?v=nu8KX...
Drupal should therefore:
Comment #52
imclean commentedI've created a new issue for #51.
Comment #53
imclean commentedTo correct myself:
This doesn't seem to be true. It's simply a default width and height, not a minimum.
Comment #55
chris matthews commentedLots of great workarounds here, but what is the path forward for a fix in core?
Comment #56
chris matthews commentedThe CSS in #27 mostly works for me, but has an issue on Bootstrap themes
I'm using:
With bootstrap themes, you can use the Field Group + Field Formatter Class modules to control the page layout by adding bootstrap .col classes to rows created with the HTML element on a Field Group. When the CSS in #27 is added, it overrides the .col classes added to the Field Group row and therefore is always displayed at full 12 column width. So, while #27 is a workaround for some themes, it doesn't work for all themes. Therefore, it seems like the proposal in #51 seems like a better path forward.
Comment #57
imclean commented@Chris Matthews, there's an issue which deals with max_width and max_height settings and should also resolve the aspect ratio problem in most cases. #2966656: Negotiate max width/height of oEmbed assets more intelligently
Comment #58
xjmComment #59
romulasry commentedYeah I would love for this to be fixed.
Comment #60
imclean commented@romulasry you can try the patch in #2966656-34: Negotiate max width/height of oEmbed assets more intelligently to see if it resolves the issue for you.
Comment #61
romulasry commented@imclean don't have the time at the moment.
Comment #62
hudriModern browsers support the new aspect-ratio property. Example for a responsive 16:9 video iframe, with a very unobtrusive 0-0-1 CSS selector specifity:
#27 which still works excellent, even with less modern browsers like SafarIE (pun intended)
Comment #63
tormi#27 required additional
position: static;override for.contextual-regioninweb/themes/custom/<olivero_subtheme>/css/components/embedded-media.pcss.csswhen using Olivero subtheme to fix the missing video preview in node editing mode:#62 worked flawlessly with Drupal version
10.0.7.Comment #65
heddnRe-roll. But I'm also testing out the css in #62.
Comment #66
jwilson3When would you ever not want to maintain the intended original aspect ratio?
Comment #67
imclean commented@jwilson3, this may change the size of embedded videos on existing sites when they upgrade.
Comment #68
lauriiiComment #69
joelpittetI agree with @jwilson3 in #66. This should be default to always use the aspect ratio. That said, if there is no value set for this setting (on existing sites) it should remain off as mentioned in #67.
It's unfortunate that the default settings are always merged, this is why we can't have nice things *whines*.
It seems like this should be set the default for new installs to be TRUE and maybe we need an update hook to set it to FALSE for existing sites? Would that work?
Comment #70
imclean commentedA related issue which could resolve this issue has been committed to 10.3 and 11. I've added some screenshots of before and after for comparison. See #2966656-31: Negotiate max width/height of oEmbed assets more intelligently
In a nutshell: oembed providers support maintaining aspect ratio at different sizes using query parameters maxwidth and maxheight. The above issue takes this into account so the width and height of the iframes will be set correctly.
Comment #71
dalemoore commentedSo is this issue considered closed and what's linked by @imclean is the solution (setting a maxwidth and maxheight manually in the field display)?
FYI, the solution in #62 should not be the solution, as this affects all oEmbed iframes. I tested it out and my Podbean oembed was sized to 16/9 so there was now a huge whitespace gap up under the podcast player. May work fine for YouTube videos that happen to be 16/9, but not all videos will always be 16/9.
Comment #72
handkerchiefI'm using this scss (copied from bootstrap 5):
Comment #73
mortona2k commentedYoutube is not only 16:9, we also have shorts now.
I think we can use the height/width in the oembed response to set the aspect ratio on either the iframe or container.
I don't really think it should be a style attribute, but here is a working proof of concept:
With this, the video can grow or shrink and the height is not a fixed size. You probably want a way to select whether it's displayed as full width or not by setting width: 100%;
Out of the box, the Remote video display is set to max height/width 0, which returns the defaults from the oembed provider.
That's pretty small for youtube, so set it to the max that you would want to display for your layout.
I set it to 1200 x 600. This means I can use a regular 16x9 video that will fill my 200px main column, or a short video that is narrow and max 600px tall.
A big question is whether the height/width values are reliable from the provider. Another is how to set overrides if desired.
For other oembed content, like codepen or spotify, I'd want settings like full width, fixed height, respect aspect ratio.
I think the way to implement this is to set a variable for aspect ratio and use that in .media-oembed-content.
I was able to add those as style options on embedded media, with notes from:
https://www.drupal.org/project/drupal/issues/3406020
https://www.drupal.org/project/drupal/issues/3117172#comment-15720466
Currently we only have display modes to select these kinds of options for embedded content.
Comment #74
laura.gatesJust adding to the above conversation, I got around this by adding a field to media asking users to select what aspect ratio they want:
I then themed as normal for my different display modes.
Comment #75
pcambraI've faced a somehow related issue with Floursish oEmbeds, not in terms of aspect ratio but in terms of the maxwidth/maxheight not overriding the default height and weight, for example:
https://app.flourish.studio/api/v1/oembed?url=https://public.flourish.st...
Should display a width of 960px but it shows the default 700px as it is forced by the weight attribute in the returned JSON file.
Adding a solution like #27 for the type_rich would be my suggestion to solve this.
Comment #77
leeksoup commented#27 worked great for me as well. Thank you, @mrpauldriver!
Amazing that this is still an issue in late 2025, Drupal version 10.5.6.
Comment #78
mayurgajar commentedComment #80
mayurgajar commentedComment #81
idiaz.ronceroI am also facing this problem, here is my recap of options tried and a proposal for an alternative fix.
The original problem/s
The oEmbed is "an iframe framing an iframe", so the outermost iframe should have the information about the aspect ratio returned by the provider.
In any case, the root of all problems is that this is being implemented as width and height HTML attributes, which do not work as an aspect ratio does. Also, iframes are known to need explicit heights - you can't just let
width: 100%andheight: autoto make them fit, as images do. In absence of a height, they will come down to a browser-defined height which usually is very small (150, see MDN)The sum of all of this is that a 1600x900 iframe inside a 1000px wide container does not become the expected 1000x526 video but a 1000x900 one as the width decreases but the height does not.
This becomes worse as the viewport narrows and you can end up with a 300x900 unwatchable video player on mobile.
To make things worse, providers such as Youtube return by default "mininum viable" sizes, as tiny as 350x200.
Drupal Core
Core proposes to fix the "tiny sizing" issue by letting you define desired max width and heights, that are apllied both on the resource query and, in the DOM, as HTML attributes on both iframes (the oembed and the provider ones).
This fixes the tiny sizing but do not fix the aspect ratio issue.
(Also: for youtube it won't work if you just set the max-width, as it throws the error "Input value "max_height" is invalid and flag "FILTER_NULL_ON_FAILURE" was not set.")
Workaround on #27 and #50
The CSS workaround is ok and could be a minimal acceptable solution, but is not an actual fix. It will use the padding trick to set a fixed aspect ratio of 16:9, independently oif the original aspect ratio. The fix on #50 is better but depends on a shortlist of aspect ratios, while we can receive actually any possible value, so values outside the shortlist will fail to apply.
MR 13951 on #79
I have tried the MR in https://git.drupalcode.org/project/drupal/-/merge_requests/13951 and it does not fully work for me
It makes max-height optional, but the youtube video is tiny again (325x200px) and aspect ratios are not preserved.
A proposal
I think actually #73 is the best way around this, even if it implies setting inline CSS.
- It will respect any other sizing present on CSS or via width (aspect ratio does not dictate width, it will just calculate the corresponding height)
- It will accept any possible ratio that can come from the external source.
- It will be responsive out of the box and preserve the original appearance of the video, without the "black box" effect (the black bacgkround seen on the edges when the video size and frame size don't match)
- It is minimal and can be manually overriden (see next:)
To make things easily overrideable, it could be set as a CSS variable intead of direct inline css, something like this:
With the additional benefit that we could have, as in #27, a globally defined fallback aspect ratio of 16:9 in case anything fails:
Which, by the way, is just as easy to override if you need a different one on your theme!
In the future
With modern css, aspect ratio could even be directly read from the attributes, but this needs the
type(<number>)conversion from string to integer that only works for some browsers.Comment #82
idiaz.ronceroCreated a new MR that implements #81 as a proof of concept.
Working correcly on my side.
Missing tests, will updated it asap but I wanted to have a POC so people can try it.
Comment #85
chris matthews commentedFor anyone using a Bootstrap 5 theme, this works great:
But, it of course not a global solution for everyone.