CSSで作るシンプルなタイムラインデザイン
Webサイトのデザインで「タイムライン」はよく使われる要素の一つです。イベントの流れを時系列で分かりやすく表示するため、履歴ページやプロジェクト進行ページなどで活用されます。
今回はCSSを使って、シンプルでわかりやすい縦型タイムラインを作成する方法を紹介します。
タイムラインの基本構造
タイムラインは以下のような構造で作成します。
- タイムライン全体の親要素 (
.timeline
) - 各イベントを囲む
.timeline-item
- イベントの内容を表示する
.timeline-content
- 中央の縦線 (
::after
を利用) - 各イベントの丸 (
::before
を利用)
HTML
以下のHTMLを用意します。
<div class="timeline">
<div class="timeline-item">
<div class="timeline-content">
<h3>2025年1月</h3>
<p>プロジェクト開始</p>
</div>
</div>
<div class="timeline-item">
<div class="timeline-content">
<h3>2025年3月</h3>
<p>開発フェーズ開始</p>
</div>
</div>
<div class="timeline-item">
<div class="timeline-content">
<h3>2025年6月</h3>
<p>テスト・調整</p>
</div>
</div>
<div class="timeline-item">
<div class="timeline-content">
<h3>2025年9月</h3>
<p>プロジェクト完了</p>
</div>
</div>
</div>
CSSでデザインを調整
次に、CSSでスタイルを適用します。
.timeline {
position: relative;
max-width: 600px;
margin: 0 auto;
}
.timeline::after {
content: '';
position: absolute;
width: 4px;
background-color: #007bff;
top: 0;
bottom: 0;
left: 50%;
transform: translateX(-50%);
}
.timeline-item {
position: relative;
width: 50%;
padding: 10px 20px;
box-sizing: border-box;
}
.timeline-item:nth-child(odd) {
left: 0;
text-align: right;
}
.timeline-item:nth-child(even) {
left: 50%;
text-align: left;
}
.timeline-item::before {
content: '';
position: absolute;
top: 50%;
width: 15px;
height: 15px;
background-color: #fff;
border: 3px solid #007bff;
border-radius: 50%;
left: 100%;
transform: translate(-50%, -50%);
}
.timeline-item:nth-child(even)::before {
left: 0;
transform: translate(-50%, -50%);
}
.timeline-content {
background: #fff;
padding: 15px;
border-radius: 6px;
box-shadow: 0 2px 5px rgba(0, 0, 0, 0.1);
}
修正ポイント
以前のバージョンでは、中央の丸 (::before
) の位置が微妙にずれていました。
これを修正するために、top: 50%
と transform: translate(-50%, -50%)
を適用しました。
top: 50%
:要素の位置を中央に設定transform: translate(-50%, -50%)
:left
やtop
の基準点を適切に調整
これにより、左右のイベントで中央の丸が適切に配置されるようになりました。
全体のコード
<!DOCTYPE html>
<html lang="ja">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>CSSタイムライン</title>
<style>
body {
font-family: Arial, sans-serif;
background-color: #f5f5f5;
margin: 0;
padding: 20px;
}
.timeline {
position: relative;
max-width: 600px;
margin: 0 auto;
}
.timeline::after {
content: '';
position: absolute;
width: 4px;
background-color: #007bff;
top: 0;
bottom: 0;
left: 50%;
transform: translateX(-50%);
}
.timeline-item {
position: relative;
width: 50%;
padding: 10px 20px;
box-sizing: border-box;
}
.timeline-item:nth-child(odd) {
left: 0;
text-align: right;
}
.timeline-item:nth-child(even) {
left: 50%;
text-align: left;
}
.timeline-item::before {
content: '';
position: absolute;
top: 50%;
width: 15px;
height: 15px;
background-color: #fff;
border: 3px solid #007bff;
border-radius: 50%;
left: 100%;
transform: translate(-50%, -50%);
}
.timeline-item:nth-child(even)::before {
left: 0;
transform: translate(-50%, -50%);
}
.timeline-content {
background: #fff;
padding: 15px;
border-radius: 6px;
box-shadow: 0 2px 5px rgba(0, 0, 0, 0.1);
}
</style>
</head>
<body>
<div class="timeline">
<div class="timeline-item">
<div class="timeline-content">
<h3>2025年1月</h3>
<p>プロジェクト開始</p>
</div>
</div>
<div class="timeline-item">
<div class="timeline-content">
<h3>2025年3月</h3>
<p>開発フェーズ開始</p>
</div>
</div>
<div class="timeline-item">
<div class="timeline-content">
<h3>2025年6月</h3>
<p>テスト・調整</p>
</div>
</div>
<div class="timeline-item">
<div class="timeline-content">
<h3>2025年9月</h3>
<p>プロジェクト完了</p>
</div>
</div>
</div>
</body>
</html>
まとめ
CSSを使って、シンプルで美しいタイムラインを作成しました。
ポイントは ::after
でタイムラインの縦線を作成し、::before
で各イベントのマーカーを適切に配置することです。
このデザインを基に、色やレイアウトをカスタマイズして、
自分のプロジェクトに応じたタイムラインを作ってみてください!
CSS変数で特定のクラスだけ透明度(alpha)を変更する方法
2月 23, 2025display: grid; の使い方を徹底解説!CSSグリッドレイアウトの基本と応用
2月 5, 2025PCサイズのコーディングにおけるmax-widthの重要性
1月 8, 2025