水曜日, 12月 27, 2023

HTMLとCSSの様々な表現 07 
HTMLとCSSだけで動画を背景イメージに

サイトの背景イメージを動画表示にしてみます。

<!-- index.html -->
<!DOCTYPE html>
<html lang="ja">
<head>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width, initial-scale=1.0">
<!--viewpoint=ページの表示領域
initial-scale=1.0は初期のズーム倍率-->
<style>
body {
margin: 0;
overflow: hidden;
/* ページスクロールを無効にする */
}
video {
width: 100%;
height: 100%;
object-fit: cover;
/* ビデオをコンテナに合わせてカバー */
position: fixed;
top: 0;
left: 0;
z-index: -1;
/* コンテンツの前面に出るようにする */
}
/* 他のコンテンツ等を追加する場合はここに記述 */
.content {
position: relative;
z-index: 1;
padding: 20px;
color: white;
}
</style>
<title>Video Background</title>
</head>
<body>
<video autoplay loop muted>
<source src="kaleidoscope.mp4" type="video/mp4">
Your browser does not support the video tag.
</video>
<div class="content">
<!-- ここにコンテンツを追加 -->
<h1>Welcome to My Website</h1>
<p>The background of this page is
a video that changes every week.</p>
</div>
</body>
</html>
<!-- index.html -->

実行すると、ブラウザーの表示サイズに合わせてビデオがリピート表示されます。