Skip to content

Using CSS

As we’ve seen earlier, in addition to your React components with TypeScript, you can also create CSS files to style your components.

There are a few ways to include CSS files in your extensions:

  • Using cssmodules to access classes through the object automatically generated by the bundler.
  • Using import to globally access the classes (e.g., className='btn').

Premises

Using CSS is recommended exclusively for styling your extensions. CSS files have access to the global scope where they are loaded, meaning they can technically target and style other elements of the Sales App core. However, using global CSS to style elements outside of your extension, relying on selectors like tag, class, id, or any other combination not specific to the extension, is discouraged for the following reasons:

  • Future releases may include sandboxing mechanisms that isolate CSS and/or extensions from the core application’s global context (e.g., DOM).
  • Using selectors like classes or IDs on core Sales App elements (e.g., the “Continue” button, the cart list, header, logo) is not feasible, as Sales App generates unique IDs for classes applied to its elements.
  • Relying on the markup to create selectors with the goal of customizing the core Sales App is risky, as the markup may change with core updates (e.g., adding or removing elements in the DOM).

With that in mind, CSS should only be used for styling the extension itself. Any styling applied to core elements is subject to breaking changes due to bug fixes, new features, and other updates.

CSS Modules Recommended

Using CSS Modules is a recommended approach because it ensures that class names are scoped locally to the component, avoiding potential conflicts with other styles in the global namespace.

With CSS Modules, classes are imported as an object, and you can apply them to your components dynamically, ensuring a clean separation of styles.

Example:

CustomMessage.tsx
import styles from './custom-message.module.css';
export const CustomMessage = () => {
return (
<p className={styles.footer}>
Extension example
</p>
);
};

Imports Not recommended

When using imports, you will include .css files in the relevant TypeScript file. You can either create a single file and import it once in src/index.tsx, or create component-specific files and import them in the component’s file.

src/index.tsx
import { defineExtensions } from '@vtex/sales-app'
import './styles.css'

CSS Variables

For the full list of available variables and usage examples, visit the CSS Variables page.