To center an <iframe> horizontally in HTML, the simplest reliable method is to make the iframe a block-level element and set its left and right margins to auto.
<iframe
src="https://example.com"
width="600"
height="400"
title="Example content">
</iframe>
iframe {
display: block;
margin: 0 auto;
}
The display: block rule makes the iframe behave like a block-level element. margin: 0 auto then divides the available horizontal space equally between the left and right margins, placing the iframe in the center of its parent element.
Center an Iframe Horizontally
For most websites, this is the preferred solution:
<div class="iframe-container">
<iframe
src="https://example.com"
width="600"
height="400"
title="Embedded content">
</iframe>
</div>
.iframe-container iframe {
display: block;
margin: 0 auto;
}
The iframe must have a width that is smaller than the available width of its parent for the centering effect to be visible. For example, a 600px wide iframe inside a 1200px wide container can be centered. An iframe already taking 100% of the container has no remaining horizontal space to center.
You can also make the iframe responsive:
.iframe-container iframe {
display: block;
width: 100%;
max-width: 800px;
margin: 0 auto;
}
This allows the iframe to shrink on smaller screens while preventing it from becoming wider than 800px on larger screens.
Center an Iframe With Flexbox
Flexbox is useful when you want more control over the iframe’s position. To center it horizontally:
.iframe-container {
display: flex;
justify-content: center;
}
With the HTML:
<div class="iframe-container">
<iframe
src="https://example.com"
width="600"
height="400"
title="Embedded content">
</iframe>
</div>
justify-content: center centers the iframe along the horizontal main axis.
To center the iframe both horizontally and vertically, use:
.iframe-container {
display: flex;
justify-content: center;
align-items: center;
min-height: 500px;
}
Here, justify-content: center handles horizontal centering, while align-items: center handles vertical centering. The parent needs a defined or meaningful height; otherwise, there may be little vertical space in which to see a difference.
Center an Iframe With text-align: center
Another method is to center an iframe as an inline-level element by applying text-align: center to its parent:
<div class="iframe-container">
<iframe
src="https://example.com"
width="600"
height="400"
title="Embedded content">
</iframe>
</div>
.iframe-container {
text-align: center;
}
This can work because an iframe is commonly laid out inline by default. However, display: block with margin: 0 auto is generally clearer when the goal is specifically to center a standalone iframe. Flexbox is more appropriate when you also need alignment control or vertical centering.
Responsive Iframe Centering
A fixed iframe width can cause horizontal scrolling on phones and smaller screens. A safer responsive pattern is:
.iframe-container {
width: 100%;
}
.iframe-container iframe {
display: block;
width: 100%;
max-width: 800px;
height: 450px;
margin: 0 auto;
}
The width: 100% rule lets the iframe use the available width, while max-width: 800px prevents it from becoming excessively wide. On small screens, the iframe can shrink to fit the container.
If you need the iframe to maintain a specific aspect ratio, you can use CSS aspect-ratio:
.iframe-container iframe {
display: block;
width: 100%;
max-width: 800px;
aspect-ratio: 16 / 9;
height: auto;
margin: 0 auto;
}
Whether height: auto behaves as expected can depend on the embedded content and browser behavior, so a wrapper using aspect-ratio is often preferable for modern responsive embeds.
Common Mistake: Using margin: auto Without a Block Display
This may not center the iframe as expected:
iframe {
margin: 0 auto;
}
The reason is that automatic horizontal margins are used for block-level layout. Explicitly setting:
iframe {
display: block;
margin: 0 auto;
}
removes that ambiguity.
Another common mistake is trying to center the iframe itself while the parent container has its own width, padding, or layout rules that affect the available space. Inspect the parent element in your browser’s developer tools when the iframe appears off-center.
Best Method to Use
For a normal horizontally centered iframe, use:
iframe {
display: block;
margin: 0 auto;
}
For a responsive iframe with a maximum width, use:
iframe {
display: block;
width: 100%;
max-width: 800px;
margin: 0 auto;
}
For horizontal and vertical centering, use Flexbox on the parent:
.iframe-container {
display: flex;
justify-content: center;
align-items: center;
}
The key point is that HTML creates the iframe, but CSS controls its alignment. You do not need the deprecated <center> element. For modern HTML, use CSS margin, Flexbox, or another layout system depending on whether you need horizontal centering only or full positioning control.
You Will Also Like This
How To Create Multiple Web Pages In Html
How To Import An Html File Into Excel
How To Integrate WordPress Blog Into Html Website
How To Link A Css File To An Html File
How To Make More Than One Page In Html
How To Put An Image Beside Text In Html
How To Put Text Under An Image In Html
How To Write A Paragraph In Html
What Is The Purpose Of Html In Web Development
Which Of The Following Are Examples Of Html Objects
How To Convert Html To WordPress


