How Would I Crop A HTML IFrame?


Inline Frame is known as iframe in HTML. The <iframe> tag designates a rectangular area within the content where a different document, complete with scroll bars and borders, may be displayed by the browser. To embed another document inside the current HTML document, use an inline frame.

An <Iframe> element's reference can be specified using the HTML iframe name property. In JavaScript, a reference to an element is also made using the name attribute. Iframes are essentially used to display a webpage inside the one that is now shown. The URL of the document that contains the iframe is specified using the src attribute.

Syntax

Following is the syntax for HTML <iframe>

<iframe src="URL" title="description"></iframe>

For getting better understanding on how would I crop a HTML iframe, let’s look into the following examples.

Example

In the following, we are using the div, making our iframe get cropped, and making no scroll of the iframe display its output.

<!DOCTYPE html>
<html>
   <body>
      <div style="position: absolute; left: 0px; top: 0px; border: solid 2px #555; width:594px; height:332px;">
         <div style="overflow: hidden; margin-top: -100px; margin-left: -25px;">
         </div>
         <iframe src="https://www.tutorialspoint.com/index.htm" scrolling="no" style="height: 490px; border: 0px none; width: 619px; margin-top: -60px; margin-left: -24px; ">
         </iframe>
      </div>
   </body>
</html>

When the script gets executed, it will generate an output consisting of an iframe that was cropped and gets embedded on the webpage without a scrollable option.

Example

Consider the following example, where we are using the div element and CSS for the iframe to make it crop and display its output.

<!DOCTYPE html>
<html>
   <body>
      <style>
         iframe {
            position: fixed;
            top: -40px;
            left: 0;
            bottom: 0;
            right: 0;
            width: 65%;
            height: 70%;
            border: none;
            margin: 0;
            padding: 0;
            overflow: hidden;
            z-index: 999999;
         }
      </style>
      <div class="iframe">
         <iframe src="https://www.tutorialspoint.com/index.htm"></iframe>
      </div>
   </body>
</html>

On running the above script, the output window will pop up, displaying the iframe that gets cropped and displayed on the webpage with scrolling.

Example

Execute the below code and observe how we are going to crop the iframe by running the script and setting scrollable to no.

<!DOCTYPE html>
<html>
   <body>
      <style>
         body {
            margin: 0;
            padding: 0;
            height: 100vh;
         }
         h1 {
            font-family: Impact, sans-serif;
            color: #8E44AD;
         }
         iframe {
            width: 1024px;
            height: calc(100vh - 300px);
            overflow: hidden;
            margin: 0 auto;
            border: none;
         }
      </style>
      <h1 id="tutorial">TutorialsPoint</h1>
      <p>The Best E-Way Learning</p>
      <iframe id="booking-content" title="booking-content" src="https://www.tutorialspoint.com/index.htm" scrolling="no" allowfullscreen="allowfullscreen">
      </iframe>
      <script>
         let tutorial = 0;
         let element = document.getElementById('tutorial');
         while (element.nodeName !== 'IFRAME') {
            tutorial += element.offsetHeight;
            element = element.nextElementSibling;
         }
         tutorial = window.innerHeight - tutorial - 100;
         document.querySelector('iframe').style.height = tutorial + 'px';
      </script>
   </body>
</html>

Output

When the script is run, it will generate an output that includes text as well as an iframe that has been cropped, reducing the scrollable to no.

Updated on: 24-Nov-2023

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements