With the juicyjuice package it’s fairly easy to transform HTML such that all CSS is inlined directly into HTML tags.
So this:
<style>div{color:blue;}</style><div/>
Will turn to this:
<div style="color: blue;"></div>
Why do this? An important use case is HTML email and this is mainly because of Outlook on Windows (it does not support multiple classes on elements). Furthermore CSS inlining can help preserve a decent layout in email clients that do not support embedded CSS (in <style>
tags), or, when an email is forwarded. Here’s another reason to use this: embedding HTML in 3rd-party websites.
This R package contains a single function, css_inline()
, where the only input required is a string of HTML text. It uses the juice JavaScript library (https://github.com/Automattic/juice), which is (1) well-tested, (2) very performant, and (3) has lots of great features. The aformentioned function will return a string of CSS-inlined HTML.
The juicyjuice package can be installed from CRAN with:
install.packages("juicyjuice")
You can also choose to install the development version of juicyjuice from GitHub:
devtools::install_github("rstudio/juicyjuice")
If you encounter a bug, have usage questions, or want to share ideas to make this package better, please feel free to file an issue.
Please note that the juicyjuice project is released with a contributor code of conduct.
By participating in this project you agree to abide by its terms.
juicyjuice is licensed under the MIT license. See the LICENSE.md
file for more details.
© Posit Software, PBC.
This project is primarily maintained by Rich Iannone. Should there also be other authors, they might occasionally assist with some of these duties.