w3tweaks.com
  • Effects
    • Scroll Effects
    • Text Effects
    • Shadow
  • Essentials
    • Arrows
    • Buttons
    • Background Patterns
    • Border Examples
    • Cards
    • Color Palettes
    • Dividers
    • Link styles
    • Loaders
    • Modal Windows
    • Notifications
    • Progress bar
    • Quote styles
    • Spinner
    • Tooltips
  • Media
    • Calendars
    • Carousels
    • Clocks
    • Gallery
    • Music Players
    • Sliders
    • Slideshows
    • Tables
    • Thumbnails
  • Navigation
  • Inputs
    • Range Sliders
    • Checkboxes
    • Toggle Switches
  • Script
    • Angularjs
    • Backbone.js
    • bootstrap
    • jQuery
    • ReactJs
    • JavaScript
    • Syntax Highlighters
    • tryit editor
    • PHP
  • API’s
    • Facebook
    • Google
    • Indeed
    • Twitter
    • YouTube
  • Tools
w3tweaks.com
Home Effects

Fit Text with CSS Variables

September 29, 2020
in Effects

Fit text to the viewport with CSS variables! All you need is to know the number of characters, which a little JS can help with…

See the Pen Fit Text with CSS Variables by Shaw (@shshaw) on CodePen.

You might also like

24 Free CSS Scroll Effect Examples

36 Free CSS 3D Text Effects

13 Beautiful CSS Button Hover Effects for Your Website

13 Free Blob Effects Using CSS3

CSS in CSS with a lot of C and S

Box Shadow Patterns

Created on March 19, 2020 Updated on March 20, 2020. A Pen by Shaw on CodePen.

Download

index.html

<div data-fit-text>Fit Text</div>
<div data-fit-text>With CSS Variables</div>
<div data-fit-text>Some longer text that wants to fit, too...</div>
<div data-fit-text>Don't forget this text!</div>
<div data-fit-text>It can work with really long text if you really want, but that's gonna be hard to read...</div>

script.js

[...document.querySelectorAll("[data-fit-text]")].forEach(el => {
  // We just need the length of the string as a CSS variable...
  el.style.setProperty("--length", el.innerText.length);
});

style.css

@import url("https://fonts.googleapis.com/css?family=Poppins:600&display=swap");

[data-fit-text] {
  

html {
  height: 100%;
  display: flex;
}
body {
  margin: auto;
  text-align: center;
}

html {
  background: #fbc490;
  color: #084177;
}
Tags: text effects
Previous Post

Buttons popper

Next Post

CSS Neumorphic Toggle

Related Stories

CSS Scroll Effects
Effects

24 Free CSS Scroll Effect Examples

November 23, 2022
Collection of Free CSS 3D Text Effects
Effects

36 Free CSS 3D Text Effects

November 11, 2022
Beautiful CSS Button Hover Effects
Effects

13 Beautiful CSS Button Hover Effects for Your Website

November 5, 2022
Free Blob Effects Using CSS3
Effects

13 Free Blob Effects Using CSS3

October 31, 2022
CSS in CSS with a lot of C and S
Effects

CSS in CSS with a lot of C and S

October 12, 2020
Box Shadow Patterns
Effects

Box Shadow Patterns

October 1, 2020
Masked & Skewed
Effects

Masked & Skewed

September 30, 2020
Buttons popper
Effects

Buttons popper

September 21, 2020

Discussion about this post

Follow Us

Popular Posts

100 Creative CSS Cards

44 Free Multi step HTML forms

13 Free HTML & CSS Dashboard Template Designs

49 CSS Tables

20 HTML & CSS pricing tables

14 Best CSS Dark Mode

11 CSS Shopping Cart UI/UX

42 Cool CSS Avatars For Better UI

55 Useful handpicked CSS Buttons with examples and demos

89 Best CSS Toggle Switches

w3tweaks

We bring you the best frontend collections that will fix perfect for news, magazine, personal blog, etc. Check our landing page for details.

  • Effects
    • Scroll Effects
    • Text Effects
    • Shadow
  • Essentials
    • Arrows
    • Buttons
    • Background Patterns
    • Border Examples
    • Cards
    • Color Palettes
    • Dividers
    • Link styles
    • Loaders
    • Modal Windows
    • Notifications
    • Progress bar
    • Quote styles
    • Spinner
    • Tooltips
  • Media
    • Calendars
    • Carousels
    • Clocks
    • Gallery
    • Music Players
    • Sliders
    • Slideshows
    • Tables
    • Thumbnails
  • Navigation
  • Inputs
    • Range Sliders
    • Checkboxes
    • Toggle Switches
  • Script
    • Angularjs
    • Backbone.js
    • bootstrap
    • jQuery
    • ReactJs
    • JavaScript
    • Syntax Highlighters
    • tryit editor
    • PHP
  • API’s
    • Facebook
    • Google
    • Indeed
    • Twitter
    • YouTube
  • Tools