使Flexbox内容居中而不重叠导航

时间:2016-05-23 09:35:45

标签: html css iframe flexbox

我试图将视频置于我页面的主体中心。我在页面顶部有一个导航,在底部有一个页脚,其中包含有关视频本身的一些信息。

我想要做的是让视频始终居中,同时确保导航和页脚在调整大小时不会与视频重叠。这是我目前的代码:



.Site {
  display: flex;
  flex-direction: column;
  height: 100vh;
  overflow: hidden;
  align-items: center;
  align-content: center;
}
.Site-content {
  flex: 1 0 auto;
  width: 100vw;
  display: flex;
  flex-flow: column nowrap;
  justify-content: center;
  align-items: center;
}
.Site-header,
.Site-footer {
  flex: none;
  position: absolute;
  z-index: 600;
  height: 10vh;
  width: 100vw;
}
.Site-header {
  top: 0;
  border-bottom: 1px solid red;
}
.Site-footer {
  bottom: 0;
  border-top: 1px solid red;
}
.video-container {
  position: relative;
  width: 70%;
  height: 0;
  padding-bottom: 56.25%;
  margin-left: auto;
  margin-right: auto;
}
.video-container iframe {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
}

<body class="Site">
  <div class="Site-header">This is a header</div>
  <div class="Site-content">
    <div class="video-container">
      <iframe src="https://player.vimeo.com/video/100978843" width="640" height="360" frameborder="0" webkitallowfullscreen mozallowfullscreen allowfullscreen></iframe>
    </div>
  </div>
  <div class="Site-footer">This is the footer</div>
</body>
&#13;
&#13;
&#13;

我尝试通过删除header来更改footerposition: absolute,但这似乎打破了Flexbox中心。

我将如何修改此事?

1 个答案:

答案 0 :(得分:1)

您不需要绝对/相对位置,您可以justify-content: space-between使用flex-direction: column,更难的部分是保持iframe响应,但您可以执行此类操作

body,
html {
  margin: 0;
  padding: 0;
}
.Site {
  height: 100vh;
  display: flex;
  flex-direction: column;
  text-align: center;
  justify-content: space-between;
}
.Site-header {
  border-bottom: 1px solid red;
  padding: 10px;
}
.Site-footer {
  border-top: 1px solid red;
  padding: 10px;
}
iframe {
  max-width: 100%;
  max-height: 70vh;
}
<body class="Site">
  <div class="Site-header">This is a header</div>
  <div class="Site-content">
    <div class="video-container">
      <iframe src="https://player.vimeo.com/video/100978843" width="640" height="360" frameborder="0" webkitallowfullscreen mozallowfullscreen allowfullscreen></iframe>
    </div>
  </div>
  <div class="Site-footer">This is the footer</div>
</body>