Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 5 additions & 17 deletions css/display-responsive-iframe-maintaining-aspect-ratio.md
Original file line number Diff line number Diff line change
@@ -1,38 +1,26 @@
# Display Responsive iframe Maintaining Aspect Ratio
# Display Responsive iframe Maintaining Aspect Ratio modern way

Generally when rendering an iframe, you'll specify the `width` and `height`
properties to give it a fixed display size.

You can make the iframe responsively expand to the full width of its parent
while maintaining its aspect ratio using a little CSS.

First, remove the `width` and `height` properties.

Second, add a wrapping iframe container:
1. First, remove the `width` and `height` properties.
2. Second, add a wrapping iframe container:

```html
<div class="iframe-container">
<iframe src="https://www.youtube.com/embed/7LDlUMMbv6k" ...></iframe>
</div>
```

Third, sprinkle on a little CSS to make it responsive:
3. Sprinkle on a little CSS to make it responsive:

```css
.iframe-container {
position: relative;
overflow: hidden;
/* 16:9 aspect ratio */
padding-top: 56.25%;
}

.iframe-container iframe {
position: absolute;
width: 100%;
height: 100%;
top: 0;
left: 0;
border: 0;
aspect-ratio: 16 / 9;
}
```

Expand Down