| 1 | # PostCSS image-set() Function [<img src="https://postcss.github.io/postcss/logo.svg" alt="PostCSS Logo" width="90" height="90" align="right">][postcss]
|
|---|
| 2 |
|
|---|
| 3 | [![NPM Version][npm-img]][npm-url]
|
|---|
| 4 | [![CSS Standard Status][css-img]][css-url]
|
|---|
| 5 | [![Build Status][cli-img]][cli-url]
|
|---|
| 6 | [<img alt="Discord" src="https://shields.io/badge/Discord-5865F2?logo=discord&logoColor=white">][discord]
|
|---|
| 7 |
|
|---|
| 8 | [PostCSS image-set() Function] lets you display resolution-dependent images
|
|---|
| 9 | using the `image-set()` function in CSS, following the [CSS Images]
|
|---|
| 10 | specification.
|
|---|
| 11 |
|
|---|
| 12 | [](https://caniuse.com/#feat=css-image-set)
|
|---|
| 13 |
|
|---|
| 14 | ```pcss
|
|---|
| 15 | .example {
|
|---|
| 16 | background-image: image-set(
|
|---|
| 17 | url(img.png) 1x,
|
|---|
| 18 | url(img@2x.png) 2x,
|
|---|
| 19 | url(img@print.png) 600dpi
|
|---|
| 20 | );
|
|---|
| 21 | }
|
|---|
| 22 |
|
|---|
| 23 | /* becomes */
|
|---|
| 24 |
|
|---|
| 25 | .example {
|
|---|
| 26 | background-image: url(img.png);
|
|---|
| 27 | }
|
|---|
| 28 |
|
|---|
| 29 | @media (-webkit-min-device-pixel-ratio: 2), (min-resolution: 192dpi) {
|
|---|
| 30 | .example {
|
|---|
| 31 | background-image: url(img@2x.png);
|
|---|
| 32 | }
|
|---|
| 33 | }
|
|---|
| 34 |
|
|---|
| 35 |
|
|---|
| 36 | @media (-webkit-min-device-pixel-ratio: 6.25), (min-resolution: 600dpi) {
|
|---|
| 37 | .example {
|
|---|
| 38 | background-image: url(my@print.png);
|
|---|
| 39 | }
|
|---|
| 40 | }
|
|---|
| 41 |
|
|---|
| 42 | .example {
|
|---|
| 43 | background-image: image-set(
|
|---|
| 44 | url(img.png) 1x,
|
|---|
| 45 | url(img@2x.png) 2x,
|
|---|
| 46 | url(img@print.png) 600dpi
|
|---|
| 47 | );
|
|---|
| 48 | }
|
|---|
| 49 | ```
|
|---|
| 50 |
|
|---|
| 51 | ## Usage
|
|---|
| 52 |
|
|---|
| 53 | Add [PostCSS image-set() Function] to your project:
|
|---|
| 54 |
|
|---|
| 55 | ```bash
|
|---|
| 56 | npm install postcss-image-set-function --save-dev
|
|---|
| 57 | ```
|
|---|
| 58 |
|
|---|
| 59 | Use [PostCSS image-set() Function] as a [PostCSS] plugin:
|
|---|
| 60 |
|
|---|
| 61 | ```js
|
|---|
| 62 | const postcss = require('postcss');
|
|---|
| 63 | const postcssImageSetFunction = require('postcss-image-set-function');
|
|---|
| 64 |
|
|---|
| 65 | postcss([
|
|---|
| 66 | postcssImageSetFunction(/* pluginOptions */)
|
|---|
| 67 | ]).process(YOUR_CSS /*, processOptions */);
|
|---|
| 68 | ```
|
|---|
| 69 |
|
|---|
| 70 | [PostCSS image-set() Function] runs in all Node environments, with special
|
|---|
| 71 | instructions for:
|
|---|
| 72 |
|
|---|
| 73 | | [Node](INSTALL.md#node) | [PostCSS CLI](INSTALL.md#postcss-cli) | [Webpack](INSTALL.md#webpack) | [Create React App](INSTALL.md#create-react-app) | [Gulp](INSTALL.md#gulp) | [Grunt](INSTALL.md#grunt) |
|
|---|
| 74 | | --- | --- | --- | --- | --- | --- |
|
|---|
| 75 |
|
|---|
| 76 | ## Options
|
|---|
| 77 |
|
|---|
| 78 | ### preserve
|
|---|
| 79 |
|
|---|
| 80 | The `preserve` option determines whether the original declaration using
|
|---|
| 81 | `image-set()` is preserved. By default, it is preserved.
|
|---|
| 82 |
|
|---|
| 83 | ```js
|
|---|
| 84 | postcssImageSetFunction({ preserve: false })
|
|---|
| 85 | ```
|
|---|
| 86 |
|
|---|
| 87 | ```pcss
|
|---|
| 88 | .example {
|
|---|
| 89 | background-image: image-set(
|
|---|
| 90 | url(img.png) 1x,
|
|---|
| 91 | url(img@2x.png) 2x,
|
|---|
| 92 | url(img@print.png) 600dpi
|
|---|
| 93 | );
|
|---|
| 94 | }
|
|---|
| 95 |
|
|---|
| 96 | /* becomes */
|
|---|
| 97 |
|
|---|
| 98 | @media (-webkit-min-device-pixel-ratio: 1), (min-resolution: 96dpi) {
|
|---|
| 99 | .example {
|
|---|
| 100 | background-image: url(img.png);
|
|---|
| 101 | }
|
|---|
| 102 | }
|
|---|
| 103 |
|
|---|
| 104 | @media (-webkit-min-device-pixel-ratio: 2), (min-resolution: 192dpi) {
|
|---|
| 105 | .example {
|
|---|
| 106 | background-image: url(img@2x.png);
|
|---|
| 107 | }
|
|---|
| 108 | }
|
|---|
| 109 |
|
|---|
| 110 |
|
|---|
| 111 | @media (-webkit-min-device-pixel-ratio: 6.25), (min-resolution: 600dpi) {
|
|---|
| 112 | .example {
|
|---|
| 113 | background-image: url(my@print.png);
|
|---|
| 114 | }
|
|---|
| 115 | }
|
|---|
| 116 | ```
|
|---|
| 117 |
|
|---|
| 118 | ### onvalid
|
|---|
| 119 |
|
|---|
| 120 | The `oninvalid` option determines how invalid usage of `image-set()` should be
|
|---|
| 121 | handled. By default, invalid usages of `image-set()` are ignored. They can be
|
|---|
| 122 | configured to display a `warning` or `throw` an error.
|
|---|
| 123 |
|
|---|
| 124 | ```js
|
|---|
| 125 | postcssImageSetFunction({ oninvalid: 'warning' }) // warn on invalid usages
|
|---|
| 126 | ```
|
|---|
| 127 |
|
|---|
| 128 | ```js
|
|---|
| 129 | postcssImageSetFunction({ oninvalid: 'throw' }) // throw on invalid usages
|
|---|
| 130 | ```
|
|---|
| 131 |
|
|---|
| 132 | ## Image Resolution
|
|---|
| 133 |
|
|---|
| 134 | The `image-set()` function allows an author to provide multiple resolutions of
|
|---|
| 135 | an image and let the browser decide which is most appropriate in a given
|
|---|
| 136 | situation. The `image-set()` also never fails to choose an image; the
|
|---|
| 137 | `<resolution>` just helps determine which of the images is chosen.
|
|---|
| 138 |
|
|---|
| 139 | Since this plugin is not a browser, the image options are sorted by device
|
|---|
| 140 | pixel ratio and the lowest ratio is used as the default, while the remaining
|
|---|
| 141 | images are pushed behind media queries.
|
|---|
| 142 |
|
|---|
| 143 | Therefore, this plugin can only approximate native browser behavior. While
|
|---|
| 144 | images should typically match the resolution as the device they’re being viewed
|
|---|
| 145 | in, other factors can affect the chosen image. For example, if the user is on a
|
|---|
| 146 | slow mobile connection, the browser may prefer to select a lower-res image
|
|---|
| 147 | rather than wait for a larger, resolution-matching image to load.
|
|---|
| 148 |
|
|---|
| 149 | [cli-img]: https://github.com/csstools/postcss-plugins/workflows/test/badge.svg
|
|---|
| 150 | [cli-url]: https://github.com/csstools/postcss-plugins/actions/workflows/test.yml?query=workflow/test
|
|---|
| 151 | [css-img]: https://cssdb.org/images/badges/image-set-function.svg
|
|---|
| 152 | [css-url]: https://cssdb.org/#image-set-function
|
|---|
| 153 | [discord]: https://discord.gg/bUadyRwkJS
|
|---|
| 154 | [npm-img]: https://img.shields.io/npm/v/postcss-image-set-function.svg
|
|---|
| 155 | [npm-url]: https://www.npmjs.com/package/postcss-image-set-function
|
|---|
| 156 |
|
|---|
| 157 | [CSS Images]: https://drafts.csswg.org/css-images-4/#image-set-notation
|
|---|
| 158 | [Gulp PostCSS]: https://github.com/postcss/gulp-postcss
|
|---|
| 159 | [Grunt PostCSS]: https://github.com/nDmitry/grunt-postcss
|
|---|
| 160 | [PostCSS]: https://github.com/postcss/postcss
|
|---|
| 161 | [PostCSS Loader]: https://github.com/postcss/postcss-loader
|
|---|
| 162 | [PostCSS image-set() Function]: https://github.com/csstools/postcss-plugins/tree/main/plugins/postcss-image-set-function
|
|---|