Ways to adjust the content width within an iframe without altering the iframe's overall width

I need help with adjusting the width slider in my iframe. I want the width slider to control the content width within the iframe, not the actual width of the iframe itself. Essentially, I want to switch between mobile and desktop mode by resizing the content inside the iframe while keeping the iframe size fixed. How can I achieve this?

<!DOCTYPE html>
<html>

<head>
  
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  
  <style>
  
    iframe {
      width: 240px;
      height: 240px;
    }
    
    #sliders {
      background-color: #ffffff;
      position: fixed;
      bottom: 0%;
      left: 0%;
      right: 0%;
      padding-left: 5px;
      padding-bottom: 8px;
      border-top: 1px solid #000000;
    }
  </style>
  
</head>

<body>
  
  <div style="overflow-x: scroll">
    <iframe id="myIframe" src="index.html"></iframe>
  </div>
  
  <div id="sliders">
    <label>width: <input type="range" id="w" min="240" max="1024" value="240" style="width:97%" /></label>
    <label>height: <input type="range" id="h" min="240" max="1024" value="240" style="width:97%" /></label>
  </div>
  
  <script>
    const iFrame = document.querySelector('#myIframe');

const sliders = document.getElementById('sliders');
const w = document.getElementById('w');
const h = document.getElementById('h');
sliders.addEventListener('input', e => {
  const tgt = e.target.closest('[type=range]');
  if (!tgt) return; // not a slider
  iFrame.style.width = w.value + 'px'
  iFrame.style.height = h.value + 'px'
})
      
  </script>
  
</body>

</html>

Answer №1

An iframe acts as a sort of window within a webpage where external web content can be displayed. It is important to note that the content inside an iframe does not merge or become part of the main page itself.

As previously mentioned by others, there are several options available:

  • Implement appropriate CSS styling for the content to be loaded into the iframe
  • If the content inside the iframe shares the same naming conventions as the parent document, you can manipulate and access it from the parent's DOM.

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

Set the value of a static file uploader using jQuery

I am having trouble retrieving the name of an existing file in a file uploader. I have a sample in jsfiddle demonstrating what I have tried. http://jsfiddle.net/shree/a4PKG/3/ However, when I click GetImage, the file uploader always remains empty. T ...

AJAX: Learn how to execute an AJAX call using a JavaScript function

Is there a method to determine (using developer tools like Chrome, Firefox, Opera, etc) the most recent function that triggers an AJAX call? This could be helpful for debugging web applications. Appreciate your help ...

Issue with React Native and Redux: Prop values are updating without being called

I'm currently facing a problem with React Native and Redux integration. Using a Redux state to toggle a modal visibility between components seems like the most efficient solution due to its cross-component nature. The modal opens and closes as expec ...

On the second attempt to call setState within the componentDidMount method, it is not functioning as expected

As a newcomer, I am delving into the creation of a memory game. The main objective is to fetch data from an API and filter it to only include items with image links. On level one of the game, the task is to display three random images from the fetched data ...

migrate the HTML variable to PHP

Hello, as a beginner in php, I have a question regarding storing the var effort value from an html5 canvas code into a php session for transfer to mysql. Can someone provide guidance on how to achieve this? Your assistance is greatly appreciated. <di ...

The tooltip feature for icon buttons within Material UI list items is not functioning properly as anticipated

Just starting out with Material UI and React, I've encountered a strange UI problem that I can't quite figure out. Hopefully someone here can help me identify what I did wrong. My Approach: I have a List in my code where each list item has butto ...

Guide to utilizing JavaScript and JQuery for retrieving a PDF from a specific URL and subsequently uploading the file to another website

I have a link to a PDF file: http://www.example.com/HelloWorld.pdf My objective is to download the file using JavaScript/JQuery/AJAX and temporarily store it in the browser, without saving it on the user's machine. Then, I want to POST it to . To ac ...

Is it possible to call a ref from a different component in React?

I'm currently working on a React chat application and I want the input field where messages are entered to be focused every time you click on the chat box. However, the challenge I'm facing is that the chat box in the main component is separate ...

React and SASS - issue with checkbox component not being properly aligned with its label

I'm brand new to React and I'm currently in the process of converting a pure HTML page into a React component. How can I adjust the SASS stylesheet to match the original HTML layout? Here is my current React setup (the checkbox displays on the r ...

Is it possible to monitor the progress of an order placed via my website as a Flipkart affiliate?

As an affiliate for flipkart, I promote their products on my website. I am interested in being able to track the orders and purchases made by users who are redirected to flipkart from my site. Is it possible for us to obtain the order/purchase id for thos ...

Is it possible to retrieve specific information from a website using the rvest package?

I've been attempting to extract and display the review rating of a song through web scraping using rvest in R from Pitchfork. The rating for this particular song is 8.5, but unfortunately, I am encountering an issue as shown below: https://i.sstatic.n ...

What is the best way to separate my Webpack/Vue code into a vendor.js file and exclude it from app.js?

My plan is to create two separate files, vendor.js and app.js, for my project. In this setup, vendor.js will contain the core code for Webpack and Vue.js, while app.js will solely focus on the Vue.js code specific to my application. To streamline this proc ...

Why does the value of a variable not print when using setTimeout in a loop?

function x(){ for(var i=1;i<=5;i++){ setTimeout(function (i){ console.log(i) },i*1000) } } x(); I'm currently facing an issue with my code where instead of printing the variable i, it's showing "undefined". ...

The contrast between using the `$("selector").find` method and the `$("selector").contents().find()` method in jQuery

When it comes to navigation, I have developed two methods that involve obtaining the elements' offsets: $("#selector").contents().find('#'+list1).each(function(){ var offset= $(this).offset(); }); $("#selector").find('#&apos ...

The @Input() property within an Angular component is producing an empty array as its result

I have a calendar component that has a data property marked as @Input(): import { Component, OnInit, Input, OnChanges, SimpleChanges } from '@angular/core'; @Component({ selector: 'app-calendar', templateUrl: './calendar.comp ...

Adjust font size using jQuery and CSS styles

I'm currently working on a website where the body has a specified font-size property that is inherited by all elements on the page. Some tags on the page have font-sizes specified in percentages (%). Others are assigned sizes in pixels (px). I&apos ...

Fill the dropdown menu with JSON keys

My challenge involves working with an array containing objects, which are sent to the client via a node.js server integrated with mongodb. I need to extract all keys/fields from the object (such as name, surname, telephone) without their values (for exampl ...

Is there a way to retrieve the input element's value within the controller using ng-if?

After clicking on the submit button, I want the ng-if condition to activate in the DOM so that I can access the properties of the input element or apply a class to it. <div ng-app="myModule"> <div ng-controller="myController"> <div ng-if ...

"Implementing a feature where users can select multiple items by checking checkboxes, then storing the selected items in an array

In the application I'm working on, there is an array of items being displayed with checkboxes. I want to bind the state of these checkboxes (checked/unchecked) to a Vuex store in order to keep track of selected items. Snippet of Vuex action code: con ...

Transferring an HTML variable to a PHP document

My HTML file includes the following: <div class="panel-body panel-refine"> <span id="price-left" class="price-left pull-left" data-currency="&euro;">500</span> <span id="price-right" class="price-right pull-right" data-currency="& ...