Skip to content(if available)orjump to list(if available)

Don't animate height

Don't animate height

168 comments

·July 19, 2025

verall

Is it possible to restrict this as a user? Like to force webpages to use under a certain amount of render/paint time/resources or else just break so that one dumb tab doesn't use up all my battery? Then I can opt-in to greater resources usage if it's a webpage I actually care about.

I've seen the "This webpage is using alot of resources" popup before but I don't think it would happen in this case.

Because honestly I think this is horrifying. I would rather it switch from grey to red "recording" dot than use even the 6% the author decided was "fixed". In 99% of cases I do not care at all about the "artistic vision" of the UI designer and in the other 1% of cases (say an in-browser game or some useful data-viz) I could choose to allow the tab to go crazy with my resources.

crazygringo

Seriously. It seems pretty reasonable to allow a web page a total amount of processing that is something like 100% CPU or GPU for 5 seconds. (E.g. can be 25% for 20 seconds.) And beyond that it gets throttled to 3% CPU cumulative max for however long it's been open so far.

And make that the default for popular browsers, so sites are forced to be efficient or else be super super janky and stuttering. And allow a permission for unrestricted processing that things like WebGPU demos or games can ask for.

HWR_14

100% of the CPU for 5 seconds seems unreasonably long for a random webpage.

crazygringo

For a news article, absolutely. But I think it needs to be able to handle JavaScript-heavy webapps too.

My suggestion wasn't really about promoting maximum best practices, but just avoiding total runaway excess.

mrob

Your browser should allow you to override all CSS on the web. Here's how I disable CSS animations in Firefox:

https://news.ycombinator.com/item?id=33223080

rayiner

They should put in something that allows users to electroshock web designers for wasting their battery.

thway15269037

I don't think combined energy output of every power station on Earth would be enough after we have Electron apps for so long. (edit: typo)

rayiner

They should call the feature “Electron.”

dleeftink

A bookmarklet that injects custom CSS and selects all/certain items and applies the `contain: content` and `content-visibility: auto;` rules could do to trick.

Additionally, the pseudo `:empty { display: none; }` selector may get you additional mileage.

orhmeh09

Yes, with StopTheMadness https://underpassapp.com/StopTheMadness/

It works on Safari, Chrome, and Firefox, but you must buy it.

barrkel

It only works on Apple though it looks like.

londons_explore

ChromeOS has logic that restricts background tabs to 1% CPU use I think.

socalgal2

How about just not using that site?

userbinator

> Don't animate

There, I fixed it. I have a custom CSS that kills all animations whenever I'm forced to use a "modern" browser, and it definitely tames the web.

deepsun

Just wrap it in a container with fixed height and "overflow: hidden".

Now the layout engine knows that it doesn't need to recalculate positions of elements outside that wrapper, and it's much faster.

By the way, the same trick was speeding up large <table> rendering back in the day. As long as you know the size of your rows or columns ahead of time, which kinda defeats the purpose of <table>.

legitster

As an old-school webguy, fixes like this article make me sad.

The average front-end person these days has so little respect for the DOM and then gets mystified about why the McMaster-Carr website is so good despite being build with ancient practices.

sema4hacker

I just went to the McMaster-Carr home page and right-clicked for "View Page Source". There's a LOT of code there.

spankalee

`contain: strict` is much better than `overflow: hidden` for this case, and implies `overflow: hidden`.

bfgeek

This likely more effective quite a few years ago, but not particularly important today.

Changing height typically only shifts elements, and browser engines typically wont relayout them due to position changes.

"overflow: clip" is also much more lightweight than "overflow: hidden"

codeptualize

This is the answer. I thought this was fairly common knowledge, height is animated quite often (think dropdowns), no need for the over complications.

liuliu

Thank you! This is what I am looking for in HN comments. Layout engine cannot be that stupid per the article. I guess it is not once you give it enough hint.

alexchantavy

Is there a good way to learn techniques like this from first principles other than trying to become an employed frontend dev?

RumourRider

You need to think about what the browser is actually doing when it displays a web page. You need to think about what happens when you type the URL in and what the browser is doing conceptually.

If you have any experience programming 2D graphics with SDL this will help you to understand what is happening as well.

There is an old school talk by Nikolas Zakas on JavaScript Performance from 2009 which while some of it is out of date now due to improvements in Browser engines, the fundamental ideas are still the same.

https://www.youtube.com/watch?v=mHtdZgou0qU

If you skip to the 35 minute mark he speaks about reflow specifically. Generally I still use many of these techniques when writing vanilla JS (which is unfortunately rare these days). There are other talks where he talks about many other performance specific topics.

Obviously none of this is as good as real world experience of dealing with a thorny issue.

brokencode

Usually you try to build something, realize it’s slow, then do a combination of searching for possible solutions, trying them, and profiling again and again to gain this knowledge.

Experience is the best teacher.

notatoad

not really. the web is 30+ years of fixes, shortcuts, compatibility hacks, tech industry politics, and other assorted mistakes.

the "first principles" to understand it all is the complete set of mailing list archives of the WhatWG, plus the archives of the bug trackers of all the major browsers.

socalgal2

I don't think it would be different for any other system. Trying to make a piece of computer software that is completely responsive to big displays, small displays, touch screens, mice, etc is just a ton of work and a ton of small details you can't easily learn until you need to learn them.

null

[deleted]

bob1029

I would also suggest looking into the following in this case:

https://developer.mozilla.org/en-US/docs/Web/CSS/contain

https://developer.mozilla.org/en-US/docs/Web/CSS/will-change

There are hints you can provide to the browser that may have an impact in scenarios where you are animating layout properties.

spankalee

CSS contain has made so many performance "truths" obsolete, but so few developers realize it. I've seen massive efforts to do things like port text editors to WebGL where rasterizing text is a huge pain where putting `contain: content` on the individual elements in the DOM version would have delivered most of the perf improvements.

Browser rendering engines are now sophisticated GPU-accelerated compositors. Absolute positioning with contain: strict removes basically all CSS layout from the perf equation, and you don't have to write your own compositor or line-layout! `* {contain: content}` and flex/grid gets you the good parts of HTML and very good performance.

bijection

I was just starting a comment on this but you beat me to it! I believe

  contain: strict;
on the parent element would have been sufficient here.

webstrand

I tested `contain: strict` on their color changing SVG and didn't see any difference. Profiler says it's still doing layout.

Quarrelsome

> Instead, we can create the illusion of a changing height by using two rectangles, applying translate to each.

Its a very clever solution and props to the engineer, but this being the fix makes me truly despair at where we are as an industry around web UI. That html and css won despite these sorts of counter-intuitive horrors.

UI layers that make me feel good reflect intent. I can take an image and write some code to darken that image (any image) and show that to the end user. It makes sense. However, in html+css I have to introduce a third element, another rectangle, slap it infront, paint it entirely black and set its opacity to something low. Sure, it works the same but it just feels so conceptually ugly.

lelandfe

It’s literally the sliding doors technique from, uh, two decades ago https://alistapart.com/article/slidingdoors/

andoando

I had a similar problem trying to animate a book flipping pages. At the half way point I had to update the text to show the backside and update the back pages.

However no matter what I did, I could not get the update to sync correctly with the page being exactly half way. There's probably a solution but I just gave up

HWR_14

What amazes me is that so much a M2 Mac's resources would be used to render a website even if it rendered everything from scratch. There is almost no graphic content compared to a video game produced decades ago, and they would easily render a frame in 1/2 the time on decades old hardware

socalgal2

Videos game rarely do any complex dynamic layout. That is where all the time is going

hombre_fatal

Well, raw graphical content is the easy part.

A live layout tree that has to be repainted and composited yet intersect with arbitrary layers like an accessibility tree and what would naively be N:N style calculations is completely different architecture.

Consider examples where people toy with the concept of replacing the DOM with a canvas but basic things like text selection don't even work anymore.

null

[deleted]

zuhsetaqi

Keep in mind that the percentage in Activity Monitor is 100 % per core, so a 10 core CPU can go up to 1000 % usage. So 60 % means 60 % of a single core not of the whole CPU.

pohuing

And it's sensitive to dynamic scaling as well, isn't it? So it's possibly not 60% of a core at max clock, but maybe of a core clocked to 800mhz or something.

I fell for that shallow analysis myself once when I noticed just how apparently bad the gpu usage was for a big of animation in a web page, until I noticed my 30% load gpu still drew as much wattage as it did on idle.

gjs278

[dead]

Levitating

Does a note taking app need such an animation if apparently it's that easy to accidentally drain the users battery?

The UI of this app literally only has a piece of text, a button and this animated indicator. To render this it needs an entire chromium browser loaded into memory, and still it has performance issues.

I will always prefer software written in native graphics toolkits. I understand the benefits of using electron but the only electron based software I've ever liked was vscode.

jtangelder

Wondering what the performance of a simple animated gif would be, instead of the composite layer transformations.

dylan604

If the bars are not related to actual data and are purely just pre-canned animation, the gif would just come with additional file size for a one time cached type of download. If the bars do need to be animated to real-time data coming in, then the gif would not be the right fit for that need.

threetonesun

Can't imagine anyone is deriving much information value from a visualizer with 3 bars. Apple Music has a similar animation and it will start bouncing when you click play, while the music is clearly downloading and there's no sound.

will-bradshaw

This was true pre iOS 18. The bars respond to the music now (at least on iOS)!

dylan604

Just because it isn't useful to the user doesn't mean some dev hasn't spent the cycles doing it. I know I've personally had to write code that was an absolute waste, but some PM wanted and it was coded. Usually so it can be a bullet point in a demo that impresses low skilled PMs or sales drone, but techy user types just roll their eyes when it's pointed out to them.

xp84

I've almost never seen these tiny icons to have any actual useful information being conveyed (meaning they are tracking amplitude or pitch). They're usually purely decorative, or boolean in function, meaning you can see they're flat if you are completely silent or they're bouncing around if not silent.

You can have 2 GIFs for "silent" and "sound happening" if one cares to truly indicate if it's picking up sound / playing sound, and switch between them. Will be 1000x simpler and 99% fewer lines of code to maintain forever.

DamonHD

And won't annoy people who get over-stimulated.

I don't normally have that issue, but one site had (has?) a bright green thing that jumped periodically that was so vastly distracting that I could not read the text on the page without covering the 'thing' somehow, eg with my hand! Absolutely pointless. I had to give up using the site entirely, even though it was otherwise useful.

norskeld

Personally, I'd be annoyed by both the resource-consuming animations and the blurry GIFs/canvas. Infisical does use the latter (canvas) for icons in their UI, and I somewhat hate it. I'd rather look at crisp, but static icons.

jasonjmcghee

Canvas should never be blurry. If it is, something is doing a bilinear upscale. I'd guess someone forgot to take the scale factor of your display into account.

Or there are images being used in the canvas, which would defeat the purpose for the use case you described.

evan_

Or if you need to actually control it, an SVG or <canvas> based animation

nightpool

GIFs historically were pretty unoptimized from a display perspective (as anyone with a moderately-active Tumblr dashboard could tell you), so I wouldn't be surprised if it was worse.

xp84

I disagree. The height of a DOM element is much more expensive to recompute as this article shows than a single element which gets frames swapped out with static images. The latter can be guaranteed to never impact any other part of the document. The end result of either is that the pixels in the animated area need to be recomputed, and end result which will be the same for both, but a GIF will have no further impact on anything else whereas resizing DOM elements has side effects that have to be constantly updated and checked for which scale with the number of sprites you're animating (unlike the GIF bitmap which is gonna be 'flat rate').

Likewise, for a tiny animation, having the frames of the GIF in memory and blitting them onto the screen in sequence sounds like orders of magnitude less CPU/GPU than performing all the ridiculous math-based transforms over and over again.

The Tumblr example is not instructive here because those were frequently many many frames, very colorful (often photos), and/or large in dimensions, taking up a ton of memory which was probably the main problem causing our browsers to choke. This would be none of those things.

Eric_WVGG

My head is sort of reeling from this. If height animations are that expensive today, imagine how expensive (and still commonplace) they were twenty years ago.

I fortunately quit animating height quite a long time ago in favor of similar transform techniques, but… wow, still crazy to learn the magnitude of this common operation.

moritzwarhier

It's really simple. Don't animate layout, especially if your DOM is huge.

If your element floats absolutely positioned outside the layout flow, animating height would not be a problem.

And if the browser facilities for animating height/max-height, especially to/from auto, get better, it surely will become more common.

Right now, I wouldn't call it common. It's a common requirement, but only until you start to consider layout.

Fwiw, animating margin and padding is possible and has the capability to cause the same amount of jank as animating height.

Eric_WVGG

You can animate an absolute positioned div without it obviously altering what a normal person would call "the layout." That's not simple unless one is willing to dig very deeply into the how web browser rendering works. And really still not simple. Otherwise this article wouldn't have been written.

hbn

The heart of the issue isn't that animating height is expensive, it's the downstream effect of that animation repeatedly changing the page's layout and forcing redraws.

cvoss

But it wasn't expensive twenty years ago. Twenty years ago people actually knew how to efficiently use their limited computational resources.

Eric_WVGG

so it wasn't expensive because people didn't do it? They did.

rkwz

CSS animations can sometimes cause weird performance issues or even crashes. 10 years back, animating box-shadow caused browsers to crash for our some of users by consuming a lot of memory: https://www.sheshbabu.com/posts/my-binary-search-debugging-s...

rectang

I read this article because just last week I set up CSS height transitions to show/hide divs in a form which displays conditional content (based on radio button selection). "Why shouldn't I do this?", I thought.

It looks like the point of this article is that you should avoid continuous animation for CPU performance reasons. These performance reasons are probably inconsequential for occasional transitions.

For many of us in the target demographic of "people who animate height", the scary title of this article is misleading.

DamonHD

I created a little CSS-pulsing 'live' button and give it a fixed number of cycles of pulsing to cap worst-case resource consumption. But it took me a while to work out that that was the right thing to do!

Ken_At_EM

The final CPU/GPU usage is still totally unacceptable.