Skip to content

Installation

Edit on GitHub

@volvo-cars/css is delivered as CSS files hosted on the shared volvocars.com CDN. File names include content-based hashes, so only changed files are purged from the cache when a new version is released.

The links() function in @volvo-cars/css/links returns an array of link attribute objects for the current version. Render each object as a <link> element, so you don’t need to update file names when you upgrade.

This guide uses React with the Next.js App Router. See alternative methods below for other setups.


  1. Install the package

    Terminal window
    yarn add @volvo-cars/css
  2. Add the CSS links to your <head>

    In your root layout, app/layout.tsx:

    import { links } from '@volvo-cars/css/links';
    export default function RootLayout({ children }) {
    return (
    <html lang="en">
    <head>
    {links().map((link) => (
    <link key={link.rel + link.href} {...link} />
    ))}
    </head>
    <body>{children}</body>
    </html>
    );
    }

    With the Pages Router, render the same links inside <Head> in pages/_document.tsx.

You can now use design system classes and tokens throughout your application.


Plain HTML (HTML string output, no JSX)

For a plain HTML page, call links() on the server and write the resulting <link> tags into the HTML you send. The array returned by links() stringifies to HTML <link> tags:

import { links } from '@volvo-cars/css/links';
const html = `<html><head>${links()}</head><body>...`;
Client-side JavaScript
import { links } from '@volvo-cars/css/links';
const fragment = document.createDocumentFragment();
for (const link of links()) {
fragment.append(Object.assign(document.createElement('link'), link));
}
document.head.append(fragment);
Micro-frontends (partial install)

For micro-frontend components you may want to exclude some link tags using the filter option:

import { links } from '@volvo-cars/css/links';
links({
filter: {
// Excludes all `<link rel=preload>` tags
preload: false,
// Excludes font-face CSS and the font preload
fonts: false,
// Excludes tokens.css
tokens: false,
},
});
Self-hosted (not on volvocars.com)

If your bundler supports it, you can import the CSS files directly from the npm package. They will be served from your own domain. Do not do this if your application is hosted on www.volvocars.com: use the CDN instead.

import '@volvo-cars/css/font-face.css';
import '@volvo-cars/css/tokens.css';
import '@volvo-cars/css/styles_all-media.css';

After installation, confirm everything is working by adding a simple element to your page:

<div class="bg-secondary p-24 border">
<h2>Hello, Design System</h2>
<p>
If this has a gray background with a border and comfortable padding, you're
all set.
</p>
</div>
  • CSS architecture. Understand how global styles, utility classes, and component-scoped CSS work together.
  • Colors. Learn about the color system and available tokens.
  • VS Code extension. Get IntelliSense for class names and tokens.
  • Linting. Set up ESLint and Stylelint for design system compliance.