When designing a component, you might need to truncate the displayed text to a fixed number of lines and show an ellipsis.
This is possible using some CSS properties such as overflow: hidden;
and text-overflow: ellipsis;
. However, we would be focusing on how to truncate text using Chakra UI.
Example: Let's dive into how to truncate the Text below
<Text>
In publishing and graphic design, Lorem ipsum is a placeholder text commonly used to demonstrate the visual form of a document or a typeface without relying on meaningful content. Lorem ipsum may be used as a placeholder before the final copy is available.
</Text>
There are 2 ways to truncate Text in Chakra UI:
1. Passing isTruncated
prop
The isTruncated
prop renders an ellipsis(...) when the text exceeds the width of the viewport or maxWidth prop.
<Text isTruncated>
In publishing and graphic design, Lorem ipsum is a placeholder text commonly used to demonstrate the visual form of a document or a typeface without relying on meaningful content. Lorem ipsum may be used as a placeholder before the final copy is available.
</Text>
Note that when using this prop, responsive truncation is automatically set across all screens.
2. Passing noOfLines
prop
Just as the name implies, this prop is used to truncate the text to a specific number of lines. Simply pass the noOfLines
prop and set it to the desired no of lines.
<Text noOfLines={3}>
In publishing and graphic design, Lorem ipsum is a placeholder text commonly used to demonstrate the visual form of a document or a typeface without relying on meaningful content. Lorem ipsum may be used as a placeholder before the final copy is available.
</Text>
Setting responsiveness using noOfLines
You may want to specify the number of lines the text should be truncated to on different devices. Doing this is pretty straightforward using Chakra's Array Syntax
<Text noOfLines={[1,2]}>
In publishing and graphic design, Lorem ipsum is a placeholder text commonly used to demonstrate the visual form of a document or a typeface without relying on meaningful content. Lorem ipsum may be used as a placeholder before the final copy is available.
</Text>
Quick Note: If you are new to Chakra UI and would love to get started, visit the Getting Started Docs
Top comments (1)
I have been trying to wrap fonts inside a table in chakra ui and had been not successful. Is there a way to do it? Here is my code and my website image
As you could see in this image... all data comes to this table via table_data[1] object. But I want the column width to be uniform and should come within the visible width of the container. The fonts are not wrapping and its overflowing. Due to this the table cell is automatically going for a scroll. Its not wrapping the content within the cell. Could you help me find out what css property or chakra ui property I could add?
Here is my code