I have a requirement for wrap text using comma(,). Is there any way to do this using CSS or other.
Nomal A1, High A2(4), Low A3
If the space is not enough this should be wrap like this, comma should be the only wrapping place.
Nomal A1, High A2(4),
Low A3
this should not be wrap like this
Nomal A1, High A2(4), Low
A3
You can wrap a long string, which does not have any whitespace character by using the CSS word-wrap property, or overflow-wrap, if you use CSS3. In this snippet, you'll find some examples for block elements, as well as for the inline ones.
If you've faced the situation when you need to wrap words in a <div>, you can use the white-space property with the "pre-wrap" value to preserve whitespace by the browser and wrap the text when necessary and on line breaks. Also, you'll need the word-wrap property.
If you want to prevent the text from wrapping, you can apply white-space: nowrap; Notice in HTML code example at the top of this article, there are actually two line breaks, one before the line of text and one after, which allow the text to be on its own line (in the code).
Wrap each pair in spans, and style them not to break, like so.
Html
<div class="paired-text">
<span>Nomal A1,</span> <span>High A2(4),</span>
<span>Low A3</span>
</div>
Css
.paired-text span{ white-space: nowrap; }
Alternatively, you could render a non-breaking space (
) between each pair of words you want to stick together. I prefer the first idea I offer though, it's cleaner.
Nomal A1, High A2(4), Low A3
It's a little uglier, but it's less code.
The safest way is to use the nobr
markup (which never made its way to any spec but keeps being the most reliable method for the purpose):
<nobr>Nomal A1,</nobr> <nobr>High A2(4),</nobr> <nobr>Low A3</nobr>
Using a little more verbose span
markup and CSS code white-space: nowrap
is almost as reliable, but naturally fails e.g. when CSS support is disabled in a browser.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With