分割画面レイアウト
CoddyのHTMLジャーニー「実践的フロントエンド」セクションの一部。レッスン 29/35。
分割画面レイアウトは、ビューポートを通常は左右に並ぶ2つの異なる領域に分けます。このパターンは、コンテンツの比較、2つのテーマの紹介、またはナビゲーションとメインコンテンツの分離に効果的です。
まず、HTMLの構造を作成しましょう。
<div class="split-screen">
<div class="split-screen__left">
<h2>Left Side</h2>
<p>This is the left side content.</p>
</div>
<div class="split-screen__right">
<h2>Right Side</h2>
<p>This is the right side content.</p>
</div>
</div>それでは、分割画面レイアウトを作成するためにCSSを追加しましょう。
.split-screen {
display: flex;
flex-direction: column;
}
/* モバイルでは、セクションが縦に並びます */
.split-screen__left {
background-color: #f0f0f0;
}
.split-screen__right {
background-color: #e0e0e0;
}
/* 大きな画面では、セクションが横に並びます */
@media (min-width: 768px) {
.split-screen {
flex-direction: row;
}
.split-screen__left,
.split-screen__right {
flex: 1;
}
}このレイアウトは、モバイルファーストのアプローチに従い、モバイル端末では縦に積み重なった状態から始まります。画面の幅が768px以上になると、レイアウトは横方向の分割に変わります。
課題の内容:
製品紹介用のレスポンシブな分割画面レイアウトを作成しましょう。左側には製品情報を表示し、右側には製品の画像を表示します。
要件:
- 分割画面は、モバイルでは縦方向(768px未満)、それより大きい画面では横方向にする
- 左側の背景は暗色(#222)、テキストは白にする
- 右側の背景は明色(#f5f5f5)にする
- 両側に2remのパディングを追加する
- 768pxより大きい画面では、左側の幅を40%、右側の幅を60%にする
チャレンジ
簡単モバイルファーストでスタイル設定された2つのセクションを持つHTMLページが与えられています。デスクトップでも適切に表示される分割画面レイアウトに変更しましょう。
- 大きな画面(min-width: 768px)では、2つのセクションを横並びにします。
- 左側の背景色を
rgb(74, 144, 226)、文字色をrgb(255, 255, 255)にします。 - 右側の背景色を
rgb(255, 255, 255)、文字色をrgb(51, 51, 51)にします。
自分で試してみよう
<!DOCTYPE html>
<html>
<head>
<title>Split Screen Layout</title>
<style>
* {
box-sizing: border-box;
margin: 0;
padding: 0;
}
body {
font-family: Arial, sans-serif;
background: #f4f4f4;
}
.container {
display: flex;
flex-direction: column;
min-height: 100vh;
}
.left,
.right {
padding: 3rem;
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
text-align: center;
}
.left h1 {
font-size: 2.5rem;
margin-bottom: 1rem;
}
.right h2 {
margin-bottom: 1rem;
}
form {
display: flex;
flex-direction: column;
width: 100%;
max-width: 300px;
}
input {
padding: 10px;
margin-bottom: 15px;
border: 1px solid #ccc;
border-radius: 5px;
}
button {
padding: 10px;
border: none;
background: #4a90e2;
color: white;
font-weight: bold;
border-radius: 5px;
cursor: pointer;
transition: background 0.3s;
}
button:hover {
background: #357ab7;
}
@media (min-width: 768px) {
}
</style>
</head>
<body>
<div class="container">
<div class="left">
<h1>Welcome!</h1>
<p>Discover new opportunities, connect with others, and grow with us.</p>
</div>
<div class="right">
<h2>Sign Up</h2>
<form>
<input type="text" placeholder="Your Name">
<input type="email" placeholder="Your Email">
<button type="submit">Join Now</button>
</form>
</div>
</div>
</body>
</html>
このレッスンには短いクイズがあります。レッスンを始めて解答し、進捗を記録しましょう。
実践的フロントエンドのすべてのレッスン
自分で練習してみよう: Webプレイグラウンド