Css break line after number of characters

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.

The overflow-wrap property in CSS allows you to specify that the browser can break a line of text inside the targeted element onto multiple lines in an otherwise unbreakable place. This helps to avoid an unusually long string of text causing layout problems due to overflow.

.example {
  overflow-wrap: break-word;
}

Values

  • normal: the default. The browser will break lines according to normal line breaking rules. Words or unbroken strings will not break, even if they overflow the container.
  • break-word: words or strings of characters that are too large to fit inside their container will break in an arbitrary place to force a line break. A hyphen character will not be inserted, even if the hyphens property is used.
  • inherit: the targeted element must inherit the value of the overflow-wrap property defined on its immediate parent.

The demo below has a toggle button that allows you to switch between normal and break-word.

See the Pen overflow-wrap/word-wrap demo by Louis Lazaris (@impressivewebs) on CodePen.

To demonstrate the problem that overflow-wrap attempts to solve, the demo uses an unusually long word inside a relatively small container. When you toggle break-word on, the word is broken to accommodate the small amount of space available, as if the word were multiple words.

A string of non-breaking space characters ( ) would be treated the same way and would also break at an appropriate spot.

overflow-wrap is useful when applied to elements that contain unmoderated user-generated content (like comments sections). This can prevent long URLs and other unbroken strings of text (e.g. vandalism) from breaking a web page’s layout.

Similarities to the word-break Property

overflow-wrap and word-break behave very similarly and can be used to solve similar problems. A basic summary of the difference, as explained in the CSS specification is:

  • overflow-wrap is generally used to avoid problems with long strings causing broken layouts due to text flowing outside a container.
  • word-break specifies soft wrap opportunities between letters commonly associated with languages like Chinese, Japanese, and Korean (CJK).

After describing examples of how word-break can be used in CJK content, the spec says: “To enable additional break opportunities only in the case of overflow, see overflow-wrap“.

From this, we can surmise that word-break is best used with non-English content that requires specific word-breaking rules, and that might be interspersed with English content, while overflow-wrap should be used to avoid broken layouts due to long strings, regardless of the language used.

The Historical word-wrap Property

overflow-wrap is the standard name for its predecessor, the word-wrap property. word-wrap was originally a proprietary Internet Explorer-only feature that was eventually supported in all browsers despite not being a standard.

word-wrap accepts the same values as overflow-wrap and behaves the same way. According to the spec, browsers “must treat word-wrap as an alternate name for the overflow-wrap property, as if it were a shorthand of overflow-wrap“. Also, all user agents are required to support word-wrapindefinitely, for legacy reasons.

Both overflow-wrap and word-wrap will pass CSS validation as long as the validator is set to test against CSS3 or higher (currently the default).

  • word-break
  • hyphens
  • white-space
  • Handling Long Words and URLs

More Information

  • Word-Wrap: A CSS3 Property That Works in Every Browser
  • Overflow Wrapping on W3C
  • Discussion on Stack Overflow on word-break vs. overflow-wrap

Browser Support

Desktop

ChromeFirefoxIEEdgeSafari
23 49 11 18 6.1

Mobile / Tablet

Android ChromeAndroid FirefoxAndroidiOS Safari
105 104 4.4 7.0-7.1

The “partial” support indicated above is due to older browsers supporting word-wrap but not overflow-wrap. Using both can ensure backwards compatibility.

The editor’s draft version of the W3C specification includes a new value called break-spaces that deals with sequences of “preserved” white space characters. There is no known browser support for this feature, and it is not included in the working draft version of the spec.

How do you automatically break lines in CSS?

The overflow-wrap property in CSS allows you to specify that the browser can break a line of text inside the targeted element onto multiple lines in an otherwise unbreakable place. This helps to avoid an unusually long string of text causing layout problems due to overflow.

How do you break a line after a particular word in CSS?

Use the default line break rule. To prevent overflow, word breaks should be inserted between any two characters (excluding Chinese/Japanese/Korean text). Word breaks should not be used for Chinese/Japanese/Korean (CJK) text. Non-CJK text behavior is the same as for normal .

How do you stop text overflowing in CSS?

How to prevent overflow scrolling in CSS.
Max viewport width..
CSS grid..
Not wrapping with Flexbox..
Using images without max-width..

How do I force a new line in CSS?

To force inline elements to a new line, however, you could do any of the following:.
Set display: block; on the element: This may be the most obvious one; a block-level element starts on a new line, and takes up the entire width available to it. ... .
Use the carriage return character ( \A ) as content in pseudo-element:.