Responsive iframe with inline code

5 replies
I'm trying to embed a page in a markdown page that supports HTML. I can do the width to 100% but I need the height to be flexible to the length of the text.

Is that possible? It's on the same website. A Javascript solution would work for me as well.

Using HTMLy blog.

Rick
#code #iframe #inline #responsive
  • Profile picture of the author ruthmarx123
    I guess you could edit the iframe size in the code iself. But what do you think of it in general. I heard that search engines do not favor iframes?

    Gosh, I typed my comment and it disappeared!

    Anywy, I was saying that you could change the iframe size in the code itself. What I was more concerned about is how search engines will treat iframed websites. I heard they do not favor that?
    {{ DiscussionBoard.errors[11742298].message }}
  • Profile picture of the author wargamez
    You can set the height dynamically with Javascript.

    eg:
    function resizeIframe() {
    var iframe = document.getElementById("myFrame");
    iframe.height = iframe.contentWindow.document.body.scrollHeight + "px";
    }
    window.onload = resizeIframe;
    window.onresize = resizeIframe;

    The function resizeIframe is called on page load and whenever the window is resized. It uses the scrollHeight property of the content document to set the height of the iframe. This will ensure that the iframe always fits the content, even if the content changes.

    Note that this solution assumes that the embedded page and the parent page are on the same domain. If they are on different domains, the same-origin policy will prevent access to the content document, and the height of the iframe cannot be set dynamically.
    {{ DiscussionBoard.errors[11745001].message }}
  • Profile picture of the author nathandiaz
    Yes, it's definitely possible to embed a page in a markdown page that supports HTML and make the height flexible to the length of the text. Here are a few approaches you could consider:

    Use an iFrame: You can use an iFrame tag in your HTML to embed the page you want to display within your markdown page. You can set the width to 100% and the height to "auto" so that the iFrame automatically adjusts its height to match the content of the embedded page.
    Here's an example of what your iFrame tag might look like:

    html
    Copy code
    <iframe src="https://www.example.com" width="100%" height="auto"></iframe>
    Use JavaScript: If you want more control over the behavior of your embedded page, you can use JavaScript to dynamically resize the height of the iFrame based on the content of the embedded page. You can use the postMessage API to communicate between the parent and embedded pages and adjust the height of the iFrame accordingly.
    Here's an example of the JavaScript code you could use to adjust the height of the iFrame:

    javascript
    Copy code
    // Get a reference to the iFrame element
    var iframe = document.getElementById("my-iframe");

    // Send a message to the embedded page to get its content height
    iframe.contentWindow.postMessage("getHeight", "*");

    // Listen for a response from the embedded page
    window.addEventListener("message", function(event) {
    if (event.origin !== "https://www.example.com") {
    return;
    }
    if (event.data.type === "height") {
    // Set the height of the iFrame to match the content height
    iframe.style.height = event.data.height + "px";
    }
    });
    Make sure to replace "my-iframe" with the ID of your iFrame element and "https://bharatlogic.com" with the URL of your embedded page.

    I hope this helps! Let me know if you have any further questions or concerns.
    {{ DiscussionBoard.errors[11746796].message }}
  • Profile picture of the author MelissaCollin
    To make the height of an iframe flexible, you can use JavaScript. Here's an example:

    <div>
    <iframe src="<https://yourwebsite.com/yourpage.html>" frameborder="0" width="100%" id="iframe"></iframe>
    </div>

    <script>
    const iframe = document.getElementById('iframe');
    const resizeIframe = () => {
    iframe.style.height = iframe.contentWindow.document.body.scrollHeight + 'px';
    };
    iframe.addEventListener('load', resizeIframe);
    window.addEventListener('resize', resizeIframe);
    </script>

    Replace with the URL of the page you want to embed. This code will create an iframe that takes up 100% of the width of its container, and the height will automatically adjust to the height of the content in the iframe.
    {{ DiscussionBoard.errors[11753393].message }}

Trending Topics