Skip to main content

Setting Two Lines of Text Height in Tailwind CSS

Explore how to use Tailwind CSS height and minimum height classes to effectively set the height of a DOM element to accommodate at least two lines of text.

––– views

In Tailwind CSS, you can use height (h-) or minimum height (min-h-) classes to guarantee that a DOM element has at least the height of two lines of text. Tailwind CSS does not have a direct class for "two lines of text height," but you can approximate this by customizing the height.

For example, if you know the line-height is fixed, you can set the element's minimum height to be twice the line height (which is normally a multiple of the text size). First, determine your text size, suppose it's text-base. In Tailwind CSS, the default text-base size is 1rem, i.e., 16px (this specific value may vary depending on your configuration). The default line height is typically about 1.5 times the font size, so we can set the element's minimum height to be approximately 3rem (the height of two lines).

You can express this using Tailwind CSS classes like this:


_10
<div class="min-h-[3rem] text-base ...">Your text content...</div>

In this example, min-h-[3rem] sets the minimum height of the element to be 3rem (which is approximately the height of two lines of text), and text-base defines the text size.

If you want to set the height more accurately based on the number of lines and are using the TailwindCSS JIT (Just-In-Time) mode, you can create custom utility classes or use inline styles to specify the height accurately:


_10
<div class="min-h-[calc(2em*1.5)] text-base ...">Your text content...</div>

In the above example, we used the calc(2em*1.5) expression to ensure that if the font size changes, the minimum height would adjust accordingly to keep the space occupied by two lines of text (assuming the line height is 1.5 times the text size).

Please keep in mind that, depending on your specific settings (such as font size, line height, font family, etc.), you'll need to adjust these values to ensure they work as intended. If the height of two lines of text is not a fixed value, you may need to tweak it slightly to match the design requirements.

Moreover, Tailwind CSS allows you to extend the default theme in the tailwind.config.js file, meaning you can custom-add additional height or minimum height utility classes and use them in your project.