
Product
Socket for Jira Is Now Available
Socket for Jira lets teams turn alerts into Jira tickets with manual creation, automated ticketing rules, and two-way sync.
rehype-resolution
Advanced tools
The rehype-resolution Node.js package provides a rehype plugin for inserting srcset attributes into img elements. rehype is a unified HTML processor.
The srcset attribute of the img element enables responsive images. This plugin inserts a srcset for resolution switching, allowing different resolutions at the same size.
If you're supporting multiple display resolutions, but everyone sees your image at the same real-world size on the screen, you can allow the browser to choose an appropriate resolution image by using srcset with x-descriptors and without sizes — a somewhat easier syntax!
The rehype-resolution plugin inserts a set of possible image sources (@1x, @2x, and @3x), following Apple’s naming convention for image size and resolution. Users have to produce these images and make them available at the according paths.
The transformation is only applied for file names ending with @1x.*. That’s the marker for the transform. Elements with an existing srcset attribute are ignored.
Generating a static website from Markdown, I want crisp images on all devices.
We have an image at three resolutions, elva@1x.jpg, elva@2x.jpg, and elva@3x.jpg. Let’s assume we don’t control the HTML, maybe it’s generated from Markdown, leaving us with HTML like this.
<img src="img/elva@1x.jpg" alt="Dressed as a fairy">
To let the browser pick the optimal image, this transform inserts the srcset attribute for three resolutions, allowing us to keep referencing the image with a single URI in our Markdown or whatever HTML source we might have.
<img srcset="img/elva@1x.jpg,img/elva@2x.jpg,img/elva@3x.jpg" src="img/elva@1x.jpg" alt="Dressed as a fairy">
Inserts srcset into HTML fragment.
const parse = require('rehype-parse')
const resolution = require('./')
const stringify = require('rehype-stringify')
const unified = require('unified')
const { log } = console
unified()
.use(parse, { fragment: true })
.use(resolution)
.use(stringify)
.process('<img src="logo@1x.png">', (er, file) => {
if (er) throw er
const { contents } = file
log(contents)
})
Try it.
$ node example.js
<img src="logo@1x.png" srcset="logo@1x.png 1x, logo@2x.png 2x, logo@3x.png 3x">
To install rehype-resolution with npm, do:
$ npm install rehype-resolution
FAQs
Insert srcset attribute into img elements
We found that rehype-resolution demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Product
Socket for Jira lets teams turn alerts into Jira tickets with manual creation, automated ticketing rules, and two-way sync.

Company News
Socket won two 2026 Reppy Awards from RepVue, ranking in the top 5% of all sales orgs. AE Alexandra Lister shares what it's like to grow a sales career here.

Security News
NIST will stop enriching most CVEs under a new risk-based model, narrowing the NVD's scope as vulnerability submissions continue to surge.