Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to align iframe always in the center

I have an iframe surrounded by div element and I am simply trying to position it always in the center. here is my jsfiddle effort : jsfiddle

and trully believe that someone could help because I am sure that it is something really simple but I just can not see it.

Here is the HTML itself:

<div id="top-element">Some div that has greater width than the inner div</div>
<div id="iframe-wrapper">
    <iframe src="http://www.apple.com/" scrolling="auto"></iframe>
</div>
like image 840
mathinvalidnik Avatar asked Dec 14 '13 12:12

mathinvalidnik


2 Answers

Center iframe

Edit: FLEX solution

Using display: flex on the <div>

div {
    display: flex;
    align-items: center;
    justify-content: center;
}

JSFiddle: https://jsfiddle.net/h9gTm/867/

One solution is:

div {
  text-align:center;
  width:100%;
}
div > iframe{
  width: 200px;
}
<div>
  <iframe></iframe>
</div>

JSFiddle: https://jsfiddle.net/h9gTm/

edit: vertical align added

css:

div {
    text-align: center;
    width: 100%;
    vertical-align: middle;
    height: 100%;
    display: table-cell;
}
div > iframe{
  width: 200px;
}
div,
body,
html {
    height: 100%;
    width: 100%;
}
body{
    display: table;
}

JSFiddle: https://jsfiddle.net/h9gTm/1/

like image 100
Facundo Colombier Avatar answered Sep 28 '22 04:09

Facundo Colombier


I think if you add margin: auto; to the div below it should work.

div#iframe-wrapper iframe {
    position: absolute;
    top: 0;
    bottom: 0;
    left: 0;
    margin: auto;
    right: 100px;
    height: 100%;
    width: 100%;
}
like image 43
atw Avatar answered Sep 28 '22 06:09

atw