Using Jinja2 templating engine
<div class="card-footer d-flex justify-content-between">
<small class="me-2 d-flex gap-2 description-container">
<span id="description-text" class="description-text">
{% if photo.description %}
{{ photo.description }}
{% endif %}
</span>
<span id="dimension-text" class="dimension-text text-muted">
{% if photo.width and photo.height %}
(Dimensions: {{ photo.width }} x {{ photo.height }})
{% endif %}
</span>
</small>
<small class="float-end text-muted text-end">
{% if photo.category %}
{{ photo.category }}
{% endif %}
</small>
</div>
When the description-text is too long, it makes a new line in the footer and wraps the text to the new line since it can’t fit itself with dimension-text. Because of this, the dimension-text also wraps to the new line to use up the extra space which looks kind of ugly.
Is there a way to find out when there is no more space to fit both the description & dimension texts, and if so, get rid of the dimension-text span? Once there is enough space to fit both on the same line, let the dimension-text re-appear.
2
Answers
After some thinking, I realized the best way of doing this was to just check the width of the footer element and compare it to the total width of its child elements to see if the child elements required more space than what's available.
Here is my solution:
This solution uses offSetWidth. The
whitespace
variable is used to account for the space taken up by things like margins and other "whitespace" in the footer.I set the display of the
dimensionText
back toblock
and calculated its offset width again because when it's display isnone
, there is no width. Since I set it back tonone
if there isn't enough space in the same "frame", there is no flickering in the browser.Hope this helps!
In elements with plain text, each line of text as it is displayed in the browser gets its own
DOMRect
. Because your.description-text
box is just plain text, you can get the element’sDOMRect
s with.getClientRects()
and if its.length
is greater than 1 (the element has more than oneDOMRect
), then the text is wrapped. This is very similar to this answer from 2011 (the method still works twelve years later).You can then use this to the hide
.dimension-text
boxes in each footer by looping over all the footers and checking if their.description-text
is wrapped. If it is, then change the.dimension-text
‘sdisplay
CSS style tonone
, otherwise you can set it toinline
(the default for<span>
elements). You’ll have to do this process when the DOM first loads and every time the window resizes because text might become wrapped or unwrapped.