Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Wrap the words by comma not by space with in html div

Tags:

css

word-wrap

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
like image 766
roshan manjula Avatar asked Nov 08 '12 14:11

roshan manjula


People also ask

How do you wrap text without spaces in HTML?

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.

How do I wrap text in a div in HTML?

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.

How do I make text not wrap in div?

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).


2 Answers

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 ( &nbsp; ) between each pair of words you want to stick together. I prefer the first idea I offer though, it's cleaner.

Nomal&nbsp;A1, High&nbsp;A2(4), Low&nbsp;A3

It's a little uglier, but it's less code.

like image 121
daveyfaherty Avatar answered Oct 02 '22 13:10

daveyfaherty


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.

like image 25
Jukka K. Korpela Avatar answered Oct 02 '22 14:10

Jukka K. Korpela