CSS のコードスタイル
テンプレートごとの CSS のフォーマッター・リンターと、プロパティの記述順
CSS のフォーマット(インデント、改行、スペースなど)と検査は、開発環境テンプレートに用意された設定で自動化します。手動でフォーマットを整える必要はありません。
| テンプレート | 使用するツール |
|---|---|
| 静的サイト(Vite) | Biome(フォーマット・リント、プロパティの並べ替え) |
| 静的サイト(Astro) | Prettier(フォーマット)、Stylelint(リント、プロパティの並べ替え、クラス名の検査) |
| WordPress(wp-env) | Prettier(フォーマット)、Stylelint(リント、プロパティの並べ替え、クラス名の検査) |
クラス名の規則はクラス命名規則を参照してください。
プロパティの記述順
プロパティは、カスタムプロパティ → 配置 → ボックスモデル → タイポグラフィ → 見た目 → アニメーション → ネストしたルール(擬似クラス・擬似要素・メディアクエリ) の順に記述します。
.block {
/* 1. カスタムプロパティ */
--color-main: #0070f3;
/* 2. 配置 */
position: relative;
z-index: 1;
/* 3. ボックスモデル */
box-sizing: border-box;
display: flex;
width: 100%;
height: 48px;
padding: 8px 16px;
margin: 0 auto;
/* 4. タイポグラフィ */
font-size: 1rem;
font-weight: 700;
line-height: 1.5;
color: #fff;
/* 5. 見た目 */
background-color: var(--color-main);
border: 1px solid #ccc;
border-radius: 4px;
box-shadow: 0 2px 8px rgb(0 0 0 / 10%);
/* 6. アニメーション */
transition: background-color 0.3s;
/* 7. ネストしたルール */
&:hover {
background-color: #005bb5;
}
&::after {
display: block;
content: "";
}
}
Biome を使うプロジェクト
Vite テンプレートでは、Biome の useSortedProperties でプロパティを並べ替えます。グループ内の細かな順序は Biome の定義に従い、Biome の自動修正の結果を正としてください。
Stylelint を使うプロジェクト
Astro テンプレートと WordPress テンプレートでは、stylelint-config-recess-order が配置〜アニメーション(2〜6)を並べ替えます。グループ内の細かな順序は recess-order の定義に従い、迷った場合は stylelint --fix の結果を正としてください。
カスタムプロパティを先頭、ネストしたルールを末尾(1・7)に置く順序は recess-order の対象外で、テンプレートでは自動化していません。自動で並べ替えたい場合は、stylelint-order の order/order ルールを追加します。
export default {
extends: ["stylelint-config-recess-order"],
rules: {
"order/order": ["custom-properties", "declarations", "rules"],
},
};