skip to Main Content

I need to scale a svg (referenced with <use>) around its center that also effect the size of the parent. ( the parent has to be absolute positioned )
The setup looks like this:

<div class="parent">
   <svg class="visual" viewBox="0 0 30 30" width="30">
      <use href="#ref" />
   </svg>
</div>

Tried the following appraches:

  1. scale by width/height property on the svg element – scale from 0, 0 instead of center ( width )
  2. scale by "scale" on svg – parent doesnt scale with child ( scale )

I also tried to translate the origin to the center but it seems that svg doesnt work like this ( most is cutoff ).

The goal is to scale the svg reference at the center without effecting the parent style ( like border ) but effecting the parent size.

2

Answers


  1. Not sure if I got you correctly but if you want the parent to scale up based on the child you can give the parent display:inline-block; style:

    div{
      display:inline-block;
      padding:5px;
    }
    
    Login or Signup to reply.
  2. You can change the cog size from the center absolute positioning to top: 50%; left: 50%; and translating the element -50% on the X and Y axis. Here is an example, the svg changed its size from the center on hover :

    .container {
      position: relative;
      width: 200px;
      height: 200px;
      background-color: green;
    }
    
    .element {
      position: absolute;
      top: 50%;
      left: 50%;
      transform: translate(-50%, -50%);
      background-color: orange;
      border: 3px solid red;
    }
    
    .visual {
      display: block;
      width: 30px;
      height: 30px;
      transition: width .5s, height .5s;
    }
    
    .element:hover .visual {
      width: 100px;
      height: 100px;
    }
    <svg style="display: none;">
      <defs>
        <symbol id="gear" viewBox="0 0 16 16">
          <!-- https://icons.getbootstrap.com/icons/gear/ -->
          <path d="M8 4.754a3.246 3.246 0 1 0 0 6.492 3.246 3.246 0 0 0 0-6.492zM5.754 8a2.246 2.246 0 1 1 4.492 0 2.246 2.246 0 0 1-4.492 0z M9.796 1.343c-.527-1.79-3.065-1.79-3.592 0l-.094.319a.873.873 0 0 1-1.255.52l-.292-.16c-1.64-.892-3.433.902-2.54 2.541l.159.292a.873.873 0 0 1-.52 1.255l-.319.094c-1.79.527-1.79 3.065 0 3.592l.319.094a.873.873 0 0 1 .52 1.255l-.16.292c-.892 1.64.901 3.434 2.541 2.54l.292-.159a.873.873 0 0 1 1.255.52l.094.319c.527 1.79 3.065 1.79 3.592 0l.094-.319a.873.873 0 0 1 1.255-.52l.292.16c1.64.893 3.434-.902 2.54-2.541l-.159-.292a.873.873 0 0 1 .52-1.255l.319-.094c1.79-.527 1.79-3.065 0-3.592l-.319-.094a.873.873 0 0 1-.52-1.255l.16-.292c.893-1.64-.902-3.433-2.541-2.54l-.292.159a.873.873 0 0 1-1.255-.52l-.094-.319zm-2.633.283c.246-.835 1.428-.835 1.674 0l.094.319a1.873 1.873 0 0 0 2.693 1.115l.291-.16c.764-.415 1.6.42 1.184 1.185l-.159.292a1.873 1.873 0 0 0 1.116 2.692l.318.094c.835.246.835 1.428 0 1.674l-.319.094a1.873 1.873 0 0 0-1.115 2.693l.16.291c.415.764-.42 1.6-1.185 1.184l-.291-.159a1.873 1.873 0 0 0-2.693 1.116l-.094.318c-.246.835-1.428.835-1.674 0l-.094-.319a1.873 1.873 0 0 0-2.692-1.115l-.292.16c-.764.415-1.6-.42-1.184-1.185l.159-.291A1.873 1.873 0 0 0 1.945 8.93l-.319-.094c-.835-.246-.835-1.428 0-1.674l.319-.094A1.873 1.873 0 0 0 3.06 4.377l-.16-.292c-.415-.764.42-1.6 1.185-1.184l.292.159a1.873 1.873 0 0 0 2.692-1.115l.094-.319z" />
        </symbol>
      </defs>
    </svg>
    
    <div class="container">
      <div class="element">
        <svg class="visual" viewBox="0 0 30 30" width="30">
          <use href="#gear" />
        </svg>
      </div>
    </div>
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search