Ensuring an adjustable iframe with maximum width and height

In order to ensure that both images and iframes within a fixed height container are responsive and do not overflow vertically or horizontally, you can apply the following CSS:

img {
    max-width: 100%;
    max-height: 100%;
}

This CSS rule will prevent portrait images from overflowing vertically and landscape images from overflowing horizontally.

For iframes, one common technique is using the "padding-ratio" method, where the padding of the wrapper element is set to the aspect ratio of the iframe (e.g. 56.25% for a 16:9 video):

.iframe-wrapper {
    position: relative;
    height: 0;
    padding-top: 56.25%;
    overflow: hidden;
}

.iframe-wrapper iframe {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
}

However, while this approach allows the iframe to scale with the width of the viewport, it does not adjust properly when the height of the viewport changes. The goal is to have the iframe behave similarly to how the images respond to different screen sizes.

Answer №1

When it comes to embedding Vimeo videos on a responsive website, I have found a solution that works perfectly across various browsers:

@media screen and (max-width: 750px) {
    iframe {
        max-width: 100% !important;
        width: auto !important;
        height: auto !important;
    }
}

One of the great things about this method is that it eliminates the need for image placeholders, making the process much simpler.

Answer №2

I found the solution with this code:

<div class="video-wrapper"><iframe.......></iframe></div>

.video-wrapper {
    position:relative;
    padding-bottom:56.25%;
    padding-top:30px;
    height:0;
    overflow:hidden;
}

.video-wrapper iframe, .video-wrapper object, .video-wrapper embed {
    position:absolute;
    top:0;
    left:0;
    width:100%;
    height:100%;
}

Source:

Answer №3

If you're looking for a solution, I have one that may work for you. However, you may need to compromise on the "padding ratio" technique as it might not be necessary ;)

Here is an example of HTML code that you can use:

<div class="embedded-video">
      <img class="ratio-img" src="http://placehold.it/16x9" alt="16:9 Image" />
      <iframe src="//www.youtube.com/embed/I_dN9IpmOZU" frameborder="0" allowfullscreen align="center"></iframe>
    </div>

And here is the corresponding CSS code:

.embedded-video {
    position: relative
}

.embedded-video .ratio-img {
    display: block;
    width: 100% !important;
    height: auto !important;
}

.embedded-video IFRAME {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
}

You can check out a demo of this solution here

Answer №4

Is it possible to limit the height to a maximum height while preserving the aspect ratio? This clever technique achieves just that, and here's how you can implement it. The code I am about to share is not mine, but it has worked effectively for me:

https://codepen.io/shshaw/pen/uzlfh

Below is the code (with slight modifications) for reference: My key adjustment involves using vh units instead of a percentage.

/* Adapted from https://codepen.io/shshaw/pen/uzlfh - thanks Shaw! */

.responsive-embed {
  height: 45vh; /* Specify height */
  display: inline-block; /* Must be inline-block */
  position: relative; /* Keep the child element contained */
  
  /* min/max-width will break the aspect ratio, but otherwise work as expected */
  /*
  min-height: 200px;
  max-height: 400px;
  */
}

.responsive-embed .ratio {
  height: 100%; /* Our ratio canvas is expanded to match the specified height above. */
  width: auto; /* Allows width to adjust based on height, maintaining aspect ratio */
  visibility: hidden; /* Prevent non-transparent image or alt text from appearing */
  text-align: left;
}

.responsive-embed iframe {
  /* Make child block same size as parent */
  position: absolute !important;
  top: 0 !important; left: 0 !important;
  width: 100% !important;
  height: 100% !important;
}
<div class="responsive-embed">
  <img class="ratio" src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAJCAAAAAAeQfPuAAAAC0lEQVQYGWMYrAAAAJkAAWzZLOIAAAAASUVORK5CIIA=" alt="16x9">
  <iframe src="https://player.vimeo.com/video/20732587/?api=0&amp;portrait=0&amp;autoplay=0&amp;color=21abb9" width="100%" height="100%" frameborder="0" webkitallowfullscreen="" mozallowfullscreen="" allowfullscreen=""></iframe>
</div>

Answer №5

For those who are working with or thinking of using the UIkit framework, you can quickly resolve this issue by including the uk-responsive attribute:

   <iframe uk-responsive src="...">

It's a simple solution.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Height of video isn't increasing

Since moving my website to a new domain, I've noticed that the video on my NEW About Us page is shrinking for some reason. It seems like the div holding the iframe is not expanding beyond 181px. When it does grow, the video adjusts itself and expands ...

What is the best way to eliminate the white border from my website's code?

If you'd like to see the code in action, check out my JSFiddle html, body { height: 100%; } #background { height: 100%; background: url(http://phosproject.com/wp-content/themes/phos_theme/images/7.jpg); no-repeat center cente ...

angular application developed using HTML, CSS, and JavaScript

With my current Angular project, I am working with 4 components (comp1, comp2, comp3, comp4). I came across a code snippet for a form consisting of HTML, CSS, and JavaScript that I want to integrate into comp1. Can you please confirm if I should place the ...

How do I incorporate pagination into a PL-SQL dynamic content table in Oracle Apex?

I've successfully created a PL-SQL dynamic content report in Oracle Apex, but I'm struggling with implementing pagination. With too many rows to display at once, adding pagination will greatly enhance the user experience. Here is a snippet of my ...

Steps to create a clickable button wrapper label in React

Contained within the components below: <CheckboxGroupLabel htmlFor={option.label}> <FormCheckbox onClick={() => onChange} key={option.label} defaultChecked={defaultChecked} {...rest} /> {option.value} ...

Is it possible to use jQuery to search an unordered list by simply pressing a key on the keyboard?

I have a list of countries displayed as an unordered list inside a table column. I would like to implement keyboard search functionality for quicker navigation within the list. <ul class="country-search"> <li id="1">Country 1</li> ...

What is the best way to insert text between the logo and menu items using bootstrap?

I've attempted several methods without much success. Here is a snippet of the code: <nav class="navbar navbar-default navbar-fixed-top"> <div class="container-fluid"> <div class="navbar-header"> <button type="button" ...

Creating flexible items with a 1:1 ratio and ensuring that the padding on the left and right of the flex container remains consistent

Whenever I adjust the size of the window, https://i.sstatic.net/Fm3d1.png the flex-container ends up with uneven padding on the left and right, which is less than ideal. So, I am looking for a way to allow the flex-item to resize when the window size c ...

I am dealing with a set of divs wrapped in a flex container that tend to overlap each other. Their heights vary and sometimes they wrap multiple times. Any suggestions on how to prevent this

I am encountering an issue where the width of the div remains the same after it has wrapped, causing overlapping. I want the first div to push the second to the right and so on. When I add a 100% width, it makes the width for the smallest div the same as ...

Styling with CSS: How to Adjust Word Wrap in a Container that Resizes Dynam

I'm struggling to ensure that long lines of text break and wrap correctly within a chat feature I am developing. The crucial elements in question are .chat__messages__item and .chat__messages__body (these are all contained within a parent element set ...

When attempting to place the letter 'X' within a button element, I notice a slight downward shift in its position. This issue seems to involve a combination of

I've been working on creating a TicTacToe game using React, and I've successfully created squares for the board. However, when I click on one of the squares, it seems to move slightly downward. Any idea why this might be happening? .square { ...

How can I update the color scheme in styled components based on the active state of the component?

I'm currently working on a react accordion component that requires changing styles based on the active and inactive states. I have successfully set the state and added two props for color and active color. The goal is to change colors when the user cl ...

Adding new elements without repeating them

Having an issue with appending an element to the end of a span on my webpage. The problem arises when there are multiple spans, causing the element to duplicate itself across all spans instead of going only at the very end of the last span. Any suggestions ...

Is there a way to temporarily toggle classes with jQuery?

Incorporating ZeroClipboard, I have implemented the following code to alter the text and class of my 'copy to clipboard button' by modifying the innerHTML. Upon clicking, this triggers a smooth class transition animation. client.on( "complete", ...

Responsive navigation menu featuring dynamic dropdown menus using HTML and CSS

Looking to design a responsive HTML and CSS navigation menu with multiple dropdown menus? You're in the right place. I've scoured countless YouTube videos, but haven't quite found the perfect template for a two-part navigation menu that tic ...

Eliminate excess space around images in Bootstrap

I have an image tag with a padding applied to it. I am using the Bootstrap CSS framework. Currently, there is a white background behind the image that I want to remove and make it partially transparent instead. The image is in PNG format, so I suspect the ...

Strange behavior in Chrome when using Bootstrap Gradient

Chrome started behaving oddly with the Bootstrap class .bs-gradient, randomly changing the gradient to color breaks of black, lightblue, darkblue, and more: https://i.sstatic.net/BGabA.png This is the code for one of those boxes: <link rel="style ...

Why doesn't Bootstrap have access to the tab view obtained on _Layout.cshtml when it is clicked?

Currently, I am working on an MVC-5 project using Bootstrap and have all my links in _Layout.cshtml. I have a drop-down tabs feature like this: http://prntscr.com/76jfba (implemented using Bootstrap, visible on Visual Studio project launch). In my _Layout. ...

Is it possible to resize background images on Safari?

Encountering an issue with Safari where the background isn't scaling properly. It functions perfectly in Firefox, Chrome, and other browsers, but Safari is presenting a problem. Here's a link to the site: This issue occurs when the resolution is ...

Wrapping words in php code

Despite using word-wrap: break-word in the content-full class, the text is not appearing correctly. What could be causing this issue? Is there a solution to ensure long words break and wrap onto the next line? Interestingly, word-wrap: break-word works for ...