CSS
- Approach
- CSS MUST be treated as a progressive enhancement
- CSS MUST be served as a static file and MUST NOT be compiled at runtime
- Compiled CSS MUST NOT contain a source file map in the
.cssfile itself - Compiled CSS SHOULD contain a link to a separate source file (
.map.css) - Compiled CSS SHOULD be compressed (e.g.
sass --style=compressed [src] [dest])
- Style/linting
- CSS SHOULD adhere to the BEM methodology
- CSS MUST be styled with Prettier
- Styling CSS with Prettier SHOULD NOT use any custom options (Prettier's philosophy on options)
- CSS MUST be linted with Stylelint
- Stylelint MUST use the
stylelint-selector-bem-patternplugin - Stylelint COULD use the
stylelint-orderplugin - Stylelint COULD extend the TNA Frontend stylelint configuration
- Stylelint COULD be configured to ignore some rules
- Stylelint MUST use the
- Style content
- CSS colours MUST be defined as either hex values or
rgb - CSS colours defined using
rgbSHOULD use the newer syntax (rgb(255 128 0)rather thanrgb(255, 128, 0)) - CSS colours with alpha SHOULD use the newer
rgbsyntax (e.g.rgb(255 128 0/0.5)) - CSS SHOULD work without support for CSS variables
- CSS variables COULD be used to enhance customisability
- CSS colours MUST be defined as either hex values or
- Media
- If we expect the web content to be printed or saved to a PDF, CSS print styles SHOULD be considered
- A
<link>element that imports CSS SHOULD have amediaattribute defined
SASS/SCSS
SASS is a preprocessor that compiles SASS syntax to CSS. It comes with a loose, indentation-based syntax.
SCSS is a newer SASS syntax and is more aligned to CSS, requiring braces and semicolons and allowing comments.
- Approach
- SCSS COULD be used as an enhancement to CSS
- As SCSS is a superset of CSS, the same CSS standards above MUST also apply to SCSS
- Style/linting
- The SCSS syntax MUST be used over the SASS syntax
- Stylelint MUST extend
stylelint-config-standard-scss
- Frameworks, tools and libraries
BEM
The CSS methodology we use is BEM.
On 4th February 2021, frontend developers met to agree a CSS methodology. Having considered a recommendation for utility CSS (as represented in tailwindcss) and results of the most recent state of CSS survey, the team agreed that the BEM methodology would best reflect the needs of the team at this time.
TNA Frontend uses a mix of BEM and classless strategies. Simple "top-level" elements such as <ul> have a class (tna-ul) while the <li> elements within it do not, as the <ul> element should only contain <li>. For more information, see details of the structure of TNA Frontend.
Stylelint
Stylelint is "a mighty CSS linter that helps you avoid errors and enforce conventions".
npm install stylelint stylelint-config-standard-scss stylelint-selector-bem-pattern stylelint-order
An example .stylelintrc file that addresses all the standards above:
{
"extends": [
"stylelint-config-standard-scss"
],
"plugins": [
"stylelint-selector-bem-pattern"
],
"rules": {
"at-rule-empty-line-before": null,
"block-no-empty": null,
"declaration-empty-line-before": null,
"property-no-vendor-prefix": null,
"value-keyword-case": null,
"scss/dollar-variable-empty-line-before": null,
"scss/double-slash-comment-empty-line-before": null,
"selector-class-pattern": null,
"plugin/selector-bem-pattern": {
"preset": "bem"
}
}
}
# Run stylelint against all CSS files in the src directory...
stylelint 'src/**/*.css'
# ...or all SCSS files
stylelint 'src/**/*.scss'
TNA Frontend Stylelint config
TNA Frontend comes with a Stylelint config which you can use in your project.
It includes property ordering for which you will need to install the stylelint-order module:
npm install stylelint-order
You can then change your stylelint.config.mjs to extend the TNA Frontend config:
import data from "./node_modules/@nationalarchives/frontend/config/stylelint.config.js";
// Add your own rules
data.ignoreFiles = ["app/**/*.css", "tmp/**/*"];
export default data;
Working with high contrast environments
There are two ways of requesting and setting high contrast views in a web application. They use the CSS media features of:
prefers-contrastforced-colors
prefers-contrast
The TNA Frontend's "system" theme follows the preference for prefers-contrast which can be no-preference, more, less or custom. See MDN Web Docs - prefers-contrast.
TNA Frontend currently only supports the prefers-contrast values of no-preference and more.
You can use this media feature to make designs with increased contrast.
forced-colors
Windows offers a "High Contrast Mode" which allows a user to choose their own colour palette. This can be queried using the forced-colors CSS media feature.
Using forced-colors has some accessibility concerns because the browser has no visibility of the colour palette.
Read the article The difference between Increased Contrast Mode and Windows High Contrast Mode (Forced Colours Mode) for more information.
Use this media feature only to make small changes such as adding extra borders. Do not set colours using forced-colors.