---
title: CSS のコードスタイル
sidebar:
  label: コードスタイル
description: テンプレートごとの CSS のフォーマッター・リンターと、プロパティの記述順
---

CSS のフォーマット（インデント、改行、スペースなど）と検査は、[開発環境テンプレート](/frontend/environment)に用意された設定で自動化します。手動でフォーマットを整える必要はありません。

| テンプレート | 使用するツール |
| :--- | :--- |
| 静的サイト（Vite） | Biome（フォーマット・リント、プロパティの並べ替え） |
| 静的サイト（Astro） | Prettier（フォーマット）、Stylelint（リント、プロパティの並べ替え、クラス名の検査） |
| WordPress（wp-env） | Prettier（フォーマット）、Stylelint（リント、プロパティの並べ替え、クラス名の検査） |

クラス名の規則は[クラス命名規則](/frontend/css/naming)を参照してください。

## プロパティの記述順

プロパティは、**カスタムプロパティ → 配置 → ボックスモデル → タイポグラフィ → 見た目 → アニメーション → ネストしたルール（擬似クラス・擬似要素・メディアクエリ）** の順に記述します。

```css
.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](https://github.com/stormwarning/stylelint-config-recess-order) が配置〜アニメーション（2〜6）を並べ替えます。グループ内の細かな順序は recess-order の定義に従い、迷った場合は `stylelint --fix` の結果を正としてください。

カスタムプロパティを先頭、ネストしたルールを末尾（1・7）に置く順序は recess-order の対象外で、テンプレートでは自動化していません。自動で並べ替えたい場合は、stylelint-order の `order/order` ルールを追加します。

```js stylelint.config.mjs
export default {
  extends: ["stylelint-config-recess-order"],
  rules: {
    "order/order": ["custom-properties", "declarations", "rules"],
  },
};
```
