Reputation: 11424
I am trying to use the react-textfit module with multi-line support. However, my text is overflowing the parent div rather than fitting neatly inside.
An excerpt of my code:
return(
<div style={{margin: '10px', border: 'solid black 2px', width: '200px', height: '80px'}}>
<Textfit mode="multi">
The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog.
</Textfit>
</div>
)
What I am seeing:
The text does auto-fit for single
mode and shows correctly on a single line. However, when I switch it to multi
mode, it is behaving as if I am not even using the <Textfit>
module.
Additional note: while the above text is hard-coded, the actual text will be dynamically pulled from a database.
Question:
What am I doing wrong? How can I fix this so that the text fits inside of the div container?
Upvotes: 0
Views: 2052
Reputation: 2216
You can either make the Textfit
know the height of the parent div with: <Textfit mode="multi" style={{height: '100%'}}>
or you can make the parent div
a display: flex
.
I'm not exactly sure of how Textfit
works under the hood but this works fine in almost every scenarios.
Here's a small Stackblitz to illustrate.
Let me know if this doesn't do exactly what you want! :)
Upvotes: 1
Reputation: 619
If you are trying to fit it in a parent div, you need to tell the Textfit component how big the height is going to be.
return(
<div style={{margin: '10px', border: 'solid black 2px', width: '200px', height: '80px'}}>
<Textfit mode="multi" style={{height: '80px'}}>
The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog.
</Textfit>
</div>
)
Upvotes: 0