Bento Fit Text
<head>
<meta charset="utf-8" /> <meta name="viewport" content="width=device-width" /> <title>bento-fit-text</title> <script src="https://cdn.ampproject.org/bento.js"></script> <script async src="https://cdn.ampproject.org/v0/bento-fit-text-1.0.js" ></script> <link rel="stylesheet" type="text/css" href="https://cdn.ampproject.org/v0/bento-fit-text-1.0.css" /> <style> body { font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Helvetica, Arial, sans-serif, 'Apple Color Emoji', 'Segoe UI Emoji', 'Segoe UI Symbol'; margin: 2rem; background: #ecf1f3; } bento-fit-text { height: calc(100vh - 120px); margin: 1rem 0; } button { border: none; padding: 1rem 2rem; border-radius: 8px; color: #269da3; background: white; font-size: 18px; font-weight: bold; margin-right: 1rem; } </style>
</head>
<body>
<bento-fit-text id="my-fit-text" max-font-size="1000"> Lorem ipsum dolor sit amet, has nisl nihil convenire et, vim at aeque inermis reprehendunt. Lorem ipsum dolor sit amet, has nisl nihil convenire et, vim at aeque inermis reprehendunt. </bento-fit-text> <div class="buttons" style="margin-top: 8px"> <button id="font-button">Change max-font-size</button> <button id="content-button">Change content</button> </div> <script> (async () => { const fitText = document.querySelector('#my-fit-text'); await customElements.whenDefined('bento-fit-text'); // set up button actions document.querySelector('#font-button').onclick = () => fitText.setAttribute('max-font-size', '16'); document.querySelector('#content-button').onclick = () => (fitText.textContent = 'new content'); })(); </script>
</body>
Determines the best font size to fit all of a given text content within the available space.
The expected content for Bento Fit Text is text or other inline content, but it can also contain non-inline content.
Use bento-fit-text as a web component or a React functional component:
↓ Web Component ↓ React / PreactWeb Component
You must include each Bento component's required CSS library to guarantee proper loading and before adding custom styles. Or use the light-weight pre-upgrade styles available inline. See Layout and style.
Import via npm
npm install @bentoproject/fit-text
import {defineElement as defineBentoFitText} from '@bentoproject/fit-text';
defineBentoFitText();
Include via <script>
<script type="module" src="https://cdn.ampproject.org/bento.mjs" crossorigin="anonymous"></script>
<script nomodule src="https://cdn.ampproject.org/bento.js" crossorigin="anonymous"></script>
<script type="module" src="https://cdn.ampproject.org/v0/bento-fit-text-1.0.mjs" crossorigin="anonymous"></script>
<script nomodule src="https://cdn.ampproject.org/v0/bento-fit-text-1.0.js" crossorigin="anonymous"></script>
<link rel="stylesheet" href="https://cdn.ampproject.org/v0/bento-fit-text-1.0.css" crossorigin="anonymous">
Example
<head>
<script type="module" async src="https://cdn.ampproject.org/bento.mjs" ></script> <script nomodule src="https://cdn.ampproject.org/bento.js"></script> <script type="module" async src="https://cdn.ampproject.org/v0/bento-fit-text-1.0.mjs" ></script> <script nomodule async src="https://cdn.ampproject.org/v0/bento-fit-text-1.0.js" ></script> <link rel="stylesheet" type="text/css" href="https://cdn.ampproject.org/v0/bento-fit-text-1.0.css" />
</head>
<body>
<bento-fit-text id="my-fit-text"> Lorem ipsum dolor sit amet, has nisl nihil convenire et, vim at aeque inermis reprehendunt. </bento-fit-text>
</body>
Overflowing content
If the content of the bento-fit-text
overflows the available space, even with a min-font-size
specified, the overflowing content is cut off and hidden. WebKit and Blink-based browsers show ellipsis for overflowing content.
In the following example, we specified a min-font-size
of 40
, and added more content inside the bento-fit-text
element. This causes the content to exceed the size of its fixed block parent, so the text is truncated to fit the container.
<div style="width: 300px; height: 300px; background: #005af0; color: #fff">
<bento-fit-text min-font-size="40">
Lorem ipsum dolor sit amet, has nisl nihil convenire et, vim at aeque
inermis reprehendunt. Lorem ipsum dolor sit amet, has nisl nihil convenire
et, vim at aeque inermis reprehendunt
</bento-fit-text>
</div>
Layout and style
Each Bento component has a small CSS library you must include to guarantee proper loading without content shifts. Because of order-based specificity, you must manually ensure that stylesheets are included before any custom styles.
<link
rel="stylesheet"
type="text/css"
href="https://cdn.ampproject.org/v0/bento-fit-text-1.0.css"
/>
Alternatively, you may also make the light-weight pre-upgrade styles available inline:
<style>
bento-fit-text {
display: block;
overflow: hidden;
position: relative;
}
</style>
Container type
The bento-fit-text
component has a defined layout size type. To ensure the component renders correctly, be sure to apply a size to the component and its immediate children (slides) via a desired CSS layout (such as one defined with height
, width
, aspect-ratio
, or other such properties):
bento-fit-text {
height: 100px;
width: 100%;
}
Accessibility considerations of overflowing content
While overflowing content is visually truncated to fit the container, note that it's still present in the document. Do not rely on the overflow behaviour to simply "stuff" large amounts of content in your pages - while visually it may look appropriate, it may lead to the page becoming overly verbose to users of assistive technologies (such as screen readers), as for these users all the truncated content will still be read/announced in full.
Attributes
Media Queries
The attributes for <bento-fit-text>
can be configured to use different options based on a media query.
min-font-size
Specifies the minimum font size in pixels as an integer that the bento-fit-text
can use.
max-font-size
Specifies the maximum font size in pixels as an integer that the bento-fit-text
can use.
API Example
Programmatically changing an attribute value will automatically update the element.
<head>
<script type="module" async src="https://cdn.ampproject.org/bento.mjs" ></script> <script nomodule src="https://cdn.ampproject.org/bento.js"></script> <script type="module" async src="https://cdn.ampproject.org/v0/bento-fit-text-1.0.mjs" ></script> <script nomodule async src="https://cdn.ampproject.org/v0/bento-fit-text-1.0.js" ></script> <link rel="stylesheet" type="text/css" href="https://cdn.ampproject.org/v0/bento-fit-text-1.0.css" />
</head>
<body>
<bento-fit-text id="my-fit-text"> Lorem ipsum dolor sit amet, has nisl nihil convenire et, vim at aeque inermis reprehendunt. </bento-fit-text> <div class="buttons" style="margin-top: 8px"> <button id="font-button">Change max-font-size</button> <button id="content-button">Change content</button> </div> <script> (async () => { const fitText = document.querySelector('#my-fit-text'); await customElements.whenDefined('bento-fit-text'); // set up button actions document.querySelector('#font-button').onclick = () => fitText.setAttribute('max-font-size', '40'); document.querySelector('#content-button').onclick = () => (fitText.textContent = 'new content'); })(); </script>
</body>
Preact/React Component
Import via npm
npm install @bentoproject/fit-text
import React from 'react';
import {BentoFitText} from '@bentoproject/fit-text/react';
import '@bentoproject/fit-text/styles.css';
function App() {
return (
<BentoFitText>
Lorem ipsum dolor sit amet, has nisl nihil convenire et, vim at aeque
inermis reprehendunt.
</BentoFitText>
);
}
Layout and style
Container type
The BentoFitText
component has a defined layout size type. To ensure the component renders correctly, be sure to apply a size to the component and its immediate children (slides) via a desired CSS layout (such as one defined with height
, width
, aspect-ratio
, or other such properties). These can be applied inline:
<BentoFitText style={{width: 300, height: 100}}>
Lorem ipsum dolor sit amet, has nisl nihil convenire et, vim at aeque inermis
reprehendunt.
</BentoFitText>
Or via className
:
<BentoFitText className="custom-styles">
Lorem ipsum dolor sit amet, has nisl nihil convenire et, vim at aeque inermis
reprehendunt.
</BentoFitText>
.custom-styles {
height: 100px;
width: 100%;
}
Props
minFontSize
Specifies the minimum font size in pixels as an integer that the bento-fit-text
can use.
maxFontSize
Specifies the maximum font size in pixels as an integer that the bento-fit-text
can use.