Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Center an iframe horizontally

This piece of code effectively centers the iframes but on their left edge not their center.

<!DOCTYPE html>

<html lang="en" xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta charset="utf-8" />
    <title>Videos</title>
    <style>
        div.center {
            width: 200px;
            display: block;
            margin-left: auto;
            margin-right: auto;
        }
    </style>
</head>
<body style="background: lightblue">
    <div class="center">
        <iframe src="http://www.youtube.com/embed/dgZ-K87NcwQ"></iframe>
        <p />
        <iframe src="http://www.dailymotion.com/embed/video/xoy7jd"></iframe>
    </div>
</body>
</html>

enter image description here

I've seen these questions :

  • How to center an iframe horizontally?
  • how do I center the full page?
  • How to horizontally center a <div> in another <div>?
  • How to center an iframe horizontally?

Unfortunately none worked for me.

How would one really center these on the screen ?

like image 929
aybe Avatar asked Dec 15 '22 09:12

aybe


1 Answers

The issue is that your iframes are wider than 200px (the fixed width you've defined for their centered containing div). This will cause their excess width to spill over the right boundry of the div.

Depending on the full structure of your site try putting the auto margin directly on the iframes.

div.center iframe{
    display: block;
    margin-left: auto;
    margin-right: auto;
}

Demo: http://jsfiddle.net/NvfKu/

like image 105
Matt Rohland Avatar answered Dec 28 '22 23:12

Matt Rohland