That one comes close. But the image hangs on the end of the second line, not vertically centred as the OP intended.
The media query method seems to work as intended, but the query width is a magic number. Changing the width units from px to em will make it more robust against text zooming, but it is still bound to the text length, so still a magic number.
If there were some property that could alter this default…
I haven’t had this problem before, so my eyes are fresh.
Hopefully I now avoid the image to break line until it is finally tuched by the shrinking container.
I decided to rely on the older algorithms for pure inline boxes. Inline boxes still acceept horizontal padding and margin. I moved the image by a neg margin on the H1 well into the H1 to not trigger a line-breake ubtil the image is tuched by the narrowing width. I then move the image back to its original position at the end of the line.
The 30px image including eventual woed-space is well covered by the 40px negative margin. Then to restore the missing H1 content width I applied a 40px right padding on the container.
I’ve tested the code on Firefox, Opera, Vivaldi, Brave, Chromium and finally the PaleMoon. Not all browsers are latest versions, they just happen to be on this machine.
.wrapper {
padding-right: 40px; /* fill the neg margin */
text-align: center;
}
h1 {
display: inline;
margin-right: -40px; /* pull in the full image */
vertical-align: middle;
}
img {
position: relative;
left: 40px; /* move the image back */
width: 30px;
vertical-align: middle;
}
/* the 40px length is safely more than the image width including word space */
I was hoping for a solution to the problem I mentioned in the two column format as I didn’t really see a problem with just letting the icon hang on the end of the last word :). (Apart from a little trickery of course to stop it breaking on its own ).
However looking at tree OPs original picture it may be what was wanted after all. I was assuming the icon should be vertically aligned to both lines of text at the furthest edge of the longest text as per my media query demo.