
Security News
Attackers Are Hunting High-Impact Node.js Maintainers in a Coordinated Social Engineering Campaign
Multiple high-impact npm maintainers confirm they have been targeted in the same social engineering campaign that compromised Axios.
react-use-fittext
Advanced tools
React hook to automatically adjust font size to fit text in its container
A powerful React hook that automatically adjusts font size to fit text within its container. Perfect for responsive typography that scales beautifully across different screen sizes and container dimensions.
npm install react-use-fittext
yarn add react-use-fittext
pnpm add react-use-fittext
import { useFitText } from 'react-use-fittext';
function ResponsiveText() {
const { containerRef, textRef, fontSize } = useFitText();
return (
<div
ref={containerRef}
style={{
width: '300px',
height: '200px',
border: '1px solid #ccc',
padding: '16px'
}}
>
<div ref={textRef}>
This text will automatically resize to fit!
</div>
</div>
);
}
function SingleLineExample() {
const { containerRef, textRef } = useFitText({
lineMode: 'single',
minFontSize: 12,
maxFontSize: 60
});
return (
<div ref={containerRef} className="banner">
<h1 ref={textRef}>BREAKING NEWS</h1>
</div>
);
}
function WidthOnlyExample() {
const { containerRef, textRef } = useFitText({
fitMode: 'width',
maxFontSize: 48
});
return (
<div ref={containerRef} style={{ width: '100%', maxWidth: '600px' }}>
<p ref={textRef}>
This text will scale based on container width only,
allowing vertical overflow if needed.
</p>
</div>
);
}
function PerformanceExample() {
const { containerRef, textRef, fontSize } = useFitText({
debounceDelay: 50, // Faster response
resolution: 1, // Higher precision
minFontSize: 8,
maxFontSize: 120
});
return (
<div ref={containerRef} className="dynamic-container">
<span ref={textRef}>
Current font size: {fontSize}px
</span>
</div>
);
}
function DynamicContentExample() {
const [text, setText] = useState('Initial text');
const { containerRef, textRef } = useFitText({
fitMode: 'both',
debounceDelay: 100
});
return (
<div>
<input
value={text}
onChange={(e) => setText(e.target.value)}
placeholder="Type to see text resize..."
/>
<div ref={containerRef} className="text-container">
<div ref={textRef}>{text}</div>
</div>
</div>
);
}
useFitText(options?)| Property | Type | Default | Description |
|---|---|---|---|
minFontSize | number | 1 | Minimum font size in pixels |
maxFontSize | number | 100 | Maximum font size in pixels |
resolution | number | 0.5 | Precision of the binary search algorithm (lower = more precise) |
fitMode | 'width' | 'height' | 'both' | 'both' | Which dimensions to fit the text into |
lineMode | 'single' | 'multi' | 'multi' | Whether to allow text wrapping |
debounceDelay | number | 100 | Debounce delay in milliseconds for resize events |
| Property | Type | Description |
|---|---|---|
containerRef | RefObject<HTMLElement> | Attach to the container element |
textRef | RefObject<HTMLElement> | Attach to the text element |
fontSize | number | Current calculated font size in pixels |
'both' (default)Text scales to fit within both width and height constraints of the container.
'width'Text scales based only on container width. Height can grow as needed.
'height'Text scales based only on container height. Width can grow as needed.
'multi' (default)Allows text to wrap across multiple lines. Text will break naturally at word boundaries.
'single'Forces text to remain on a single line. Long text will be truncated with ellipsis if it exceeds container width.
debounceDelay - Lower values (50-100ms) for faster response, higher values (200-300ms) for better performanceresolution - Use 1 for pixel-perfect sizing, 0.5 for good balance, 2-3 for faster calculationsfitMode - Use 'width' or 'height' instead of 'both' when you only need single-axis fittingminFontSize and maxFontSize to prevent extreme scalingconst { containerRef, textRef } = useFitText({
lineMode: 'single',
fitMode: 'width',
minFontSize: 14,
maxFontSize: 24
});
const { containerRef, textRef } = useFitText({
fitMode: 'both',
minFontSize: 32,
maxFontSize: 96,
debounceDelay: 50
});
const { containerRef, textRef } = useFitText({
lineMode: 'single',
fitMode: 'width',
minFontSize: 10,
maxFontSize: 16
});
Text not resizing?
containerRef and textRef are properly attachedPerformance issues?
debounceDelay to reduce calculation frequencyresolution for faster (but less precise) calculationsfitMode: 'width' or 'height' instead of 'both'Text overflowing?
overflow: hidden if neededminFontSize isn't too large for the containerContributions are welcome! Please feel free to submit a Pull Request. For major changes, please open an issue first to discuss what you would like to change.
MIT © akozma89
FAQs
React hook to automatically adjust font size to fit text in its container
We found that react-use-fittext demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Security News
Multiple high-impact npm maintainers confirm they have been targeted in the same social engineering campaign that compromised Axios.

Security News
Axios compromise traced to social engineering, showing how attacks on maintainers can bypass controls and expose the broader software supply chain.

Security News
Node.js has paused its bug bounty program after funding ended, removing payouts for vulnerability reports but keeping its security process unchanged.