<header class="header">固定ヘッダー</header>
<div class="image-container">
<img src="https://via.placeholder.com/300" alt="サンプル画像">
<div class="overlay-text">画像上のテキスト</div>
</div>
<main>
<p>スクロールしても固定ヘッダーは画面上部に表示され続けます。<br>下に画像の上に重ねたテキストがあります。</p>
</main>
/* 固定ヘッダー */
.header {
position: fixed;
top: 0;
left: 0;
width: 100%;
background-color: #333;
color: white;
padding: 10px;
text-align: center;
z-index: 1000;
}
/* コンテンツエリア */
main {
margin-top: 70px; /* ヘッダー分のマージン */
padding: 20px;
}
/* 画像コンテナと重ねたテキスト */
.image-container {
position: relative;
width: 300px;
margin: 0 auto;
}
.image-container img {
width: 100%;
}
.overlay-text {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
color: white;
font-size: 24px;
background-color: rgba(0, 0, 0, 0.5);
padding: 10px;
text-align: center;
}
This Pen doesn't use any external CSS resources.
This Pen doesn't use any external JavaScript resources.