CSS プロパティの記述順を考える
今までずっとやろうと思っていて怠けていた「CSSをきちんと書くこと」ですが、Web制作会社で働かせてもらった関係もあって最近自分のCSSの記述を見直しています。今さら^^;
(ちなみにこのサイトの記述は一年くらい前のなのでぐちゃぐちゃです。汗)
リストアップ
参考にさせてもらったのが、hail2u.netさまの記事にあった、CSS2 Specificationの記述順です。全部並べてみると、
- margin-top
- margin-right
- margin-bottom
- margin-left
- margin
- padding-top
- padding-right
- padding-bottom
- padding-left
- padding
- border-top-width
- border-right-width
- border-bottom-width
- border-left-width
- border-width
- border-top-color
- border-right-color
- border-bottom-color
- border-left-color
- border-color
- border-top-style
- border-right-style
- border-bottom-style
- border-left-style
- border-style
- border-top
- border-bottom
- border-right
- border-left
- border
- display
- position
- top
- right
- bottom
- left
- float
- clear
- z-index
- direction
- unicode-bidi
- width
- min-width
- max-width
- height
- min-height
- max-height
- line-height
- vertical-align
- overflow
- clip
- visibility
- content
- compact
- run-in
- quotes
- marker-offset
- list-style-type
- list-style-image
- list-style-position
- list-style
- size
- marks
- page-break-before
- page-break-after
- page-break-inside
- page
- orphans
- widows
- color
- background-color
- background-image
- background-repeat
- background-attachment
- background-position
- background
- font-family
- font-style
- font-variant
- font-weight
- font-stretch
- font-size
- font-size-adjust
- font
- text-indent
- text-align
- text-decoration
- text-shadow
- letter-spacing
- word-spacing
- text-transform
- white-space
- caption-side
- table-layout
- border-collapse
- border-spacing
- empty-cells
- speak-header
- cursor
- outline
- outline-width
- outline-style
- outline-color
- volume
- speak
- pause-before
- pause-after
- pause
- cue-before
- cue-after
- cue
- play-during
- azimuth
- elevation
- speech-rate
- voice-family
- pitch
- pitch-range
- stress
- richness
- speak-punctuation
- speak-numeral
こんなたくさんあったのね。こういう順。ちなみにhail2u.netさんの記事に加えてテーブル系のプロパティcaption-side,border-collapse,border-spacingとユーザインターフェイス系のプロパティoutline-width,outline-style,outline-colorを足しています。
シェイプアップ
実際このすべてを使う訳でもないし、プロパティによって使用頻度の差が結構あります。
しかもこの数を毎回見て順序を探したりするのは結構大変なので、これをもとに自分なりの順にまとめてみることにします。ポイントは、
- 基本は上のCSS2 Specificationの出現順
- まとめて記述できるプロパティは(自分が使う程度で)まとめる
- 関連したプロパティをまとめる
- 使用頻度が低いもの、現時点でこれは使わないでしょ、ってものは思い切って省略する
主に音声スタイルシート系、ページ媒体系のプロパティは省略しています。また、border-***-width,border-***-color,border-***-style、outline-***は通常使わないので省略。
これを踏まえると以下のようになります。
- margin
- margin-top
- margin-right
- margin-bottom
- margin-left
- padding
- padding-top
- padding-right
- padding-bottom
- padding-left
- border
- border-top
- border-bottom
- border-right
- border-left
- border-width
- border-color
- border-style
- display
- position
- top
- right
- bottom
- left
- float
- clear
- z-index
- width
- min-width
- max-width
- height
- min-height
- max-height
- line-height
- vertical-align
- overflow
- clip
- visibility
- content
- list-style
- list-style-type
- list-style-image
- list-style-position
- color
- background
- background-color
- background-image
- background-repeat
- background-attachment
- background-position
- font
- font-family
- font-style
- font-variant
- font-weight
- font-size
- text-indent
- text-align
- text-decoration
- text-shadow
- letter-spacing
- text-transform
- white-space
- caption-side
- table-layout
- border-collapse
- border-spacing
- empty-cells
- cursor
- outline
まとめ、+α
だいぶ僕にも覚えられる数になりました。これからこれをもとにしてプロパティを記述していこうと思っています。
ちなみに、このCSS2 Specificationの目次を見るとわかるように、上記のCSSのプロパティは以下のようなカテゴリで括られて書かれています。
- ボックスモデル
- 視覚整形モデル
- 視覚整形モデル詳細
- 視覚効果
- 内容生成、自動番号振り、リスト
- ページ媒体
- 文字色と背景
- フォント
- テキスト
- テーブル
- ユーザインターフェイス
- 音声スタイルシート
この名称と上の記述順を見比べると、なんとなくCSSでスタイルを指定していくイメージが湧く気がしますね。