How can I display images on a HiDPI display using HTML produced from Markdown such that the images are shown in high resolution. Normally, I'd create <img>
tags with srcset
attributes or set the width
and height
on each image appropriately or use media queries. However, the result of Markdown images only allow for an alt
attribute. Specifically, I'm using the marked library (I have no control over this).
I do have complete control over the CSS. Is there a way, in CSS, to at least make all images 1/3rd the logical pixels wide than they have physical pixels in width? That way I could require all images referenced in Markdown to be 3x. Is it as simple as this?
img {
width: 33.33%;
}
Much better would be some way to have the browser download a different image depending on the devicePixelRatio
of the device.
Using HTML is not an option. marked is used with the sanitize
flag to escape any HTML. I'm hoping there's a CSS solution or some other clever mechanism.
One way I've been able to work with HiDPI images coming out of Markdown is utilizing the
title
attribute. Image titles can be semantically meaningful and provide information to CSS to effectively size images using attribute selectors.As an example, I can create a Markdown image like this:
Then I can use an attribute selector to set the width from 1200px down to 400px, making it very crisp for devices with a
devicePixelRatio
of 3:That says for all important examples to have a logical width of 400 pixels. If I feed it a 1200 pixel image, standard DPI devices will have a slower image download, but will otherwise be fine.
Ideally, there's a solution that I could feed multiple images for different
devicePixelRatio
s. However, this is at least good enough for now.