Automattic/juice
Snapshot của Automattic/juice: 3.3k★ · JavaScript · Frontend. Juice inlines CSS stylesheets into your HTML source.
Tóm tắt dựng từ metadata GitHub của chính dự án — chưa có bài review TopGit. Trang sẽ tự động cập nhật khi bài review đầy đủ được xuất bản.
TopGit viết bài đầy đủ cho repo có nhiều sao nhất và được yêu cầu nhiều nhất. Trang này là snapshot trong thời gian chờ — xem README gốc ở tab READ ME.
Snapshot
Cộng tác viên hàng đầu
Xem cộng tác viên hàng đầu
Juice 
Given HTML, Juice will inline your CSS properties into the style attribute.
Some projects using Juice →
How to use
Juice has a number of functions based on whether you want to process a file, HTML string, or a cheerio document, and whether you want Juice to automatically get remote stylesheets, scripts and image dataURIs to inline.
To inline HTML without getting remote resources, using default options:
import juice from 'juice';
const result = juice("<style>div{color:red;}</style><div/>");
result will be:
<div style="color: red;"></div>
Try out the web client version
What is this useful for?
- HTML emails. For CSS support in email clients see Can I Email.
- Embedding HTML in 3rd-party websites.
Documentation
Juice is exposed as a standard module, and from CLI with a smaller set of options.
Modern CSS support
Juice handles modern CSS out of the box: nested rules (CSS Nesting Module Level 1, .card { &:hover { ... } }) are flattened automatically before inlining, @container and @layer at-rules pass through verbatim, and specificity for :is() / :where() / :has() / :not() is computed per the CSS Selectors Level 4 spec.
Options
All Juice methods take an options object that can contain any of these properties, though not every method uses all of these:
| Option | Default value | Description |
|---|---|---|
applyAttributesTableElements | true | Create attributes for styles in juice.styleToAttribute on elements set in juice.tableElements. |
applyHeightAttributes | true | Use any CSS pixel heights to create height attributes on elements set in juice.heightElements. |
applyStyleTags | true | Inline styles in <style> tags. |
applyWidthAttributes | true | Use any CSS pixel widths to create width attributes on elements set in juice.widthElements. |
decodeStyleAttributes | false | Decode the value of style attributes. |
extraCss | "" | Extra CSS to apply to the file. |
inlineDuplicateProperties | false | When true, declarations with identical CSS properties will all be inlined, instead of only the one with the highest specificity. Useful for progressive enhancement. |
insertPreservedExtraCss | true | Whether to insert into the document any preserved @media or @font-face content from extraCss when using preserveMediaQueries, preserveFontFaces or preserveKeyFrames. When set to true, order of preference to append the <style> element is into head, then body, then at the end of the document. When a string the value is treated as a CSS/jQuery/cheerio selector, and when found, the <style> tag will be appended to the end of the first match. |
inlinePseudoElements | false | Insert pseudo elements (::before and ::after) as <span> into the DOM. Note: Inserting pseudo elements will modify the DOM and may conflict with CSS selectors elsewhere on the page (e.g., :last-child). |
preserveFontFaces | true | Preserve all @font-face within <style> tags as a refinement when removeStyleTags is true. Other styles are removed. |
preserveImportant | false | Preserve !important in CSS values. |
preserveMediaQueries | true | Preserve all media queries (and contained styles) within <style> tags as a refinement when removeStyleTags is true. Other styles are removed. |
preserveKeyFrames | true | Preserve all key frames within <style> tags as a refinement when removeStyleTags is true. Other styles are removed. |
preservePseudos | true | Preserve all rules containing pseudo selectors defined in ignoredPseudos within <style> tags as a refinement when removeStyleTags is true. Other styles are removed. |
preservedSelectors | [] | Array of strings that represent CSS selectors to preserve inside <style> tags when removeStyleTags or removeInlinedSelectors are true. Matches substrings. |
removeInlinedSelectors | false | Remove CSS rules from <style> tags after (possibly) inlining them. Other rules are preserved. Works only if removeStyleTags is false. |
removeStyleTags | true | Remove the original <style> tags after (possibly) inlining their CSS content. Overrides removeInlinedSelectors. |
resolveCSSVariables | true | Resolve CSS variables. |
webResources | {} | An options object that will be passed to web-resource-inliner for Juice functions that will get remote resources (juiceResources and juiceFile). |
xmlMode | false | Output XML/XHTML with all tags closed. Note that the input must also be valid XML/XHTML or you will get undesirable results. |
Methods
juice(html [, options])
Returns a string containing the HTML with inlined CSS.
Does not fetch remote resources.
html- HTML string, accepts complete documents as well as fragmentsoptions- optional, see Options above
juice.juiceResources(html, options, callback)
Callback returns a string containing the HTML with inlined CSS.
Fetches remote resources.
html- (string) HTMLoptions- see Options abovecallback(err, html)err-Errorobject ornullhtml- (string) HTML with inlined CSS
juice.juiceFile(filePath, options, callback)
Callback returns a string containing the HTML with inlined CSS.
Fetches remote resources.
filePath- path to the HTML file to be Juicedoptions- see Options abovecallback(err, html)err-Errorobject ornullhtml- (string) HTML with inlined CSS
juice.juiceDocument($ [, options])
This takes a cheerio instance and performs inlining in-place. Returns the same cheerio instance.
Does not fetch remote resources.
$- acheerioinstance, be sure to use the samecheerioversion that Juice usesoptions- (object) optional, see Options above
juice.inlineContent(html, css [, options])
This takes HTML and CSS and returns new HTML with the provided CSS inlined.
It does not look at <style> or <link rel="stylesheet"> elements at all.
html- HTML stringcss- CSS stringoptions- (object) optional, see Options above
juice.inlineDocument($, css [, options])
Given a cheerio instance and CSS, this modifies the cheerio instance so that the provided CSS is inlined. It does not look at <style> or <link rel="stylesheet"> elements at all.
$- acheerioinstance, be sure to use the samecheerioversion that juice usescss- CSS stringoptions- (object) optional, see Options above
Global settings
juice.codeBlocks
Type: Object
Default:
{
EJS: { start: '<%', end: '%>' },
HBS: { start: '{{', end: '}}' }
}
An object where each value has a start and end to specify fenced code blocks that should be ignored during parsing and inlining.
For example, Handlebars (hbs) templates are juice.codeBlocks.HBS = {start: '{{', end: '}}'}. codeBlocks can fix problems where otherwise juice might interpret code like <= as HTML, when it is meant to be template language code.
Note that codeBlocks is a dictionary which can contain many different code blocks, so don't do juice.codeBlocks = {...} do juice.codeBlocks.myBlock = {...}
juice.ignoredPseudos
Type: Array
Default: ['hover', 'active', 'focus', 'visited', 'link']
Array of ignored pseudo-selectors such as 'hover' and 'active'.
juice.widthElements
Type: Array
Default: ['TABLE', 'TD', 'TH', 'IMG']
Array of HTML elements that can receive width attributes.
juice.heightElements
Type: Array
Default: ['TABLE', 'TD', 'TH', 'IMG']
Array of HTML elements that can receive height attributes.
juice.styleToAttribute
Type: Object
Default:
{
'background-color': 'bgcolor',
'background-image': 'background',
'text-align': 'align',
'vertical-align': 'valign'
}
Object of style property names (key) to their respective attribute names (value).
juice.tableElements
Type: Array
Default: ['TABLE', 'TH', 'TR', 'TD', 'CAPTION', 'COLGROUP', 'COL', 'THEAD', 'TBODY', 'TFOOT']
Array of table HTML elements that can receive attributes defined in juice.styleToAttribute.
juice.nonVisualElements
Type: Array
Default: [ 'HEAD', 'TITLE', 'BASE', 'LINK', 'STYLE', 'META', 'SCRIPT', 'NOSCRIPT' ]
Array of elements that will not have styles inlined because they are not intended to render.
juiceClient.excludedProperties
Type: Array
Default: []
Array of CSS properties that won't be inlined.
Special markup
data-embed
Add data-embed to any <style> tag to prevent Juice from inlining its CSS and removing it.
Can be used to embed email client support hacks that rely on CSS selectors into your email templates:
<style data-embed>
u + .body .your-class-name {
/* CSS here targets Gmail */
}
</style>
The data-embed attribute will be removed from the output HTML, but no inlining or style tag removal will take place:
<style>
u + .body .your-class-name {
/* CSS here targets Gmail */
}
</style>
data-juice-duplicates
Override the inlineDuplicateProperties option for a single element by adding data-juice-duplicates to it. The attribute presence (or data-juice-duplicates="true") enables duplicate declarations for that element; data-juice-duplicates="false" disables them:
<!-- keeps both background-color declarations, even if the option is off -->
<div class="header" data-juice-duplicates></div>
<!-- collapses to the highest-specificity declaration, even if the option is on -->
<div class="header" data-juice-duplicates="false"></div>
The data-juice-duplicates attribute is removed from the output HTML.
data-juice-important
Override the preserveImportant option for a single element by adding data-juice-important to it. The attribute presence (or data-juice-important="true") preserves !important in that element's inlined declarations; data-juice-important="false" strips it:
<!-- keeps !important in the inlined style, even if the option is off -->
<div class="cta" data-juice-important></div>
<!-- strips !important from the inlined style, even if the option is on -->
<div class="cta" data-juice-important="false"></div>
The data-juice-important attribute is removed from the output HTML.
Ignoring CSS with comments
You can use special CSS comments to prevent Juice from inlining entire CSS files, rules, or even just declarations.
Ignore entire file
Add a /* juice ignore */ comment on the first line in your CSS:
<style>
/* juice ignore */
body { color: red; }
.test { color: blue; }
</style>
<body>
<div class="test">Hello World</div>
</body>
The entire CSS will be ignored (as in, not inlined).
With removeStyleTags: true, the whole <style> tag will be preserved.
Ignore next rule
Add /* juice ignore next */ before any CSS rule to skip inlining that rule:
body { color: black; }
/* juice ignore next */
h1 {
color: blue;
}
p { color: green; }
The h1 rule will not be inlined, but will be preserved when removeStyleTags: true. The body and p rules will be inlined normally.
Ignore next declaration
Add /* juice ignore next */ inside a CSS rule to skip inlining the next declaration:
.test {
color: red;
/* juice ignore next */
font-weight: bold;
font-size: 14px;
}
The color and font-size will be inlined, but font-weight will not. The rule with the ignored declaration will be preserved in a <style> tag when removeStyleTags: true.
Ignore blocks of code
Use start/end comment pairs to ignore multiple rules:
h1 {
color: black;
}
/* juice start ignore */
h2 {
color: pink;
}
h3 {
color: lightcoral;
}
/* juice end ignore */
h4 {
color: green;
}
The h2 and h3 rules will not be inlined but will be preserved in a <style> tag when removeStyleTags: true. The h1 and h4 rules will be inlined normally.
CLI Options
To use Juice from CLI, run juice [options] input.html output.html
For a listing of all available options, just type juice -h.
Note that if you want to just type
juicefrom the command line, you shouldnpm install juice -gso it is globally available.
The CLI should have all the above options with the names changed from camel case to hyphen-delimited, so for example extraCss becomes extra-css and webResources.scripts becomes web-resources-scripts.
These are additional options not included in the standard juice options listed above:
--css [filepath]will load and inject CSS intoextraCss.--options-file [filepath]will load and inject options from a JSON file. Options from the CLI will be given priority over options in the file when there is a conflict.codeBlocksis optionally supported in the options file if you include it. This will allow you to support different template languages in a build process.
Running Juice in the Browser
Juice's default entry imports Node-only modules (fs, path), so it cannot run as-is in the browser. For browser builds, import juice/client instead, which exposes juiceDocument, inlineDocument, and inlineContent (but not juiceFile, juiceResources, or inlineExternal).
Juice is ESM-only as of v12. Any modern bundler — Vite, webpack 5, esbuild, Rollup, Parcel 2+ — will pick up juice/client correctly via the "browser" field in package.json. Browserify is not supported on v12+ (it can't parse ESM). Note that automated tests are not running in the browser yet.
License
MIT Licensed, see LICENSE.md.
3rd-party
- Icon by UnheardSounds
Repo liên quan
Bootstrap is an open-source front-end framework maintained at twbs/bootstrap, with 174,577 stars and 78,678 forks on GitHub. It ships compiled and minified CSS and JavaScript for building responsive, mobile-first sites, is licensed under MIT, and the release referenced throughout the README's install instructions is v5.3.8. The project was created by Mark Otto and Jacob Thornton, and its documentation is built with Astro and hosted at getbootstrap.com.
Electron (electron/electron on GitHub) is an open-source, MIT-licensed framework for building cross-platform desktop apps with JavaScript, HTML, and CSS on top of Node.js and Chromium. The README covers installing it as a dev dependency through npm, lists platform support for macOS, Windows, and Linux, and points to a companion tool, Electron Fiddle, for trying the API before starting a full project.
App Ideas Collection is a GitHub repository maintained by Florin Pop and Jim Medlock that catalogs application ideas developers can build to practice their skills, organized into three difficulty tiers. Each entry defines an objective, user stories, optional bonus features, and resource links rather than a bare project name, and the repository is sponsored by CodeRabbit and licensed under MIT with 96,743 stars and 10,579 forks.
Web Development for Beginners - A Curriculum is Microsoft's free, MIT-licensed course of 24 lessons spread across 12 weeks, covering JavaScript, CSS, and HTML. Built by Microsoft Cloud Advocates, it pairs quizzes with five hands-on projects, a terrarium, a typing game, a browser extension, a space game, and a banking app, and ships in 50+ language translations.
Trả lời nhanh
Automattic/juice có phải mã nguồn mở không?
Có — Automattic/juice phát hành theo license MIT, nghĩa là mã nguồn mở để đọc, fork và (tùy license) tái sử dụng. Mã: github.com/Automattic/juice.
Automattic/juice là gì?
Automattic/juice (Automattic/juice) là dự án JavaScript trên GitHub. Theo mô tả gốc: Juice inlines CSS stylesheets into your HTML source.
Automattic/juice so với các dự án Frontend khác thế nào?
Automattic/juice được TopGit xếp vào nhóm Frontend, với 3.3k sao GitHub và viết bằng JavaScript. Xem trang chủ đề Frontend trên TopGit để so sánh với các dự án tương tự theo số sao và mức độ hoạt động.
Cùng nhóm Frontend còn repo nào?
Automattic/juice thuộc nhóm Frontend trên TopGit, cùng 4 topic GitHub. Trang Trending và Topics liệt kê các repo cùng số sao và cùng ngôn ngữ để so sánh.
Đọc thêm về Automattic/juice ở đâu?
Trang TopGit này là một snapshot — tab "Readme" hiển thị nguyên văn README của repo (đã bỏ link, giữ ảnh). Repo GitHub ở github.com/Automattic/juice là nguồn chính thức.
Vì sao Automattic/juice được xếp vào nhóm Frontend?
TopGit xếp Automattic/juice vào nhóm Frontend dựa trên GitHub topics và mô tả của repo (gắn thẻ: "css", "email", "html"). Việc phân loại dựa trên metadata thật của repo, không phải đoán theo cảm tính biên tập.
Đọc đầy đủ README ở tab phía trên.
Chưa chắc juice có hợp với bạn?
Để ChatGPT, Claude hoặc Perplexity tìm hiểu giúp — bấm bên dưới và xem AI nói gì về juice.