如何将iframe居中但是还能将其显示为内联?

时间:2014-06-19 18:18:05

标签: html css iframe

我有一个youtube视频,我想以div为中心。我添加了

display: block;

按照此处How to center an iframe horizontally?建议的css,它确实正确地居中。但是,我有两个图像,我想在iframe旁边显示内嵌,一个在左边,一个在右边。问题当然是当我在iframe中设置display:block时,它会阻止图像被放置在图片的左侧和右侧。

这是html

<div class="content">

    <a id="logo" href="http://www.example.com"> 
        <img id="logo" src="img/logo.png" alt="logo">
    </a> 
    <br>

    <nav class="topNav">
    <a href="index.html">
            <img id="home" class="topNav" src="img/home.png" 
            alt="home">
        </a>
        <a href="photos.html">
            <img id="photos" class="topNav" src="img/photos.png" 
            alt="gallery">
        </a>
        <a href="about.html">
            <img id="about" class="topNav" src="img/about.png" alt="about">
        </a>
    </nav>
    <br> 

    <
        <img id="image1" src="img/image1.png" alt="img1">
        <iframe id="video" src="http://www.youtube.com/embed/L-9y6jP-HO4">
        </iframe>
        <img id="image2" src="image2.png" alt="img2">

<br>

    <a id="downloadmp3" href="mp3/poo-on-the-shoe-ringtone.mp3"
       target="_blank" onmouseover="mOver()" onmouseout="mOut()">
        Download the ringtone! 
    </a>
    <br>

    <a href="https://www.facebook.com"
       target="_blank">
        <img id="facebookPic" src="img/facebook.png"
             alt="Like Us On Facebook"> 
    </a>

这是css

body {
  background: #563C21;
  text-align: center;
}

.content {
  text-align: center;
  background-color: #4D361E;
  box-shadow: 0 0 150px black;
  border-radius: 15%;
}

#logo {
  margin: auto;
  text-decoration: none;
}  

.topNav {
      width: 210px;
  height: 50px;
  margin: auto;
  display: inline;
}

.topNav a {
  text-decoration: none;
 }

#video {
  width: 540px;
  height: 315px;
  display: block;
  margin: auto;
  border: 0;
}



如何在保持图像的内联行为的同时使iframe居中?

1 个答案:

答案 0 :(得分:1)

链接到示例:http://jsfiddle.net/BeEg9/

CSS:

 #video {
  width: 540px;
  height: 315px;
  margin: auto;
  border: 0;
  display: inline;
}

#image1, #image2 {
  height: 200px;
  display: inline;
}

.content {
  text-align: center;
  display: block;
}

HTML:

<div class="content">
    <img id="image1" src="img/image1.png" alt="img1" />
        <iframe id="video" src="http://www.youtube.com/embed/L-9y6jP-HO4">
        </iframe>
    <img id="image2" src="image2.png" alt="img2" />
</div>