タイポグラフィのテーマ設定
CoddyのHTMLジャーニー「実践的フロントエンド」セクションの一部。レッスン 15/35。
Typography は theming と design の重要な一部です。 あらゆる場所に font の style を記述する代わりに、それらを CSS variables として定義し、サイト全体で再利用できます。これにより style の一貫性が保たれ、後からの変更も簡単になります。
次の変数を定義できます:
- Font family(例:
--font-family-base、--font-family-heading)
- フォントサイズ(例:
--font-size-base、--font-size-h1) - 行の高さ
- 字間
First、:root 要素で typography variables を定義しましょう:
: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;
}では、これらの変数をtext要素に適用します:
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か所で variables の値を変更することで簡単に変更できる、統一された typography system を creates します。
チャレンジ
簡単blog website 用の typography theme を作成し、headings と段落の text に個別の variables を設定してください。タスク:
- 次の CSS variables を定義します:
- headings の font sizes(
--h1-sizeと--h2-size) - paragraphs の text color (
--text-color): 読みやすくするため、純粋な黒以外の color を選んでください - headings の text color (
--heading-color): body text から目立つ color を選んでください
- headings の font sizes(
- これらの variables を適用して、次の要素を style します:
- body text
- h1 と h2 headings
CSS によって、headings と段落の text の間に明確な visual hierarchy が作られるようにしてください。
自分で試してみよう
<!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プレイグラウンド