如何在响应视频上垂直居中文本



由于某种原因,这让我很头疼,找不到确切的答案。

我有一个在背景中播放的视频,它是全宽的,对屏幕大小有反应。我在视频上有一个标题/文字。但我一辈子都不知道如何以响应的方式将文本垂直居中到视频中!谢谢你的帮助!

代码笔:

http://codepen.io/149203/pen/VagPxe

html:

<body>
  <div class="header-container">
    <div class="video-container">
      <video preload="true" autoplay="autoplay" loop="loop" volume="0">
            <source src="https://originate-v3-prod.s3.amazonaws.com/sites/53854785dc60d94b96000002/pages/53854785dc60d94b96000004/files/Originate_B_W_Small6.mp4" type="video/mp4">
        </video>
    </div> <!-- video-container -->
    <h3>Centered Title</h3>
    <h6>This should be vertically and horizontally centered</h6>
  </div> <!-- header-container -->
</body>

css:

.header-container {
  width: 100%;
  height: 100vh;
  position: relative;
  padding: 1px;
}
.video-container {
  position: absolute;
  top: 0;
  right: 0;
  bottom: 0;
  left: 0;
  height: 100%;
  width: 100%;
  overflow: hidden;
}
video {
  position: absolute;
  z-index: -100;
  width: 100%;
}
h3,
h6 {
  text-align: center;
  color: white;
}

拉入此css:

https://maxcdn.bootstrapcdn.com/bootswatch/3.3.6/cyborg/bootstrap.min.css

将包装器显示为display: table,将子元素更改为display: table-cellvertiacl-align: middle.http://codepen.io/anon/pen/ZWwLPP

我找到并分叉了这个CodePen。这是我找到的最好的解决方案。(使用引导程序。)

http://codepen.io/149203/pen/JXxbEL

HTML

<div class="row">
  <div class="col-xs-12 vert-center-container">
    <video autoplay loop style="width:100%" class="img-responsive">
      <source src="https://originate-v3-prod.s3.amazonaws.com/sites/53854785dc60d94b96000002/pages/53854785dc60d94b96000004/files/Originate_B_W_Small6.mp4" />
    </video>
    <div class="vert-center-text">
      <h1>Caption Text</h1>
    </div>
  </div>  
</div>

CSS

.vert-center-text {
   position: absolute; 
   top: 40%; 
   left: 0;
   text-align: center; 
   width: 100%; 
}
.vert-center-text h1 { 
    color: white;
}
.vert-center-container {
  position:relative;
}

使用vertical-align:middle 将头容器模拟为表,将h3、h6模拟为表单元格

.header-container {
  width: 100%;
  height: 100vh;
  position: relative;
  padding: 1px;
  display:table;
}
.video-container {
  position: absolute;
  top: 0;
  right: 0;
  bottom: 0;
  left: 0;
  height: 100%;
  width: 100%;
  overflow: hidden;
}
video {
  position: absolute;
  z-index: -100;
  width: 100%;
}
h3,
h6 {
  display:table-cell;
  vertical-align:middle;
  text-align: center;
  color: white;
}
<body>
  <div class="header-container">
    <div class="video-container">
      <video preload="true" autoplay="autoplay" loop="loop" volume="0">
            <source src="https://originate-v3-prod.s3.amazonaws.com/sites/53854785dc60d94b96000002/pages/53854785dc60d94b96000004/files/Originate_B_W_Small6.mp4" type="video/mp4">
        </video>
    </div>
    <!-- video-container -->
    <h3>Centered Title</h3>
    <h6>This should be vertically and horizontally centered</h6>
  </div>
  <!-- header-container -->
</body>

相关内容

  • 没有找到相关文章

最新更新