CSS Beginner Struggles: aspect-ratio and height
Reviewing CSS from web.dev's "Learn CSS!" course provided a refresher on a lot of material and also introduced me to new material I hadn't seen before. I had hoped for a bit of "aha" insight to help me with CSS struggles in my project, but that didn't happen. The closest was a particular piece of information (Flexbox for laying out along one dimension, Grid for two dimensions) that told me I'm on the right track using Flexbox.
A recurring theme with my CSS frustration is the fact height and width are not treated the same way in HTML layout. I like to think of them as peers, two equal and orthogonal dimensions, but that's not how things work here. It traces back to HTML fundamentals of laying out text for reading text in a left-to-right, top-to bottom languages like English. Like a typesetter, the layout is specified in terms of width. Column width, margin width, etc. Those were the parameters that fed into layout. Height of a paragraph is then determined by the length of text that could fit within specified width. Thus, height is an output result, not an input parameter, of the layout process.
For my Compass web app, I had a few text elements I knew I wanted to lay out. Header, footer, sensor values, etc. After they have all been allocated screen real estate, I wanted my compass needle to be the largest square that could fit within the remaining space. That last part is the problem: while we have ways to denote "all remaining space" for width, there's no such equivalent for height because height is a function of width and content. This results in unresolvable circular logic when my content (square compass) is a function of height, but the height is a function of my content.
I could get most of the way to my goal with liberal application of "height: 100%
" in my CSS rules. It does not appear to inherit/cascade, so I have to specify "height: 100%
" on every element down the DOM hierarchy. If I don't, height of that element collapses to zero because my compass doesn't have an inherent height of its own.
Once I get to my compass, I could declare it to be a square with aspect-ratio
. But when I did so, I find that aspect-ratio does its magic by changing element height to satisfy specified aspect ratio. When my remaining space is wider than it is tall, aspect-ratio expands height so it matches width. This is consistent with how the rest of HTML layout treats width vs. height, and it accomplishes the specified aspect ratio. But now it is too tall to fit within remaining space!
Trying to reign that in, I played with "height: 100%
", "max-height: 100%
", and varying combinations of similar CSS rules. They could affect CSS-specified height values, but seems to have no effect on height change from aspect-ratio. Setting aspect-ratio means height is changed to fit available width and I found no way to declare the reverse in CSS: change width to fit within available height.
From web.dev I saw Codepen.io offered ability to have code snippets in a webpage, so here's a test to see how it works on my own blog. I pulled the HTML, CSS, and minimal JavaScript representing a Three.js <canvas> into a pen so I could fiddle with this specific problem independent of the rest of the app. I think I've embedded it below but here's a link if the embed doesn't work.
After preserving a snapshot of my headache in Codepen, I returned to Compass app which still had a problem that needed solving. Unable to express my intent via CSS, I turned to code. I abandoned using aspect-ratio and resized my Three.js canvas to a square whose size is calculated via:
Math.floor(Math.min(clientWidth, clientHeight));
Taking width or height, whichever is smaller, and then rounding down. I have to round down to the nearest whole number otherwise scroll bars pop up, and I don't want scroll bars. I hate solving a layout problem with code, but it'll have to do for now. Hopefully sometime in the future I will have a better grasp of CSS and can write the proper stylesheet to accomplish my goal. In the meantime, I look for other ways to make layout more predictable such as making my app full screen.
The source code for my project is publicly available on GitHub, though it no longer uses aspect-ratio
as per the workaround described at the end of this poist.