Last active
August 17, 2024 19:33
-
Star
(128)
You must be signed in to star a gist -
Fork
(13)
You must be signed in to fork a gist
-
-
Save tkadlec/683b26344cde774170b94c0fcf0088b4 to your computer and use it in GitHub Desktop.
CSS used to highlight potential performance issues
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
:root { | |
--violation-color: red; /* used for clear issues */ | |
--warning-color: orange; /* used for potential issues we should look into */ | |
} | |
/* IMAGES */ | |
/* | |
* Lazy-Loaded Images Check | |
* ==== | |
* Highlight any lazy loaded images so we can see if any are inside the viewport | |
* | |
* Uses an outline so it can pair with Unsized Images and Legacy Format checks | |
* Credit: https://twitter.com/csswizardry/status/1346477682544951296 | |
*/ | |
img[loading=lazy] { | |
outline: 10px solid var(--warning-color) !important; | |
} | |
/* | |
* Unsized Images Check | |
* ==== | |
* Highlight images that don't have a height or width attribute set | |
* | |
* Uses a border so it can pair with Lazy-Loaded and Legacy Format checks | |
*/ | |
img:not([height]), img:not([width]) { | |
border: 10px solid var(--violation-color) !important; | |
} | |
/* | |
* Legacy Format Check | |
* ==== | |
* Highlight tiff's and bmp's because we can do better | |
* Also JPG's because maybe we can use something like webp or avif instead | |
* | |
* Use opacity so we don't conflict with Lazy-Loaded and Unsized Images checks | |
*/ | |
img[src*='.jpg'], | |
img[src*='.tiff'], | |
img[src*='.bmp']{ | |
opacity: .5 !important; | |
} | |
/* SCRIPTS */ | |
/* Synchronous Scripts Check | |
* ==== | |
* Display any blocking synchronous scripts | |
* | |
* Credit: https://twitter.com/csswizardry/status/1336007323337285633 | |
*/ | |
head, | |
script[src] { | |
display: block; | |
border: 10px solid var(--violation-color);; | |
} | |
/* | |
* Display the URL/filepath of external scripts | |
*/ | |
script[src]::before { | |
content: attr(src); | |
font-size: 1rem; | |
} | |
/** | |
* Hide other head content and non-blocking scripts | |
*/ | |
head *, | |
script[src][async], script[src][defer], script[src][type=module] { | |
display: none; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Took me a while, but here's an interesting solution. Probably longer than I wanted but gives nice, in-place detection for images. These are steps that are needed to make this work:
The
wrap
function is optional. Since there's no way to use the:after
pseudo-selector for animg
element, we need to wrap allimg
s to apicture
element. This is only a convenient way to differentiate with the "default"opacity
behaviour specified in the original gist.Attached is an example for an image that is served from Cloudinary:
This renders the warning, since the image is viewed in Chrome but it has a JPG extension. Adding
f_auto
to the URL, will render a WebP:Please remember however that some Image CDNs do analyse the image and it may be more optimal to load a JPEG as opposed to a WebP image in Chrome as well.