1

I'm trying to scale text based on the parent div. I'm using reactjs and I've tried to use the react-textfit package, but the font size is defined in pixels making it not responsive.

Remembering that the texts are dynamic. I don't know how many letters each will have

I make this example in https://jsfiddle.net/2hsz16q8/6/

here i did it using a second class but, i would like to do this dynamically.

HTML:

<ul>
 <li>
  <div>
   <h1 id="dynamic-text-1">Why do we use it?</h1>
  </div>
 </li>
 <li>
  <div>
   <h1 id="dynamic-text-2">Where does it come from?</h1>
  </div>
 </li>
</ul>

CSS:

#dynamic-text-1 {
  font-size: 4em;
}

#dynamic-text-2 {
  font-size: 2.9em;
}

div {
  border: 1px solid red;
  width: 70vw;
}
kelvin sand
  • 413
  • 4
  • 13

1 Answers1

0

You could use vw for font-size instead of em, rem, % or px. This way the font will change depending on the browser width and it will not be relative to the browser rather it will be relative to the screen size.

HTML

<ul>
  <li>
    <div>
      <h1 id="text-2">Why do we use it?</h1>
    </div>
  </li>
  <li>
    <div>
      <h1 id="text-1">Where does it come from?</h1>
    </div>
  </li>
</ul>

CSS

h1 {
  font-size: 2vw;
}

div {
  border: 1px solid red;
  width: 70vw;
}

Note that you should use this only with media queries otherwise the text will scaleup on the large screens.

Leon Vuković
  • 489
  • 3
  • 16