2
0

css_guidelines.rst 4.4 KB

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