Tailwind CSS Whitespace

The whitespace utility class in Tailwind CSS allows you to control the handling of whitespace within elements. With Tailwind's whitespace class, you can easily modify the whitespace behavior to achieve the desired text and layout effects.

Applying Whitespace

To apply a specific whitespace behavior to an element, you can use the whitespace-{value} utility class, where {value} represents the desired whitespace behavior. Here are some common values you can use:

  • whitespace-normal: Allows normal whitespace behavior, where multiple consecutive spaces are collapsed into a single space and line breaks are treated as a single space.
  • whitespace-nowrap: Prevents line breaks and truncates text if it exceeds the container's width.
  • whitespace-pre: Preserves whitespace and line breaks as they appear in the source code.
  • whitespace-pre-line: Preserves line breaks but collapses multiple consecutive spaces into a single space.
  • whitespace-pre-wrap: Preserves whitespace and line breaks, wrapping text as necessary.
html
<p class="whitespace-nowrap">
This text will not wrap and will be truncated if it exceeds the container's width.
</p>

Preview

This text will not wrap and will be truncated if it exceeds the container's width.

Responsive Whitespace

Tailwind CSS allows you to apply whitespace classes responsively at different breakpoints. To use responsive whitespace classes, you can append the breakpoint prefix to the utility class. For example, md:whitespace-pre-wrap sets the whitespace behavior to preserve whitespace and line breaks, wrapping text as necessary starting from the medium breakpoint and above.

html
<p class="whitespace-normal md:whitespace-pre-wrap">
This text will have normal whitespace behavior by default, but starting from the medium breakpoint
and above, it will preserve whitespace and line breaks, wrapping text as necessary.
</p>

In the above example, the whitespace behavior of the <p> element is set to normal by default (whitespace-normal), but starting from the medium breakpoint and above, it changes to preserve whitespace and line breaks, wrapping text as necessary (md:whitespace-pre-wrap).

Tailwind CSS WhiteSpace Class Table

ClassProperties
whitespace-normalwhite-space: normal;
whitespace-nowrapwhite-space: nowrap;
whitespace-prewhite-space: pre;
whitespace-pre-linewhite-space: pre-line;
whitespace-pre-wrapwhite-space: pre-wrap;

Windframe Tailwind blocks

landing page

Build modern projects using Bootstrap 5 and Contrast

Trying to create components and pages for a web app or website from scratch while maintaining a modern User interface can be very tedious. This is why we created Contrast, to help drastically reduce the amount of time we spend doing that. so we can focus on building some other aspects of the project.

Contrast Bootstrap PRO consists of a Premium UI Kit Library featuring over 10000+ component variants. Which even comes bundled together with its own admin template comprising of 5 admin dashboards and 23+ additional admin and multipurpose pages for building almost any type of website or web app.
See a demo and learn more about Contrast Bootstrap Pro by clicking here.

ad-banner