All geek questions in one place

Scale the embedded SVG character referenced by <use>

I have an SVG logo defined as a symbol:

<svg class="SpriteSheet"> <symbol id="icon-logo" viewBox="-12 -79.61 407.19 108.36"> <path id="logo-upperLeft" d="M0-67.61l83.66 0 0-10 -93.66 0 0 30.92 10 0Z" transform="translate(-2 -2)"></path> <path id="logo-upperRight" d="M383.19-67.61l-83.66 0 0-10 93.66 0 0 30.92 -10 0Z" transform="translate(2 -2)"></path> <path id="logo-lowerLeft" d="M0 16.75l83.66 0 0 10 -93.66 0 0-30.92 10 0Z" transform="translate(-2 2)"></path> <path id="logo-lowerRight" d="M383.19 16.75l-83.66 0 0 10 93.66 0 0-30.92 -10 0Z" transform="translate(2 2)"></path> </symbol> </svg> 

This definition is included at the beginning of the body and in the style of display:none .

Later in the document, I use the logo this way:

 <header class="Header"> <h1 class="Header-headline"> <a href="/"> <svg class="Header-logo"> <use xlink:href="#icon-logo"></use> </svg> </a> </h1> </header> 

I want to make a logo of a certain height with automatic width:

 .Header-logo { height: 5rem; } 

The result is the following: svg width not scaling

Although the height is correct (here 1rem is 24px), the width remains the default 300px. Adding width:auto does not cause any changes. Studying this problem, I found here several possible solutions here and here . However, none of them worked with my application: reusing the viewBox at the point of use cuts off most of the image and using the <img> not possible because I need to keep access to the SVG DOM for styling purposes.

I could add hardcoded width based on the known aspect ratio of the image, but this seems like a suboptimal solution: what if the logo aspect ratio changes in the future? Another option is to define width:100% , which works, however, this makes the area with the ability to β€œclick” <a> expand over the entire width of the header, which I would like to avoid.

Is it possible to have a width with automatic size at a certain height when the viewBox is described in the <symbol> definition? Should I be attributed to using an <img> or absolute width for an SVG element?

+6
css svg
Scott colby Jun 25 '15 at 14:27
source share
1 answer

Unfortunately, the dimensions of the <svg> element that appears in your <header> are important. It is not possible to inherit a viewBox from a reference to a child character.

You will need to copy the viewBox (width and height) from the character.

 .Header-logo { height: 5rem; } .Header-logo2 { height: 8rem; } 
 <svg class="SpriteSheet" width="0" height="0"> <symbol id="icon-logo" viewBox="-12 -79.61 407.19 108.36"> <path id="logo-upperLeft" d="M0-67.61l83.66 0 0-10 -93.66 0 0 30.92 10 0Z" transform="translate(-2 -2)"></path> <path id="logo-upperRight" d="M383.19-67.61l-83.66 0 0-10 93.66 0 0 30.92 -10 0Z" transform="translate(2 -2)"></path> <path id="logo-lowerLeft" d="M0 16.75l83.66 0 0 10 -93.66 0 0-30.92 10 0Z" transform="translate(-2 2)"></path> <path id="logo-lowerRight" d="M383.19 16.75l-83.66 0 0 10 93.66 0 0-30.92 -10 0Z" transform="translate(2 2)"></path> </symbol> </svg> <header class="Header"> <h1 class="Header-headline"> <a href="/"> <svg class="Header-logo" viewBox="0 0 407.19 108.36"> <use xlink:href="#icon-logo"></use> </svg> </a> </h1> </header> <header class="Header"> <h1 class="Header-headline"> <a href="/"> <svg class="Header-logo2" viewBox="0 0 407.19 108.36"> <use xlink:href="#icon-logo"></use> </svg> </a> </h1> </header> 
+4
Paul LeBeau Jun 25 '15 at 15:07
source share

Source: https://habr.com/ru/post/989751/

More articles:

  • In angular datatable, how to change the display of records on the options page - angularjs
  • Node.js error:% 1 is not a valid Win32 application - node.js
  • Connect a standalone UCMA application to a SIP trunk provider - c #
  • C ++ Compiler Optimization and Short Circuit Evaluation - c ++
  • AngularJS Animation Element - angularjs
  • Missing "***" - obstructive working copy detected - svn
  • Custom finger for Android search - android
  • SVG does not inherit values ​​from the parent - html
  • What is an array class in Java - java
  • Robust Rail Tuning Solution, Sidekiq, Redis All On AWS Elastic Beanstalk - ruby-on-rails

All Articles

Geek Questions | 2019