css_guidelines.rst 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. CSS coding guidelines
  2. ===========================
  3. Our CSS is written in `Sass <https://sass-lang.com/>`_, using the SCSS syntax.
  4. Compiling
  5. ~~~~~~~~~
  6. The SCSS source files are compiled to CSS using the
  7. `gulp <https://gulpjs.com/>`_ build system.
  8. This requires `Node.js <https://nodejs.org>`_ to run.
  9. To install the libraries required for compiling the SCSS,
  10. run the following from the Wagtail repository root:
  11. .. code-block:: console
  12. $ npm install --no-save
  13. To compile the assets, run:
  14. .. code-block:: console
  15. $ npm run build
  16. Alternatively, the SCSS files can be monitored,
  17. automatically recompiling when any changes are observed, by running:
  18. .. code-block:: console
  19. $ npm start
  20. Linting and formatting SCSS
  21. ~~~~~~~~~~~~~~~~~~~~~~~~~~~
  22. Wagtail uses the `stylelint <https://stylelint.io/>`_ linter.
  23. You'll need Node.js and npm on your development machine.
  24. Ensure project dependencies are installed by running ``npm install --no-save``
  25. Linting code
  26. ------------
  27. Run the linter from the wagtail project root:
  28. .. code-block:: console
  29. $ npm run lint:css
  30. The linter is configured to check your code for adherence to the guidelines
  31. below, plus a little more.
  32. Formatting code
  33. ---------------
  34. If you want to autofix errors, you can run that command directly with:
  35. .. code-block:: console
  36. $ npm run lint:css -- --fix
  37. Changing the linter configuration
  38. ---------------------------------
  39. The configuration for the linting rules is managed in an external
  40. repository so that it can be easily shared across other Wagtail projects
  41. or plugins. This configuration can be found at
  42. `stylelint-config-wagtail <https://github.com/wagtail/stylelint-config-wagtail>`_.
  43. Styleguide Reference
  44. ~~~~~~~~~~~~~~~~~~~~
  45. Spacing
  46. -------
  47. - Use soft-tabs with a four space indent. Spaces are the only way to
  48. guarantee code renders the same in any person's environment.
  49. - Put spaces after ``:`` in property declarations.
  50. - Put spaces before ``{`` in rule declarations.
  51. - Put line breaks between rulesets.
  52. - When grouping selectors, put each selector on its own line.
  53. - Place closing braces of declaration blocks on a new line.
  54. - Each declaration should appear on its own line for more accurate
  55. error reporting.
  56. - Add a newline at the end of your ``.scss`` files.
  57. - Strip trailing whitespace from your rules.
  58. - Add a space after the comma, in comma-delimited property values e.g ``rgba()``
  59. Formatting
  60. ----------
  61. - Use hex color codes ``#000`` unless using ``rgba()`` in raw CSS
  62. (SCSS' ``rgba()`` function is overloaded to accept hex colors as a
  63. param, e.g., ``rgba(#000, .5)``).
  64. - Use ``//`` for comment blocks (instead of ``/* */``).
  65. - Use single quotes for string values
  66. ``background: url('my/image.png')``
  67. - Avoid specifying units for zero values, e.g., ``margin: 0;`` instead
  68. of ``margin: 0px;``.
  69. - Strive to limit use of shorthand declarations to instances where you
  70. must explicitly set all the available values.
  71. Sass imports
  72. ------------
  73. Leave off underscores and file extensions in includes:
  74. .. code-block:: scss
  75. // Bad
  76. @import 'components/_widget.scss'
  77. // Better
  78. @import 'components/widget'
  79. Pixels vs. ems
  80. --------------
  81. Use ``rems`` for ``font-size``, because they offer
  82. absolute control over text. Additionally, unit-less ``line-height`` is
  83. preferred because it does not inherit a percentage value of its parent
  84. element, but instead is based on a multiplier of the ``font-size``.
  85. Specificity (classes vs. ids)
  86. -----------------------------
  87. Always use classes instead of IDs in CSS code. IDs are overly specific and lead
  88. to duplication of CSS.
  89. When styling a component, start with an element + class namespace,
  90. prefer direct descendant selectors by default, and use as little
  91. specificity as possible. Here is a good example:
  92. .. code-block:: html
  93. <ul class="category-list">
  94. <li class="item">Category 1</li>
  95. <li class="item">Category 2</li>
  96. <li class="item">Category 3</li>
  97. </ul>
  98. .. code-block:: scss
  99. .category-list { // element + class namespace
  100. // Direct descendant selector > for list items
  101. > li {
  102. list-style-type: disc;
  103. }
  104. // Minimal specificity for all links
  105. a {
  106. color: #f00;
  107. }
  108. }
  109. Class naming conventions
  110. ------------------------
  111. Never reference ``js-`` prefixed class names from CSS files. ``js-`` are
  112. used exclusively from JS files.
  113. Use the SMACSS ``is-`` `prefix <https://smacss.com/book/type-state>`__
  114. for state rules that are shared between CSS and JS.
  115. Misc
  116. ----
  117. As a rule of thumb, avoid unnecessary nesting in SCSS. At most, aim for
  118. three levels. If you cannot help it, step back and rethink your overall
  119. strategy (either the specificity needed, or the layout of the nesting).
  120. Examples
  121. --------
  122. Here are some good examples that apply the above guidelines:
  123. .. code-block:: scss
  124. // Example of good basic formatting practices
  125. .styleguide-format {
  126. color: #000;
  127. background-color: rgba(0, 0, 0, .5);
  128. border: 1px solid #0f0;
  129. }
  130. // Example of individual selectors getting their own lines (for error reporting)
  131. .multiple,
  132. .classes,
  133. .get-new-lines {
  134. display: block;
  135. }
  136. // Avoid unnecessary shorthand declarations
  137. .not-so-good {
  138. margin: 0 0 20px;
  139. }
  140. .good {
  141. margin-bottom: 20px;
  142. }