CSS CSS Tutorial CSS Advanced CSS Responsive Web Design(RWD) CSS Grid CSS Properties Sass Tutorial Sass Functions



word-break

The word-break property in CSS is used to specify how words should be broken and wrapped when they exceed the boundaries of their container. This property is particularly useful when dealing with long words or strings of characters that cannot fit within a given space.

The word-break property has four possible values:

  • normal: This is the default value. Words are broken at allowed break points, which are determined by the browser.
  • break-all: Words are broken at any point, even if it means breaking them in the middle of a character. This can result in uneven spacing and jagged edges.
  • keep-all: Words are not broken at all, even if they exceed the boundaries of their container. This can result in overflow and hidden text.
  • break-word: This value is similar to break-all, but it only breaks words if they cannot fit within the container. This can result in more even spacing and fewer jagged edges.

Let's take a look at some examples of how the word-break property can be used:

Example 1: Normal

In this example, we have a container with a fixed width of 200px. The text inside the container is a long string of characters with no spaces. The word-break property is set to normal, which means that the browser will break the string at allowed break points.

  <div style="width: 200px; word-break: normal;">
    Thisisalongstringofcharacterswithnospaces.
  </div>
Thisisalongstringofcharacterswithnospaces.

Example 2: Break-All

In this example, we have the same container and text as in Example 1, but the word-break property is set to break-all. This means that the browser will break the string at any point, even if it means breaking it in the middle of a character.

  <div style="width: 200px; word-break: break-all;">
    Thisisalongstringofcharacterswithnospaces.
  </div>
Thisisalongstringofcharacterswithnospaces.

Example 3: Keep-All

In this example, we have a container with a fixed width of 200px. The text inside the container is a long string of characters with no spaces. The word-break property is set to keep-all, which means that the browser will not break the string at all.

  <div style="width: 200px; word-break: keep-all;">
    Thisisalongstringofcharacterswithnospaces.
  </div>
Thisisalongstringofcharacterswithnospaces.

Example 4: Break-Word

In this example, we have a container with a fixed width of 200px. The text inside the container is a long string of characters with no spaces. The word-break property is set to break-word, which means that the browser will break the string only if it cannot fit within the container.

  <div style="width: 200px; word-break: break-word;">
    Thisisalongstringofcharacterswithnospaces.
  </div>
Thisisalongstringofcharacterswithnospaces.

As you can see, the word-break property can be very useful when dealing with long words or strings of characters that cannot fit within a given space. By using this property, you can control how words are broken and wrapped, resulting in more even spacing and fewer jagged edges.

References

Activity