Use the Canvas API to calculate the width and height of the text and adjust the font size.
sample
Returns the width of an element in pixels. Works correctly with inline elements.Returns value as an integer.If you need a decimal you should use `getBoundingClientRect()`.
const b1 = document.getElementById("btn1");
b1.addEventListener("click", () =>
{
const txt= document.getElementById("txt");
adjustFontSize(txt);
});
function adjustFontSize(label)
{
const maxWidth = label.offsetWidth;
const maxHeight = label.offsetHeight;
let fontSize = parseInt(window.getComputedStyle(label).fontSize);
let text = label.textContent;
//A function to get the text height (implementation varies by browser, so choose the appropriate one)
function getTextHeight(text, fontSize, width)
{
//Example: How to use the canvas element
const canvas = document.createElement("canvas");
const context = canvas.getContext("2d");
context.font = `${fontSize}px sans-serif`;
const metrics = context.measureText(text);
return metrics.width;
}
while (getTextHeight(text, fontSize, maxWidth) > maxWidth || label.scrollHeight > maxHeight)
{
fontSize--;
label.style.fontSize = `${fontSize}px`;
}
}
offsetWidth and offsetHeight
Properties for getting the width and height of the raw display area. These return the overall size of the element in pixels, including the border, padding, and content area.
offsetWidth
Measures the overall width, including the border, padding, and content area.
offsetHeight
Measures the overall height, including the border, padding, and content area.
Common Elements
Returns the width of an element in pixels. Works correctly with inline elements. Returns the value as an integer. If you need a decimal, you must use "getBoundingClientRect()".
window.getComputedStyle(element).fontSize
Gets the value of the fontSize property of the computed style of the specified element. Returned as a string, e.g. "16px".
document.createElement("canvas")
This method dynamically creates an HTML canvas element. This method is used to create a new canvas element and add it to an HTML document or use it for other processing.
context.font = `${fontSize}px sans-serif`;
In the Canvas API, this process sets the font setting of the drawing context to the number of pixels specified by fontSize and the sans-serif font family. This determines the font type and size when drawing text on the Canvas.
[context]
This usually refers to a CanvasRenderingContext2D object. This represents the 2D drawing context of the canvas element and is used to draw shapes, text, etc.
[font]
This defines the font used for text drawing. The ${fontSize} part is the numeric value (font size) stored in the fontSize variable, converted to a string and inserted. For example, if fontSize is 16, it will be 16px. sans-serif is the name of the font family. This means a font without serifs (decorations at the ends of letters). For example, Arial and Helvetica belong to this family.
const metrics = context.measureText(text);
This method is used to get the width of the specified text in pixels when drawing text on the HTML5 canvas element. This method measures the specified text text based on the current canvas context (font settings, etc.). The measurement result is returned as a TextMetrics object. This object contains not only the text width (width property), but also the text height and other information. You can use the width obtained by measureText() to draw the text at the appropriate position on the canvas. This prevents the text from overlapping or overflowing other elements.