CSSコーディングスタイル

提供:MoodleDocs
2021年12月18日 (土) 15:06時点におけるMitsuhiro Yoshida (トーク | 投稿記録)による版
移動先:案内検索

作成中です - Mitsuhiro Yoshida (トーク)

概要 Overview

範囲

このドキュメントではMoodleコードを扱う開発者のためのスタイルガイドラインを説明します。純粋にコードレイアウトのメカニズムおよびMoodleのための私たちの選択についてのみ説明します。

ゴール

一貫したコーディングスタイルはどのような開発プロジェクトにおいても重要であり、特に多くの開発者が関わる場合はなおさらです。標準的なスタイルではコードが読みやすく理解しやすいものとなるため、全体的な品質向上にもつながります。

私たちが目指す目標概要:

  • 簡潔性
  • 可読性
  • ツールの利便性

便利なツール

私たちはコーディングスタイルへの適合性を確認するため、stylelintを使用しています。また、stylefmtはCSSを自動的に再フォーマットするために使用できます - 詳細はLintingをご覧ください。

ファイルネーミング

通常、CSSファイルはプラグイン内で

styles.css

というファイル名になります。

テーマのファイル名はテーマデザイナの希望に沿ったものにできますが、以下に従ってください:

  • 小文字および「-」で単語を区切ること。
  • 可能な限り簡潔にすること。
  • 説明的であること。
  • CSSファイルの場合は
    style/
    
    フォルダ、LESSファイルの場合は
    less/
    
    のフォルダに置くこと。

ブロック

  • 各セレクタは1行にまとめてください。セレクタのリストにカンマがある場合、カンマの後に改行を入れてください。
  • プロパティと値のペアはそれぞれの行を4つの半角スペースでインデントして最後にセミコロンを付けてください。
  • 閉じブレースは開始セレクタと同じレベルのインデントを使用する必要があります。
  • ブロック間は1行空けてください。

正しい

@media only screen and (min-width: 768px) {
    .selector-one,
    .selector-two {
        color: #fff;
        background-color: #000;
    }
}

間違い

.selector_one, .selector_two { color: #fff; background-color: #000; }

セレクタ

  • 常に小文字およびアンダースコアまたはハイフンを使用してください。ハイフンの使用を推奨します。
  • 名称は簡単な英単語で構成されるべきです。
  • 表現力を重視します。理解を深めるため、名称はできるだけ例示的にしてください。
  • 意味のある名称の使用: 名称はどのように見えるのかではなく、これが何であるのか伝えてください。
  • IDの使用は避けてください。IDを使用した場合、メンテナンスおよびオーバーライドが極めて困難になります。
  • タグネームとクラスまたはIDを組み合わせて過度にルールを限定しないでください。

正しい

.selector_name {
    color: #fff;
}

.selector-name {
    color: #fff;
}

間違い

div#selName {
    color: #fff;
}

.Color-White {
    color: #fff;
}

プロパティおよび変数

  • コロンおよび半角スペースで区切られている必要があります。これらは最小化しないでください。
  • フォント名およびベンダ固有のプロパティを除き、半角小文字で記述してください。
  • カラーコードには可能な限り小文字および略語を使用してください。
  • カラーコードでHSLAまたはRGBAを使用する場合、常に16進数のフォールバックを提供してください。
  • background, border, font, list-style, margin, paddingの値には (スタイルを上書きする場合を除いて) ショートハンドを使用してください。
  • !important
    
    は使用しないでください。代替手段がない場合、あなたが上書きを試みるCSSに問題が生じる可能性があります。
  • ベンダ固有のプロパティ接頭辞は参照する一般的なプロパティの前に直接表示してください。

単位

  • 許可されるCSSの単位は次のとおりです: px, rem, em
  • 値がゼロの場合、単位を宣言しないでください。

正しい

.something {
    margin-top: 0;
    font-size: 1.25em;
}

間違い

.something {
    margin-top: 0px;
    font-size: 1pt;
}

ドキュメンテーションおよびコメント

一般的なコーディングスタイルに従って、コメントは大文字で始まりピリオドで終えてください。

CSSファイルの先頭に入れたブロックスタイルのコメントでファイル内のルールの目的を説明してください。

/**
 * File base.css.
 * Contains base styles for theme basic.
 */

ブロックスタイルのコメントはCSSファイル内で特定のコンポーネント、ビュー、または機能に関するすべてのルールがあるセクションを示すためにも使用できます。

/**
 * SCORM Navigation Sidebar.
 */

1行のコメントを使用して単一ルールまたはルールの小さなサブセットについて他の開発者に詳細な情報を提供してください:

/* Required because YUI resets add a black border to all tables */

プログレッシブエンハンスメント

  • フォールバックは常に提供されるべきです。例えば背景画像やグラデーションの背景色のフォールバックを提供する場合です。
  • ベンダプリフィックスは対応ブラウザが接頭辞なしプロパティに対応していない場合のみ使用してください。
  • 標準プロパティはベンダプリフィックスプロパティの後に来るようにしてください。
.selector {
    background-color: #444; /* Fallback for browsers that don't support gradients. */
    filter: progid:DXImageTransform.Microsoft.gradient(startColorStr='#444', EndColorStr='#999'); /* IE6-IE9. */
    background-image: -webkit-gradient(linear, left top, left bottom, from(#444), to(#999)); /* Safari 4+, Chrome. */
    background-image: -webkit-linear-gradient(top, #444, #999); /* Chrome 10+, Safari 5.1+, iOS 5+. */
    background-image: -moz-linear-gradient(top, #444, #999); /* Firefox 3.6. */
    background-image: -ms-linear-gradient(top, #444, #999); /* IE10. */
    background-image: -o-linear-gradient(top, #444, #999); /* Opera 11.10+. */
    background-image: linear-gradient(top, #444, #999); /* W3C Standard. */
}

ブラウザハック

  • ブラウザ固有のハックは使用しないでください。Moodleではbody要素に追加されるクラスを使ってより適切なブラウザ固有のCSSを記述する方法を提供します。例えば以下のようになります:
.ie7 .forum-post {
    min-height: 1px;
}
  • Moodle coreがサポートしていないブラウザのバージョン (例: Moodle 3.0のIE8) のハックを含める必要はありません。

プラグイン Plugins

In plugins, the file names can be: プラグインではファイル名は以下のようにできます:

  • styles.css (Recommended)
  • styles.css (推奨)
  • styles_<theme name>.css (Not recommended, reserved to 3rd party plugins)
  • styles_<theme name>.css (非推奨、サードパーティプラグインのために予約)

プラグインではファイル名は以下のようにできます:

  • styles.css (推奨)
  • styles_<テーマ名>.css (非推奨、サードパーティ製プラグインに限ります)

必要最小限 Barebones

Plugins should define the strict minimum, no text sizes, colours, etc ... those should belong to the theme and not be hardcoded in plugins to allow for easy theming. Of course, this requires Moodle core to provide re-usable classes to style the elements. As Moodle 2.7 has made Bootstrap 2 by default we can start using their classes, but we should make sure that there is a sensible fallback for themes not extending bootstrapbase, such as base.

右から左へ Right-to-left

Since Moodle 3.2, most of the work is automatically done for developers.

自動フリッピング Auto flipping

Using the tool RTLCSS-PHP, the styles are automatically flipped when the language is right-to-left. However, as there always are a few exceptions, special comments can be added to the CSS files to prevent rules from being flipped, etc...

Here is an example.

.hello-world {
    margin-left: 10px;
    /*rtl:ignore*/
    padding-left: 10px;

    background: url('logo.png') 0 0 no-repeat;
    /*rtl:raw:
    background-image: url('logo-rtl.png');
    */

    color: blue;
}

Will become:

.hello-world {
    margin-right: 10px;
    padding-left: 10px;

    background: url('logo.png') 0 100% no-repeat;
    background-image: url('logo-rtl.png');

    color: blue;
}

For more information on the special comments, refer to the documentation of the tool RTLCSS-PHP.

注意事項 Caveats

It is important to note that the special comments must NOT end a file or a rule block. All comments not preceding a block, or a rule are ignored. Or in other words: currently the comments must always precede a statement, they will not work if they are not followed by anything.

Also note that comments in SCSS or LESS will often not produce the desired effect. When dealing with SCSS and LESS, ensure that your special RTL comments are attached to pure CSS statements: no

@extend

, indented rules, etc...

テキストの方向を強制する Forcing the text direction

Whilst

.dir-rtl

should not be used any more, we've added

.text-ltr

which allows developers to force the direction of the text to left-to-right. This is especially needed for forms fields like: URLs, OS level Folders paths & apps, theme hex colors, English DB field names, emails, English text, numbers, regular expression patterns, symbols, code snippets or configuration samples.

LESS

LESS works like CSS with some extra features. It should follow the CSS guidelines.

変数 Variables

  • They should use camelCase to follow Bootstrap 2 that is used in core.
  • As for CSS selectors, use semantic names: names tell what this is instead of what should it look like.
  • As Bootstrap 2 does, do not add the word "Color" to variables for _background_ or _border_ and their derivatives.
  • Declaring new variables should be done sparingly, too many variables kill the purpose of using them. If you declare one, try to set its default value from another one.
  • Do not declare more variables than necessary. E.g.: If the background color and border color are likely to always be the same, prefer one variable.

正しい Correct

@textColor: red;
@wellBackground: #ccc;
@tableBackground: blue;
@blockBackground: @wellBackground;
@calendarGroupEvent: #f90;

間違い Incorrect

@text-color: red;
@wellBackground: #ccc;
@tableBackgrounColor: blue;
@blockBackground: #ccc;
@calendarGroupEventBackground: #f90;
@calendarGroupEventBorder: #f90;

セレクタ Selectors

  • Selectors should be encapsulated rather than duplicated.

正しい Correct

div {
    .something {
        color: red;
        a {
            color: blue;
        }
    }
    .something-else a {
        color: green;
    }
}

間違い Incorrect

div .something {
    color: red;
}
div .something a {
    color: blue;
}
div .something-else a {
    color: green;
}

値およびプロパティ Values and properties

  • Colours, font sizes, etc... should never be hardcoded. You should, where possible, use a variable instead.
  • The use of 'mixins' is encouraged instead of duplicating values and properties.

正しい Correct

.error {
    font-size: @fontSizeSmall;
    color: @errorText;
    padding: 1em;
    background-color: @errorBackground;
}
div .form-error {
    .error;
}

間違い Incorrect

.error {
    font-size: 12px;
    color: red;
    padding: 1em;
    background-color: white;
}
div .form-error {
    font-size: 12px;
    color: red;
    padding: 1em;
    background-color: white;
}

テーマ「Clean」および「More」 Themes Clean and More

Clean theme should, where possible, not contain any CSS or LESS content. More theme, in comparison, inherits CSS styles for the logo from Clean theme, but also contains a small amount of LESS as an example for when customising a theme. Both Clean and More inherit fully all the CSS from their parent theme 'Bootstrap Base'. Cleanテーマは可能な限りCSSまたはLESSのコンテンツを含まないようにしています。それに比べてMoreテーマはロゴのCSSスタイルをCleanテーマから継承していますが、テーマのカスタマイズ時の例として少量のLESSも含んでいます。CleanおよびMoreは親テーマである「Bootstrap Base」からすべてのCSSを完全に継承しています。

謝辞 Credits

This document was drawn from the following sources:

  1. The WordPress CSS Coding Standards

関連情報