Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Svg created by Angular 2 doesn't scale

Tags:

css

angular

svg

It's known that svg-element with viewbox should automatically scale to the sizes from styles.

For example, let's take a 200*200 circle and place it into a 100*100 div. Before that let's make svg to take all div's space. Svg is scaled so that we can see the full circle:

div {
  width: 100px;
  height: 100px;
  outline: 1px dotted red;
}

svg {
  width: 100%;
  height: 100%;
}
<div>
  <svg viewbox="0 0 200 200">
    <circle cx="100" cy="100" r="99" />
  </svg>
</div>

Now let's place this markup into Angular 2 component:

import {Component, View, CORE_DIRECTIVES} from 'angular2/angular2'

@Component({
  selector: 'my-app',
  template: `
    <div>
      <svg viewbox="0 0 200 200">
        <circle cx="100" cy="100" r="99" />
      </svg>
    </div>
  `
})
export class App {
  constructor() {
  }
}

With the same styles we'll see:

Screenshot

Why does it happen and how can I fix it?
I need svg to scale automatically.

Full example: http://plnkr.co/edit/jNJNJLo8ygVcp9SIVcDx?p=preview


It's interesting that if you select this svg-element in devtools and run

$0.outerHTML = $0.outerHTML

the element will get its expected representation.


PS: Same question in Russian.

like image 431
Qwertiy Avatar asked Nov 27 '25 13:11

Qwertiy


1 Answers

SVG is case sensitive. The correct attribute is viewBox rather than viewbox.

like image 78
Robert Longson Avatar answered Nov 30 '25 05:11

Robert Longson



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!