Adding custom css to a `bs_theme()`

I would like to generate a custom bs theme object that includes a few additional CSS files. However I am not entirely clear on how to do this.

I tried the following, but the CSS files are not found in the correct location, I am guessing they need to be included as an htmlDependency or similar, just wasn't sure what the best way to do this would be.

my_theme <- function(...) {
  style <- system.file("assets", "style.css", package = "mypkg")
  theme <- bslib::bs_theme(...)
  theme <- bslib::bs_add_rules(theme, sass::sass_file(style))
  theme
}

I haven't tried adding multiple CSS to bs_theme() directly, but would you consider using htmltools::htmlDependency()? Or does it HAVE to be through bs_theme()?

I am trying to package up a theme so it can be used by other people, so ideally it would a drop in replacement for a bs_theme() call.

What exactly do you mean by "not found in the correct location"? If you could make a small github repo as a reprex that would help... but also it is a lot of work, maybe wait till someone with more experience with bslib comes along.

I personally always use htmlDependency and never created a custom bs_theme but I'd like to help if you're still stuck later

e.g. if you run the following, then check the dev console you will see the css file is not found

writeLines("
body {
  background: black;
}
", con = "style.css")

my_theme <- function(...) {
  style <- normalizePath("style.css")
  theme <- bslib::bs_theme(...)
  theme <- bslib::bs_add_rules(theme, sass::sass_file(style))
  theme
}

bslib::bs_theme_preview(my_theme())

After doing some testing, it seems like it would work if you change style.css to style.scss. I initially thought that might be the case but refused to believe that because I assumed anything that works with scss file should work with css file. But I was wrong :slight_smile:

@jimhester did that help?

This topic was automatically closed 54 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.