Angular 2's SVG creations do not resize properly

It is a common knowledge that an svg element with the attribute viewbox should automatically scale to fit the sizes specified in the styles.

For instance, let's consider a 200*200 circle placed inside a 100*100 div. Prior to that, we need to ensure that the svg takes up all of the space within the div. The svg is scaled accordingly so that the entire circle is visible:

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>

Next, let's integrate this markup into an 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 styling, the result will look like this:

https://i.sstatic.net/KDfUH.png

Why does this occur and how can it be resolved?
I require the svg to scale automatically.

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


Interestingly, if you select the svg element in devtools and execute the following:

$0.outerHTML = $0.outerHTML

the element will display correctly as expected.


PS: Same question in Russian.

Answer №1

Remember that SVG is case sensitive - the proper attribute to use is viewBox, not viewbox.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

How to ensure the right size for your sections in HTML5

I am currently learning HTML5 and attempting to create a specific sized section in the browser where various elements like buttons and text will be displayed based on user interaction. However, I am running into an issue with adjusting the size of the sect ...

Tips on how to prevent certain classes from being impacted by a hue-rotate filter applied to all elements on a webpage

I am currently in the process of adding a feature that allows users to choose between a dark or light theme, as well as select a specific theme color for the app. The implementation involves using CSS filters such as invert(1) for the dark theme and hue-ro ...

Use CSS to style an unordered list into a dropdown menu

Would it be feasible to style an unordered list to resemble a dropdown menu using only CSS, without relying on external libraries like jQuery or MooTools? Appreciate your insights. Warm regards, Neil. ...

Using HTML and CSS, create a layout with three columns where the side columns are flexible in width and the middle

Currently, I am facing an issue with the header design of my website. The layout consists of 3 columns of divs and my goal is to have a middle column with a fixed width of 980px. In addition, I want the left side of the header to span across the entire bro ...

I am currently engaged in a Portfolio project, where my objective is to ensure that all images are optimized for responsiveness

While Bootstrap ensures that relative images are responsive, absolute images may not be. This can cause alignment issues when the browser window is resized on mobile devices. .title-img { position: relative; width: 100%; height: 100%; } .skill-b ...

Dynamic Styling in Angular 2/4: A Modern Approach

Can someone help me with Angular 2/4 syntax for this code snippet? $('nav ul li a').click(() => { $(this).closest('li').addClass('active'); }); ...

As the cursor moves, the image follows along and rotates in sync with its

Can anyone help me figure out how to create a moving image that follows the mouse cursor? I have a radial pie menu with an image in the middle, and I want it to spin and rotate as the mouse moves. Any ideas on how I can achieve this effect? I would greatl ...

Explore the ngx-swiper-wrapper with stunning effects like Cube, Coverflow, and Flip!

I'm currently using ngx-swiper-wrapper and I haven't been able to locate the Cube, Coverflow, and Flip effects that are available in the original Swiper plugin. Has anyone come across these effects? Are they included in ngx-swipper-wrapper? ngx- ...

Looking for a drum set with clickable buttons? Having trouble changing the background color in CSS/HTML? Wondering how to map keyboard keys to HTML buttons?

Behold my HTML creation: <H1> <center> EPIC GUITAR JAM </center> </H1> <img class="guitar" src="guitar.jpg" /> <button class="strum" onclick="Strum()"> Strum Chord </button> <button class="pluck" o ...

What could be causing the issue with converting a Firestore timestamp to a Date object in my Angular app?

Currently, I am developing an Angular project that involves using a FireStore database. However, I have encountered a problem with the setup. Within my Firestore database, I have documents structured like the example shown in this image: https://i.sstatic ...

Expanding the Background Color when Hovered Over

Despite my extensive search on SO, none of the answers I found seem to address my specific situation. In a col-md-2 section where I have a navigation bar, I am trying to change the background color to extend up to the border when hovering. While I can cha ...

Preventing a particular CSS file from being applied to my Angular component

Currently, my modal component is built using bootstrap, but we encountered an issue with our project's CSS file which also utilizes the same classes as bootstrap (.Modal, .Modal-header). This conflicting styling was causing problems for the design of ...

Retrieve the image and insert it using an img tag

Working on a project, I want to include Instagram profile pictures by embedding them. Although I have the image URL, I am struggling to integrate it into my HTML page. Take a look at this sample: This provided link displays the Instagram profile picture. ...

Adjust the child content within a React component based on the element's width, rather than the overall window size, by implementing dynamic resizing without fixed breakpoints

I am working with a react component that displays a div containing menu items which are aligned horizontally using inline-block. The menu items have text labels "Toy Store", "Configure your Toy", and "About Us". My challenge is to dynamically change the ...

What is the best way to overlay an SVG line on top of a CSS style?

Is there a way to make SVG lines appear on top of CSS-styled elements in my HTML file? I have a white background SVG created with JavaScript using d3, and I am adding CSS-styled rectangles on top of it. However, I also want SVG lines (created with JavaScri ...

Content within the two inline divs is experiencing alignment problems?

I am experiencing alignment issues with the content inside two divs that contain multiple nested divs. The content is not properly aligned. CSS Code: .breadcrumb-nav{ margin-left: 230px; left: 70px; top: 70px; width: 1291px; height: 6 ...

Synchronize Angular 5's provision of injection tokens

Is there a way to delay the provision of an InjectionToken until a previous provider's useFactory is finished? For instance, I would like to set MyInjectionToken only after the APP_INITIALIZER token has been allocated. providers: [ HttpClient, MySer ...

Create a recursive CSS style for an angular template and its container

I am struggling with styling CSS inside an ng-container that is used in a tree recursive call to display a tree hierarchy. <ul> <ng-template #recursiveList let-list> <li *ngFor="let item of list" [selected]="isSelected"> <sp ...

What is the method for altering the look of a button using JavaScript?

As a beginner in web development, I have a question. Typically, I know how to style the submit button when it's created in the HTML page using CSS. However, I'm wondering if it's possible to apply CSS styling to the JavaScript block instead. ...

Moving pictures using css3 transitions

Trying to create a straightforward image slider. Below is the provided JavaScript code: $(document).ready(function() { $('#slide1_images').on('click', 'img', function(){ $("#slide1_images").css("transform","translateX ...