html/css optimization

Hey, what are some recommended tips for improving performance that you can incorporate into your html/css, i've heard of things like the preconnect attribute for font sites etc. Thanks in advance.
36 Replies
vince
vince6mo ago
add widths / heights to images, download fonts and store them in your project rather than using a separate stylsheet (don't use google fonts link or @import for example). add loading=lazy to your images; use .webp and compress more that i'm not thinking of but that should help a lot
snxxwyy
snxxwyyOP6mo ago
awesome, appreciate that, i'll give them a look have you got any resources by any chance explaining the reasoning behind these and how to efficiently do things like downloading the fonts etc?
vince
vince6mo ago
iirc I think web.dev has some good optimization resources -- it's by google i think i learned a lot by just putting my site in pagespeed insights (lighthouse) and looking up the errors or warnings it gave me and seeing how to fix them for example adding explicit width / height attributes on your html element (rather than css) allows the page to know how much space to make for the image or element before the css is loaded. this basically stops the image from having 0 height / width to 500px for example and it'll be jarring for the user and worse for performance ( i dont know the performance specific parts though so can't explain well)
snxxwyy
snxxwyyOP6mo ago
alrighty i'll give them a shot, thanks
ἔρως
ἔρως6mo ago
also add decoding="lazy" to your images you MUST HAVE A GOOD alt it's a requirement, not optional you can also use <link preconnect>, to speed up loading resources from other domains, like a cdn
vince
vince6mo ago
whats that? i dont see anything in mdn about it
vince
vince6mo ago
u mean async?
ἔρως
ἔρως6mo ago
yes, async, sorry
vince
vince6mo ago
nw! ty
ἔρως
ἔρως6mo ago
wrong value but yeah, using that is a good idea there's more things to do, like using lazyloading for iframes too
vince
vince6mo ago
web.dev also has something for lazy loading videos too, it's a pretty simple js file
ἔρως
ἔρως6mo ago
yes, you can set it to just preload metadata, which makes the video preload just what it needs you can also use fake progressive loading by encoding the image as a very small base64 url, and blur it while the image is loading, you can show it as a background that represents the image that will be loaded (this because we don't have jpegxl support yet, and progressive jpegs aren't common) also, using the nasty webp oh, and some critical path css as well, that's very important reducing all svg icons into a single file with <symbol> is a good way to save space there's so much you can do to optimize your website
vince
vince6mo ago
i was going to suggest that but i've never found a good way to manually figure out the critical css. i've tried sites that supposedly set it but they dont help either. only way ive gotten critical css to work is using plugins in Astro or some Vite build step nvm just looked it up and looks like an easy npm package if you're using that
ἔρως
ἔρως6mo ago
what i do is to send css variables and just enough to produce some layout so it doesn't look all wacky and keep a look at your content layout shifting
vince
vince6mo ago
btw i didnt know this but i've seen this done in a larger codebase and now it makes sense
ἔρως
ἔρως6mo ago
OH, something i forgot: use defer on your scripts defer renders the page and loads the javascript at the same time, but only runs it after everything is done rendering it makes a lot more sense than you can imagine i use it a lot on a project at work i've used it before on other personal projects
vince
vince6mo ago
so basically you only have a request for one svg file instead of 20?
ἔρως
ἔρως6mo ago
if you load data dynamically, and it needs to show icons, this is a good way yes, specially if you use 80-90% of the icons on the same page anyways that's huge savings
vince
vince6mo ago
do u have an article for this? just curious how it'd work in my setup since i manually copy and paste the svg code instead of importing it or some other way so wondering if it would work in for my usecase
ἔρως
ἔρως6mo ago
one thing: people hate this, but, EVERY SINGLE ASSET should have ?v=x.y.z or ?t=<timestamp> this is a cache busting technique, which is used to force the browser to download the new version of the file, and if you go back the browser will load the old version from cache as well this avoids situations where your browser refuses to load the new css/js/image file i think that iconify does this for you
vince
vince6mo ago
hm not trying to add another dependency, ill have to see how to do it manually
ἔρως
ἔρως6mo ago
i made my own utility that generates this by reading all blade templates and read a specific directive
vince
vince6mo ago
niceee
ἔρως
ἔρως6mo ago
it depends a lot of your setup i did add a lot of stuff, like, automatically removing js events and accessibility attributes (because they can cause weird stuff), ids, data attributes and cleaned up the whitespace and set the fill or stroke to currentColor but i recommend doing this manually, as you get better results
vince
vince6mo ago
yea we have a cms and twig templates with it, but a lot of our svgs are just in our public dir, so i think it would make the most sense to just do it manually ofc users can go in and add svgs, but im not too worried on that as thats a separate issue with a lot of complexity
ἔρως
ἔρως6mo ago
i mean, optimize the svgs manually you have no idea the garbage that svgs have people put groups inside of groups inside of groups and then use css styles to set things that can be an attribute
vince
vince6mo ago
yea our svg files are definitely like that lol
ἔρως
ἔρως6mo ago
and removing the repeated attributes to groups also saves A TON there's so much you can do to optimize svgs for example, if you have 5-6 paths that use the same exact attributes, you can just convert them to a single path by throwing everything at the end of one path (but make sure that the m command is converted to M - which converts from relative to absolute) this is all things that can save a lot of space and optimize the html a lot
vince
vince6mo ago
that's another thing on my bucket list to look into now 😂 ty!
ἔρως
ἔρως6mo ago
you're welcome by the way, something that people overlook: using a link or a button instead of using a div or a span with events attached some people use the most cursed shit as a button or link use the proper element for it's job saves a ton of time, js, html and css and at the end of the project, when you're about to publish, make sure you minify your js and css which should be part of the production build step, if you have one
vince
vince6mo ago
i see that all the time, idk if its just a legacy thing of older web stuff but i allllllways see a href="#" for buttons in these old wp sites i have to manage for clients and it drives me nuts lol
ἔρως
ἔρως6mo ago
because people think "i need something clickable", and what almost everybody thinks of is a link why? it looks the most like a link - which is something clickable by default
snxxwyy
snxxwyyOP6mo ago
oh wow a lot of stuff to look into here haha, thanks everyone you haven't got any resources on how to do this by any chance have you?
ἔρως
ἔρως6mo ago
not really
snxxwyy
snxxwyyOP6mo ago
no worries
Want results from more Discord servers?
Add your server