Do you want to know how to center an iframe? In this article, we will explore 5 different methods.

If you know the right way of using CSS and JavaScript, there are several ways you can center an iframe. Whether you prefer CSS-only solutions or JavaScript-based approaches, you'll find suitable options below.

how to center iframe

1. Using CSS Display and Margin

This method is one of my favorite methods and I personally use it in so many different ways, and the

If you having trouble by creating an iFrame then you can our iFrame Generator tool to creata a new iFrame.

The good thing is that you just need to add two CSS property which is “Display” and “Margin".

First, add a class or ID to your iFrame you can give any name you have, even if you want to add your pet’s or friend's name you can.

<iframe id="centered-iframe" src="your_url_here"></iframe>

Now into your CSS file select the iframe using the class name or ID you just given.

Once you select the iFrame, now add the “display” property and set the value to “block”, in the second line add the margin property and just the value to “0 auto”,

  #centered-iframe {
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);

We add “0” because we do not want to add margin to iFrame top and bottom, and “auto” because we want to add an equal margin on the left and right of the iframe.

Now once you saved the CSS file and open the preview you will see that the iframe is perfectly set to the center of the screen.

2. CSS Flexbox and Justify-Content

Define a CSS class called .container (you can use any class name of your choice) that will serve as the parent container for the iframe.

In the CSS rules for the .container class, set the display property to flex. This enables the flexbox layout for the container.

Set the justify-content property to the center. This centers the iframe horizontally within the container.

Place the iframe element within the container.
      .container {
        display: flex;
        justify-content: center;
    <div class="container">
      <iframe src="your_url_here"></iframe>

In this approach, the container element acts as a flex container, and the justify-content property is set to center, which horizontally aligns its child elements. As a result, the iframe is horizontally centered within the container.

3. CSS Position

Set the CSS position property of the iframe's parent container (or the iframe itself) to relative or absolute. This allows us to position the iframe relative to its parent container or the document itself.

Set the top and left properties of the iframe to 50%%\. This moves the top-left corner of the iframe to the center of its parent container.

Apply the CSS transform property to the iframe with the valuetranslate(-50%, -50%). This translates the iframe to 50% of its own width and height to the left and up, respectively, effectively centering it within its parent container.

4. Using JavaScript to Calculate Dimensions

Add an event listener to the DOMContentLoaded event, which ensures that the JavaScript code runs after the HTML document has finished loading.

In the event handler function, retrieve the reference to the iframe element using its ID or any other suitable method.

Calculate the width of the iframe using the offsetWidth property, which gives the width of the iframe including borders and padding.

Calculate the width of the window or the container element, depending on your specific use case. You can use window.innerWidth to get the width of the window or use the parent container's width.

Set the style property of the iframe element to apply the calculated horizontal margins. In this case, we use marginLeft and marginRight to center the iframe.

Adjust any other styles or properties of the iframe as needed.

      window.addEventListener('DOMContentLoaded', centerIframe);
      function centerIframe() {
        var iframe = document.getElementById('your-iframe-id');
        var iframeWidth = iframe.offsetWidth;
        var windowWidth = window.innerWidth;
    = 'block'; = (windowWidth - iframeWidth) / 2 + 'px'; = (windowWidth - iframeWidth) / 2 + 'px';
<iframe id="your-iframe-id" src="your_url_here"></iframe>


In this method, the JavaScript code calculates the width of the iframe and the width of the window or container. By subtracting the iframe's width from the window's width and dividing by 2, we can determine the required left and right margins to center the iframe horizontally.

I hope by reading this post you understand the process of how to center an iframe, if you like this post then make sure to share it with your friends so that they can also learn this process.