In the case where I have a container with display: block
and I need to dynamically populate it with elements styled as display: inline
, such as <span>
tags. Each of these elements should act as a "word", so when they reach the right edge of the parent element, they should wrap by inserting a line break before overflowing.
While this can be achieved manually in HTML by adding spaces after each element, doing this programmatically can become messy as it involves manipulating the text content of the parent rather than just working with nodes (considering that nodes may need to be inserted or removed programmatically in any order).
I'm hoping there might be a CSS property for the parent element that specifies to treat the end of any child element as whitespace for better element flow. Any clean solution is welcomed.
View some examples of the desired behavior here (screenshot). The code snippet includes two div
tags. The first one demonstrates the desired behavior using whitespaces between spans in the HTML, while the second does not achieve the desired effect due to the lack of whitespaces resulting in no line breaks between spans.
div { width: 200px; background-color: pink; margin-bottom: 10px; }
span { width: 80px; background-color: cyan; }
<div>
<span>text</span>
<span>text</span>
<span>text</span>
<span>text</span>
<span>text</span>
<span>text</span>
</div>
<div><span>text</span><span>text</span><span>text</span><span>text</span><span>text</span><span>text</span></div>