首页 > 解决方案 > 限制标题背景视频的高度?

问题描述

我正在处理放置标题中的背景视频。页眉只有 100%-257px 的高度,因此页面的其余部分在加载页面时可见。但是,我无法让视频成为标题中的完整背景。在全尺寸时,它总是溢出标题容器。我错过了一些CSS吗?非常感谢你的帮助!

html:

<header id="home">
        <div id="videoBackground">
            <video loop="loop" playsinline="playsinline" autoplay="autoplay" muted="muted" poster="Images/posterImg.png" id="videoBackgroundVideo">
                <source type="video/mp4" src="Images/BillVid.mp4">
            </video>    
        </div>
        <h1> This is a title</h1>
        <h2> This is a subtitle</h2>
        <div id="welcomeMessage">
            <h3> Welcome at <span>My Page</span></h3>
            <img src="Images/LogoRoundWhite.png" id="homeLogoWhite" style="border-style:none;" />
        </div>
    </header>

CSS:

header{
    padding-top: 100px;
    padding-bottom: 30px;
    margin-top: 0px;
    min-height: 400px;
    text-align: center;
    height: calc(100% - 257px);
    justify-content: center;
    overflow: hidden;
}

#videoBackground {
    position: absolute;
    top: 0;
    right: 0;
    left: 0;
    bottom: 0;
    z-index: -10;
}

#videoBackgroundVideo {
    position: absolute;
    top: 50%;
    left: 50%;
    width: auto;
    height: auto;
    min-width: 100%;
    min-height: 100%;
    -webkit-transform: translate(-50%, -50%);
        -moz-transform: translate(-50%, -50%);
            -ms-transform: translate(-50%, -50%);
               transform: translate(-50%, -50%);
}

标签: cssvideobackgroundfullscreen

解决方案


尝试添加

header {
    position:relative;
}

绝对定位元素将使用 position:relative 相对于最近的父元素定位自己;

如果这不存在,它只会相对于我相信的 body 标签定位自己,这也是为什么溢出:隐藏可能没有效果。


推荐阅读