How can I force a long string without any blank to be wrapped?
Asked Answered
B

16

239

I have a long string (a DNA sequence). It does not contain any whitespace character.

For example:

ACTGATCGAGCTGAAGCGCAGTGCGATGCTTCGATGATGCTGACGATGCTACGATGCGAGCATCTACGATCAGTCGATGTAGCTAGTAGCATGTAGTGA

What would be the CSS selector to force this text to be wrapped in a html:textarea or in a xul:textbox?

Buckinghamshire answered 31/1, 2009 at 16:44 Comment(1)
Ironically the string doesn't break in Stack Overflow either...Quarterstaff
H
326

for block elements:

<textarea style="width:100px; word-wrap:break-word;">
  ACTGATCGAGCTGAAGCGCAGTGCGATGCTTCGATGATGCTGACGATGCTACGATGCGAGCATCTACGATCAGTC
</textarea>

for inline elements:

<span style="width:100px; word-wrap:break-word; display:inline-block;"> 
   ACTGATCGAGCTGAAGCGCAGTGCGATGCTTCGATGATGCTGACGATGCTACGATGCGAGCATCTACGATCAGTC
</span>
Harlot answered 31/1, 2009 at 16:53 Comment(12)
It's supported only in IE, Safari, and FF3.1 (alpha).Friesland
just tested your solution with the last FF . Worked fine.Buckinghamshire
this works only in new wave of browsers - see caniuse.com/#search=word-breakAbney
@Michael: The answer uses the "word-wrap" rule, not the "word-break" one; the former is supported as used in almost every browser used today. Where "partial support" is indicated, it appears that the "break-word" value for the "word-wrap" rule is still viable.Mime
I don't think this solution works any longer. At least not in current Chrome.Konstanze
The property got renamed to overflow-wrap since in the standards, but wrod-wrap is widely implemented.Cortneycorty
It doesn't work. It breaks words in the middle of the word.Ferrocene
Also works with "display: table-cell", was necesary in my caseAlexandrina
In case a predefined width is not desired, using <span style="overflow-x:hidden; word-wrap:break-word;"> works too. (Firefox 58)Billybillycock
I needed word-break: break-all for this as mentioned in emiks answerClarkin
I needed overflow-wrap: 'anywhere'Robedechambre
now... the most relevant comments all refer to overflow-wrap... i upvoted each comment to try an draw more attention to them... overflow-wrap:anywhere did it for meHorton
B
113

Place zero-width spaces at the points where you want to allow breaks. The zero-width space is &#8203; in HTML. For example:

ACTGATCG&#8203;AGCTGAAG&#8203;CGCAGTGC&#8203;GATGCTTC&#8203;GATGATGC

Breastbeating answered 18/4, 2009 at 19:33 Comment(7)
Thanks for this solution. Was having a hard time getting something like this to work inside a table, and this solution is the only one that I found works in IE, Firefox and Chrome.Malayan
+1, this works better as it covers more cases, even though question was for a more particular case.Envious
You could alternatively use the <wbr> tag, which serves the same purpose of providing an optional line-break opportunity.Mallarme
Watch if you do this in things that might be copied and pasted.Halibut
Regex \s (whitespace), doesn't match a zero-width space. If you want to remove them, you need to explicitly write /\u200B/g or similar. See developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…Savannahsavant
Note: Some of us prefer to spell it in hex, as &#x200B;, so that we can find the Unicode reference (code U+200B) more easily.Guenon
This is very useful for wrapping inside HTML emails (if you are able to process text before rendering templates) without the headache of worrying about rendering logic in different email clientsThwack
C
53

Here are some very useful answers:

How to prevent long words from breaking my div?

to save you time, this can be solved with css:

white-space: -moz-pre-wrap; /* Mozilla */
white-space: -hp-pre-wrap; /* HP printers */
white-space: -o-pre-wrap; /* Opera 7 */
white-space: -pre-wrap; /* Opera 4-6 */
white-space: pre-wrap; /* CSS 2.1 */
white-space: pre-line; /* CSS 3 (and 2.1 as well, actually) */
word-wrap: break-word; /* IE */
word-break: break-all;
Cistern answered 28/8, 2013 at 9:30 Comment(5)
+1 this because it mentions word-break:break-all; which worked for me in IE9Babby
word-break: break-all; was the only one that worked in Android WebView for me.Jaggy
Thank you for word-break: break-all;!Letsou
TIL there's a -hp- prefix!Gernhard
It appears the break-word value for word-wrap is deprecated. Instead you can use overflow-wrap: anywhere or overflow-wrap: break-word.Fredkin
P
20

For me this works,

<td width="170px" style="word-wrap:break-word;">
  <div style="width:140px;overflow:auto">
    LONGTEXTGOESHERELONGDIVGOESLONGTEXTGOESLONGTEXTGOESLONGTEXTGOESLONGTEXTGOESLONGTEXTGOESLONGTEXTGOESLONGTEXTGOESLONGTEXTGOESLONGTEXTGOESLONGTEXTGOESLONGTEXTGOESLONGTEXTGOESLONGTEXTGOESLONGTEXTGOESLONGTEXTGOESLONGTEXTGOESLONGTEXTGOESLONGTEXTGOESLONGTEXTGOESLONGTEXTGOESLONGTEXTGOESLONGTEXTGOESLONGTEXTGOESLONGTEXTGOESLONGTEXTGOESLONGTEXTGOESHERELONGDIVLONGTEXTLONGTEXT
  </div>
</td>

You can also use a div inside another div instead of td. I used overflow:auto, as it shows all the text both in my Opera and IE browsers.

Pimpernel answered 13/7, 2010 at 4:52 Comment(1)
This did not work for me. I have to move "word-wrap" property into div and remove "overflow" property. With this changes, works.Mitchmitchael
S
12

I don't think you can do this with CSS. Instead, at regular 'word lengths' along the string, insert an HTML soft-hyphen:

ACTGATCG&shy;AGCTGAAG&shy;CGCAGTGC&shy;GATGCTTC&shy;GATGATGC&shy;TGACGATG

This will display a hyphen at the end of the line, where it wraps, which may or may not be what you want.

Note Safari seems to wrap the long string in a <textarea> anyway, unlike Firefox.

Staub answered 31/1, 2009 at 17:8 Comment(2)
Wow, didn't even know about that. Neat!Pollaiuolo
I did not know about that either. Double Neat!Nadenenader
S
12

Use a CSS method to force wrap a string that has no white-spaces. Three methods:

1) Use the CSS white-space property. To cover browser inconsistencies, you have to declare it several ways. So just put your looooong string into some block level element (e.g., div, pre, p) and give that element the following css:

some_block_level_tag {
    white-space: pre;           /* CSS 2.0 */
    white-space: pre-wrap;      /* CSS 2.1 */
    white-space: pre-line;      /* CSS 3.0 */
    white-space: -pre-wrap;     /* Opera 4-6 */
    white-space: -o-pre-wrap;   /* Opera 7 */
    white-space: -moz-pre-wrap; /* Mozilla */
    white-space: -hp-pre-wrap;  /* HP Printers */
    word-wrap: break-word;      /* IE 5+ */
}

2) use the force-wrap mixin from Compass.

3) I was just looking into this as well and I think might also work (but I need to test browser support more completely):

.break-me {
    word-wrap: break-word;
    overflow-wrap: break-word;
}

Reference: wrapping content

Singles answered 14/2, 2013 at 1:13 Comment(2)
Yes, the #3 there works in all modern browsers and even older IE6+.Singles
#3 only works if there are opportunities to break by words. An overly long string does not break ( tested on Chrome 52.0.2743.82 ).Awed
S
9

My way to go (when there is no appropiate way to insert special chars) via CSS:

-ms-word-break: break-all;
word-break: break-all;
word-break: break-word;
-webkit-hyphens: auto;
-moz-hyphens: auto;
-ms-hyphens: auto;
hyphens: auto;

As found here: http://kenneth.io/blog/2012/03/04/word-wrapping-hypernation-using-css/ with some additional research to be found there.

Strobotron answered 16/9, 2014 at 13:35 Comment(0)
C
5

For word-wrap:break-word; to work for me, I had to make sure the display was set to block, and that the width was set on the element. In Safari, it had to have a p tag and the width had to be set in ex.

Cumulative answered 7/12, 2010 at 20:9 Comment(0)
B
4

Use <wbr>tag:

ACTGATCG<wbr>AGCTGAAG<wbr>CGCAGTGC<wbr>GATGCTTC<wbr>GATGATGC

I think this is better than using zero-width space &#8203; which could cause problems when you copy the text.

Badge answered 5/9, 2017 at 7:14 Comment(0)
D
3

If you're using PHP then the wordwrap function works well for this: http://php.net/manual/en/function.wordwrap.php

The CSS solution word-wrap: break-word; does not seem to be consistent across all browsers.

Other server-side languages have similar functions - or can be hand built.

Here's how the the PHP wordwrap function works:

$string = "ACTGATCGAGCTGAAGCGCAGTGCGATGCTTCGATGATGCTGACGATGCTACGATGCGAGCATCTACGATCAGTCGATGTAGCTAGTAGCATGTAGTGA";

$wrappedstring = wordwrap($string,50,"&lt;br&gt;",true);

This wraps the string at 50 characters with a <br> tag. The 'true' parameter forces the string to be cut.

Davit answered 4/1, 2011 at 11:25 Comment(1)
You can mix this solution with Remy's solution to insert zero-width spaces: wordwrap ($longtext, 5, "&#8203;", true);Sletten
S
3
<textarea style="width:100px; word-wrap:break-word;">
  place your text here
</textarea>
Sonar answered 12/8, 2015 at 6:50 Comment(0)
M
3

In a case where the table isnt of fixed size, below line worked for me:

style="width:110px; word-break: break-all;"
Mccarver answered 31/10, 2016 at 17:10 Comment(0)
P
3

In case if you use Bootstrap, better case for you is use this class "text-break".

Example:

<p class="text-break">mmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmm</p>

More informationg you should get in official Bootstrap documentation page

Photoelectrotype answered 2/3, 2021 at 18:46 Comment(0)
E
3

Simply:

word-break: break-word;
Earthly answered 7/12, 2021 at 19:1 Comment(0)
A
2

Here is the code I come into using white-space: pre-wrap;

.code {
        width: 40vw;
        white-space: pre-wrap; 
        word-break: break-word;
        overflow-wrap: break-word;   
        border    : 1px solid darkgray;
    }
<p class="code">ACTGATCGAGCTGAAGCGCAGTGCGATGCTTCGATGATGCTGACGATGCTACGATGCGAGCATCTACGATCAGTCGATGTAGCTAGTAGCATGTAGTGA</p>

I know the width value is looks odd, you should change it to value fits your needs .

Andriette answered 13/10, 2021 at 16:14 Comment(0)
P
1

just setting width and adding float worked for me :-)

width:100%;
float:left;
Perturb answered 7/2, 2014 at 6:18 Comment(3)
It is a complete and easy answer i think that guy who posted this problem originally should use width:100%; with float:left; on element which contains that string and his problem will be resolved. then why is this answer not relevant?Perturb
because that guy who posted this problem don't think your solution worked five years ago.Buckinghamshire
Yes, but this forum is not only about that guy only its about this forum and other people facing similar problem like me today can also have benefit from the same.Perturb

© 2022 - 2024 — McMap. All rights reserved.