Alternative for `word-break: break-word` - css

Is there an alternative for word-break: break-word that works in firefox ?
In firefox[Version 57.0]
In Chrome[Version 62.0.3202.94]
Is there a way or an alternative to use break-word attribute in firefox also.? [if it works in older versions it would be much better.]
Sample Code
table {
width: 300px;
display: inline-block;
overflow: hidden;
word-break: break-word;
}
.text-right {
width: 30%;
}
<table>
<tr>
<td class="text-right">Sample text </td>
<td>Sample texttexttexttexttexttexttexttext 000000000000000000000000000000</td>
</tr>
</table>

The valid values of word-break are as follows:
/* Keyword values */
word-break: normal;
word-break: break-all;
word-break: keep-all;
/* Global values */
word-break: inherit;
word-break: initial;
word-break: unset;
The word-break CSS property specifies whether or not the browser
should insert line breaks wherever the text would otherwise overflow
its content box.
Chrome, Safari and other WebKit/Blink browsers also support the
unofficial break-word value which is treated like word-wrap: break-word.
So could you use word-wrap: break-word instead?

I'm a bit late to the party, but I think I found the answer.
I've always used word-break: break-word because it's perfect in most cases, if there is enough space it wraps and keeps the words, and if there's not enough space it breaks the word. MDN Web Docs say that word-break: break-word is deprecated but will probably still works, but the real reason I started looking at an alternative is that visual studio doesn't auto-complete break-word anymore :)
Solution ?
MDN - overflow-wrap
overflow-wrap: anywhere;
Wraps the words to new line, and only breaks the word if it need to.
"The property was originally a nonstandard and unprefixed Microsoft extension called word-wrap, and was implemented by most browsers with the same name. It has since been renamed to overflow-wrap, with word-wrap being an alias."
So overflow-wrap should be used instead of word-wrap.
var s = document.querySelector('div');
function a(){
s.removeAttribute('class');
s.classList.add('a');
}
function b(){
s.removeAttribute('class');
s.classList.add('b');
}
#class{
border: 1px solid rgb(255, 120, 120);
width: 100px;
margin-top: 10px;
margin-bottom: 10px;
border-radius: 2px;
}
btn{
border: 1px solid gray;
padding: 2px;
border-radius: 5px;
box-shadow: 0 0 2px gray;
cursor: pointer;
}
.a{
overflow-wrap: normal;
}
.b{
overflow-wrap: anywhere;
}
<span>overflow-wrap:</span>
<btn onClick="a()">Normal</btn>
<btn onClick="b()">Anywhere</btn>
<div id='class'>
We need a very long word like Supercalifragilisticexpialidocious
</div>
<i>There's also overflow-wrap: break-word;</i>

According to this thread on Bugzilla, word-break: break-word now works in Firefox as of version 67. It got implemented in this changeset.
break-word does come with this note in the MDN docs:
This deprecated API should no longer be used, but will probably still
work.

To manipulate words in your case you have two CSS properties: word-break and word-wrap:
word-break: https://www.w3schools.com/cssref/css3_pr_word-break.asp
word-wrap: https://www.w3schools.com/cssref/css3_pr_word-wrap.asp

For compatibility with legacy content, the word-break property also supports a deprecated break-word keyword. When specified, this has the same effect as word-break: normal and overflow-wrap: anywhere, regardless of the actual value of the overflow-wrap property. source here
It worked perfectly on my websites.

Related

Why is word-wrap not working as expected?

I'm getting very frustrated with the head titles of some of my pages, I have word-wrap: break-all, and for some reason its breaking the word so im left with a letter on its own,
My css is as follows:
.spacing {
font-size: 22px !important;
letter-spacing: 0 !important;
line-height: 28px!important;
width: 80%;
margin: auto;
word-break: break-all;
word-wrap: break-word;
}
it's become infuriating I've tried word-wrap and word-break but none of them seem to be doing what I ask, any ideas why this is happening on the letters in the word rather than the word its self.
word-break: break-all; signifies that there can be a break between any two letters, even in the middle of a word. Try word-break: normal; (or perhaps even just removing word-break) and see if that fixes the issue.
Reference

Responsive code blocks

I have seen many websites with code blocks on my cellphone, but only a few were responsive. If the code lines were to long I couldn't see the rest of the code. I am also now developing a website with code in. How can I make the code blocks responsive? Here is my code.
pre {
width: 70%;
font-family: 'Courier New', Courier, monospace;
background-color: #b3b3b3;
color: #ededed;
}
One solution is to give the pre block an overflow property to hide the excess or cause it to scroll. The other solution is to have it wrap.
pre {
white-space: pre-wrap; //css3
white-space: moz-pre-wrap; //firefox
white-space: -pre-wrap; //opera 4-6
white-space: -o-pre-wrap; //opera 7
word-wrap: break-word; //internet explorer
}
In 2018, the solution that worked for me is to use the white-space: property with a value of "pre-wrap" like below:
.markdown-body pre>code {
padding: 0;
margin: 0;
font-size: 100%;
background: 0 0;
border: 0;
white-space: pre-wrap;
}
The value "pre-line" may work too if you are ok with the collapsing of multiple whitespaces.
The value "pre" doesn't work for me as it only breaks if there's a new line in the source (useless when there are very long litteral urls in source).
Here is how MDN describes the relevant values of the white-space: property:
pre :
Sequences of whitespace are preserved. Lines are only broken at newline characters in the source and at <br> elements.
pre-wrap :
Sequences of whitespace are preserved. Lines are broken at newline characters, at <br>, and as necessary to fill line boxes.
pre-line :
Sequences of whitespace are collapsed. Lines are broken at newline characters, at <br>, and as necessary to fill line boxes.
the word-wrap CSS property has been mapped to word-break. Therefore, using the CSS attribute word-break: break-word; can also help resolve the issue
code {
word-break: break-word;
white-space: pre-wrap;
}
<html>
<head></head>
<body>
<p>
The HTML <code>button</code> tag defines a clickable button.
</p>
<p>The CSS property defines the background color of an element.</p>
<div>
<code>
gclid={gclid}&utm_id={campaignid}
&utm_source=google&utm_medium=cpc&utm_campaign={campaignid}&utm_term=
{adgroupid}&utm_content={creative}
</code>
</div>
</body>
</html>;
pre {
padding: 0.1em 0.5em 0.3em 0.7em;
border-left: 11px solid #ccc;
margin: 1.7em 0 1.7em 0.3em;
overflow: auto;
width: 93%;
font-family: "Courier New", Courier, monospace, sans-serif;
}
If you couldn't get the line numbers work with word-wrap:break-word, simply use Alex Gorbatchev's syntax highlighter like everyone else ;) No need to reinvent the wheel.

how to break long word in Span?(using Twitter Bootstrap)

I'm using Twitter bootstrap in my site and I'm having span which may contain long word and I need to break it. I use this Css but not working? what's problem?
.fidDivComment {
white-space: pre-wrap;
display: inline-block;
}
You need to add this to your CSS for that span
span {
-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;
}
See this article for an explanation.
The questions stated this:
(using Twitter Bootstrap)
so the bootstrap fix is this: #include hyphens; this bootstrap out of the box mixin will add word-wrap: break-word;
this mixin is exist under this path:
/bootstrap-sass/assets/stylesheets/bootstrap/mixins/_vendor-prefixes.scss

How do I wrap text with no whitespace inside a <td>?

I've used:
word-break:break-all;
table-layout:fixed;
and the text wraps in Chrome but not Firefox.
Update: I decided to change the design so it didn't need the wrap; trying to sort out a CSS fix/hack was proving too frustrating and time consuming.
Try this, I think this will work for something like "AAAAAAAAAAAAAAAAAAAAAARRRRRRRRRRRRRRRRRRRRRRGGGGGGGGGGGGGGGGGGGGG"
will produce
AARRRRRRRRRRRRRRRRRRRR
RRGGGGGGGGGGGGGGGGGGGG
G
I have taken my example from a couple different websites on Google. I have tested this on ff 5.0, IE 8.0, and Chrome 10.
.wrapword {
white-space: -moz-pre-wrap !important; /* Mozilla, since 1999 */
white-space: -webkit-pre-wrap; /* Chrome & Safari */
white-space: -pre-wrap; /* Opera 4-6 */
white-space: -o-pre-wrap; /* Opera 7 */
white-space: pre-wrap; /* CSS3 */
word-wrap: break-word; /* Internet Explorer 5.5+ */
word-break: break-all;
white-space: normal;
}
<table style="table-layout:fixed; width:400px">
<tr>
<td class="wrapword">
</td>
</tr>
</table>
Here is advanced version of what OP asked.
Sometimes, what happens is that, our client wants us to give '-' after word break to end of line.
Like
AAAAAAAAAAAAAAAAAAAAAAABBBBBBBBBBB
break to
AAAAAAAAAAAAAAAAAAAAAAA-
BBBBBBBBB
So, there is new CSS property if supported, usually supported in latest browsers.
.dont-break-out {
/* These are technically the same, but use both */
overflow-wrap: break-word;
word-wrap: break-word;
-ms-word-break: break-all;
/* This is the dangerous one in WebKit, as it breaks things wherever */
word-break: break-all;
/* Instead use this non-standard one: */
word-break: break-word;
/* Adds a hyphen where the word breaks, if supported (No Blink) */
-ms-hyphens: auto;
-moz-hyphens: auto;
-webkit-hyphens: auto;
hyphens: auto;
}
I am using this one.
I hope somebody will have demand like this.
For an automatic table layout try to style the concerned td combining the attributes max-width and word-wrap.
Eg: <td style="max-width:175px; word-wrap:break-word;"> ... </td>
Tested in Firefox 32, Chrome 37 and IE11.
You can manually inject zero width spaces (​) to create break points.
Set a column width for the td tag.
What worked for me was:
.output {
overflow-wrap: break-word;
word-wrap: break-word;
word-break: break-word;
}
I think this is a long standing issue in Firefox, that harks back to Mozilla and Netscape. I'll bet you were having the issue with the display of long URLs. I think it is an issue with the rendering engine rather than something you can fix with CSS, without some ugly hacks.
Makes sense to change the design.
This looked hopeful though: http://hacks.mozilla.org/2009/06/word-wrap/
I'm using Angular for my project, and managed to solve this with a simple filter:
Template:
<td>{{string | wordBreak}}</td>
Filter:
app.filter('wordBreak', function() {
return function(string) {
return string.replace(/(.{1})/g, '$1​');
}
})
You can't see it, but after $1 there is an invisible space (thanks #kingjeffrey for the tip), which enabled word breaks for table cells.
One slightly hackish way of doing this is by processing the text to add space between each letter. Replace spaces with Then use the letter-spacing css attribute to bring the spaces down.
I know, it's a hack... but if NOTHING else works, it should wrap without problem.

CSS line break in TD

If I have a very long string (without spaces!). Can I force the browser to line break it, for example, in a table's td via CSS. Width does not seem to have any effect.
Note: I know it is very unlikely for someone to submit a long string without spaces but you never know ...
I tried:
.gridrow td
{
padding: 1em;
text-align: center;
word-wrap: break-word;
}
and I use FF as my browser. word-wrap only seems to be used in CSS3?
I think what you mean is in CSS:
#id
{
max-width: 100px; /*or whatever*/
word-wrap: break-word;
}
Tested on your site with Firebug, it works.
Reference article.
Tables are a special case. They'll keep on expanding if they can, so as not to obscure any content; you can't use overflow: hidden on them either. Here are some options:
Use an optional breaking character. (Source: http://www.quirksmode.org/oddsandends/wbr.html ) If you can control insert a character like ­ or <wbr> programatically into long strings, this may be a solution.
Perhaps preferably, depending on your situation, you can limit the table to use strict widths, at which point it should obey your rule:
table {
table-layout: fixed;
width: 100%;
}
table td {
word-wrap: break-word; /* All browsers since IE 5.5+ */
overflow-wrap: break-word; /* Renamed property in CSS3 draft spec */
}
i think there wordwrap element. search for the wordwrap.
word-wrap: break word
EDITED
REF Word-wrap in a html table
it suggest that there's no good way of doing this.

Resources