Units Converters
Converts between CSS units such as px, rem, and em to support responsive and scalable styling.
Converts between CSS units such as px, rem, and em to support responsive and scalable styling.
rem and em functions are converters for changing values in various formats into values with rem or em units.
const rem: (value: unknown) => string;
const em: (value: unknown) => string;value (unknown): The value to convert. Can be a number, string, or supported expression (e.g. calc, clamp).
Returns:
rem(16);
// Output: "1rem"
em("32px");
// Output: "2em"
rem("calc(100% - 10px)");
// Output: "calc(100% - 0.625rem)"The px function is used to convert a value to pixels or get a numeric value from a string in pixels, rems, or ems.
function px(value: unknown): string | number;value (unknown): The value to convert. Can be a number or a string.
Returns:
px(16);
// Output: 16
px("1rem");
// Output: 16
px("2em");
// Output: 32
px("calc(100% - 10px)");
// Output: "calc(100% - 10px)"This function is a utility to create a custom converter with specific units. For example, rem and em are created using this function.
function createConverter(units: string, { shouldScale }?: { shouldScale?: boolean }): (value: unknown) => string;units (string): The units used by the converter (e.g. rem, em).options (object) (optional):
shouldScale (boolean): Specifies whether the conversion result should be scaled using the scaleRem function.const pt = createConverter("pt");
pt(16);
// Output: "1pt"Flexible: Supports various input formats such as numbers, strings, and complex expressions.High Compatibility: Can be used for various unit conversion needs in CSS, including responsive units such as rem and em.Advanced Expressions: Supports operations with calc, clamp, and other CSS functions.Scalability: The createConverter function allows the creation of custom converters for other units in the future.