如何在 CSS3/HTML5 中将标题与视频背景的中心对齐?

How do I align a title to the center of a video background in CSS3/HTML5?

我想将我的标题放在视频背景的中央。 你能帮忙吗? 我知道如何使用 position:absolute 放置它,但我不确定这是一个正确的解决方案! 这是我的 css

* {
    box-sizing: border-box;
}

body {
    margin: 0;
}

#bg {
    width: 100%;
}

.header {
    background-color: #f1f1f1;
    text-align: center;
}

#h-content {
    position: absolute;
    top: 0px;
}

这是html!

<!DOCTYPE html>
<html lang="en">
<head>
    <title>CSS Website Layout</title>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="stylesheet" type="text/css" href="public/css/main.css">
</head>

<body>

<div class="header">

    <video id="bg" src="public/media/test.mp4" autoplay="true" loop="true" muted="true"></video>
    <div id="h-content">
    <h1>Header</h1>
    <p>Resize the browser window to see the responsive effect.</p>
    </div>
</div>

</body>
</html>

请帮帮我! 我需要在没有任何框架或库(如 bootstrap 等

的情况下完成该任务

transform: translate(-50%, -50%) 是您的最佳选择,试试这个片段。

* {
    box-sizing: border-box;
}

body {
    margin: 0;
}

#bg {
    width: 100%;
}

.header {
    background-color: #f1f1f1;
    text-align: center;
    position:relative
}

#h-content {
    position: absolute;
    top:50%;
    left:50%;
     transform: translate(-50%, -50%);
}
<div class="header">

    <video id="bg" src="public/media/test.mp4" autoplay="true" loop="true" muted="true"></video>
    <div id="h-content">
    <h1>Header</h1>
    <p>Resize the browser window to see the responsive effect.</p>
    </div>
</div>