When i have long word which is longer than my container i am using «word-break: break-all;». I was wondering is it possible to put dash(-) at the break point of the word?
This is the css that i am using for the text:
p{
-ms-word-break: break-all;
word-break: break-all;
-webkit-hyphens: auto;
-moz-hyphens: auto;
hyphens: auto;
}
http://fiddle.jshell.net/L18c813r/1/
asked May 22, 2015 at 10:43
2
You need the full set:
-webkit-hyphens: auto;
-moz-hyphens: auto;
-ms-hyphens: auto;
hyphens: auto;
The issue is there isn’t full browser support for them. Test in IE, Firefox, or Safari.
There’s a javascript library you can use to remedy that though. It’s called Hypenator. http://mnater.github.io/Hyphenator/
answered May 22, 2015 at 10:49
AibreanAibrean
6,2871 gold badge21 silver badges38 bronze badges
2
An alternative is to use ­
in the word where it could wrap.
For this method, you need to specify possible «to-long-words» and add the html-tag manually.
also see this Stackoverflow answer
answered May 22, 2015 at 11:01
WavemasterWavemaster
1,77617 silver badges27 bronze badges
2
-
html
-
css
-
text
- 08-06-2019
|
Question
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 simply wraps to a newline on the hyphen?
Solution
Replace your hyphens with this:
­
It’s called a «soft» hyphen.
div {
width: 150px;
}
<div>
12333­2333­233­23339392­332332323
</div>
OTHER TIPS
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>
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 :
111111­222222­-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.
- Home
- > Tutorials
- > CSS
- > CSS properties
CSS property word-break specifies line breaking rules between letters. Note that this different from word-wrap which applies when word does not fit in line box.
CSS property word-break
CSS version: | CSS 3 |
Value: | normal | keep-all | break-all |
Initial: | normal |
Inherited: | yes |
Animatable: | no |
word-break values
normal | Words are broken according to usual rules. |
break-all | In addition to normal break points, word may break at any point. |
keep-all | Breaks are prohibited between pairs of letters. This is more applicable for CJK words. For other words it behaves same as normal. |
Example – normal and break-all word-break
- normal: word will only break at natural break points (like dash).
- break-all: word will break anywhere between two letters.
In the following code, CSS property word-break can be changed to the following values
- normal
- break-all
<style> .box { width: 100px; background-color: lightgreen; word-break: normal; } </style> <div class="box"> This is some text containing some very-longwords </div>
word-break refresh
Related
- CSS word-wrap/overflow-wrap — specify if a word can break anywhere
Specification
- W3C CSS 3 word-break specification
Share this article:
The word-break
CSS property sets whether line breaks appear wherever the text would otherwise overflow its content box.
Try it
Syntax
/* Keyword values */
word-break: normal;
word-break: break-all;
word-break: keep-all;
word-break: break-word; /* deprecated */
/* Global values */
word-break: inherit;
word-break: initial;
word-break: revert;
word-break: revert-layer;
word-break: unset;
The word-break
property is specified as a single keyword chosen from the list of values below.
Values
normal
-
Use the default line break rule.
break-all
-
To prevent overflow, word breaks should be inserted between any two characters (excluding Chinese/Japanese/Korean text).
keep-all
-
Word breaks should not be used for Chinese/Japanese/Korean (CJK) text. Non-CJK text behavior is the same as for
normal
. break-word
Deprecated
-
Has the same effect as
word-break: normal
andoverflow-wrap: anywhere
, regardless of the actual value of theoverflow-wrap
property.
Note: In contrast to word-break: break-word
and overflow-wrap: break-word
(see overflow-wrap
), word-break: break-all
will create a break at the exact place where text would otherwise overflow its container (even if putting an entire word on its own line would negate the need for a break).
Note: While word-break: break-word
is deprecated, it has the same effect, when specified, as word-break: normal
and overflow-wrap: anywhere
— regardless of the actual value of the overflow-wrap
property.
Formal definition
Initial value | normal |
---|---|
Applies to | all elements |
Inherited | yes |
Computed value | as specified |
Animation type | discrete |
Formal syntax
word-break =
normal |
keep-all |
break-all |
break-word
Examples
HTML
<p>1. <code>word-break: normal</code></p>
<p class="normal narrow">
This is a long and Honorificabilitudinitatibus califragilisticexpialidocious
Taumatawhakatangihangakoauauotamateaturipukakapikimaungahoronukupokaiwhenuakitanatahu
グレートブリテンおよび北アイルランド連合王国という言葉は本当に長い言葉
</p>
<p>2. <code>word-break: break-all</code></p>
<p class="breakAll narrow">
This is a long and Honorificabilitudinitatibus califragilisticexpialidocious
Taumatawhakatangihangakoauauotamateaturipukakapikimaungahoronukupokaiwhenuakitanatahu
グレートブリテンおよび北アイルランド連合王国という言葉は本当に長い言葉
</p>
<p>3. <code>word-break: keep-all</code></p>
<p class="keepAll narrow">
This is a long and Honorificabilitudinitatibus califragilisticexpialidocious
Taumatawhakatangihangakoauauotamateaturipukakapikimaungahoronukupokaiwhenuakitanatahu
グレートブリテンおよび北アイルランド連合王国という言葉は本当に長い言葉
</p>
<p>4. <code>word-break: break-word</code></p>
<p class="breakWord narrow">
This is a long and Honorificabilitudinitatibus califragilisticexpialidocious
Taumatawhakatangihangakoauauotamateaturipukakapikimaungahoronukupokaiwhenuakitanatahu
グレートブリテンおよび北アイルランド連合王国という言葉は本当に長い言葉
</p>
CSS
.narrow {
padding: 10px;
border: 1px solid;
width: 500px;
margin: 0 auto;
font-size: 20px;
line-height: 1.5;
letter-spacing: 1px;
}
.normal {
word-break: normal;
}
.breakAll {
word-break: break-all;
}
.keepAll {
word-break: keep-all;
}
.breakWord {
word-break: break-word;
}
Specifications
Specification |
---|
CSS Text Module Level 3 # word-break-property |
Browser compatibility
BCD tables only load in the browser
See also
DigitalOcean provides cloud products for every stage of your journey. Get started with $200 in free credit!
The word-break
property in CSS can be used to change when line breaks ought to occur. Normally, line breaks in text can only occur in certain spaces, like when there is a space or a hyphen.
In the example below we can make the word-break
between letters instead:
.element {
word-break: break-all;
}
If we then set the width of the text to one em
, the word will break by each letter:
See the Pen Setting text vertically with word-break by CSS-Tricks (@css-tricks) on CodePen.
This value is often used in places with user generated content so that long strings don’t risk breaking the layout. One very common example is a long copy and pasted URL. If that URL has no hyphens, it can extend beyond the parent box and look bad or worse, cause layout problems.
See the Pen Fixing links with word-break by CSS-Tricks (@css-tricks) on CodePen.
Values
normal
: use the default rules for word breaking.break-all
: any word/letter can break onto the next line.keep-all
: for Chinese, Japanese and Korean text words are not broken. Otherwise this is the same asnormal
.
This property is also often used in conjunction with the hyphens property so that when breaks occur a hypen is inserted, as per the standard in books.
The full usage, with needed vendor prefixes, is:
-ms-word-break: break-all;
word-break: break-all;
/* Non standard for WebKit */
word-break: break-word;
-webkit-hyphens: auto;
-moz-hyphens: auto;
hyphens: auto;
Using these properties on the universal selector can be useful if you have a site with a lot of user-generated content. Although fair warning, it can look weird on titles and pre-formatted text (
).
- overflow-wrap
- hyphens
- white-space
- Handling Long Words and URLs
Browser Support
Desktop
Chrome | Firefox | IE | Edge | Safari |
---|---|---|---|---|
23 | 49 | 11 | 18 | 6.1 |
Mobile / Tablet
Android Chrome | Android Firefox | Android | iOS Safari |
---|---|---|---|
111 | 110 | 4.4 | 7.0-7.1 |
Safari and iOS support the break-all
value but not keep-all