Try setting aspect-ratio on images lacking width and height #1420
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Summary
I started looking at this because I was checking how well Image Prioritizer optimizes a Timber-based theme. It adds
fetchpriority=high
and lazy-loading correctly, but the Timber starter theme does not includewidth
andheight
attributes on the featured image, and Image Prioritizer hasn't yet implemented this either. So I wanted to see what it would take.This experiment tries to set an
aspect-ratio
onimg
tags that are missingwidth
andheight
attributes. There is currently a big problem, however, in that it is setting theaspect-ratio
based on theclientBoundingRect
and not the intrinsic dimensions of the image. This means that once theaspect-ratio
is set the next time thatdetect.js
measures the image, it will use theaspect-ratio
for subsequent measurements, meaning that the image dimensions will become locked in place. In order to get around this, I thinkdetect.js
may need to be extend to obtain the intrinsic dimensions of elements that have them:img
,video
,canvas
,svg
(maybe), andinput[type=image]
(ewwww).Another problem is that in order to set the
aspect-ratio
, the element has to also havewidth:100%
added. This may not be what is intended!Fixes #10