Is it possible to adapt font size to div width with CSS?

Engkus Kusnadi picture Engkus Kusnadi · Jun 29, 2016 · Viewed 44.1k times · Source

I have a div with 70% width, and here’s what I want to do:

  • Put some words in that div
  • Adapt font size so that those words occupy all div width


Is this possible with only css? Here is my code:

Note: the div size is responsive, this is important. Thanks in advance!

Answer

Anthony Astige picture Anthony Astige · Jun 29, 2016

Perhaps not quite what you're looking for, but something - You can make both the font and the element relative to view width.

p {
  font-size: 5vw;
  background-color: red;
  width: 50vw;
}
<p>
  I'm a P!
</p>