Loading CSS only for the page you visit helps your website load faster and keeps styles organized.
CSS imports per route in Remix
export function links() {
return [{ rel: 'stylesheet', href: '/styles/route-style.css' }];
}The links function is exported from a route file in Remix.
It returns an array of objects telling Remix which CSS files to load for that route.
home.css only on the home page route.export function links() {
return [{ rel: 'stylesheet', href: '/styles/home.css' }];
}export function links() {
return [
{ rel: 'stylesheet', href: '/styles/about.css' },
{ rel: 'stylesheet', href: '/styles/shared.css' }
];
}export function links() {
return [];
}This Remix route loads dashboard.css only when the Dashboard page is visited. The styles inside dashboard.css will apply only to this page, helping keep the CSS focused and the page fast.
import { LinksFunction } from '@remix-run/node'; export const links: LinksFunction = () => { return [{ rel: 'stylesheet', href: '/styles/dashboard.css' }]; }; export default function Dashboard() { return ( <main> <h1>Dashboard</h1> <p>Welcome to your dashboard.</p> </main> ); }
Make sure the CSS file path in href is correct and accessible.
Each route can have its own links function to load different CSS files.
Using this method improves performance by loading only needed styles.
Use the links function in Remix routes to load CSS per page.
This keeps styles organized and improves page load speed.
Each route can load one or more CSS files as needed.
Practice
Solution
Step 1: Understand Remix route styling
Remix uses a speciallinksexport in route files to load CSS only for that route.Step 2: Why
This method keeps styles scoped and improves loading by only fetching needed CSS.linksis preferredFinal Answer:
By exporting alinksfunction that returns CSS links from the route file -> Option AQuick Check:
Route CSS =linksexport [OK]
links export in route files for CSS [OK]- Importing CSS inside JSX instead of using links
- Adding all CSS globally instead of per route
- Using inline styles everywhere
links function?Solution
Step 1: Identify correct
Remix expectslinksexport syntaxlinksas a function returning an array of objects withrelandhref.Step 2: Check arrow function usage
export const links = () => [{ rel: 'stylesheet', href: './styles.css' }]; uses an arrow function returning the correct array format, which is valid and common.Final Answer:
export const links = () => [{ rel: 'stylesheet', href: './styles.css' }]; -> Option AQuick Check:
Arrow function returning array = export const links = () => [{ rel: 'stylesheet', href: './styles.css' }]; [OK]
- Using default export instead of named export
- Returning JSX instead of array
- Importing CSS directly without links function
import styles from '~/styles/page.css';
export const links = () => [
{ rel: 'stylesheet', href: styles }
];Solution
Step 1: Understand CSS import in Remix
Importing CSS asstylesgives the path string for the CSS file.Step 2: Check
Thelinksusagelinksfunction returns an array with the CSS file href set tostyles, so it loads that CSS file.Final Answer:
The CSS file at '~/styles/page.css' will be loaded -> Option BQuick Check:
Imported CSS path used in links = The CSS file at '~/styles/page.css' will be loaded [OK]
- Thinking import gives CSS content, not path
- Assuming all folder CSS loads automatically
- Confusing global CSS with route CSS
export function links() {
return {
rel: 'stylesheet',
href: '/styles.css'
};
}Solution
Step 1: Check return type of
Remix expectslinkslinksto return an array of link objects, not a single object.Step 2: Validate other attributes
The href and rel values are correct; only the return type is wrong.Final Answer:
Thelinksfunction should return an array, not an object -> Option DQuick Check:
linksreturns array = Thelinksfunction should return an array, not an object [OK]
links [OK]- Returning a single object instead of array
- Incorrect function name casing
- Wrong rel attribute value
base.css and theme.css, only on a specific Remix route. How should you write the links function to achieve this?Solution
Step 1: Understand multiple CSS imports in
Thelinkslinksfunction must return an array of objects, each withrelandhref.Step 2: Check each option's correctness
export const links = () => [ { rel: 'stylesheet', href: '/base.css' }, { rel: 'stylesheet', href: '/theme.css' } ]; correctly returns an array with two link objects. Others either return an object, JSX, or invalid array contents.Final Answer:
export const links = () => [ { rel: 'stylesheet', href: '/base.css' }, { rel: 'stylesheet', href: '/theme.css' } ]; -> Option CQuick Check:
Array of link objects = export const links = () => [ { rel: 'stylesheet', href: '/base.css' }, { rel: 'stylesheet', href: '/theme.css' } ]; [OK]
links [OK]- Returning a single object with duplicate keys
- Returning JSX instead of array
- Returning array of strings instead of objects
Ready to prove it?
Pass the test and this pattern moves unknown
