Given a relatively simple CSS:
div {
width: 150px;
}
<div>
12333-2333-233-23339392-332332323
</div>
How do I make it so that the string stays constrained to the width
of 150, and wraps to a new line on the hyphen?
Replace your hyphens with this:
It's called a "soft" hyphen.
div {
width: 150px;
}
<div>
12333233323323339392332332323
</div>
In all modern browsers* (and in some older browsers, too), the <wbr> element is the perfect tool for providing the opportunity to break long words at specific points.
To quote from that link:
The Word Break Opportunity (<wbr>) HTML element represents a position within text where the browser may optionally break a line, though its line-breaking rules would not otherwise create a break at that location.
Here's how it could be used to in the OP's example (or see it in action at JSFiddle):
<div style="width: 150px;">
12333-<wbr>2333-<wbr>233-<wbr>23339392-<wbr>332332323
</div>
*I've tested it in IE9, IE10, and the latest versions of Chrome, Firefox, and Opera, and Safari.
div {
width: 150px;
}
<div>
12333-<wbr>2333-<wbr>233-<wbr>23339392-<wbr>332332323
</div>
As part of CSS3, it is not yet fully supported, but you can find information on word-wrapping here. Another option is the wbr tag, , and none of which are fully supported either.
Your example works as expected in Google Chrome, Safari (Windows), and IE8. The text breaks out of the 150px box in Firefox 3 and Opera 9.5.
Additionally won't work for your example, as it will either:
work when word-breaking but when not word-breaking not display any hyphens, or
work when not word-breaking but display two hyphens when word-breaking
since it adds a hyphen on a break.
In this specific instance (where your string is going to contain hyphens) I'd transform the text to this server-side:
<div style="width:150px;">
<span>12333-</span><span>2333-</span><span>233-</span><span>23339392-</span><span>332332323</span>
</div>
Depending on what you want to see exactly, you can use a combination of hyphen, soft hyphen, and/or zero width space.
On a soft hyphen, your browser can word-break (adding an hyphen).
On a zero width space, your browser can word break (without adding anything).
Thus, if your code is something like :
111111222222-333333444444-555555
then your browser will show this with no word-break :
1111112222222-33333334444444-5555555
and this will every possible word-break :
111111-
222222-
-333333
444444-
555555
Just pick up the option you need. In your case, it may be the one between 4s and 5s.
You can also use :
word-break:break-all;
ex.
<div style='width:10px'>ababababababbabaabababababababbabababa</div>
output:
abababababa
ababababbba
abbabbababa
ababb
word-break is break all the word or line even if no-space in sentence that not feets in provided width or height. nut for that you must be provide a width or height.
The non-breaking hyphen works well.
HTML Entity (decimal)
‑
Instead of - you can use ‐ or \u2010.
Also, make sure the hyphens css property was not set to none (The default value is manual).
<wbr> is not supported by Internet Explorer.
Hope this may help
use <br>(break) tag where you want to break the line.
You can use 0 width space after hyphen character:
div {
width: 150px;
}
<div>
12333-2333-233-23339392-332332323
</div>
if You want line break before hyphen use - instead.
Related
I have a word that I have put a inside. It hyphens alright until I get to a small enough screen size then it adds an extra hyphen at the end.
Edit: Can't reproduce it in jsfiddle, because it seems to interpret the html differently. I can show a picture of the problem however
<h3>
Fluffyluffy Somesomething</h3>
Becomes
With an extra hyphen at the end
I saw this and didn't really like the answer, Extra hyphen at end of word if is used in middle of the word and the parent div is narrow. "just make the div wider".
Are there any ways to make it not add extra hyphens I didn't ask for instead?
Writing
<h3 class="something">Something SomethingSomething</h3>
<style>
.something{
width:85px
}
<\style>
On http://htmledit.squarefree.com/
Causes a similar issue but not quite. I can only seem to replicate with Chrome
Can you use flex?
Codepen
HTML
It seems to work in chrome for me if I add display: inline-flex;
<h3 class="something">
Fluffyluffy Somesomething
</h3>
CSS
.something{
width:60px;
display: inline-flex;
}
It seems that IE starting from IE7 to IE10 wraps text in the textarea control incorrectly when using \n (or \r\n - doesn't matter - results are the same). Is this a bug in IE or they treat the html standard differently than other browsers - who is right? I have defined:
<textarea id="TextArea1" runat="server" style="width: 190px; height: 390px; white-space: normal; word-wrap: normal; overflow: scroll" ></textarea>
When I try to add long string like "VeryLongStringEndingWithNewLine\n" by using JavaScript code (obj.value += text;) the text is shown in one line with scroll (this is ok) but added with an additional empty line (\r\n) - why?
When I try to add short string like "Short\n" multiple times, again via JavaScript code the text is on the same line (should be on the separate lines because normal wrapping should be applied).
Moreover when I do postback then all \r\n's are replaced with spaces (why?) and then text parsed correctly (assuming if I used spaces instead of crlf normal wraping with space only wraps when does not fit in the area).
When using FF or Chrome same control behaves correctly - long lines are shown without an additional empty next line, short lines are on the different lines, no replacement with spaces when doing postback.
I know I could probably use other options or white space characters, but I feel that above is not correct about IE. Any comments?
Under normal circumstances, textareas treat whitespace literally and wrap as needed.
But because you're overriding it with CSS, the result is potentially unstable.
In this case, whitespace (\n) is collapsing to a single space, since that's what white-space:normal does.
I'm not entirely sure what you're trying to achieve here, since I'm fairly sure the default behaviour for textareas is perfectly fine. If you don't want that behaviour, you shouldn't be using a textarea.
Imagine I have a long, multi-word line of text in a DIV:
Hello there, dear customer. Please have a look at our offer.
The DIV has a dynamic width. I want to word wrap the above text. Currently, the wrapping happens on a word boundary which maximizes the length of the first line:
|-DIV WIDTH------------------------------------|
Hello there, dear customer. Please have a look
at our offer.
I would prefer that the wrapping happen on the sentence boundary. However, if no wrapping is necessary, I would like the line to remain as one.
To illustrate my point, please look at the various DIV widths and how I would like my text to wrap:
|-DIV WIDTH--------------------------------------------------------|
Hello there, dear customer. Please have a look at our offer.
|-DIV WIDTH-----------------------------------|
Hello there, dear customer.
Please have a look at our offer.
|-DIV WIDTH--------|
Hello there, dear
customer.
Please have a look
at our offer.
With words, you can use a soft hyphen so that the word wrapping happens on suggested syllable boundaries. If no wrapping is needed, the remains invisible. If wrapping is needed, the is where it happens:
magnificently
Is there an analogous method for hinting word-wrapping in HTML?
Use in words or <wbr> between words, as <wbr> won't introduce a hyphen.
See also:
quirksmode: <wbr> compat list
Not quite exactly, but close: http://jsfiddle.net/uW4h8/1/.
In brief, you should set white-space: nowrap; for you text container and use <wbr> to insert breaks between words as desired.
The elements <wbr> and often work, but not always. They are especially problematic when designing a static landing page that (a) has to work on a variety of screens and browsers and (b) has to look good.
In this case I want control over line-break hints at a variety of different screen resolutions. To do so, I use <br> tags and css. It can become a mess if it gets complex, but I found it works up to a point. For example:
<p class='break-hints'>
Hello there, dear customer. <br class='break-big'>
Please have a look <br class='break-small'> at our offer.
</p>
I then use CSS with media queries to indicate when the various breaks should be triggered.
p.break-hints br {
display: none;
}
#media only screen and (max-width: 300px) {
p.break-hints br.break-small {
display: inline;
}
}
p.break-hints br.break-big {
display: inline;
}
Use a no-break space U+00A0 (or if you have no convenient way of entering the character as such) between words when a line break is not to be allowed, and normal space otherwise.
This won’t work when words contains hyphens “-”, and some other characters, such as parentheses, may cause problems as well, because some browsers treat them as allowing a line break after them. See http://www.cs.tut.fi/~jkorpela/html/nobr.html for a lengthy treatise, including various techniques of coping with the problems. But if you have normal words with normal punctuation only and no hyphens, you should be fine with the simple approach.
I just had the same issue. I had text like the following:
<div>Assistant Something / Anything Pabulum Nautical</div>
Where breaking it after the / character would really help readability.
In my case, I solved it by wrapping the desired "lower line-break priority" chunks with inline-block (now inline flow-root apparently) elements:
<div><span class="inline">Assistant Something</span> / <span class="inline">Anything Pabulum Nautical</span></div>
See snippet for results.
.d {
margin: 1em;
}
#b span {
display: inline-block;
}
<div class="d" id="a">Assistant Something / Anything Pabulum Nautical</div>
<div class="d" id="b"><span>Assistant Something</span> / <span>Anything Pabulum Nautical</span></div>
Firefox and seems to render a hyphen followed by differently to other browsers. Here is some test code that demonstrates the problem:
<html>
<body>
<div style="border: 1px solid blue;width: 50px">
This text can break. Do not - break this text.
</div>
</body>
</html>
Notice how in Firefox the second sentence does not break, but in Chrome and IE it breaks at the hyphen.
Does anyone know a good workaround so that the break does not occur?
The problem is that the "minus" sign in your code allows breaking.
There are lots of ways how you can write a "horizontal line" in HTML. –, —, -. There is "A list apart" article describing all the possible ways.
But also several specific Unicode characters exist which can be used by their code point numbers. One of them is a "non-breaking hyphen", which can be written like this: ‑.
I tried using it in your example and now the text does not break.
Naivist has the answer, but, for the record, I ended up using CSS's white-space: nowrap;
I am trying to get a DIV element to wrap its content despite the content not having any whitespace. The content is a nucleic acid sequence, so inserting whitespace every x-characters is possible, but I'd rather do it more elegantly if possible.
e.g.
<div>TCTTGCTGCGCCTCCGCCTCCTCCTCTGCTCCGCCACCGGCTTCCTCCTCCTGAGCAGTCAGCCCGCGCGCCGGCCGGCTCCGTTATGGCGACCCGCAGCCCTGGCGTCGTGGTGAGCAGCTCGGCCTGCCGGCCCTGGCCGGTTCAGGCCCACGCGGCAGGTGGCGGCCGGGCCCTGAGGCGCGGGATCCGCAGTGCGGGCTCGGGCGGCCGGGCCCAGGGAACCCCGCAGGCGGGGGCGGCCAGTTTCCCGGGTTCGGCTTTACGTCACGCGAGGGCGGCAGGGAGGACGGAATGGCGGGGTTTGGGGTGGGTCCCTCCTCGGGGGAGCCCTGGGAAAAGAGGACTGCGTGTGGGAAGAGAAGGTGGAAATGGCGTTTTGGTTGACATGTGCCGCCTGCGAGCGTGCTGCGGGGAGGGGCCGAGGGCAGATTCGGGAATGATGGCGCGGGGTGGGGGCGTGGGGGCTTTCTCGGGAGAGGCCCTTCCCTGGAAGTTTGGGGTGCGATGGTGAGGTTCTCGGGGCACCTCTGGAGGGGCCTCGGCACGGAAAGCGACCACCTGGGAGGGCGTGTGGGGACCAGGTTTTGCCTTTAGTTTTGCACACACTGTAGTTCATCTTTATGGAGATGCTCATGGCCTCATTGAAGCCCCACTACAGCTCTGGTAGCGGTAACCATGCGTATTTGACACACGAAGGAACTAGGGAAAAGGCATTAGGTCATTTCAAGCCGAAATTCACATGTGCTAGAATCCAGATTCCATGCTGACCGATGCCCCAGGATATAGAAAATGAGAATCTGGTCCTTACCTTCAAGAACATTCTTAACCGTAATCAGCCTCTGGTATCTTAGCTCCACCCTCACTGGTTTTTTCTTGTTTGTTGAACCGGCCAAGCTGCTGGCCTCCCTCCTCAACCGTTCTGATCATGCTTGCTAAAATAGTCAAAACCCCGGCCAGTTAAATATGCTTTAGCCTGCTTTATTATGATTATTTTTGTTGTTTTGGCAATGACCTGGTTACCTGTTGTTTCTCCCACTAAAACTTTTTAAGGGCAGGAATCACCGCCGTAACTCTAGCACTTAGCACAGTA</div>
I need not support every browser. I'm mainly interested in Chrome, Safari and Firefox and other standards-compliant browsers.
CSS style like this will help:
word-wrap:break-word
Other CSS settings that control wrapping are described here.
You can use a "breaking zero-width space" (), which will break the word but will be ignored when copying into, say, notepad.