.box {
width: 230px;
border: 1px solid black;
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
}
svg {
/* The size of the icon can vary */
width: 24px;
height: 24px;
}
<div class="box">
<span class="text truncate">
This is text
<svg width="48" height="48" viewBox="0 0 48 48">
<path id="icon-info" d="M22 34h4V22h-4v12zm2-30C12.95 4 4 12.95 4 24s8.95 20 20 20 20-8.95 20-20S35.05 4 24 4zm0 36c-8.82 0-16-7.18-16-16S15.18 8 24 8s16 7.18 16 16-7.18 16-16 16zm-2-22h4v-4h-4v4z">
</path>
</svg>
and it should truncate
</span>
</div>
In this example we have some Text with an SVG in it. I gave the SVG a height & width of 24px but that size may change.
Is there a way to consistently center the SVG regardless of the size of the SVG without breaking the truncation? (Adding display: flex breaks the ellipsis truncation).
I have also tried the strategy listed here in this blog post but it requires the SVG to have height/width of 1em, if I increase that size I’m not sure how to dynamically calculate the top value.
2
Answers
Change the
viewbox
to the real values used in thepath
(4 4 40 40
not0 0 48 48
),Add css
vertical-align
.Do not duplicate css
width
andheight
attributes.
Yes —
vertical-align: middle
.