38

Is there a way in CSS that I can cause the 3rd element in this list, which is longer than the rest, to wrap to a new line if it extends over 100 characters? so that the rest of the text is on the next line?

I tried giving that <li> a less width, but the content overflows.

I also tried overflow:hidden; and that cuts it off, but the rest is hidden and not on a new line.

How can I do this?

Here is a JS Fiddle where I am trying to do this: http://jsfiddle.net/ZC3zK/

Irfan Mir
  • 2,107
  • 8
  • 27
  • 33

8 Answers8

62

Your line isn't breaking naturally because you don;t have any spaces in it. You can use word-wrap to force the breaks, then add a max-width to say how wide it can be.

CSS

li{
    max-width:200px;
    word-wrap:break-word;
}

HTML

<ul>
    <li>Hello</li>
    <li>How are you</li>
    <li>SearchImagesMapsPla yYouTubeNewsGmailDriveMoreCalendarTranslat eMobileBooksOffersWalletShoppingBloggerFin ancePhotosVideosEven more »Account OptionsSign inSearch...</li>
    <li>SearchImagesMapsPla yYouTubeNewsGmailDriveMoreCalendarTranslateMobileBooksOffersWalletShoppingBloggerFinancePhotosVideosEvenmore»AccountOptionsSigninSearch...</li>
</ul>

http://jsfiddle.net/daCrosby/ZC3zK/1/

You'd need JavaScript to count exactly 100 characters and break the line there. Check here and here for JavaScript examples.

Community
  • 1
  • 1
DACrosby
  • 11,116
  • 3
  • 39
  • 51
  • 2
    I ended up using `max-width:160em;` as an em is one letter m's width and 160 of them would be a 160 characters. – Irfan Mir May 26 '13 at 02:15
  • Did that work? I believe font-size is measured in heights (specifically, `font-size`) - not necessarily character widths. [For example](http://jsfiddle.net/daCrosby/m4ZxV/2/), I have set a number of different em widths and font-sizes an you'll see the final widths don't match with the character widths. – DACrosby May 26 '13 at 05:00
  • The `em` unit has no defined relationship to the width of “m” or any other letter; it is easy to see, by testing, that in most fonts, “m” is much wider than `em`. – Jukka K. Korpela May 26 '13 at 05:33
  • @DACrosby Yes, 160em seems to have fixed the problem. Maybe it is by coincidence? – Irfan Mir May 27 '13 at 01:07
25

The closest you can get is to set a maximum width in ch units. This unit is defined to be the width of the digit zero “0”, but it’s the closest approximation to “average width of characters” that we have in CSS. This means that some lines may be a little longer than 100 characters. Since ch is not universally supported, some backup is advisable, using the em unit. As a very rough rule of thumb, for typical English text, the average width of characters is 0.4em or a little more. (This depends on many things, including the nature of the text and the font.)

li { max-width: 40em; max-width: 100ch; }

This will cause normal wrapping, which means (for English text) breaking at spaces when needed, and possibly after some characters like the hyphen. If you want abnormal wrapping, too, you need to define exactly how and implement it using various techniques like hyphenation or zero-width spaces. Avoid the setting word-wrap: break-word, often offered as snake oil – it liter ally brea ks word s, and it is suitable for very special occasions only.

You can get an exact correspondence between the ch unit and the average width of characters only by using a monospace font. That’s normally not suitable for normal text, only for computer code.

Note that 100 characters is much larger than line lengths commonly recommended by typographers. The optimal length is around 60 or even less (again, depending on nature of text and font, as well as line spacing), and 90 should be regarded as the maximal.

Jukka K. Korpela
  • 195,524
  • 37
  • 270
  • 390
  • Thank you for providing this information. I found it very useful. Do you know of any articles covering the ch unit or anything talking about its support? Is it valid CSS ( part of the spec )? What about your one letter is 0.4em. Do you have an article stating or proving that? – Irfan Mir May 27 '13 at 01:09
  • My link for the `ch` unit describes the support and links to the spec; [CSS Values and Units Module Level 3](http://www.w3.org/TR/css3-values/) is a Candidate Recommendation, which means it’s rather mature. The `0.4em` rule of thumb is based on my experiments with different commonly used fonts, and if you want a more exact figure, test with the font(s) that you specify in your CSS code. – Jukka K. Korpela May 27 '13 at 02:58
  • Oh, I didn't see the link. Yes, it is quite mature. Okay, well I'll take your word for it. 0.4em = 1 ch. – Irfan Mir May 27 '13 at 15:13
  • Thanks for sharing. Is there any place I can see the support coverage for ch? Its real time saver. – Mutant Dec 10 '15 at 19:15
1

That worked pretty well for me :

Using a tag where you want the line to wrap, and then prevent white spaces from wrapping.

.h2_wrap
{
   white-space: nowrap;
  font-size: 18px;
}
<h2 class="h2_wrap">Pretty long sentence that looks<wbr> better when cut at one specific spot</h2>
  • The live preview here doesn't work for me, but `` works fine for me. Furthermore, `` is now also fully standardised in HTML 5. – qräbnö Jan 22 '21 at 18:17
0

You can give the container a width and use the CSS3 word-wrap property. Not exactly 100 characters though.

<ul style="width: 100px; word-wrap:break-word;">
  <li>Hello</li>
  <li>How are you</li>
  <li>SearchImagesMapsPlayYouTubeNewsGmailDriveMoreCalendarTranslateMobileBooksOffersWalletShoppingBloggerFinancePhotosVideosEven more »Account OptionsSign inSearch...</li>
<ul>
Lance Hudson
  • 151
  • 7
0

I have one more solution for wrapping String.

My code will wrap String without breaking word.

outputString=''+$('cssSelector').text()+''

0

Applying CSS 'overflow-wrap' with 'max-width' instructs browser to break the word beyond max width, below CSS helped me to achieve the same.

break-description-text {
  width: 30% !important;
  max-width: 25em !important;
  white-space: normal !important;
  overflow-wrap: break-word !important;
}
Bhupendra Kumar
  • 179
  • 3
  • 7
-1

Please try this code. I have successfully implemented word-wrapping using Javascript.

var a = $('.targetDiv').text().match(/.{1,50}/g);
var b = '';
if (a != null) {
    for (var i = 0; i< a.length; i++) {
        if (a[i].toString() != null) {
            b = b + '<p>' + a[i].toString() + '</p>';
        }
    }
    $('.targetDiv').html(b);
}
Florian Moser
  • 2,583
  • 1
  • 30
  • 40
-4

If you are working with excel you can use this:

A1 is the cell text

=IF(LEN(A1)<100;A1;MID(A1;1;100))

Text end after 100 characters

=IF(LEN(A1)<100;A1;MID(A1;100;LEN(A1)-1))

text starts and ends after 100 to end.

Cfun
  • 8,442
  • 4
  • 30
  • 62