The CSS for SVG circles breaks in Firefox, causing the browser to strip away the radius property

Currently, I am working on creating a progress ring using SVG and CSS. It is functioning well in Chrome; however, Firefox (61.0.1 (64-bit)) is giving me trouble as it does not display the circle. I attempted to apply the method from this question but did not succeed. Is there a way to style the ring so that it appears correctly in both Chrome and Firefox (latest versions)? Could it be an issue if I'm adding styles with [ngStyles] dynamically? This dynamic styling is necessary for calculating the space and displaying the progress.

You can view a live example on CodeSandbox. The example currently only works in Chrome and not in Firefox.

HTML

<div class="my-progress-ring">
    <svg>
        <circle class="progress" [ngStyle]="getCircleStyles()"/>
    </svg>
</div>

CSS

div.my-progress-ring {
  width: 50px;
  height: 50px;
}

svg {
  height: 100%;
  min-height: 100%;
  width: 100%;
  min-width: 100%;
}

circle.progress {
  stroke: red;
  stroke-width: 4px;
  stroke-linecap: round;
  transform: rotate(-90deg);
  transform-origin: 50% 50%;
  cx: 50%;
  cy: 50%;
  fill: transparent;
}

Typescript

public getCircleStyles(): any {
    return {
      'r': 21,
      'stroke-dasharray': 131.947,
      'stroke-dashoffset': 32.987
    };
  }

EDIT:
In this example, the values in the getCircleStyles function are hardcoded. In my application, I use a function to calculate these values based on the size of the progress ring and the current progress.

EDIT 2:
It appears that Firefox may be rejecting some properties or values in my styling. The r property seems to be missing https://i.sstatic.net/aKhTa.png

Answer №1

It appears that there is a discrepancy in how Firefox implements the SVG 2.0 spec. This could be due to incomplete support for SVG 2.0 in Firefox at the moment. According to the specification:

Some styling properties can be specified not only in style sheets and ‘style’ attributes, but also in presentation attributes.

Therefore, both style sheets and attributes should function as expected.


A quick solution would be to include the r, cx, and cy as presentation attributes in your circle element (as advised here):

<circle _ngcontent-c1="" class="progress" style="stroke-dasharray: 131.947; stroke-dashoffset: 32.987;" cx="50%" cy="50%" r="21" fill="transparent" ng-reflect-ng-style="[object Object]"></circle>

Answer №2

Upon closer observation, I have discovered that in Firefox version 102, units (px) must be added for r, x, and y to function properly:

.ring
{
    r: 150px; /* r: 150; not functioning correctly */
}

Fortunately, units are effective in both Chrome and Firefox browsers.

An interesting note is that the following rules work without needing units:

.ring
{
    cx: calc(var(--svg-width) / 2);
    cy: calc(var(--svg-height) / 2); /* calc() works without 'px' */
    stroke-width: 5; /* stroke-width also functions without specifying units */
}

Answer №3

Hey there, @JohnDizzle! I've observed that Chrome has been backing the circle's css r attribute for quite some time now. Interestingly, my latest encounter with Firefox 74.0b8 on Mac revealed that it also embraces this feature.

Furthermore, Safari 13.0.5 seems to have added support for css r, although I cannot recollect if this was lacking in previous versions.

Answer №4

In the past, we had to include attributes for cx, cy, and r to ensure our code worked smoothly across different browsers besides Chrome. Fortunately, Safari and Firefox have since updated their compatibility, so now your code should function seamlessly in all major browsers.

The inclusion of cx, cy, and r as styles is a recent addition to the SVG 2 specification. This transition has led to many attributes being converted into CSS properties instead.

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

OpenID Connect does not provide confirmation when handling user logouts

We have integrated the angular-oauth2-oidc plugin into our system. Specifically, we are using: angular 13 angular-oauth2-oidc 13.0.1 Our OAuth IDP is WSO2 Identity Server, and here is a sample of the discovery service implemented by WSO2 IS: { // ...

What could be causing checked="checked" to fail validation in JavaScript?

I'm feeling a bit puzzled about this issue. Here's the HTML code I have - <input type="radio" id="20577091" name="q_engine" value="20577091" style="position: absolute; opacity:0;" checked="checked" /> and here is the corresponding JavaScr ...

Tips for inserting values into a string array using jQuery or Angular 7

I have a series of checkboxes with values obtained from a loop. Instead of pushing these values into an array, I need to join them together as "parent1", "parent2", "parent3" and display the result in the console. Below is the code snippet for reference: ...

The replacement of className in inline SVG is not permitted (Error: 'Cannot read property 'className.replace' of undefined')

I've hit a roadblock trying to manipulate classes within an SVG document to modify the rect elements. The code works smoothly on divs in an external document, but I've tried several other solutions as well – all listed below. I'm still rel ...

Placing an element in a fixed position behind a non-fixed element

I am facing a challenge with an element that has two children, one of them is absolutely positioned. My goal is to position this absolutely placed element behind its sibling. Unfortunately, the z-index property doesn't seem to be working as expected. ...

Tips for consistently showing the addition symbol in my search bar

I created a code snippet that showcases a search box with CSS animations and jQuery toggling functionality. One issue I encountered is ensuring that the plus icon remains visible at all times, whether the search box is expanded or contracted. How can I ac ...

Display movie details in a responsive column layout that condenses into a single column

I'm having trouble displaying movie information on a website due to CSS and HTML issues. My goal is to align the title (Director:, Cast:, etc) with the first item in the list, and have all subsequent items follow below. I initially tried using a defin ...

Calculating the median in JavaScript utilizing a for loop

Currently, I am in the process of learning JavaScript. My goal is to calculate the median of a set of numbers that I input through a prompt when I click the button labeled "find median". function CalculateMedia() { var n = prompt("Enter the number of e ...

Is there a way to automatically insert page numbers into every internal link on an HTML page?

When in print mode, I want to display links like this: <a href="#targetPage">link</a> but I'd prefer them to appear as: <a href="#targetPage">link (page 11)</a> (assuming the target page is on page 11 in the print preview). ...

Updating a data table using POST values in ASP.NET: A step-by-step guide

I am working on updating values in my database and want to ensure that my script is functioning correctly. In order to check, I created a POST method to send the values and confirm they are being received. Now, my question is, once the values are being se ...

Interactive table with dynamic data retrieval

I need help displaying data dynamically from a datatable using an API. The API I am working with is located at this URL and I am utilizing the bootstrap4 datatable. You can also find my code on this JSFiddle. Can someone provide guidance or an example on h ...

What causes a webpage to overflow when using the position:absolute property?

Despite my understanding that an element positioned absolutely should be removed from the normal content flow, why does it still cause the page to overflow? Running the code snippet below reveals a horizontal scrollbar, which I didn't expect. .rela ...

What is the best way to display a scrollbar at the bottom of the table while setting the table height to

After writing the css provided below, I noticed that it works fine on my laptop screen. However, when viewing it on a larger monitor, there is empty space at the end of the table. .wrapper{ width:auto; height: 500px; overflow-x: scroll; ...

Trouble with scrolling horizontally on the website

After completing the coding for this website with bootstrap, I encountered an unexpected issue with a horizontal scroll appearing. This is my first project using bootstrap. I attempted to troubleshoot by using the 'divide et impera' method - rem ...

Aurelia TypeScript app experiencing compatibility issues with Safari version 7.1, runs smoothly on versions 8 onwards

Our team developed an application using the Aurelia framework that utilizes ES6 decorators. While the app works smoothly on Chrome, Firefox, and Safari versions 8 and above, it encounters difficulties on Safari 7.1. What steps should we take to resolve th ...

Angular is throwing a RangeError due to exceeding the maximum call stack size

Encountering a stackOverflow error in my Angular app. (see updates at the end) The main issue lies within my component structure, which consists of two components: the equipment component with equipment information and the socket component displaying conn ...

There seems to be an issue with the OpenAPI generator for Angular as it is not generating the multipart form data endpoint correctly. By default

Here is the endpoint that needs to be addressed: @PostMapping(value = "/file-upload", consumes = MediaType.MULTIPART_FORM_DATA_VALUE) public List<FileReference> handleFileUpload( @RequestPart(value = "file", name = "f ...

Top method for uploading outside materials

My website is running smoothly with fast load speeds on a one-page layout, but I want to ensure it stays that way by preventing slow loading times. Right now, I have a portfolio page with tiles that pop up an overlay and slider when clicked. The current m ...

Guide on creating and deploying an iOS API file onto a physical device with a Mac computer

I recently switched to using a Mac and have installed Xcode successfully, along with adding the platform for iOS. However, when I use adb devices, my iPhone device is not detected, but my Android device is recognized when connected. Additionally, when ru ...

The settings of the button return to their default state once it is clicked on

I've been working on a small project and I added a button with hover and CSS effects. However, the issue is that once the button is clicked, it reverts back to its basic state without any of the CSS properties applied. I attempted to troubleshoot if ...