セレクター
基本セレクター
*ユニバーサル — すべての要素
divタイプ — すべての
要素
.classクラス — クラスを持つ要素
#idID — id を持つ要素
[attr]属性 — 属性を持つ要素
[attr="val"]属性が値と一致
コンビネーター
A B子孫(任意の深さ)
A > B直接の子のみ
A + B隣接する兄弟(次の要素)
A ~ B一般的な兄弟(以降すべて)
疑似クラス
:hoverマウスオーバー中の要素
:focusフォーカスがある要素
:first-child親の最初の子
:nth-child(n)N 番目の子(1始まり、oddeven2n+1
:not(sel)否定 — 一致を除外
:has(sel)親セレクター(一致を含む)
疑似要素
::before要素の前にコンテンツを挿入
::after要素の後にコンテンツを挿入
::placeholderinput のプレースホルダーテキストをスタイル
::selectionハイライトされたテキストをスタイル
ボックスモデル
ボックスサイジング
/* padding/border を幅に含める */ *, *::before, *::after { box-sizing: border-box; }
プロパティ
marginボーダーの外側のスペース
borderpadding の周囲の枠
paddingボーダーの内側のスペース
width / heightコンテンツの寸法
outlinemargin の外側のリング(スペースなし)
ショートハンド
margin: 10px; /* 全辺 */ margin: 10px 20px; /* 垂直 | 水平 */ margin: 10px 20px 30px; /* 上 | 水平 | 下 */ margin: 10px 20px 30px 40px; /* 上 右 下 左 */
Flexbox
コンテナ
.flex { display: flex; justify-content: center; /* 主軸 */ align-items: center; /* 交差軸 */ gap: 1rem; }
コンテナプロパティ
flex-directionrow | column | row-reverse | column-reverse
flex-wrapnowrap | wrap | wrap-reverse
justify-contentflex-start | center | space-between | space-around | space-evenly
align-itemsstretch | center | flex-start | flex-end | baseline
align-content複数行の交差軸方向の揃え
gapフレックスアイテム間のスペース
アイテムプロパティ
flex: 1利用可能なスペースを埋めるように伸長
flex: 0 0 200px固定幅、伸長/縮小なし
align-self1つのアイテムの align-items を上書き
order表示順を変更(デフォルト 0)
Grid
コンテナ
.grid { display: grid; grid-template-columns: repeat(3, 1fr); grid-template-rows: auto 1fr auto; gap: 1rem; }
コンテナプロパティ
grid-template-columns列トラックを定義: 1fr 2frrepeat(3, 1fr)
grid-template-rows行トラックを定義
grid-template-areas名前付き領域: "header header" "nav main"
gap行と列のギャップ
justify-itemsインライン方向(水平)でアイテムを揃え
align-itemsブロック方向(垂直)でアイテムを揃え
アイテムの配置
.item { grid-column: 1 / 3; /* 列 1-2 にまたがる */ grid-row: 1 / -1; /* すべての行にまたがる */ grid-area: header; /* 名前付き領域 */ }
タイポグラフィ
フォントプロパティ
font-familyフォントスタック: 'Inter', sans-serif
font-sizeサイズ: 1rem16pxclamp(1rem, 2vw, 2rem)
font-weightnormal (400) | bold (700) | 100900
font-stylenormal | italic | oblique
line-height行間: 1.5(単位なし推奨)
letter-spacing文字間隔: 0.05em
テキストプロパティ
text-alignleft | center | right | justify
text-decorationnone | underline | line-through
text-transformuppercase | lowercase | capitalize
text-overflowellipsis(overflow: hidden と併用)
white-spacenowrap | pre | pre-wrap
word-breakbreak-all | break-word
色と背景
カラーフォーマット
color: #ff6600; /* hex */ color: rgb(255, 102, 0); /* rgb */ color: hsl(24, 100%, 50%); /* hsl */ color: oklch(70% 0.15 50); /* oklch */
背景プロパティ
background-color単色塗りつぶし: #f0f0f0
background-imageurl(img.jpg) またはグラデーション
background-sizecover | contain | 100px 200px
background-positioncenter | top right | 50% 50%
background-repeatno-repeat | repeat-x | repeat-y
グラデーション
background: linear-gradient(to right, #f00, #00f); background: radial-gradient(circle, #fff, #000); background: conic-gradient(red, yellow, green);
トランジションとアニメーション
トランジション
.btn { transition: background 0.3s ease, transform 0.2s; } .btn:hover { background: #0056b3; transform: scale(1.05); }
トランジションプロパティ
transition-propertyアニメーションするプロパティ
transition-duration長さ: 0.3s300ms
transition-timing-functionease | linear | ease-in-out | cubic-bezier()
transition-delay開始前の待機時間
キーフレームアニメーション
@keyframes spin { from { transform: rotate(0deg); } to { transform: rotate(360deg); } } .icon { animation: spin 1s linear infinite; }
アニメーションプロパティ
animation-name@keyframes 名を参照
animation-duration1サイクルの長さ
animation-iteration-count1 | 3 | infinite
animation-directionnormal | alternate | reverse
animation-fill-modeforwards | backwards | both
レスポンシブデザイン
メディアクエリ
@media (max-width: 768px) { .sidebar { display: none; } } @media (prefers-color-scheme: dark) { body { background: #1a1a1a; color: #eee; } }
一般的なブレークポイント
max-width: 480pxモバイル端末
max-width: 768pxタブレット
max-width: 1024px小型ノートPC
max-width: 1280pxデスクトップ
ビューポート単位
vw / vhビューポート幅 / 高さの %
dvh動的ビューポート高さ(モバイル対応)
svh / lvh小 / 大ビューポート高さ
cqiコンテナクエリインラインサイズ
コンテナクエリ
.card-wrapper { container-type: inline-size; } @container (min-width: 400px) { .card { flex-direction: row; } }
ポジショニング
position の値
staticデフォルト — 通常のドキュメントフロー
relative通常位置からオフセット;スペースを保持
absolute最も近い位置付き祖先に対して配置
fixedビューポートに対して配置;スクロールしても固定
stickyスクロールに基づいて relative/fixed を切り替え
センタリングパターン
/* Flex でセンタリング */ display: flex; justify-content: center; align-items: center; /* Grid でセンタリング */ display: grid; place-items: center;
スタッキング
z-index重なり順(大きいほど上);position が必要
isolation: isolate新しいスタッキングコンテキストを作成
カスタムプロパティ
定義と使用
:root { --color-primary: #3b82f6; --spacing-md: 1rem; } .btn { background: var(--color-primary); padding: var(--spacing-md); }
フォールバック値
color: var(--accent, #ff6600); /* --accent が未定義の場合は #ff6600 を使用 */
動的テーマ
[data-theme="dark"] { --bg: #1a1a2e; --text: #e0e0e0; } body { background: var(--bg); color: var(--text); }