Thursday, June 11, 2026
HomeLanguagesJavascriptHow to change the width of an iframe to 100% using JavaScript...

How to change the width of an iframe to 100% using JavaScript ?

In this article, we are given an HTML document containing an <iframe> element and the task is to change the width of the <iframe> element to 100% with the help of JavaScript. There are two methods to change the width of the iframe which are discussed below:

Method 1: This method uses the id attribute of iframe with width property to change the width of <iframe> element. JavaScript’s code is written within the <script> tag.

HTML




<!DOCTYPE html>
<html
    
<head
    <title
        How to change the width of an 
        iframe to 100% with JavaScript? 
    </title
</head
     
<body
    <center>
        <h1 style="color:green;"
            neveropen  
        </h1
        <h3
            How to change the width of a <br>
            to 100% with JavaScript? 
        </h3
        <iframe id="iframe" height="100%" width="40%"
            src="https://www.youtube.com/embed/HzeK7g8cD0Y"
            frameborder="0" allowfullscreen> 
        </iframe>
  
        <br><br
        <button onclick="changewidth()"
            Click to change 
        </button
          
        <script
  
            // JavaScript code to change the 
            // width to 100% of <iframe
            function changewidth() { 
                var x = document.getElementById('iframe'); 
                x.style.width = "100%"; 
            
        </script
    </center>
</body>  
</html>


Output:

 Change the width of an iframe to 100%

 Change the width of an iframe to 100%

Method 2: This method uses the id attribute of the iframe with window.innerWidth property to change the width of <iframe> element. JavaScript code is written within the <script> tag.

HTML




<!DOCTYPE html>
<html
<head
    <title
        How to change the width of an 
        iframe to 100% with JavaScript? 
    </title
</head
     
<body
    <center>
        <h1 style="color:green;"
            neveropen  
        </h1
        <h3
            How to change the width of a <br>
            to 100% with JavaScript? 
        </h3
        <iframe id="iframe" height="100%" width="40%"
            src="https://www.youtube.com/embed/HzeK7g8cD0Y"
            frameborder="0" allowfullscreen> 
        </iframe>
  
        <br><br
        <button onclick="changewidth()"
            Click to change 
        </button
          
        <script
            // JavaScript code to change the 
            // width to 100% of <iframe
            function changewidth() { 
                var x = document.getElementById('iframe'); 
                x.style.width = window.innerWidth; 
            
        </script
    </center>
</body>  
</html>


Output:

 Change the width of an iframe to 100%

 Change the width of an iframe to 100%

Whether you’re preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, neveropen Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we’ve already empowered, and we’re here to do the same for you. Don’t miss out – check it out now!
RELATED ARTICLES

1 COMMENT

Most Popular

Dominic
32515 POSTS0 COMMENTS
Milvus
131 POSTS0 COMMENTS
Nango Kala
6896 POSTS0 COMMENTS
Nicole Veronica
12012 POSTS0 COMMENTS
Nokonwaba Nkukhwana
12109 POSTS0 COMMENTS
Shaida Kate Naidoo
7019 POSTS0 COMMENTS
Ted Musemwa
7262 POSTS0 COMMENTS
Thapelo Manthata
6976 POSTS0 COMMENTS
Umr Jansen
6963 POSTS0 COMMENTS