All class names start with a .govuk-
namespace to reduce the likelihood of
conflicting with existing classes in your application. It also helps to identify
where the styling for a particular element is coming from.
If you are building components for your own application or framework you should
use a different prefix, for example .app-
or the initials of your department.
GOV.UK Frontend uses the Block Element Modifier (BEM) methodology when naming CSS classes. This is designed to help developers understand how the different classes relate to each other.
The naming convention follows this pattern:
.block {}
.block__element {}
.block--modifier {}
.govuk-card // Block - the root of a component
.govuk-card__body // Element - a part of the block
.govuk-card--active // Modifier - a variant of the block
It uses double hyphens (--
) and underscores (__
) so that the block, element
or modifiers themselves can be hyphen delimited without causing ambiguity.
For example:
.govuk-phase-banner
.govuk-phase-banner__phase-tag
.govuk-phase-banner__phase-tag--light-blue
Break elements and modifiers outside of blocks rather than nesting using a
parent selector &
.
This makes the codebase easier to read, and makes it easier to search for a given class name. It also discourages excessive nesting.
Bad:
.govuk-breadcrumb {
...
&__item {
...
}
}
Good:
.govuk-breadcrumb {
...
}
.govuk-breadcrumb__item {
...
}
BEM stands for Block__Element--Modifier
, not Block__Element__Element--Modifier
.
Avoid including multiple elements when naming classes.
Each class has a single purpose, so you can be sure when making a change to a class - it will only affect the element that class is applied to.
Also when deprecating classes, all of the CSS for this class can be removed without affecting another component that had reused this css.
Why?
To ensure that styles can safely be added or removed without fear of breaking other components.
Keep all of the variants of a component in the same place.
.govuk-error-summary
modifies the .govuk-list
component.
Component modifiers use an extra class, scoped to the component:
.govuk-error-summary__list
This class is part of the component, rather than a parent of a component.
Why? This makes it easier to keep track of different contexts.
To ensure code quality and consistency in our Sass files we check that certain style rules are followed using a project YAML file
As we're using node-sass parser to parse our scss files, the package of choice is sass-lint.
See testing and linting for more information.
You can run the linter in gulp or check linting directly in Sublime Text, Atom or other IDE's
See also testing and linting.
We use the following rules when linting files:
Bad:
.selector {border: 0; padding: 0;}
Good:
.selector {
border: 0;
padding: 0;
}
Bad:
.selector {
color: #005ea5;
}
Good:
.selector {
color: $govuk-blue;
}
Bad:
$white: #FFF;
Good:
$white: #ffffff;
Bad:
.selector {
border: none;
}
Good:
.selector {
border: 0;
}
Bad:
#content {
...
}
Good:
.govuk-wrapper {
...
}
Bad:
p {
margin: 0;
em {
...
}
}
a {
...
}
Good:
p {
margin: 0;
em {
...
}
}
a {
...
}
Bad:
.govuk-breadcrumb {
...
&__item {
...
}
}
Good:
.govuk-breadcrumb {
...
}
.govuk-breadcrumb__item {
...
}
Bad:
@extend %contain-floats;
Good:
@include clearfix;
Bad:
margin: 1px 2px 3px 2px;
Good:
margin: 1px 2px 3px;
The basenames of @import
ed SCSS partials should not begin with an underscore and should not include the filename extension
Bad:
@import '_foo.scss';
@import '_bar/foo.scss';
Good:
@import 'foo';
@import 'bar/foo';
Bad:
.foo {
content:'bar';
}
Good:
.foo {
content: 'bar';
}
Operators should be formatted with a single space on both sides of an infix operator. These include +, -, *, /, %, ==, !=, >, >=, <,
and <=
Bad:
.selector {
margin: 5px+15px;
}
$foo: 1;
$bar: 3;
.selector {
margin: $foo+$bar+'px';
}
$foo: 1+1;
$bar: 2-1;
@if $foo==$bar {
$baz: 1;
}
@if ($foo!=$bar) {
$baz: 1;
}
Good:
.selector {
margin: 5px + 15px;
}
$foo: 1;
$bar: 3;
.selector {
margin: $foo + $bar + 'px';
}
$foo: 1 + 1;
$bar: 2 - 1;
@if $foo == $bar {
$baz: 1;
}
@if ($foo != $bar) {
$baz: 1;
}
Bad:
@function foo( $bar, $baz ) {
@return $bar + $baz;
}
Good:
@function foo($bar, $baz) {
@return $bar + $baz;
}
Functions, mixins, variables, and placeholders should be declared with all lowercase letters and hyphens instead of underscores
Bad:
@mixin FONT_STACK() {
font-family: $govuk-font-stack;
}
Good:
@mixin font-stack() {
font-family: $govuk-font-stack;
}
Bad:
.selector {
margin: 0px;
}
Good:
.selector {
margin: 0;
}
Bad:
.selector {
margin: 0
}
$my-example-var: value
Good:
.selector {
margin: 0;
}
$my-example-var: value;
Bad:
.selector {
font-size: 0.50em;
}
Good:
.selector {
font-size: 0.5em;
}
More write up on supported rules.
We document SCSS using SassDoc. This includes most of the settings, helpers and tools layers, with variables, functions and mixins being marked as private or public.
The syntax is used to generate a SassDoc application that documents SCSS in a readable format.
See colour.scss for an example of SassDoc syntax.