If a user types in a long line without any spaces or white space, it will break formating by going wider than the current element. Something like:
HAHAHAHAHAHAHAHAHAHAHAHAHAHAHAHAHAHAHAHA.............................................................................................................................................
I've tried just using wordwrap() in PHP, but the problem with that is if there is a link or some other valid HTML, it breaks.
There seems to be a few options in CSS, but none of them work in all browsers. See word-wrap in IE.
How do you solve this problem?
in CSS3:
word-wrap:break-word
I was trying to solve the same problem and I found de solution here:
http://perishablepress.com/press/2010/06/01/wrapping-content/
Solution: adding to the container the following CSS properties
div {
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+ */
}
The idea is using them all so you get better cross-browser compatibility
Hope this helps
I like to use the overflow: auto CSS property/value pairing. This will render the parent object the way you'd expect it to appear. If the text within the parent is too wide, scrollbars appear within the object itself. This will keep the structure the way you want it to look and provide the viewer with the ability to scroll over to see more.
Edit: the nice thing about overflow: auto compared to overflow: scroll is that with auto, the scrollbars will only appear when overflowing content exists. With scroll, the scrollbars are always visible.
I haven't personally used it, but Hyphenator looks promising.
Also see related (possibly duplicate) questions:
word wrap in css / js
Who has solved the long-word-breaks-my-div problem? (hint: not stackoverflow)
I'm surprised that nobody has mentioned one of my favorite solutions to this problem, the <wbr> (optional line-break) tag. It's fairly well-supported in browsers and essentially tells the browser that it can insert a line-break if it's necessary. There's also the related zero-width space character, with the same meaning.
For the use case mentioned, displaying user comments on a web page, I would assume that there is already some output formatting to prevent injection attacks, etc. So it's simple to add these <wbr> tags every N characters in words that are too long, or in links.
This is especially useful when you need control over the format of the output, which CSS doesn't always let you do.
I would put the post in a div that would have a fixed width setting overflow to scroll (or to hide completely depending on the content).
so like:
#post{
width: 500px;
overflow: scroll;
}
But that's just me.
EDIT: As cLFlaVA points out... it is better to use auto then scroll. I do agree with him.
There is no "perfect" HTML/CSS solution.
The solutions either hide the overflow (ie scrolling or just hidden) or expand to fit. There is no magic.
Q: How can you fit a 100cm wide object into a space only 99cm wide?
A: You can't.
You can read break-word
EDIT
Please check out this solution
How to apply a line wrap/continuation style and code formatting with css
or
How to prevent long words from breaking my div?
I dodge the problem by not having my right sidebar fixed like that :P
Here's what I do in ASP.NET:
Split the text field on spaces to get all the words
Iterate the words looking for words that are longer than a certain amount
Insert every x characters (e.g. every 25 characters.)
I looked at other CSS based ways of doing this, but didn't find anything that worked cross-browser.
based on Jon's suggestion the code that I created:
public static string WrapWords(string text, int maxLength)
{
string[] words = text.Split(' ');
for (int i = 0; i < words.Length; i++)
{
if (words[i].Length > maxLength) //long word
{
words[i] = words[i].Insert(maxLength, " ");
//still long ?
words[i]=WrapWords(words[i], maxLength);
}
}
text = string.Join(" ", words);
return (text);
}
I didn't want to add libraries to my pages just for word breaking.
Then I wrote a simple function which I provide below, hope it helps people.
(It is breaking by 15 characters, and applying "& shy;" between, but you can change it easily in the code)
//the function:
BreakLargeWords = function (str)
{
BreakLargeWord = function (word)
{
var brokenWords = [];
var wpatt = /\w{15}|\w/igm;
while (wmatch = wpatt.exec(word))
{
var brokenWord = wmatch[0];
brokenWords.push(brokenWord);
if (brokenWord.length >= 15) brokenWords.push("");
}
return brokenWords.join("");
}
var match;
var word = "";
var words = [];
var patt = /\W/igm;
var prevPos = 0;
while (match = patt.exec(str))
{
var pos = match.index;
var len = pos - prevPos;
word = str.substr(prevPos, len);
if (word.length > 15) word = BreakLargeWord(word);
words.push(word);
words.push(match[0]);
prevPos = pos + 1;
}
word = str.substr(prevPos);
if (word.length > 15) word = BreakLargeWord(word);
words.push(word);
var text = words.join("");
return text;
}
//how to use
var bigText = "Why is this text this big? Lets do a wrap <b>here</b>! aaaaaaaaaaaaa-bbbbb-eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee";
var goodText = BreakLargeWords(bigText);
Add the Zero width space () to the string and it will wrap.
Here is a Javacript example:
let longWordWithOutSpace = 'pneumonoultramicroscopicsilicovolcanoconiosis';
// add between every character to make it wrap
longWordWithOutSpace.split('').join('');
! I did not wanted to make my code more complex with Javascript.
my developing Env was Blazor and UI was for Smartphone.
the Code had a list of file names and some of them where a very long name without space or any other helping Char.
for me this works:
https://developer.mozilla.org/en-US/docs/Web/CSS/overflow-wrap
overflow-wrap: anywhere;
" overflow-wrap: normal; " not work becase it needs space in strings to wrap.
"overflow-wrap: break-word;" not worked for me maybe because it was not a word or something else. I am not sure!
I have posted a solution which uses JavaScript and a simple Regular Expression to break long word so that it can be wrapped without breaking your website layout.
Wrap long lines using CSS and JavaScript
I know that this is a really old problem and since I had the same problem I searched for a easy solution.
As mentioned in the first post I decided to use the php-function wordwrap.
See the following code example for information ;-)
<?php
$v = "reallyreallyreallylonglinkreallyreallyreallylonglinkreallyreallyreallylonglinkreallyreallyreallylonglinkreallyreallyreallylonglinkreallyreallyreallylonglink";
$v2 = wordwrap($v, 12, "<br/>", true);
?>
<html>
<head>
<title>test</title>
</head>
<body>
<table width="300" border="1">
<tr height="30">
<td colspan="3" align="center" valign="top">test</td>
</tr>
<tr>
<td width="100"><?php echo $v2; ?></td>
<td width="100"> </td>
<td width="100"> </td>
</tr>
</table>
</body>
</html>
Hope this helps.
Related
So I'm stuck! I'm looking for any solution that is simple enough for a novice coder.
Currently I have the following code which adds an ellipsis to an echo
<?php echo ucwords($query->name = (strlen($query->name) > 15) ? substr($query->name,0,13).'...' : $query->name); ?>
This works just fine but I have it set on an iPhone 5 screen size to keep the text clean and not disrupt the screen layout. However, on a larger device this looks odd because there is extra space. So is there another way to make the ellipsis show depending on the screen size?
Thanks!
Just style the element that contains that output (without server-side truncation) with
overflow: hidden;
text-overflow: ellipsis;
I've got a fiddle with the data here:
http://jsfiddle.net/ktpmm5/Z5z8n/
and page is staged here:
http://steppingstonez.com/daretorepair/magazines.php
Basically my paging element gets shoved to the left and lays over top of my heading. This happens only in IE 8 - works fine in Chrome, FF and Opera. I'm going crazy trying to figure out what is wrong. html validates fine.
Any ideas?
Quick solution:
To make it roughly work, change the paging wrapper to have the position: relative, and float the ul.paging right (remove the position).
You'll need to add height into the head_text to include the paging wrapper (as paging_wrapper is now positioned out of the flow, so its height does not count).
Longer solution:
Even with the fix above, you still have the problem that a long title will overlap regardless, so I would define the area/width of the header (making it wrap if too long) and also restrict the area of the paging device (by limiting number of buttons that show).
Another quick solution: Specify the correct width for your .paging CSS class (UL). For example a width of 220px seems correct.
.paging {
...
...
width: 220px; /* new */
}
I think it seems more of an issue with utilizing negative margins. You have both a margin-top on the paging_wrapper and the paging li a. This is probably causing some weirdness for support in IE.
I am building a wordpress plugin which is generating an HTML table and sending to gravityforms html block via shortcode.
My problem is that cell contents can contain:
23.24
1,234.665
123.4
etc...
Notice the differing number of decimal places.
Is there a non-hack & best practice way of aligning this column data by decimal point? In this case, Aligning right will not work.
Inserting 0s is not acceptable because this indicates a degree of accuracy which is not there.
As you can see, I have attempted to use align="char" char="." inside the td elements with no luck.
Any help anybody can help with this would be much appreciated.
Many thanks.
Is there a way of using printf("%8.3f",d1) or similar without actually printing to the screen? e.g. structuring the variable d1 for later use but not actually printing it?
There is no direct way to do this. HTML 4.01 has align=char, but without any browser support. CSS 2.0 had a counterpart, using the text-align property, with equal lack of support, so it was dropped from CSS 2.1. CSS3 drafts have a nice system for such alignment, but indicated as being in danger of being cut from the spec if there are no (correct) implementations.
As a workaround, you could right-pad the values with something invisible (blank) so that when the values aligned to the right, the decimal markers get aligned. There are several ways to try to achieve this:
1) Use digit 0 but set a style on it, making it invisible, e.g.
123.4<span class=s>00</span>
with
.s { visibility: hidden; }
2) Use FIGURE SPACE U+2007, defined to have the same width as digits (when digits are of equal width), e.g.
123.4
For this to work, you need to set the font so that it contains U+2007. According to http://www.fileformat.info/info/unicode/char/2007/fontsupport.htm even Arial contains it, but I’m afraid this might not apply to old versions of Arial still in use.
3) Use a no-break space and set its width to the desired number of digits, using the ch unit (define to have the width of digit 0), though this unit is relatively new and not supported by old browsers. Example:
123.4<span class=d2> </span>
with
.d2 { width: 2ch; display: inline-block; }
I would probably use the first method. As a matter of principle, it has the drawback that when CSS is off, the data contains zeroes, which imply wrong information about accuracy, whereas in other methods, switching CSS off “only” messes up the layout.
(It’s probably obvious that digits must be of equal advance width, so that you can align numeric data at all. This just means that the font used for the values must have that property. Most fonts will do in this respect, but e.g. Georgia, Constantia, and Corbel won’t.)
I wrote a jQuery plugin that solves this. It's found here: https://github.com/ndp/align-column
Using your raw HTML table, it will align a column by decimal points:
$('table').alignColumn(3);
It does this by adding another column, but does its best to not corrupt the other spacing. There's also a reference to a different solution on the Github page.
Would it be acceptable to put the value into two columns?
Use sprintf() to convert the value into a string, and then put the bits up to the decimal point in the left column (but right aligned), and the decimal places in the second column.
See http://jsfiddle.net/alnitak/p4BhB/, but ignore the JS bit...
The thing is, you've gotta ensure that they all have the same number of digits after the decimal.
Once you do that, use text-align. All it will take is a: style='text-align: right'
Better still, you could use a css class instead of inline styles. Your markup would look like this:
<tr><td>Item 1</td><td>15</td><td class='price'>£123.25</td></tr>
Then in your stylesheet:
td.price{
text-align: right;
}
With php, you can format a number as a string with number_format. You don't have to echo it or print it, just wrap your variable in that function. For example:
$table .= "<td class='price'>£" . $price . "</td></tr>";
becomes:
$table .= "<td class='price'>£" . number_format($price,3) . "</td></tr>";
It might be overkill but I needed the same thing and just solved with a length of the output and adding whitespace based on that length.
I.e.:
if (strlen($meal_retail) == 5) {
echo " ";
}
else (strlen($meal_retail) == 6) {
echo " ";
}
This lined up my decimals correctly with a bit of extra doing, and i'm sure an array could clean the above code up even nicer.
Additionally, i've been conforming my numbers adjusting with:
echo money_format('%i',$meal_retail) (makes it a two decimal money number)
Just wanted to provide my solution as I was looking at this page before coming up with my own resolution.
this is my solution, hope it help!!
<style type="text/css">
td{
font-family: arial;
}
.f{
width: 10px;
color: white;
-moz-user-select: none;
}
</style>
<table>
<tr><td><span class="f">00</span>1.1<span class="f">00</span></td></tr>
<tr><td><span class="f">0</span>12.34<span class="f">0</span></td></tr>
<tr><td>123.456</td></tr>
</table>
with this, you can't see the zeros and can't select them!
I have used javascript for this, I hope this will help.......
</tr>
</table>
</body>
for(var i=0; i<numarray.length; i++){
var n = numarray[i].toString();
var res= n.split(".");
n = res[0];
if(highetlen < n.length){
highetlen = n.length;
}
}
for(var j=0; j<numarray.length; j++){
var s = numarray[j].toString();
var res= s.split(".");
s = res[0];
if(highetlen > s.length){
var finallevel = highetlen - s.length;
var finalhigh = "";
for(k=0;k<finallevel;k++){
finalhigh = finalhigh+ ' ';
}
numarray[j] = finalhigh + numarray[j];
}
var nadiss = document.getElementById("nadis");
nadiss.innerHTML += "<tr><td>" + numarray[j] + "</td></tr>";
}
What I want to do is so that when word riches the end of the line to divide word into two words and insert hyphen before line-break. It's similar to <wbr> tag but it doesn't inserts anything.
Examples:
Java- <--- I want to insert this hyphen before line-break
Script
or
Ja- <---- Same here
vaScript
Is there anyway of doing it? I don't mind using php, javaScript but jQuery would preferable and if I can achieve it using CSS or HTML that would be great.
Thnx in advance!
If you want to do it client-side, you will need a hyphenation plugin. Reason being, there needs to be a dictionary of hyphenation rules to follow, and that's something that's not simply included in the browser. [update: although, that statement is partially false already! Chris's link shows limited (very limited, but still... promising!) support.]
Here's one jQuery plugin I found: http://archive.plugins.jquery.com/plugin-tags/hyphenation and I know I've used one in a project before (may have been this one even). It will of course insert the hyphen into the node's contents, but the content doesn't need to have it included, the hyphenation is calculated by the function after DOM ready.
Whether or not the rules are extensive enough to break up words outside of the 'norm' is another question.
Side note: JavaScript is a proper noun, which should be excluded from hyphenation. I imagine that was just an example off the top of your head, though. ;-)
It looks like there is some limited browser support for CSS hyphenation. Does that work for you?
This function should solve your problem. It insert Soft Hyphen | $shy; into "long" words.
pText = Your string
pMax = Every X Char in a word insert a Soft Hyphen;
function hyphen(pText, pMax){
var t = pText.split(" ");
for(i=0; i<t.length; i++){
if(t[i].length<pMax){
var w = t[i]
var re = new RegExp("(.{"+pMax+"})","g");
if(w.length<pMax+3){
var pos = t[i].length - 3;
re = new RegExp("(.{"+ pos +"})","g");
}
t[i] = w.replace(re,"$1"+);
}
}
return t.join(" ");
}
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
text-overflow:ellipsis in Firefox 4?
I have the same issue mentioned in Truncating long strings with CSS: feasible yet?. It's been nearly two years since that post, and Firefox still ignores the text-overflow: ellipsis; property.
My current solution is to truncate long strings in PHP like so:
if(strlen($some_string) > 30)
$some_string = substr($some_string,0,30)."...";
That more or less works, but it doesn't look as nice or as accurate as text-overflow: ellipsis; in browsers that support it. The actual width of thirty characters varies since I'm not using a monospace font. The XML fix and jQuery plugins posted in the other thread appear to no longer work in Firefox either.
Is there currently a way to do this in CSS that is browser independent? If not, is there a way to measure the width of a string given a font and font size in PHP so that I might more accurately place my ellipsis?
This answer might be useful for getting your output truncated to the nearest word, and then simply append a … (…) HTML entity onto the end of the output to get your final output.
As you've noticed there's not sufficiently wide browser support yet the CSS solution yet, and you've still got to worry about old browsers too.
It is a shame that all browsers don't handle the same CSS features. However, you could always do something like this using JavaScript (with help from jQuery).
Here's an example of how such a thing might look: http://jsfiddle.net/VFucm/
The basic idea is to turn your string into an array of words, like so:
var words = full.split(/\s+/g);
Loop through them and take the first N (in this case I chose 24) and push them into another array:
for (var i = 0; i < 24; i++) {
short.push(words[i]);
}
Throw them back into the HTML element they came from:
$('.snip').html(short.join(" ") + ' <span class="expand">...</span>');
... here I added a "link" to expand the shortend text. It's made to look and act like a link using CSS. I also provided a function to replace the shortened text with the foll text again:
$('.expand').click(function() {
$('.snip').html(full);
});