Search code examples
csssvgfont-awesomefont-awesome-5

How to expand a FontAwesome icon


I use the following Font Awesome font on my site with SVG's: https://fontawesome.com/icons/toggle-on?style=solid

How can I widen this Font Awesome font's width but not height?


Solution

  • Basically you cannot change the width/height of icon unless you adjust some property of the SVG like preserveAspectRatio for example which will not give you a good result

    svg {
      width:200px;
      height:50px;
      color:red;
    }
    <svg aria-hidden="true" preserveAspectRatio="none" focusable="false" data-prefix="fas" data-icon="toggle-on" role="img" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 576 512" ><path fill="currentColor" d="M384 64H192C86 64 0 150 0 256s86 192 192 192h192c106 0 192-86 192-192S490 64 384 64zm0 320c-70.8 0-128-57.3-128-128 0-70.8 57.3-128 128-128 70.8 0 128 57.3 128 128 0 70.8-57.3 128-128 128z" class=""></path></svg>

    Another idea is to simply recreate this using pure CSS and you can easily scale the width of the icon:

    .icon {
      display:inline-block;
      height:60px;
      width:200px;
      border-radius:60px;
      background:
        radial-gradient(circle at calc(100% - 30px) 50%, transparent 20px,orange 21px);
    }
    <div class="icon">
    </div>
    <div class="icon" style="width:120px;">
    </div>
    
    <div class="icon" style="width:60px;">
    </div>

    You can also control the height by adding CSS variables:

    .icon {
      --h:30px;
      display:inline-block;
      height:calc(2*var(--h));
      width:200px;
      border-radius:calc(2*var(--h));
      background:
        radial-gradient(circle at calc(100% - var(--h)) 50%, transparent calc(var(--h) - 10px),orange calc(var(--h) - 9px));
    }
    <div class="icon">
    </div>
    <div class="icon" style="width:120px;--h:40px;">
    </div>
    <div class="icon" style="width:100px;--h:20px;">
    </div>
    <div class="icon" style="width:60px;">
    </div>

    And you can also have the toggle effect by adjusting background-position:

    .icon {
      --h:30px;
      display:inline-block;
      height:calc(2*var(--h));
      width:200px;
      border-radius:calc(2*var(--h));
      background:
        radial-gradient(circle , transparent calc(var(--h) - 10px),orange calc(var(--h) - 9px));
      background-size:calc(200% - 2*var(--h)) 100%;
      transition:1s;
    }
    
    .icon:hover {
      background-position:100% 0;
    }
    <div class="icon">
    </div>
    <div class="icon" style="width:120px;--h:40px;">
    </div>
    <div class="icon" style="width:100px;--h:20px;">
    </div>
    <div class="icon" style="width:60px;">
    </div>