How to center an iframe horizontally?
Asked Answered
U

14

315

Consider the following example: (live demo)

HTML:

<div>div</div>
<iframe></iframe>

CSS:

div, iframe {
    width: 100px;
    height: 50px;
    margin: 0 auto;
    background-color: #777;
}

Result:

enter image description here

Why the iframe is not centrally aligned like the div? How could I centrally align it?

Unsuspected answered 3/12, 2011 at 10:11 Comment(1)
why not wrap a div for this iframe?Grandstand
M
489

Add display:block; to your iframe css.

div, iframe {
    width: 100px;
    height: 50px;
    margin: 0 auto;
    background-color: #777;
}

iframe {
    display: block;
    border-style:none;
}
<div>div</div>
<iframe src="data:,iframe"></iframe>
Marnie answered 3/12, 2011 at 10:14 Comment(5)
So, alternately, you can give the container a text-align: center rule, correct?Identity
yes adding text-align: center to the container would work tooAnyplace
I found I also had to add margin: auto;Telic
class="d-block mx-auto" on my <iframe> satisfied (using bootstrap 4.0.0-beta.3)Cryolite
I had a similar experience, needed this to get an iframe macro in Confluence to center properly: style="border:1;display:block;margin:auto"Tenfold
P
82

best way and more simple to center an iframe on your webpage is :

<p align="center"><iframe src="http://www.google.com/" width=500 height="500"></iframe></p>

where width and height will be the size of your iframe in your html page.

Plainsman answered 15/11, 2015 at 14:18 Comment(5)
I thought the align attribute was deprecated? Why should the OP use this approach instead of using CSS - what's the advantage?Whirlybird
style="text-align:center" isn't deprecated and would do the same jobLashondalashonde
even though it is depreciated, this works, while everything else does not.Parian
This works in the HTML file and thus is easier for newbs like me to impliment. I just did it in 2017 so it must not be depreciated. Thanks for the tip!Scabbard
the align attribute has been removed in HTML5(although it works -though not on strict-HTML5 platforms like Wordpress or Github) and text-align does not work on iframesMillicentmillie
B
22

HTML:

<div id="all">
    <div class="sub">div</div>
    <iframe>ss</iframe>
</div>

CSS:

#all{
    width:100%;
    float:left;
    text-align:center;
}
div.sub, iframe {
    width: 100px;
    height: 50px;
    margin: 0 auto;
    background-color: #777;

}
Barnum answered 3/12, 2011 at 10:20 Comment(0)
R
19

The simplest code to align the iframe element:

<div align="center"><iframe width="560" height="315" src="www.youtube.com" frameborder="1px"></iframe></div>
Radiator answered 18/7, 2017 at 15:46 Comment(2)
Please see this first how-to-answer This question is answered before, obviously, you can add your answer here. But You Need to understand some points before answering. First, don't add an answer which is previously added with the same code or suggestion. Second, don't add an overly complicated answer if the user has asked very specifically about the problem and what he needs to solve this. Third, You can add a comment if you want to suggest anything regarding the answer or question.Caroylncarp
Thanks, this works for me, while the more upvoted answers don't for me. @Caroylncarp you are adding unnecessary noise, please stop.Electrojet
T
14

If you are putting a video in the iframe and you want your layout to be fluid, you should look at this webpage: Fluid Width Video

Depending on the video source and if you want to have old videos become responsive your tactics will need to change.

If this is your first video, here is a simple solution:

<div class="videoWrapper">
    <!-- Copy & Pasted from YouTube -->
    <iframe width="560" height="349" src="http://www.youtube.com/embed/n_dZNLr2cME?rel=0&hd=1" frameborder="0" allowfullscreen></iframe>
</div>

And add this css:

.videoWrapper {
	position: relative;
	padding-bottom: 56.25%; /* 16:9 */
	padding-top: 25px;
	height: 0;
}
.videoWrapper iframe {
	position: absolute;
	top: 0;
	left: 0;
	width: 100%;
	height: 100%;
}

Disclaimer: none of this is my code, but I've tested it and was happy with the results.

Tomfoolery answered 13/10, 2014 at 18:38 Comment(0)
D
14

My simplest solution to this.

iframe {
    margin:auto;
    display:block;
}
Darendaresay answered 13/10, 2020 at 10:10 Comment(0)
Z
5

You can put iframe inside a <div>

<div>
    <iframe></iframe>
</div>

It works because it is now inside a block element.

Zeiger answered 3/12, 2011 at 10:18 Comment(0)
E
5

You can try

<h3 style="text-align:center;"><iframe src=""></iframe></h3>

I hope its useful for you

link

Engels answered 25/4, 2018 at 4:2 Comment(0)
S
5

If you can't access the iFrame class then add below css to wrapper div.

<div style="display: flex; justify-content: center;">
    <iframe></iframe>
</div>
Solitta answered 23/9, 2020 at 18:28 Comment(0)
A
4

In my case solution was on iframe class adding:

    display: block;
    margin-right: auto;
    margin-left: auto;
Armijo answered 31/5, 2020 at 8:23 Comment(0)
C
3

According to http://www.w3schools.com/css/css_align.asp, setting the left and right margins to auto specifies that they should split the available margin equally. The result is a centered element:

margin-left: auto;margin-right: auto;
Cabana answered 25/9, 2014 at 16:18 Comment(1)
Only if the parent element has style text-align set to center.Molech
L
2

<iframe src="https://www.facebook.com/plugins/page.php?href=https%3A%2F%2Fwww.facebook.com%2FRishabh-Cars-Jodhpur-110479104559774&tabs=timeline&width=500&height=1200&small_header=false&adapt_container_width=true&hide_cover=false&show_facepile=true&appId" width="500" height="1200" style="border:none;overflow:hidden;display:block;margin:0 auto;" scrolling="yes" frameborder=".6" allowfullscreen="true" allow="autoplay; clipboard-write; encrypted-media; picture-in-picture; web-share"></iframe>

use it and embed facebook in iframe in center of html page

Lanalanae answered 7/2, 2022 at 10:50 Comment(1)
Thank you for sharing. The most important piece of information in your answer is to replace the default facebook page plugin style with your variation: style="border:none;overflow:hidden;display:block;margin:0 auto;"Grettagreuze
D
0

Here I have put snippet for all of you who are suffering to make iframe or image in center of the screen horizontally. Give me THUMBS UP VOTE if you like.👍⯅.

style > img & iframe > this is your tag name so change that if you're want any other tag in center

<html >
 <head> 
            <style type=text/css>
            div{}
            img{
                 margin: 0 auto;
	         display:block;
         	}
	 iframe{	
		margin: 0 auto;
		display:block;
		}
				
            </style>
</head>
 <body >
           
			<iframe src="https://test-videos.co.uk/vids/bigbuckbunny/mp4/h264/360/Big_Buck_Bunny_360_10s_1MB.mp4" width="320" height="180" frameborder="0" allowfullscreen="allowfullscreen"></iframe> 
			
			<img src="http://commondatastorage.googleapis.com/gtv-videos-bucket/sample/images/BigBuckBunny.jpg" width="320" height="180"  />
            </body> 
            </html>
Demetri answered 31/5, 2020 at 6:26 Comment(0)
I
0

I used

iframe {
    align-items: center;
    margin: auto;
}

to put the iframe div in the center, as well as to center the items inside the iframe.

Inveteracy answered 21/5, 2023 at 22:58 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.