Aligning Text Smartly in CSS
nocode.in
nocode.in
Initially, when figcaption text is short, it behaves something like this:
| <figcaption> |
| <figcaption > |
|<figcaption >|
The text inside the figcaption element is always aligned left but the element itself is centered. When the text is longer, figcaption grows to a 100% width while the text is still aligned left.Thanks for the comment. Wouldn't have figured out what the line does without it!
tl;dr
``` /* the simple trick */ display: flex; flex-direction: column; align-items: center; ```
I remember that eight or ten years ago, there were a lot of blog posts dedicated to sharing nice tricks and hacks to work around the limitations of CSS and browser implementations. Some techniques even got catchy names[1] and some of their authors became quite popular.
I dreamed of coming with a clever technique myself that would buy me some 15 minutes of internet fame after being published in A List Apart or something. Unfortunately my poor english skills at the time crushed my dreams.
Since CSS3 became the norm and browsers got better I stopped seeing such articles. I supposed we have CSS pretty much figured up. Anyway, all I wanted to say is that this post made me nostalgic and I actually learned something today.
[1]: https://en.wikipedia.org/wiki/Fahrner_Image_Replacement
http://stackoverflow.com/questions/20443220/how-to-absolutel...
Which leads me to ask how frequently and where he's changing his fonts. My design specs are usually pretty simple and consistent and would never require something like this.
At other times, we may want the user to define/select the font, and keep the baselines at the same position.
Plus, based on the description and requirements of the problem it's likely to not be solved because the font-size is an unknown variable in the equation. If it was known how the font-size is determined and applied then maybe something could be leveraged to adjust for the problem.
The easy solution, knowing the font size, is to position a parent element at the coordinate and then move a child element containing the text upward an amount based on the font-size. That way the first line of text in the child element sits on the top line of the parent element, more or less. But we don't know the font-size and it is easily broken if the site is constantly being changed code-wise. In fact, a badly thought out line-height may break it as well.
No there is no CSS property text-align:smart or smartly
I laughed out loud at thisOh, the Erlang days of glory past, I yearn you so much.
Getting elements to align nicely in CSS is a real pain point for a lot of developers. The popularity of layout frameworks is also evidence of that. It's not trivial to take a design from balsamiq or photoshop and replicate it in CSS. Tricks such as this can make it a lot easier to do with CSS rather than relying on javascript tweaks.
I've seen functions attached to window.resize which are principally there to resize a whole bunch of elements dynamically so they still fit together well on a new layout.
Unfortunately people outside the software development team don't think in markup, and they're the ones from whom the requirements often come.
Even more technical users often base their designs on the worlds of desktop software, or type-setting, or desktop publishing, or a myriad of other domains where "I want these two boxes to always be the same size and aligned to the bottom of this third box, and I'd like the font size to fit across the resultant size" is a perfectly reasonable request.
We can't keep going saying "Well the web doesn't work that way, you're going to have to rethink how you think about sizing" all the time, eventually the web has fix itself so that sizing and aligning fits better with how a lot of other domains work.
Flexbox is a step in the right direction but it's not a complete solution. (And we're still supporting IE9, so we can't even use that yet, thanks Vista!).
I read this blog post thinking I would see something amazing... but it was simply basic css. I may be biased because I actually learned CSS instead of randomly trying stuff until it work...