Basically I am trying to align a picture with text, and it is easy for the desktop version, when everything is on one line.
But when I go smaller resolutions there is a big gap between them.
Here is a codepen that I have made.
This is on one line and it’s perfect as I want it.
And this is on the responsive version.
I want the image to automatically be on the side of the “George” and close to it. Not in the middle of the tex.
This is an age old conundrum and I still don’t think this is possible unless you actually set the width for the text container at the point the text would break (using media queries ).
The flex box algorithm (and also grid) layout the boxes on the expected content but do not monitor when text wraps as such otherwise the container would reflow awkwardly at every twist and turn when resizing and that would have an enormous impact on all the rest of the page structure in terms of laying out the page.
What may help is of you just align the text to the right so that on small screen you would get text tight to the right side of the image instead.
Otherwise you are stuck with just using a media query at an arbitrary width and forcing the text to break.
I’m pleased you said that. I tried having a go in Codepen earlier today, thinking it would be an easy fix with flex. But I was staggered at how difficult it actually was and had to give up in the end.
The h1 boundary doesn’t shrink to fit when the text wraps. Of course adding flex-shrink causes it to wrap unnecessarily.
Sometimes it’s simple if you’re allowed to add something to the html.
In this case a span wrapping the last word together with the image.
Like: https://jsfiddle.net/Erik_J/eyu9caqn/
If you wanted to stop that happening then placing a span around the last word and the image could stop that by adding white-space:nowrap and display:inline-block;.
However that changes the html from the original which I was trying to avoid
The problem seems pretty clear to me. It’s the solution that is looking very hazy.
I think the Codepen is sufficient to see it, anything more (such as the rest of the page) would be a distraction from the problem elements.
Though I find adding element outlines helpful in visualising problems like this:-
Obviously if you treat the image as a word at the end of the line then it will wrap with the line just like normal text and you get the icon at the end of the second line but unless you add the extra span as in my example the image may drop to a third line as is normal behaviour.
e.g.
Of course there are many variables such as screen width and font-size and word length to take into account.
Yes Erik I also tried something like that also and although it mostly works it will do this at a certain point.
The image is going to drop first if certain conditions are met.
It may indeed be good enough but the answer I was hoping to find is the automatic resizing of the parent container when text wraps in the 2 column layout as that gives the best display.
I was hoping that some of the new css properties like min and max-content and their variations would suffice (or indeed CSS grid) but the specs make it clear that the container is laid out but then doesn’t keep checking on its width when text inside starts to wrap. Which of course for most layouts is what you want but this edge case has been eluding me for years
No I don’t think it’s actually needed when using the extra span around the last word and the image and using inline-block. I added it just in case in my demo