7.8 CSS and themes

The format xaringan::moon_reader has a css option, to which you can pass a vector of CSS file paths, e.g.,

---
output:
  xaringan::moon_reader:
    css:["default", "extra.css"]
---
  • The file path should contain the extension .css. If a path does not contain a filename extension, it is assumed to be a built-in CSS file in the xaringan package.

  • Make sure to insert the relative path to the file if it is not placed in the same directory as your .Rmd file.

  • When you specify multiple CSS files, the one that comes later will take priority and override the previous ones if there are conflicting CSS rules.

  • To see all built-in CSS files, call xaringan:::list_css() in R.

    xaringan:::list_css() # print path of built-in CSS files
    names(xaringan:::list_css()) # print names only
  • When you only want to override a few CSS rules in the default theme, you do not have to copy the whole file default.css; instead, create a new (and hopefully smaller) CSS file that only provides new CSS rules.

  • Users have contributed a few themes to xaringan. For example, you can use the metropolis theme (https://github.com/pat-s/xaringan-metropolis):

Read R Markdown: The Definitive Guide, Section 7.5 for more details about CSS and themes.