居中 iframe div

Centering iframe div

在对 iframe 进行编码以随屏幕调整大小时,我无法将其居中。我尝试了 THIS 问题的所有回复,但没有成功。我是否遗漏了一些明显的东西,或者没有办法做到这一点?

HTML

<div class="videoWrap">
    <iframe src="http://www.youtube.com/embed/playlist?list=PLn0iVeY0xhgZvWDQ1K_6EChZe_4TL5zDZ"></iframe>
</div>

CSS

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

您可以尝试应用margin: auto; css 属性 到你的 div。 https://www.w3schools.com/css/css_align.asp

在我的示例中,我使用常规设置将包装元素水平和垂直居中(position: absolute` 在此处 应用),并在此处定义宽度和高度。视频本身只是填充居中的包装纸。

html, body {
  height: 100%;
  margin: 0;
}

.videoWrap {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  /* 16:9 */
  width: 80vw;
  height: 45vw;
}

.videoWrap iframe {
  width: 100%;
  height: 100%;
}
<div class="videoWrap">
  <iframe src="http://www.youtube.com/embed/playlist?list=PLn0iVeY0xhgZvWDQ1K_6EChZe_4TL5zDZ"></iframe>
</div>

使用left: 50%transform: translateX(-50%):

.videoWrap {
    position: relative;
    padding-bottom: 56.25%; /* 16:9 */
    padding-top: 25px;
    height: 0;
}
.videoWrap iframe {
    position: absolute;
    top: 0;
    left: 50%;
    transform: translateX(-50%);
    width: 80%;
    height: 100%;
}

使用您链接的问题...

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

.videoWrap iframe {
  width: 300px;
  height: 300px;
}

div, body, html {
  height: 100%;
  width: 100%;
}
<div class="videoWrap">
  <iframe src="http://www.youtube.com/embed/playlist?list=PLn0iVeY0xhgZvWDQ1K_6EChZe_4TL5zDZ"></iframe>
</div>