pavlkara1
pavlkara1

Reputation: 170

Set fixed dimension to font-awesome icons

I have a program that depending on the output of one function, gets the status code of one function and returns a font-awesome icon in a card in angular. However the font awesome icons have different dimensions each, so I cannot use the transform: scale() function of css, but I all the icons have to be 45x45px.

I have already tried using transform but I found out the icons didn't have the same dimensions. I have also tried using font-size which fixed only the height (obviously).

Here is the element with the icon

 <div class="msgbox_icon">
      <span class="text-gray-500 fa-4x far {{icon}}"></span>
 </div>

my default font-size is $font-size-base: 0.875rem; //14px

and here is my

.msgbox_icon {
    padding-right:60px;
    padding-left:20px;
    display: flex;
    align-items: center;
}

so I had tried adding font-size: 3.2142857em!important; and then using transform: scale(1.15, 0.987) because for some reason the height wouldn't be 45 but 45.6

I had also tried earlier using width and height but then found out that font-awesome icons behave like text characters.

Upvotes: 1

Views: 2201

Answers (2)

Martin
Martin

Reputation: 22760

Alternative answer:

Because Font Awesome fonts are provided as SVG elements, you can scale them and force their size more easily. Please read this Q&A

This Q&A is also extremely helpful. Your Font-Awesome SVG must have a ViewBox attribute.

Currently Pseudo-code only:

HTML:

<div class="msgbox_icon">
    <span><svg width="45" height="45" viewBox='0 0 50 50'><path d="M1408 131q0 -120 -73 -189.5t-194 -69.5h-874q-121 0 -194 69.5t-73 189.5q0 53 3.5 103.5t14 109t26.5 108.5t43 97.5t62 81t85.5 53.5t111.5 20q9 0 42 -21.5t74.5 -48t108 -48t133.5 -21.5t133.5 21.5t108 48t74.5 48t42 21.5q61 0 111.5 -20t85.5 -53.5t62 -81 t43 -97.5t26.5 -108.5t14 -109t3.5 -103.5zM1088 1024q0 -159 -112.5 -271.5t-271.5 -112.5t-271.5 112.5t-112.5 271.5t112.5 271.5t271.5 112.5t271.5 -112.5t112.5 -271.5z"/></svg></span>
</div>

CSS:

.msgbox_icon > span {
    height: 45px;
    display: flex;
    position:relative
} 
.msgbox_icon > span > svg {
    position:absolute; 
}

Read here about how to exort Font Awesome as SVG.

I am unable to test the above on a font-awesome at the moment, but please reference the linked answers above,

Upvotes: 2

Martin
Martin

Reputation: 22760

This is in fact a very interesting problem and I found some very interesting reading which gives salivating hints to an answer....


Are font metrics controllable with CSS?

...

Short answer: no.


BUT.....

If you are able to view the font glyphs and discover their metric's values, CSS can soup up a solution.

Issue:
To force a/any font glyph to be 45px high.

Solution:

(To research the values referenced below you will need to use a font-reading program such as found here, see also here. I can't find these values online easily, but apparently you should explore the Font-awesome SVG's )

HTML:

<div class="msgbox_icon">
    <span><i class="fas fa-some-icon"></i></span>
</div>

CSS:

.msgbox_icon {
    /* Font metrics */
    /* YOU WILL NEED TO RESEARCH THESE VALUES!! */
    --fm-capitalHeight: 0.68;
    --fm-descender: 0.54;
    --fm-ascender: 1.1;
    --fm-linegap: 0;

    /* Desired font-size for capital height */
    --capital-height: 45;

    /* Apply font-family */
    font-family: 'font-awesome';

    /* Compute font-size to get capital height equal desired font-size */
    --computedFontSize: (var(--capital-height) / var(--fm-capitalHeight));
    font-size: calc(var(--computedFontSize) * 1px);

    /* Then....to centre the glyph */
    --lineheightNormal: (var(--fm-ascender) + var(--fm-descender) + var(--fm-linegap));
    --contentArea: (var(--lineheightNormal) * var(--computedFontSize));

    /* Then... to centre the line height */
    --distanceBottom: (var(--fm-descender));
    --distanceTop: (var(--fm-ascender) - var(--fm-capitalHeight));

    /* Then.... vertical align */
    --valign: ((var(--distanceBottom) - var(--distanceTop)) * var(--computedFontSize));
    
    /* desired line-height */
    --line-height: 2;
    line-height: calc(((var(--line-height) * var(--capital-height)) - var(--valign)) * 1px);
}
span {
    vertical-align: calc(var(--valign) * -1px);
}

Therefore any font awesome icon loaded into the .msgbox_icon > span element will be forced to be 45px in height.

This is untested. but please read the full documentation. And do the required font-awesome research.

Upvotes: 1

Related Questions