The Wayback Machine - https://web.archive.org/web/20241008125851/https://www.geeksforgeeks.org/how-to-create-responsive-iframes-using-css/
Open In App

How to create Responsive iFrames using CSS ?

Last Updated : 02 Aug, 2024
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

An iframe, or inline frame, is an HTML element used to embed another document or webpage within the current document. Responsive iframes can be achieved by defining the aspect ratio, which refers to the proportional relationship between the width and height of an element. To maintain the aspect ratio we will use padding-top Property.

Syntax

To calculate the aspect ratio for 16:9 ( 9/ 16 *100 = 56.25), Use 56.25% as a value to the CSS property padding-top.

padding-top: 56.25%;  /* 16:9 Aspect Ratio */
padding-top: 75%; /* 4:3 Aspect Ratio */
padding-top: 66.66%; /* 3:2 Aspect Ratio */
padding-top: 62.5%; /* 8:5 Aspect Ratio */

Responsive Iframe with 4:3 aspect ratio

It defines the element <iframe> and set the URL of the video that you want to embed in your project. The element class name box have the CSS property padding-top: 75%;. This percentage value is used to maintain a 4:3 aspect ratio.

Example: Illustration of Responsive Iframe with 4:3 aspect ratio.

HTML
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content=
        "width=device-width,initial-scale=1.0">
    <title>Iframe Aspect Ratio</title>
    <style>
        h2,
        h3 {
            text-align: center;
            font-size: 20px;
            color: green;
        }

        .box {
            position: relative;
            width: 100%;
            overflow: hidden;
            padding-top: 75%;
        }

        .iframe-element {
            position: absolute;
            top: 0;
            left: 0;
            bottom: 0;
            right: 0;
            width: 100%;
            height: 100%;
            border: none;
        }
    </style>
</head>

<body>
    <h2>GeeksforGeeks</h2>
    <h3>
        Responsive Iframe
        Aspect Ratio 4:3
    </h3>
    <div class="box">
        <iframe class="iframe-element" src=
"https://www.youtube.com/embed/TnV34Lfdk7k?si=gEPAYFb9CjOvFKTp">
        </iframe>
    </div>
</body>

</html>

Output:

Responsive Iframes with 16:9 aspect ratio

Define the element <iframe> and set the URL of the video that you want to embed in your project. The element class name box have the CSS property padding-top: 56.25%;. This percentage value is used to maintain a 16:9 aspect ratio.

Example: Illustration of Responsive Iframe with 16:9 aspect ratio.

HTML
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" 
          content="width=device-width,initial-scale=1.0">
    <title>Iframe Aspect Ratio</title>
    <style>
        h2,
        h3 {
            text-align: center;
            font-size: 20px;
            color: green;
        }

        .box {
            position: relative;
            width: 100%;
            overflow: hidden;
            padding-top: 56.25%;
        }

        .iframe-element {
            position: absolute;
            top: 0;
            left: 0;
            bottom: 0;
            right: 0;
            width: 100%;
            height: 100%;
            border: none;
        }
    </style>
</head>

<body>
    <h2>GeeksforGeeks</h2>
    <h3>
          Responsive Iframe
        Aspect Ratio 16:9
    </h3>
    <div class="box">
        <iframe class="iframe-element" src=
"https://www.youtube.com/embed/5YlR3yZV9bE?si=uk-Ej0LB6but4wDr">
        </iframe>
    </div>
</body>

</html>

Output:

Responsive Iframes with 3:2 aspect ratio

It defines the element <iframe> and set the URL of the video that you want to embed in your project. The element class name box have the CSS property padding-top: 66.66%;. This percentage value is used to maintain a 3:2 aspect ratio.

Example: Illustration of Responsive Iframe with 3:2 aspect ratio.

HTML
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" 
          content="width=device-width,initial-scale=1.0">
    <title>Iframe Aspect Ratio</title>
    <style>
        h2,
        h3 {
            text-align: center;
            font-size: 20px;
            color: green;
        }

        .box {
            position: relative;
            width: 100%;
            overflow: hidden;
            padding-top: 66.66%;
        }

        .iframe-element {
            position: absolute;
            top: 0;
            left: 0;
            bottom: 0;
            right: 0;
            width: 100%;
            height: 100%;
            border: none;
        }
    </style>
</head>

<body>
    <h2>GeeksforGeeks</h2>
    <h3>
          Responsive Iframe
        Aspect Ratio 3:2
    </h3>
    <div class="box">
        <iframe class="iframe-element" src=
"https://www.youtube.com/embed/578Pzg7tVF4?si=ObaYQpK2h0DWfIKZ">
        </iframe>
    </div>
</body>

</html>

Output:

Responsive Iframes with 8:5 aspect ratio

It defines the element <iframe> and set the URL of the video that you want to embed in your project. The element class name box have the CSS property padding-top: 62.5%;. This percentage value is used to maintain a 8:5 aspect ratio.

Example: Illustration of Responsive Iframe with 8:5 aspect ratio.

HTML
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" 
          content="width=device-width,initial-scale=1.0">
    <title>Iframe Aspect Ratio</title>
    <style>
        h2,
        h3 {
            text-align: center;
            font-size: 20px;
            color: green;
        }

        .box {
            position: relative;
            width: 100%;
            overflow: hidden;
            padding-top: 62.5%;
        }

        .iframe-element {
            position: absolute;
            top: 0;
            left: 0;
            bottom: 0;
            right: 0;
            width: 100%;
            height: 100%;
            border: none;
        }
    </style>
</head>

<body>
    <h2>GeeksforGeeks</h2>
    <h3>
        Responsive Iframe
        Aspect Ratio 8:5
    </h3>
    <div class="box">
        <iframe class="iframe-element" src=
"https://www.youtube.com/embed/xvUopUeVS0U?si=cUVgNCFWfNFbK98s">
        </iframe>
    </div>
</body>

</html>

Output:

Responsive Iframes with 1:1 aspect ratio

It defines the element <iframe> and set the URL of the video that you want to embed in your project. The element class name box have the CSS property padding-top: 100%;. This percentage value is used to maintain a 1:1 aspect ratio.

Example: Illustration of Responsive Iframe with 1:1 aspect ratio.

HTML
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" 
          content="width=device-width,initial-scale=1.0">
    <title>Iframe Aspect Ratio</title>
    <style>
        h2,
        h3 {
            text-align: center;
            font-size: 20px;
            color: green;
        }

        .box {
            position: relative;
            width: 100%;
            overflow: hidden;
            padding-top: 100%;
        }

        .iframe-element {
            position: absolute;
            top: 0;
            left: 0;
            bottom: 0;
            right: 0;
            width: 100%;
            height: 100%;
            border: none;
        }
    </style>
</head>

<body>
    <h2>GeeksforGeeks</h2>
    <h3>
        Responsive Iframe
        Aspect Ratio 1:1
    </h3>
    <div class="box">
        <iframe class="iframe-element" src=
"https://www.youtube.com/embed/ehXa1Hxxu3Y?si=eI-2CUYgU5rhwLDU">
        </iframe>
    </div>
</body>

</html>

Output:



Similar Reads

HTML Iframes
HTML iframes are used to embed external content such as videos, maps, or entire web pages into their web projects. This article will cover the syntax, attributes, and use cases of iframes, providing a solid foundation for leveraging them in web development. What are HTML Iframes?An iframe (short for inline frame) is an HTML element used to embed an
5 min read
How to create Responsive Profile Card using HTML and CSS ?
In this article, we are going to create a profile card from where a user can check out the basic details of other users and connect with them through different handles as well as message the user. Approach:Set up HTML with a container div containing an image and content sections.Style the container, user image, and content sections with appropriate
5 min read
How to Create Responsive Sidebar using HTML & CSS ?
We will create a responsive sidebar which is created using HTML and CSS. The responsive sidebar will be interactive for small devices also. ApproachCreate HTML and CSS files. After getting the boiler plate code title has been changed as Responsive sidebar.CSS File has been linked to HTML using link tag within head tag.Create a div with the class na
2 min read
How to Create Responsive Modal Images using CSS & JavaScript?
Modal images provide an interactive way to display larger versions of images without navigating away from the current page and it takes user attention and users can stay on our website some more time. ApproachFirst, create a basic HTML layout for your image gallery and modal pop-ups. Each image in the gallery should have a corresponding modal with
4 min read
How to Create a Responsive Image Grid using CSS?
A responsive image grid is a layout technique used to display images in a grid-like structure that adjusts dynamically based on the screen size to ensure that the grid looks good and functions well across various devices and screen resolutions. Below are the approaches to creat a responsive image grid using CSS: Table of Content Using CSS GridUsing
3 min read
how to create a responsive checkout form using CSS
Creating a responsive checkout form using CSS ensures that users can efficiently and comfortably enter their billing and payment information on any device. ApproachCreate a form that is wrapped inside a <form> tag.Inside the form create two sections for "Address Details" and "Payment Information".Include required input fields inside the form.
3 min read
Create a Responsive Admin Dashboard using HTML CSS and JavaScript
In this article, we will see how to create the Responsive Admin Dashboard using HTML, CSS & JavaScript, along with understanding its implementation through the illustration. This project involves structuring the HTML layout for navigation, sidebar, and content, styling with CSS to ensure responsiveness, incorporating interactive elements using
10 min read
How to Create a Responsive Text using CSS ?
This article will show you how to create responsive text with HTML and CSS. Responsive text means the text size will change in different screen sizes. We can create Responsive Text using CSS Media Query or using viewport width (vw). Example 1: In this example, we will create a responsive text using CSS Media Query. [GFGTABS] HTML <!DOCTYPE html
2 min read
How to create Responsive Floating Elements using CSS ?
The Responsive Floating Elements are useful for adaptable layouts on different devices. It ensures a seamless user experience by adjusting the size and arrangement of cards based on the screen size. The float property is employed to arrange the cards horizontally, making them responsive on various devices. PreviewApproachCreate the basic structure
3 min read
How to create a Responsive Inline Form using CSS ?
When the browser window is resized, the labels and inputs will stack on top of each other for smaller screens. To create a responsive inline form using CSS, use a container with flexible styling, such as Flexbox or Grid, to arrange form elements horizontally. Utilize media queries to adjust the layout for smaller screens, ensuring a user-friendly e
3 min read
Create A Responsive Navbar with Icons using HTML CSS and JavaScript
The navigation bar, or Navbar is an essential component in modern web design. It allows users to navigate through a website easily. Adding icons to the navbar is not only enhances visual appeal but also improves user experience. The Responsive Navigation bar means the Navbar elements are visible on all sizes of devices i.e. in mobile devices, the n
2 min read
How to Create a Responsive Login Form in Navbar using CSS?
A Responsive Login form in the Navbar allows users to log in from any page of the website without navigating to a separate login page. This article explains how to create a Responsive Login form within a Navbar using CSS. ApproachStart by creating a <nav> element to contain the navbar.Inside the <nav> create a <div> element for th
3 min read
How to Create a Responsive Image Gallery using CSS?
Creating a responsive image gallery using CSS is a common task in web development. A responsive gallery adjusts to different screen sizes and ensures that images look good on devices ranging from mobile phones to desktop monitors. In this article, we will explore different approaches to creating a responsive image gallery using CSS. These are the f
3 min read
How to create responsive image gallery using HTML, CSS, jQuery and Bootstrap?
With the advent of new frameworks in web technologies, it has become quite easy to design and implement feature-rich and responsive web pages. Here, we are going to design a responsive image gallery using HTML, CSS, jQuery, and Bootstrap.  Features or Functionalities to implement: Responsive imagesResponsive Grid SystemImage viewer Prerequisites: B
3 min read
How to create responsive stacked cards hover effect using CSS ?
We will create responsive stacked cards using HTML and CSS. To achieve a multi-layer stacking effect, you have to follow certain steps given below. Note: By hovering over the cards, we can achieve various directions or effects on the cards, such as top-left, bottom-right, diagonal, rotate, etc. ApproachDesign a simple card structure in HTML.Use CSS
6 min read
Create a Responsive Testimonials Accordion with Quotes in Tailwind CSS
This article demonstrates creating a Responsive Testimonials Accordion with Quotes in Tailwind CSS. After linking Tailwind CSS to the HTML file, the Testimonials Accordion with Quotes is structured and styled using Tailwind's classes. Users interact with the Testimonials Accordion with Quotes by clicking the previous and next buttons. Approach:Firs
3 min read
How to Create a Responsive CSS Grid Layout ?
A grid layout is said to be responsive when fixed-sized grid items will shift position according to the viewport size. Responsiveness gives more professional look to the website. It is necessary to out perform your website design. we are going to learn how can we create a responsive CSS grid layout. These are the following approaches: Table of Cont
4 min read
How To Create a Responsive Table in CSS ?
Creating a responsive table is important for ensuring that the table's content remains accessible and readable on devices with different screen sizes and resolutions. The below approaches can be used to make a table responsive: Table of Content Using Media QueriesUsing overflow-x propertyUsing Media QueriesIn this approach, we are using Media Queri
3 min read
Create a Responsive Animated Clock in Tailwind CSS
We will create a simple Responsive Animated Clock using the Tailwind CSS and JavaScript. We'll utilize Tailwind CSS for styling to create an attractive user interface and JavaScript for the logic. Prerequisites:HTMLTailwind CSSJavaScriptApproach: Create an HTML file named index.html and link the Tailwind CSS stylesheet for styling.Use SVG to create
2 min read
Create Responsive Column Cards with CSS
Column cards showcase articles, products, or user profiles attractively. Learning these techniques will help beginners and intermediate developers understand CSS better, creating responsive and attractive layouts. In this article, we'll explore different ways to make column cards responsive with CSS. Using CSS GridWe can create responsive column ca
3 min read
Create a Responsive Image Carousel in Tailwind CSS
Image Carousel is a UI element that acts like a slideshow by cycling through different images. You can easily design a Carousel in Tailwind CSS by using anchor tags along with proper width, height, and overflow properties. The Carousel can also be made responsive using the Tailwind responsive utility classes. Preview Image Tailwind CSS CDN Link<
2 min read
Create a Responsive Blog Layout in Tailwind CSS
A blog layout is a fundamental component of many websites, enabling content creators to showcase their articles in an organized and visually appealing manner. We will explore how to create a responsive Blog Layout in Tailwind CSS for styling purposes. Prerequisites:Tailwind CSSJavaScriptApproachFirst, Create a layout in HTML and Integrate Tailwind
9 min read
How to Create a Responsive Top Navigation Menu with CSS & JavaScript?
Top navigation menus offer users immediate access to key sections of a website. A responsive top navigation menu enhances user experience because if our website is fully responsive then it can be accessible by any device like PC, mobile, or tab. ApproachFirst, create a basic HTML structure for your web page and add some nav elements and you can add
5 min read
How to create a Responsive Bottom Navigation Menu with CSS and JavaScript.
A responsive bottom navigation menu provides mobile-friendly access to important links. We will see how to create a responsive bottom navigation menu that adjusts to different screen widths. The menu will include elements like Home, Contact, and About. When viewed on a smaller screen, a hamburger menu appears, and clicking it displays the navigatio
2 min read
How to Create Responsive Column Cards with CSS ?
Creating a responsive card layout is very beneficial in development. These cards are a great way to display content in a neat and organized manner. You can create responsive cards using the below approaches. Table of Content Using FlexboxUsing CSS GridUsing FlexboxThe display: flex property establishes a flex container, enabling flexbox layout for
3 min read
How to create a responsive scrollbox in CSS ?
The purpose of this article is to create a responsive scrollbar in HTML page structure using CSS. In CSS, a responsive scroll box is an interaction technique that contains text, images, or any other elements. They can be scrolled in preset directions, which allows users to scroll if the contents of the box are larger. Scroll boxes are often used wh
3 min read
How to create a Responsive Navigation Bar in Tailwind CSS ?
A Responsive Navigation Bar with collapsible elements is a crucial component of modern web design, allowing users to navigate seamlessly across various screen sizes. In this article, we'll explore how to implement such a navigation bar using Tailwind CSS, a utility-first CSS framework that enables rapid development and easy customization. Preview A
2 min read
How to Create Responsive Testimonials with CSS?
Testimonials are a powerful way to build trust and credibility for your brand. They showcase the positive feedback and experiences of your clients or users. To ensure your testimonials look great on all devices, it’s essential to make them responsive. Here, we’ll explore different approaches to creating responsive testimonials with CSS. These are t
3 min read
How to create responsive website zoomed out to full width on mobile using Bootstrap?
Container Classes: This is one of the predefined classes of bootstrap, that contains the entire content of the web-page in it. There are two container classes, namely, container and container-fluid classes. These classes have different properties and one can use the class that fits one's requirement. CONTAINER-FLUID: When the content of a web-page
2 min read
Create a Responsive Navbar using ReactJS
Responsive Navbar short for Navigation Bar in React JS provides a routing function for the web application that can change the layouts according to different screens. Navbar provides multiple-page navigation support for the ReactJS single-page application. Responsiveness helps in loading the Nav component for different screens and layouts. Preview
5 min read