Skip to content
Tutorials Center
My learning

Topics

Lesson 3 of 36 ยท Getting Started

First App: ThemeProvider and CssBaseline

Wire up a real Material UI app: wrap it in ThemeProvider, add CssBaseline for consistent styles, use createTheme, and learn how the theme reaches every component.

A Material UI button works with no setup at all, but a real app needs two more pieces: ThemeProvider, which gives every component access to your design choices, and CssBaseline, which resets the browser's default styles so your pages look the same everywhere. In this lesson you build the standard app shell used in almost every MUI project.

The default theme

Every MUI component reads its colors, fonts, spacing and shadows from a theme object. If you do nothing, MUI uses its built-in default theme: a blue primary color, a purple secondary color, the Roboto font and an 8px spacing unit. You do not have to wrap your app to use the default theme; components fall back to it automatically.

You wrap your app in ThemeProvider only when you want a different theme, or to switch themes at runtime (for example dark mode). Even so, many projects add the provider from day one so the structure is ready.

ThemeProvider and createTheme

createTheme() takes an object of overrides and merges it with the default theme. ThemeProvider puts the result into React context so that all components below it can read it. This real MUI code is read-only here:

JSX
import { createTheme, ThemeProvider } from '@mui/material/styles';
import CssBaseline from '@mui/material/CssBaseline';
import Button from '@mui/material/Button';

const theme = createTheme({
  palette: {
    primary: { main: '#0b7a75' },
    secondary: { main: '#e07a1f' },
  },
  typography: {
    fontFamily: '"Inter", "Roboto", "Helvetica", "Arial", sans-serif',
  },
  shape: { borderRadius: 10 },
});

export default function App() {
  return (
    <ThemeProvider theme={theme}>
      <CssBaseline />
      <Button variant="contained">Teal button</Button>
    </ThemeProvider>
  );
}

Only the values you pass are changed. Everything else (spacing, shadows, other palette colors, breakpoints) keeps the default. A later chapter on theming goes deeper; for now remember the shape: createTheme(options) builds a theme, ThemeProvider theme={...} applies it.

Create the theme outside your component (at module level) or memoize it. Calling createTheme inside a component body creates a new object on every render, which forces all themed components to re-render.

What CssBaseline does

Browsers ship slightly different default styles. CssBaseline is a small component (no visible output) that injects a global stylesheet. It:

  • Removes the default 8px margin on body.
  • Sets body background and text color from the theme, so dark mode flips the whole page.
  • Applies box-sizing: border-box to every element, which makes width and padding calculations predictable.
  • Sets the theme's font family, size and line height on the body.
  • Enables font smoothing and fixes a few cross-browser quirks.

Place it once, at the top of the tree, inside ThemeProvider so it can read the theme. If you only want some of it, ScopedCssBaseline applies the same reset to a single subtree, which helps when adding MUI to a legacy page without restyling everything.

See the difference

Below is a stand-in that shows what a baseline does. It imitates CssBaseline's reset with a plain stylesheet and a toggle. Turn it off to see the browser's default margins and mismatched sizing, then turn it on.

Stand-in: what a CSS baseline changes

The standard app shell

Here is the complete entry file of a typical Vite project. This is real MUI code (read-only):

JSX
// src/main.jsx
import { StrictMode } from 'react';
import { createRoot } from 'react-dom/client';
import { ThemeProvider } from '@mui/material/styles';
import CssBaseline from '@mui/material/CssBaseline';
import '@fontsource/roboto/400.css';
import '@fontsource/roboto/500.css';
import theme from './theme';
import App from './App';

createRoot(document.getElementById('root')).render(
  <StrictMode>
    <ThemeProvider theme={theme}>
      <CssBaseline />
      <App />
    </ThemeProvider>
  </StrictMode>
);

And the theme lives in its own file so any component can import it if needed:

JSX
// src/theme.js
import { createTheme } from '@mui/material/styles';

const theme = createTheme({
  palette: { mode: 'light', primary: { main: '#1d4ed8' } },
});

export default theme;

Remove the Vite starter styles (src/index.css and the default App.css) or they will fight with the baseline, for example by centering everything or setting a dark background.

How the theme reaches components

Under the hood the provider uses React context, exactly like the one you built in the context lesson. Every MUI component calls useTheme() (or the equivalent in its styled wrapper) and reads values. You can do the same in your own components:

JSX
import { useTheme } from '@mui/material/styles';

function Badge({ children }) {
  const theme = useTheme();
  return (
    <span style={{ color: theme.palette.primary.main, padding: theme.spacing(1) }}>
      {children}
    </span>
  );
}

theme.spacing(1) returns 8px, theme.spacing(2) returns 16px, and so on. The nested feature is also useful: a ThemeProvider inside another one merges its theme into the outer one, so you can give one section of the page a dark look.

The next stand-in builds a miniature theme system so you can see the mechanics: a context holds a theme object, a useTheme hook reads it, and a nested provider overrides one value.

Stand-in: a tiny ThemeProvider and useTheme

Real MUI does the same thing with a much richer theme: palette, typography, spacing, breakpoints, shadows, z-index, component defaults and more.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What does ThemeProvider do?

    ThemeProvider places the theme in React context; MUI components and your own useTheme calls read from it.
  2. 2. Which of these does CssBaseline do?

    CssBaseline injects global base styles: margin reset, border-box sizing, theme background, text color and font smoothing.
  3. 3. Where should createTheme usually be called?

    Creating the theme at module level gives a stable object. Creating it in render makes a new theme each time and re-renders themed components.
  4. 4. What happens to theme values you do not pass to createTheme?

    createTheme deep-merges your options into the default theme, so spacing, shadows and other values you did not override still exist.

Summary

  • Every MUI component reads a theme object; without a provider it uses the default theme.
  • createTheme(options) merges your overrides into the defaults; ThemeProvider shares the result via context.
  • CssBaseline resets browser styles, sets border-box sizing and applies theme colors and fonts to the page.
  • Create the theme once, at module level, and remove conflicting starter CSS.
  • Nested providers merge into outer ones, so a section can have its own look.

Next: The sx Prop Basics