By setting the display
of an item to flex
I am finding the last space is removed from a text string so.
<div class="has_flex"> Some text <a href="link">Link</a></div>
Becomes
<div class="has_flex"> Some text<a href="link">Link</a></div>
.has_flex {
display: flex;
}
<div class="no__flex">Some text <a href="link">Link</a></div>
<div class="has_flex">Some text <a href="link">Link</a></div>
I have wrapped the text in a span and that makes no difference.
Reason
When you don't use
display: flex
, the your layout becomes something likeThe text (including the space at the end) is wrapped inside an anonymous inline box:
However, Flexbox layout blockifies the flex items:
Then, the layout will be like
This might not seem directly related, but it's relevant because of the
white-space
processing model:So when the anonymous element and the link were both inline, the space was at the middle of a line. If you had multiple spaces they would collapse into a single one, but not disappear completely.
However, when you use flexbox, each flex item has its own lines, and the space is therefore at the end of a line. So it's removed.
Note this problem is not specific of flexbox, spaces at the end of an inline-block are also removed.
However, in that case you can just move the space outside the inline-block. In flexbox that's not possible, because anonymous flex items that contain only white space are not rendered.
Solution
If you want to preserve the space, you can set
white-space
to another value.white-space: pre-wrap
will allow text wrapping,white-space: pre
won't.The end of the
div
is missing. So, strange things can happen. This could be what you want: