タイポグラフィのテーマ設定
CoddyのHTMLジャーニー「実践的フロントエンド」セクションの一部。レッスン 15/35。
タイポグラフィは、テーマ設定とデザインの重要な要素です。 フォントのスタイルをあちこちに書く代わりに、CSS変数として定義し、サイト全体で再利用できます。これによりスタイルの一貫性が保たれ、後から変更しやすくなります。
次の項目に変数を定義できます。
- フォントファミリー(例:
--font-family-base、--font-family-heading)
- フォントサイズ(例:
--font-size-base、--font-size-h1) - 行の高さ
- 文字間隔
まず、:root 要素でタイポグラフィ変数を定義しましょう。
:root {
--font-primary: 'Open Sans', sans-serif;
--font-secondary: 'Roboto', sans-serif;
--font-size-base: 16px;
--line-height: 1.5;
--heading-color: #333;
--text-color: #555;
}では、これらの変数をテキスト要素に適用しましょう。
body {
font-family: var(--font-primary);
font-size: var(--font-size-base);
line-height: var(--line-height);
color: var(--text-color);
}
h1, h2, h3, h4, h5, h6 {
font-family: var(--font-secondary);
color: var(--heading-color);
}この設定により、1か所で変数の値を変更するだけで簡単に調整できる、統一されたタイポグラフィシステムが作成されます。
チャレンジ
簡単見出しと段落のテキストに別々の変数を使って、ブログサイト用のタイポグラフィテーマを作成しましょう。課題:
- 次のCSS変数を定義します:
- 見出しのフォントサイズ(
--h1-sizeと--h2-size) - 段落の文字色 (
--text-color):読みやすくするため、純粋な黒以外の色を選びます - 見出しの文字色 (
--heading-color):本文の文字色と見分けやすい色を選びます
- 見出しのフォントサイズ(
- 次のスタイル設定にこれらの変数を適用します:
- 本文
- h1とh2の見出し
CSSで、見出しと段落のテキストの間に明確な視覚的階層を作りましょう。
自分で試してみよう
<!DOCTYPE html>
<html>
<head>
<title>Typography Theming</title>
<style>
:root {
/* Typography variables */
--primary-font: "Georgia", serif; /* for paragraphs */
--secondary-font: "Arial", sans-serif; /* for headings */
}
body {
font-family: var(--primary-font);
color: #000000;
line-height: 1.6;
margin: 0;
padding: 20px;
background-color: #f9f9f9;
}
h1, h2 {
font-family: var(--secondary-font);
color: #000000;
margin-bottom: 0.5em;
}
h1 {
font-size: 16px;
}
h2 {
font-size: 16px;
}
p {
margin-bottom: 1.2em;
}
.blog-post {
max-width: 700px;
margin: 0 auto;
background: #fff;
padding: 2rem;
border-radius: 10px;
box-shadow: 0 2px 8px rgba(0,0,0,0.1);
}
</style>
</head>
<body>
<div class="blog-post">
<h1>The Beauty of Typography in Web Design</h1>
<p>Typography plays a crucial role in how users read and experience content online. A good choice of fonts and sizes can make a website more readable and enjoyable.</p>
<h2>Why Typography Matters</h2>
<p>Clear and consistent typography improves accessibility, creates a visual hierarchy, and gives your website a professional feel. Without it, even the best design can look unpolished.</p>
<h2>Mobile-First Typography</h2>
<p>Starting with smaller, readable text for mobile and scaling up for desktops ensures your content looks great on any device. This approach is part of mobile-first design and theming.</p>
</div>
</body>
</html>
このレッスンには短いクイズがあります。レッスンを始めて解答し、進捗を記録しましょう。
実践的フロントエンドのすべてのレッスン
自分で練習してみよう: Webプレイグラウンド