You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

README.md 5.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. # [gulp](https://github.com/wearefractal/gulp)-inline-css [![Build Status](https://travis-ci.org/jonkemp/gulp-inline-css.svg?branch=master)](https://travis-ci.org/jonkemp/gulp-inline-css) [![Coverage Status](https://coveralls.io/repos/jonkemp/gulp-inline-css/badge.svg?branch=master&service=github)](https://coveralls.io/github/jonkemp/gulp-inline-css?branch=master)
  2. [![NPM](https://nodei.co/npm/gulp-inline-css.png?downloads=true)](https://nodei.co/npm/gulp-inline-css/)
  3. > Inline your CSS properties into the `style` attribute in an html file. Useful for emails.
  4. Inspired by the grunt plugin [grunt-inline-css](https://github.com/jgallen23/grunt-inline-css). Uses the [inline-css](https://github.com/jonkemp/inline-css) module.
  5. ## What's new in 3.0?
  6. - Uses Promises with [inline-css](https://github.com/jonkemp/inline-css) version 2.0
  7. ## How It Works
  8. This gulp plugin takes an html file and inlines the CSS properties into the style attribute.
  9. `/path/to/file.html`:
  10. ```html
  11. <html>
  12. <head>
  13. <style>
  14. p { color: red; }
  15. </style>
  16. <link rel="stylesheet" href="style.css">
  17. </head>
  18. <body>
  19. <p>Test</p>
  20. </body>
  21. </html>
  22. ```
  23. `style.css`
  24. ```css
  25. p {
  26. text-decoration: underline;
  27. }
  28. ```
  29. Output:
  30. ```html
  31. <html>
  32. <head>
  33. </head>
  34. <body>
  35. <p style="color: red; text-decoration: underline;">Test</p>
  36. </body>
  37. </html>
  38. ```
  39. ## What is this useful for ?
  40. - HTML emails. For a comprehensive list of supported selectors see
  41. [here](http://www.campaignmonitor.com/css/)
  42. - Embedding HTML in 3rd-party websites.
  43. - Performance. Downloading external stylesheets delays the rendering of the page in the browser. Inlining CSS speeds up this process because the browser doesn't have to wait to download an external stylesheet to start rendering the page.
  44. ## Install
  45. Install with [npm](https://npmjs.org/package/gulp-inline-css)
  46. ```
  47. npm install --save-dev gulp-inline-css
  48. ```
  49. ## Usage
  50. ```js
  51. var gulp = require('gulp'),
  52. inlineCss = require('gulp-inline-css');
  53. gulp.task('default', function() {
  54. return gulp.src('./*.html')
  55. .pipe(inlineCss())
  56. .pipe(gulp.dest('build/'));
  57. });
  58. ```
  59. With options:
  60. ```js
  61. var gulp = require('gulp'),
  62. inlineCss = require('gulp-inline-css');
  63. gulp.task('default', function() {
  64. return gulp.src('./*.html')
  65. .pipe(inlineCss({
  66. applyStyleTags: true,
  67. applyLinkTags: true,
  68. removeStyleTags: true,
  69. removeLinkTags: true
  70. }))
  71. .pipe(gulp.dest('build/'));
  72. });
  73. ```
  74. Options are passed directly to [inline-css](https://github.com/jonkemp/inline-css).
  75. ## API
  76. ### inlineCss(options)
  77. #### options.extraCss
  78. Type: `String`
  79. Default: `""`
  80. Extra css to apply to the file.
  81. #### options.applyStyleTags
  82. Type: `Boolean`
  83. Default: `true`
  84. Whether to inline styles in `<style></style>`.
  85. #### options.applyLinkTags
  86. Type: `Boolean`
  87. Default: `true`
  88. Whether to resolve `<link rel="stylesheet">` tags and inline the resulting styles.
  89. #### options.removeStyleTags
  90. Type: `Boolean`
  91. Default: `true`
  92. Whether to remove the original `<style></style>` tags after (possibly) inlining the css from them.
  93. #### options.removeLinkTags
  94. Type: `Boolean`
  95. Default: `true`
  96. Whether to remove the original `<link rel="stylesheet">` tags after (possibly) inlining the css from them.
  97. #### options.url
  98. Type: `String`
  99. Default: `filePath`
  100. How to resolve hrefs. Must be a string of one character or more. **Required**.
  101. Relative urls in links will have this value prepended to them. So these links:
  102. * `<a href="page-relative">Page</a>`
  103. * `<a href="/root-relative">Root</a>` <- _note leading /_
  104. With this option:
  105. ```js
  106. inlineCss(html, { url: 'http://example.com/mushroom'})
  107. .then(function(html) { console.log(html); });
  108. ```
  109. Will result in
  110. * `<a href="http://example.com/mushroom/page-relative">Page</a>`
  111. * `<a href="http://example.com/root-relative">Root</a>`
  112. If you don't need this feature, simply set the property to a short string eg `{url: ' '}` (one space) and everything will work.
  113. #### options.preserveMediaQueries
  114. Type: `Boolean`
  115. Default: `false`
  116. Preserves all media queries (and contained styles) within `<style></style>` tags as a refinement when `removeStyleTags` is `true`. Other styles are removed.
  117. #### options.applyWidthAttributes
  118. Type: `Boolean`
  119. Default: `false`
  120. Whether to use any CSS pixel widths to create `width` attributes on elements.
  121. #### options.applyTableAttributes
  122. Type: `Boolean`
  123. Default: `false`
  124. Whether to apply the `border`, `cellpadding` and `cellspacing` attributes to `table` elements.
  125. #### options.removeHtmlSelectors
  126. Type: `Boolean`
  127. Default: `false`
  128. Whether to remove the `class` and `id` attributes from the markup.
  129. #### options.codeBlocks
  130. Type: `Object`
  131. Default: `{ EJS: { start: '<%', end: '%>' }, HBS: { start: '{{', end: '}}' } }`
  132. 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 `HBS: {start: '{{', end: '}}'}`. `codeBlocks` can fix problems where otherwise inline-css 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 `codeBlocks: {...}` do `codeBlocks.myBlock = {...}`.
  133. ### Special markup
  134. #### data-embed
  135. When a data-embed attribute is present on a <style></style> tag, inline-css will not inline the styles and will not remove the <style></style> tags.
  136. This can be used to embed email client support hacks that rely on css selectors into your email templates.
  137. ### cheerio options
  138. Options to passed to [cheerio](https://github.com/cheeriojs/cheerio).
  139. ## License
  140. MIT © [Jonathan Kemp](http://jonkemp.com)