How do you break a word without space in css?

Possible Duplicate:
CSS: how can I force a long string (without any blank) to be wrapped in XUL and/or HTML

Lets say one has the following code:

This-is-a-long-line-of-text-without-any-spaces-This-is-a-long-line-of-text-without-any-spaces-This-is-a-long-line-of-text-without-any-spaces-This-is-a-long-line-of-text-without-any-spaces

I apologize but stack overflow (according to the preview) does not line break code snippets.

Under most (?) circumstances, the following will cause the container that holds the long line of text to stretch to the full width of the text contained within.

We would like to force this to line break (even mid word) according to the css specified width (width:100px) of the parent container.

Is this possible via a css tag I do not know about?

IE6+ comparability, plus gecko/webkit/opera please.

The CSS white-space property controls how text is handled on the element it is applied to. Let’s say you have HTML exactly like this:

A bunch of words you see.

You’ve styled the div to have a set width of 100px. At a reasonable font size, that’s too much text to fit in 100px. Without doing anything, the default white-space value is normal, and the text will wrap. See the example below or follow along at home with the demo.

div {
  /* This is the default, you don't need to
     explicitly declare it unless overriding
     another declaration */
  white-space: normal; 
}
How do you break a word without space in css?

If you want to prevent the text from wrapping, you can apply white-space: nowrap;

How do you break a word without space in css?

Notice in HTML code example at the top of this article, there are actually two line breaks, one before the line of text and one after, which allow the text to be on its own line (in the code). When the text renders in the browser, those line breaks appear as if they are stripped out. Also stripped out are the extra spaces on the line before the first letter. If we want to force the browser to display those line breaks and extra white space characters we can use white-space: pre;

How do you break a word without space in css?

It’s called pre because the behavior is that as if you had wrapped the text in

 tags (which by default handle white space and
line breaks that way). White space is honored exactly as it is in the HTML and the text does not wrap until a line break is present in the code. This is particularly useful when literally displaying code, which benefits aesthetically from some formatting (and sometimes is absolutely crucial, as in white-space-dependent languages!)

Perhaps you like how pre honors white space and breaks, but you need the text to wrap instead of potentially break out of its parent container. That’s what white-space: pre-wrap; is for:

How do you break a word without space in css?

Finally, white-space: pre-line; will break lines where they break in code, but extra white space is still stripped.

How do you break a word without space in css?

Interestingly, the final line break is not honored. As per the CSS 2.1 spec: “Lines are broken at preserved newline characters, and as necessary to fill line boxes.” so perhaps that makes sense.

Here is a table to understand the behaviors of all the different values:

 New linesSpaces and tabsText wrapping
normalCollapse Collapse Wrap
prePreserve Preserve No wrap
nowrapCollapse Collapse No wrap
pre-wrapPreserve Preserve Wrap
pre-linePreserve Collapse Wrap

In CSS3, the white-space property is literally going to follow that chart and map the properties to text-space-collapse and text-wrap accordingly.

More information

  • CSS write-space Property (DigitalOcean)
  • How to Prevent Line Breaks Using CSS (DigitalOcean)
  • Mozilla Docs

Browser support

IEEdgeFirefoxChromeSafariOpera
8+ All All All All All
Android ChromeAndroid FirefoxAndroid BrowseriOS SafariOpera Mobile
All All All All All
Source: caniuse

How do you break a whole word in CSS?

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. If we then set the width of the text to one em , the word will break by each letter: HTML.

How do you put a break in text in CSS?

A line-break can be added in HTML, using only CSS, by employing the pseudo-class ::after or ::before . In the stylesheet, we use these pseudo-classes, with the HTML class or id, before or after the place where we want to insert a line-break. In myClass::after : Set the content property to "\a" (the new-line character).

How do you break a long string in CSS?

The element If you know where you want a long string to break, then it is also possible to insert the HTML element. This can be useful in cases such as displaying a long URL on a page.

What is the Do not Break word in CSS?

Wrap the words that you don't want to break.

This is my paragraph. It will wrap normally, except for the part about happily-hyphenated-hillbillies.