I'm struggling to make my website display correctly on both desktop and mobile devices.
The problem I'm facing right now is text flowing out of container (and out of mobile browser bounds). The text is composed by some website adresses.
I'm aware of word-break: break-all CSS property, but I noticed before changing its value that my default browser, Dolphin browser (Android device) already did this in a smarter way
I understand that this behaviour is likely to be related to the browser itself. I was wondering if there's a way to achieve the same result on all browsers, rather than using CSS word-break property that will result in something like this
EDIT:
The HTML code for one box is:
<div class="col-30 col-m-30">
<div class="shadow-box wow fadeInRight">
<p><img src="/_common/_images/_soundcloudicon/dark_128.png"/></p>
<h3>SoundCloud</h3>
<div style="height: 4px; width: 128px; background-color: rgb(15, 15, 30); margin: 4px auto;"></div>
<p>www.soundcloud.com/thelastminutemusic</p>
</div>
</div>
and this is the CSS class used (wow and fadeInRight are from an animation CSS file and col-30 set the width of the div to 50%):
.shadow-box
{
border-radius: 4px;
margin: 32px;
padding: 16px;
box-shadow: 1px 1px 8px rgba(15, 15, 30, 0.5);
}
You can use <wbr> tag to break the link in a specific location.
When a word is too long, or you are afraid that the browser will break
your lines at the wrong place, you can use the element to add
word break opportunities.
You should add it in the place you want your text will break:
<p>www.soundcloud.com/<wbr>thelastminutemusic</p>
What you want is this snippet of css:
.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;
}
Shamelessly copied from CSS-Tricks : Handling Long Words and URLs (Forcing Breaks, Hyphenation, Ellipsis, etc)
Related
I am trying to use CSS hyphens to hyphenate long words, such as in
this example
As you can see the last letter drops on the new line. This happens in Chrome, not for example in Safari. As my layout is using blocks of certain width and I would like to fit the words into blocks, the solution is not to change the font-size or block width, but rather to solve the hyphenate issue.
p {
width: 105px;
border: 1px solid black;
text-align: center;
}
p.hyphenate {
-ms-hyphens: auto;
-webkit-hyphens: auto;
-moz-hyphens: auto;
hyphens: auto;
word-wrap: break-word;
}
<p class="hyphenate">ANONYMOUSLY</p>
<p class="hyphenate">EXEMPLARITY</p>
<p class="hyphenate">THAT EXEMPLARITY</p>
In the last paragraph "Y" drops on the new line without any hyphenation.
Here is the CodePen example. Works fine in Safari, does not work in Chrome:
https://codepen.io/jospo/pen/MWWjzLe
Thanks for any suggestions.
hyphens is only a Working Draft CSS property at present and currently is not fully implemented or supported by all browsers.
I suggest you review the support tables at CanIUse.com which has the following notes.
Chrome < 55 and Android 4.0 Browser support "-webkit-hyphens: none", but not the "auto" property. It is advisable to set the #lang attribute on the HTML element to enable hyphenation support and improve accessibility.
For Chrome: Only supported on Android & Mac platforms (and only the "auto" value) for now.
In items with longer product titles the text fails to wrap/break when the screen is resized and overlaps. Ive used word-wrap, white-space, word-break & overflow-wrap but the text refuses to break. See code below, you can also see the behavior on the site here: goo.gl/VsFBkx
.product-item-name {
font-size: 16px;
font-family: 'Work Sans','Helvetica Neue',Helvetica,Arial,sans-serif;
font-weight: normal !important;
}
styles-m.css:1
.product-item-name {
display: block;
margin: 5px 0;
word-wrap: break-word;
-webkit-hyphens: auto;
-moz-hyphens: auto;
-ms-hyphens: auto;
hyphens: auto;
font-size: 15px;
}
At the webpage you linked, on line 188 of custom.css, you have this property:
a {
white-space: nowrap !important;
}
That says that all links will refuse to wrap, and because the !important flag is there, it can't be overridden. Remove that and your link titles will wrap.
For future reference, I highly recommend you get familiar with the developer tools built into your web browser (google "[name of browser] developer tools"). It was very easy to find this issue using Chrome's dev tools.
How to mimic word-break: break-word; for IE9, IE11 and Firefox?
It seems to work in Chrome. I have learnt and understood that it is a is non-standard, webkit only.
FYI, I have tried using,
white-space: pre-wrap;
And some more like,
overflow-wrap: break-word;
Also tried the below mentioned CSS,
word-wrap: break-word;
word-break: break-word;
But these don't seem to work.
I can't provide fixed width to the span(which contains the text) by making it display: block; explicitly as the text is dynamic and will differ according to the user's Geo-location. Currently we support around 18 languages.
This is how the code looks,
The html,
<div id="grid2">
<span id="theSpan">Product Support</span>
</div>
The CSS,
#theSpan{
white-space: pre-wrap; /* CSS3 */
white-space: -moz-pre-wrap; /* Firefox */
white-space: -pre-wrap; /* Opera 7 */
white-space: -o-pre-wrap; /* Opera 7 */
word-wrap: break-word; /* IE */
word-break: break-all;
}
#grid2{
width: 100px;
}
It looks like this,
I want it to be like,
Please note:
I had to use word-break: break-all; as for Some of the languages the translated text is too long and it overflows out of the grid. The text 'Product Support' is dynamic.
Update:
I have a fixed width for the div with id, grid2. In one of the languages the translated text is too long, it's a Single word and it flows out of the grid2 div.
Updated the code too.
I have had good success in Chrome, Firefox and IE with using only:
word-break: break-word;
word-wrap: break-word;
In my problem case I was already using:
display: table-cell;
and I ended up having to include
max-width: 440px;
to get wrapping in all browsers. In most cases the max-width was not necessary.
#grid2{
white-space: pre-wrap;
word-wrap: break-word;
}
This should work for IE11, but not for IE9
Use display:table-caption to achieve what you are looking for.
LIVE DEMO
The HTML:
<div id="grid2">
<span id="theSpan">Product Support</span>
</div>
The CSS:
#theSpan{
display:table-caption;
}
Hope this helps.
I read (and tried):
Break long word with CSS
And I now have:
<span style="font-size: xx-small; white-space: -moz-pre-wrap !important; white-space: -pre-wrap;white-space:
-o-pre-wrap;white-space: pre-wrap; word-wrap: break-word;word-break: break-all;white-space:normal;width: 385px;">
but still the large strings will NOT auto wrap in FF or IE but it DOES in Chrome...
(example: http://ed.je/2L6 or http://jsfiddle.net/92kSU/ )
In this case it looks like you need to set word-break: break-all at a higher level. If I open your example page in Firefox and use Firebug to set style word-break: break-all on .entry-content then the large strings wrap.
Edit:
Alternatively, you could instead set the display style of you spans as inline-block.
The following snippet is a catch-all for word-wrapping:
.class_name {
-ms-word-break: break-all;
word-break: break-all;
/* Non standard for webkit */
word-break: break-word;
-webkit-hyphens: auto;
-moz-hyphens: auto;
-ms-hyphens: auto;
hyphens: auto;
}
word-break: break-all will work except for IE8 and Firefox, so you need the ms-word-break prefixed line included as well. As usual, IE8 requires that its prefixed line be added first. This doesn't solve it for Firefox, however.
In FF, you need to use a new item called hyphenations, which is supported except for Chrome (but it's okay, because Chrome will use the basic word-break: break all) in this lengthy list: -webkit-hyphens: auto; -moz-hyphens: auto; -ms-hyphens: auto; hyphens: auto;.
Hyphenation will insert hyphens at the correct location for word-breaks, which is a better solution than just splitting a word in two.
More information on why this is a catch all.
I'm break-wording a container so that extremely long words won't overflow. While Chrome and Safari deal with this really well, it seems that Firefox and IE like to break words randomly - even short words, at the most ridiculous points. See the screenshots below:
Here is the code I'm using to prevent break the words:
.break-word {
-ms-word-break: break-all;
word-break: break-all;
word-break: break-word;
-webkit-hyphens: auto;
-moz-hyphens: auto;
hyphens: auto;
}
This is the the CSS I'm using for the container and the text:
.wrap {
position: relative;
text-align: center;
margin: 40px auto 20px;
padding: 50px;
border: 4px double #f7f7f7;
display: block;
}
.quote-text {
font-size: 40px;
line-height: 50px;
font-weight: 400;
}
HTML
<div class="wrap break-word">
<div class="row-fluid quotation-marks">“”</div>
<span class="quote-text">
Having a partner definitely allows you to take more risks.
</span>
<span class="author">Arianna Huffington</span>
</div>
Why is this happening? Any clues on how I could get the words to break normally across all browsers? Firefox is definitely a priority.
Thanks in advance!
You're doubling up on CSS parameters. Try this..
.break-word {
-ms-word-break: break-all;
-ms-word-wrap: break-all;
-webkit-word-break: break-word;
-webkit-word-wrap: break-word;
word-break: break-word;
word-wrap: break-word;
-webkit-hyphens: auto;
-moz-hyphens: auto;
hyphens: auto;
}
I did a temporary fix by changing my break-word code to:
.break-word {
word-break: break-word;
word-wrap: break-word;
}
The words are no longer breaking weirdly. I'm just not sure how safe this is in terms of cross-browser support (as I've only tested with the latest versions of Chrome, Firefox, and Safari)
Apparently hyphenation is only supported with en-US explicitly declared. If anybody has a more valid answer/explanation, I will gladly accept your answer.
Remove word-break: break-all. Its meaning is that it creates the exact problem you are describing: the browsers is instructed to break words into pieces at will.
Using word-wrap: break-all is more sensible (or less nonsensical), because it tells a browser to break a word only when there is no other way to make a line fit into the available width. But it, too, violates the rules of English and most other languages: it is incorrect to break a word at an arbitrary point. So take it away, too.
Hyphenation makes much more sense, but it has limited browser support, and for apparent reasons it requires the language of text to be declared (because hyphenation rules are strongly language-dependent). You can use JavaScript-based hyphenation such as Hyphenator.js to deal with browsers that don’t support CSS hyphenation
word-break:keep-all;
solved the problem for me