-
Notifications
You must be signed in to change notification settings - Fork 13
CSS
There are four types of CSS files in this project:
-
Page stylesheets - There's one of these per page; they live next to the page's
.jsx
file and share the same name. -
Component stylesheets - There's one of these per component; they live next to the component's
.jsx
file and share the same name. - gu-sass stylesheets - These contain exclusively SCSS functions and mixins, to be used in either of the other types of stylesheets described above.
- skeleton stylesheets - These contain CSS generic to the entire site, e.g. accessibility, resets, basic html etc.
To add a new stylesheet to the project simply create the file and then import
it into your page or component. Webpack will resolve this and add it to a CSS file named after your bundle's entry point.
Example
π myComponent.jsx
import React from 'react';
import './myComponent.scss'; /*looks weird at first*/
Currently in support-frontend
there are two types of components.
Shared component: Components that are used in more than one page, they are located inside the global components
folder. These should only contain rules which are context-independent. For instance, font sizes, colours, padding (since it's internal), margins of child elements. But margin on the parent, for example, should probably be avoided, because it's presupposing that it will be used in a particular place. This kind of positional styling should happen in the page-level CSS.
Non-shared component: Components specific to a page which are not used outside that page. Typically located inside the components
folder of a certain page.
Inside gu-sass/layout.scss
you'll find shared variables for sizing and spacing. You should be able to derive most metrics from these. This lets the next person understand where a number is coming from and what it aligns to:
//π bad
.component-button {
padding: 6px 12px;
}
//β
better
.component-button {
padding: $gu-v-spacing/2 $gu-h-spacing/4;
}
In the same manner, gu-sass/typography.scss
contains several mixins for fonts based on their ise (body, headline, info). Always prefer these to rolling your own. If a design contains a font combo that's very similar to an existing fontset but not quite, consult about reusing the same fontset rather than overriding it.
//π bad
.component-button {
font-family: $gu-text-sans-web;
font-size: 17px;
line-height: 20px;
font-weight: 700;
}
//β
better
.component-button {
@include gu-fontset-body-sans;
font-weight: 700;
}
The classes should follow the BEM convention system. The shared components should have the component
prefix in their class names.
//π bad
#double-heading {
@include gu-fontset-body-sans;
}
h2 {
color: gu-colour(neutral-1);
}
//β
better
.component-double-heading {
@include gu-fontset-body-sans;
}
.component-double-heading__subheading {
color: gu-colour(neutral-1);
}
Nesting should be avoided. The only case where it is justified is inside a CSS file of a page, where we nest the entire style of a page inside the page's id to avoid conflicts with other pages' rules.
//π bad
.component-button {
background: gu-colour(highlight-main);
.component-button__text {
@include gu-fontset-body-sans;
}
}
//β
better
.component-button {
background: gu-colour(highlight-main);
}
.component-button__text {
@include gu-fontset-body-sans;
}
The .scss
file of examplePage
will have the following shape:
#example-page {
.component-example-component{
// Specific rules for a shared component in this specific page
}
}
When adding a shared component into a page, sometimes you need add extra CSS rules for that component. Those rules should be put inside the CSS file of a page. Additionally, you shouldn't override any rule from the shared component.
If you find yourself overriding a lot of CSS rules, that is an indicator that you should either:
- Add a new prop to the component itself to define and style that appearance.
- Move those rules out of the component altogether and place them inside the page specific file.
As an example, a shared component which will have a different position (determined via margin-right
) in each page, should not have a margin-right
value in the shared component's css file but inside each specific page.
As an example consider the following CSS for a shared component called DoubleHeading
.
.component-double-heading {
font-size: 28px;
line-height: 32px;
}
.component-double-heading__heading {
@include gu-fontset-headline;
color: gu-colour(brightness-7);
}
.component-double-heading__subheading {
color: gu-colour(brightness-7);
@include gu-fontset-body-sans;
@include mq($from: phablet) {
text-transform: uppercase;
}
}
Note that we avoid nesting, we use a BEM approach and we prefix the classes with component
.
- Redux Glossary
- Why Redux Toolkit?
- Writing state slices with Redux Toolkit
- Handling action side effects in Redux
- Presentational and Container Components
- Scoped actions and reducers
- Server Side Rendering
- Form validation
- CI build process
- Post deployment testing
- Post deployment test runbook
- TIP Real User Testing
- Code testing and validation
- Visual testing
- Testing Apple Pay locally
- Test Users
- Deploying to CODE
- Automated IT tests
- Deploying Fastly VCL Snippets
- End-to-end Tests with Playwright
- Archived Components
- Authentication
- Switchboard
- How to make a fake contribution
- The epic and banner
- Environments
- Tech stack
- Supported browsers
- Contributions Internationalisation
- Payment method internationalisation in Guardian Weekly
- Print fulfilment/delivery
- Updating the acquisitions model
- Runscope testing
- Scala Steward for dependency management
- Alarm Investigations
- Ticker data
- Ophan
- Quantum Metric
- [Google Tag Manager] (https://github.com/guardian/support-frontend/wiki/Google-Tag-Manager)