0

I am using font awesome for a very long time. But few days back i came across a situation where in i needed to set the font size automatically so that the content covers the entire space of the parent element.

I know about some libraries and methods to do it with text fit and similar but the problem is that they work for the text inside an element. However font awesome works with the css content property and we are required to se the size of the font manually. Is there any way so that i can set the font size to scale to parent element.

Example of css:

div {
  width: 300px;
  height: 300px;
  border:1px solid;
}

i {
  font-size:20px;
}
<link href="https://use.fontawesome.com/releases/v5.6.3/css/all.css" rel="stylesheet" />
<div>
  <i class="far fa-user-circle" />
</div>
Sudhir
  • 766
  • 8
  • 17

1 Answers1

0

Here is some script to do the job

const getComputedStyle = (element, style) => {
  let computedStyle = window.getComputedStyle(element, null);  
  return computedStyle[style];
};

let container = document.querySelector('div');
let icon = document.querySelector('div i');
let h = getComputedStyle(container, 'height');
icon.style.fontSize = `${h}`;
div {
  width: 300px;
  height: 300px;
  border:1px solid;
}

i {
  font-size:20px;
}
<link href="https://use.fontawesome.com/releases/v5.6.3/css/all.css" rel="stylesheet" />
<div>
  <i class="far fa-user-circle" />
</div>
Bibberty
  • 4,670
  • 2
  • 8
  • 23