VitePress Custom Theme
While VitePress's default theme is powerful and configurable, sometimes you may need a completely unique design. VitePress allows you to take complete control of your site's UI by creating a custom theme.
Theme Entry Point
To use a custom theme, you need to create a theme directory under .vitepress and add an index.js or index.ts file to it. This file is your custom theme's entry point.
A most basic custom theme configuration file looks like this:
Layout Component
The Layout component is the core of a custom theme. It's a Vue component responsible for rendering the overall structure of the page, including the header, footer, sidebar, and most importantly—the Markdown content.
-
Create Layout File
Create a Vue component in the
themedirectory, such asMyLayout.vue. -
Write Layout Component
VitePress provides a special
<Content />component that you must include in your layout. It's responsible for rendering the current page's Markdown content. -
Register Layout in Theme
Now, import and use your layout component in
index.js.
Accessing Page Data
In your custom layout or any custom component, you may need to access the current page's data, such as frontmatter, title, path, etc. VitePress provides the useData composable function to achieve this.
Adding Styles
You can add styles to your custom theme in several ways:
-
In Layout Component: Use
<style>tags directly in.vuefiles. -
Import CSS Files: Import a CSS file directly in your
theme/index.js.
Creating a custom theme provides extremely high freedom, but it also means you need to handle more details yourself, such as navigation, responsive layout, etc. For most users who just want to make some adjustments, extending the default theme (next chapter content) might be a simpler and more efficient choice.