This project provides a simple way to implement fluid typography in Sass using mixins and functions. It enables responsive font sizing that adjusts based on the viewport width while maintaining a smooth transition between different breakpoints.
- Uses
clamp()for smooth scaling between min and max font sizes. - Supports multiple predefined text styles.
- Automatically converts pixel values to
remfor better scalability. - Customizable via SCSS maps.
Simply copy the scss file into your project and import it into your main Sass file:
@import 'fluid-typography';To apply predefined typography styles, use the provided mixins in your SCSS:
.title {
@include fluid-typography($text-preset-1);
}
.subtitle {
@include fluid-typography($text-preset-2);
}You can define your own font presets using the same structure:
$text-preset-2: (
font-size: (
font-size-1: (fs: 2.5rem, vw: 375px),
font-size-2: (fs: 3rem, vw: 768px),
font-size-3: (fs: 3.75rem, vw: 1024px)
),
styles: (
color: black,
font-family: ("Open Sans", sans-serif),
font-weight: 900,
letter-spacing: 0px,
line-height: 110%
)
);
.subtitle {
@include fluid-typography($text-preset-2);
}The fluid-typography mixin extracts font-size values from a provided preset and generates responsive styles using clamp(). It ensures smooth transitions between defined breakpoints without the need for multiple media queries.
px-to-rem($value-in-px): Converts pixel values torem.is-px($value): Checks if a value is in pixels and converts it if necessary.clamp-size($size-min, $size-max, $viewport-min, $viewport-max): Creates aclamp()function for font scaling.
For the .subtitle class, the following CSS is generated:
.subtitle {
color: #aaa;
font-family: "Open Sans", sans-serif;
font-weight: 900;
letter-spacing: 4px;
line-height: 150%;
text-transform: uppercase;
font-size: clamp(0.75rem, calc(0.6307251908rem + 0.5089058524vi), 0.875rem);
}
@media (min-width: 48rem) {
.subtitle {
font-size: clamp(0.875rem, calc(0.5rem + 0.78125vi), 1rem);
}
}This project is open-source and free to use.