SVG Opacity Transitions seem to ignore the constraints of CSS rules and bounce chaotically beyond their designated boundaries

I'm currently working on implementing a hover effect for an SVG rect embedded in HTML. The transition I've set up is not smooth, and the opacity values seem to be inconsistent during the animation.

My browser of choice is Firefox. The rect I'm working with has a fill color of pure red (#FF0000). Initially, its opacity is set to 0.1, which increases to 1 on hover. To monitor the transition's progress, I recorded a video where I noted the changes in the rect's red value at different intervals. Given that the rect is positioned on a black background, I believe the red value should reflect the overall opacity of the shape. Ideally, the transition should smoothly go from a red value of approximately 25 (255 * 0.1) to 255 (255 * 1). However, in reality, the actual transition ranges between a red value of 23 and 234. This inconsistency might be attributed to limitations in my recording software.

During the hovering action over the rect:

  1. The initial red value of the rect is 23
  2. Upon hover, the rect immediately disappears with a red value of 0
  3. The red value then transitions to 26
  4. Suddenly, there's a jump to a red value of 100
  5. Another fade occurs to reach a red value of 140
  6. This process ends with a jump to a final red value of 234

When the mouse moves away from the rect, the following sequence unfolds:

  1. The rect fades to a red value of 102
  2. An abrupt jump back to a red value of 45 follows
  3. Another fade brings it down to a red value of 10
  4. Lastly, another sudden jump results in a final red value of 23

Two key points confuse me. Firstly, why does the opacity fluctuate so drastically? Secondly, why does the opacity drop all the way to 10 (as seen in step 9), instead of stopping at 23? How can these issues be rectified?

Here is the code snippet I am using:

<html>
<head>
    <title>Opacity transition test</title>
    <style>
        body > svg {
            background: black;
        }
        #the-rect {
            opacity: 0.1;
            transition: opacity 1s linear;
        }
        #the-rect:hover {
            opacity: 1;
        }
    </style>
</head>
<body>
    <svg width="500" height="500">
        <rect id="the-rect" x="0" y="0" width="250" height="250" fill="#FF0000"/>
    </svg>
</body>
</html>
 

Answer №1

Opacity doesn't always animate smoothly in Firefox.

However, fill-opacity may be a suitable alternative for your needs.

<html>
<head>
    <title>Testing Opacity Transition</title>
    <style>
        body > svg {
            background: black;
        }
        #the-rect {
            fill-opacity: 0.1;
            transition: fill-opacity 1s linear;
        }
        #the-rect:hover {
            fill-opacity: 1;
        }
    </style>
</head>
<body>
    <svg width="500" height="500">
        <rect id="the-rect" x="0" y="0" width="250" height="250" fill="#FF0000"/>
    </svg>
</body>
</html>
 

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

Developing a unique attribute using AngularJS

As a beginner in AngularJS, I am experimenting with creating directives to manipulate the background-color of a <div> based on specific conditions. I aim to write code like this within my view: <div effect-color="#2D2F2A">content here</div& ...

Is it possible to eliminate the background color by double clicking on a row within an HTML table?

I am currently working on an HTML table with jQuery functionality that allows for highlighting a row when selected, and double-clicking to remove the highlight. Additionally, I would like to ensure that only one row is selected at a time. The code below s ...

The overlay background is not covering the entire height of the container

The CSS includes a .wrapper with the style min-height: calc(50% - 30px), along with an overlay blur div to cover the entire height of the .wrapper element. Here's a link to a Codepen example showcasing this setup: https://codepen.io/palaniichukdmytro/ ...

Tips for aligning two objects side by side in HTML

Apologies for the basic question, forgive my ignorance but I am struggling with keeping two objects in the same line within the same division. Please take a look at the code below: <html> <head> <style> .search-input { width: ...

What are some techniques for concealing a rendered element retrieved using the fetch API?

Currently, I am grappling with a coding challenge that requires me to extract data from https://jsonplaceholder.typicode.com/users using the fetch API function along with bootstrap and jquery. To display the data in a div element, I utilized the array.map( ...

Change the type of button in the Jquery UI dialog form to be submit

I'm struggling with my Ui dialog form using jquery. The Submit and Cancel buttons are currently set to type="button", but I want to change the type of the submit button to "submit". Does anyone know how I can achieve this? I've tried multiple ti ...

Is it possible to change the src attribute after the page has loaded and execute it

A. I'm trying to figure out how to dynamically change the src attribute of an image using JavaScript after the page has loaded. <img src="http://example.com/image.png" /> to <img src="http://domain.com/different.jpg" /> B. Another questi ...

Unusual behavior being exhibited by the background in an HTML5 canvas

I have an 800 by 100 image for the background and I'm trying to extract sections from it (like a sprite sheet) in order to create a background. I believe this method is efficient and allows me to experiment and learn how to generate backgrounds in the ...

"Utilizing a custom CSS style for a half heart rating system

I am currently working on implementing a rating system using the font-awesome fa-heart class. While I have successfully colored the full heart, I am facing difficulty in filling only the first half of the heart in red. Despite my research efforts, all solu ...

Reactjs Rendering problem with retrieving data from the backend in a popover

Take a look at the test environment where this problem is occurring https://codesandbox.io/s/nice-cache-kl12v My website design is being done with antd. Right now, I'm facing an issue where I need to display notifications to the user, which are acces ...

Clickability issue with searchbar results caused by onBlur event

My searchbar functionality includes showing results in a dropdown list when the user searches. However, I am facing an issue with the onBlur event that changes the display of the list to none when the user clicks away from the search box. The problem aris ...

What is the best way to apply a border-radius to the top of bars in @nivo/bar?

Is it possible to apply a border radius to the tops of each stack in a stacked responsive bar created from the Nivo library, without affecting the bottom? Currently, the responsive bar's border radius applies to both the top and bottom. Thank you! ...

Using App Storage on Windows Phone 8 with HTML5

Exploring storage options for my html5 app compatible with both windows 8 and windows phone 8. Considering: Local Storage IndexDB Looking for advice on the best storage mechanism to use. Is IndexDB compatible with windows phone 8? Any insights on quota ...

Using style binding and ngStyle doesn't appear to be effective for setting a background image within a DIV element in Angular5

After facing difficulties in customizing the spacing of Angular material cards, I decided to create my own cards. However, during this process, I encountered an issue where I needed to set an image as a background inside a div. Despite trying various CSS ...

Angular pop-up message not displaying title or content

I have integrated the toaster directive into my AngularJS web project. Following the documentation, I have declared the container on my view as shown below. <toaster-container toaster-options="{'time-out': 3000, 'close-button':true ...

Is it possible to trigger a reflow prior to initiating a lengthy JavaScript operation?

Ready to face the criticism, I understand that this question has been asked many times before, and I am aware that there are likely more efficient ways to achieve what I'm trying to do... In a JavaScript function, I have a process that can take up to ...

HTML: Mark the chosen hyperlink or tag

In my HTML page, I am looking to keep the link selected when it is clicked on. Here is the initial HTML code: <table class="main-dev"> <tr> <td> <a class='titleForm' style="cursor:pointer"> ...

Ways to display jQuery values as HTML text

Trying to concatenate the HTML text with the values of item.certificate_name has been a challenge for me. I've experimented with various methods, but none of them seem to work. The specific line I'm referring to is where I wrote . I have alread ...

What is the CSS method for determining the distance from the top of a container to the edge of the window?

I am working on a basic HTML layout that contains a few elements, including a scrollable div container located below them. Since the height of unknown-height is uncertain due to dynamic element generation, I need a simple method to enable scrolling in the ...

Issues with the functionality of the submit button (html, js, php)

Seeking assistance with a submit button functionality issue - I have a simple submit button that allows visitors to enter their email address. Upon clicking the button, it should add their email to a CSV file and display a pop-up thank you message. The pr ...