Issue with SVG mask not functioning when transform is applied

I am struggling to apply a mask to a transformed SVG element. When I try to do this with a path, the structure is the same. If I apply the mask to an element outside of the transformed group, it works as expected. However, if I try to do the same inside, the element simply disappears.

Here is the HTML:

<svg width="350pt" height="100pt" >
  <defs>
    <pattern id="circleFill" patternUnits="userSpaceOnUse" width="5" height="5" > 
      <circle cx="2" cy="2" r="2" fill="red"></circle>
    </pattern>
    <mask id="circleMask">
      <rect x="0" y="0" width="100%" height="100%" fill="url(#circleFill)" />
    </mask>
  </defs>

  <g transform="translate(0,150) scale(.1,-.1)">
    <rect class="holder" x="300" y="300" height="1000" width="1000"/>
    <rect class="main t" x="350" y="350" height="400" width="400" />
  </g>
  <rect x="300" y="50" height="50" width="50" class="main masked o" />
</svg>

CSS:

.holder {
    fill: darkgray;
    stroke: black;
    stroke-width: 2px;
}

.o{ fill: red; }
.t{ fill: purple; }
.masked{ mask: url(#circleMask); }

When I add the "masked" class to the second rect (with classes "main t"), it disappears.

The following structure does work:

<g class="masked">
  <g transform="...">
    <rect ... />
  </g>
</g>

However, I have over 50 elements in the image that should have the same transform, and only some of them should be masked (with 5 different masks).

Here is the fiddle link: Fiddle

What could I be doing wrong? Is it possible to mask an element inside a transformed group?

Answer №1

The challenge you are encountering when trying to apply a mask to a transformed SVG element lies in how SVG handles masking and transformations. According to the SVG specification, the mask-image property remains unaffected by the transform property.

As a result, when you apply a transformation to the group containing the masked element, the mask is no longer applied as expected. One way to address this issue is by utilizing the mask-position property, which enables you to define the position of the mask image in relation to the masked element. By setting the mask-position property to match the position of the transformed element, you can ensure proper application of the mask.

If you need to apply a mask to a transformed element with a viewBox (where an explicit viewBox is added), you can combine the mask-position property with the viewBox attribute. The viewBox attribute defines a user space rectangle that should be mapped to the bounds of the SVG viewport. While the viewBox attribute values are pre-transform coordinates, it is crucial to render and adhere to the post-transform coordinates.

In situations where you have over 50 elements in your image and wish to apply masks to specific ones, you can follow the same strategy. Just remember to adjust the mask-position property for each masked element to align with its transformed position.

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

Tips for fixing alignment problems using CSS

I am experiencing a problem where I am unable to figure out how to align the content in one column with text positioned next to it in a right-aligned format. /* Personal Details */ .personal-info { width: 90%; padding: 25px; border-bottom: 1px so ...

Button appears and disappears sporadically while browsing in Safari

I created a slider using SCSS, JavaScript, and HTML. You can view the demo at this link: https://jsfiddle.net/rr7g6a1b/ let mySlider = { initializeSlider: function (options) { let slider = options.container; let slides = slider.querySelectorAll( ...

Adding a border/stroke around a material-ui icon: a step-by-step guide

Is there a way to create a color outline around material-ui icons? I would like them to resemble the following: Instead of their current appearance: I've attempted using stroke and webkit-stroke without success. It seems to only add a border around ...

Styling content in a CSS file and then rendering it as a

My current project involves converting a webpage into a PDF file and I am using @page @bottom-left to insert text in the bottom left corner. @page {size: portrait A4; @bottom-left {content: "Some text to display at the bottom left. Some additional text ...

Problem with jQuery image gallery

Currently, I am in the process of creating a simple image gallery that enlarges an image when it is clicked. To achieve this functionality, I am utilizing jQuery to add and remove classes dynamically. $(".image").click(function() { $(this).addClass("b ...

Utilizing Material UI with Appbar logo aligned to the left and Tabs featured at the center

I am currently working on creating a react material ui AppBar. The design includes a logo and tabs, with the tabs supposed to be centered in the AppBar and the logo positioned on the left side. However, I have been facing difficulty in making the logo al ...

The list in the navbar is misaligned and not centered vertically within the flex container

I've been working with flex for a while now, but I'm struggling to vertically align the content of my navbar to the center in the code. <nav className="nav navbar"> <h3> Logo </h3> <ul className= &q ...

Position the Bootstrap Modal at the start of the designated DIV

When using a Bootstrap Modal to display larger versions of thumbnails in a photo gallery, there can be some challenges. The default behavior of Bootstrap is to position the modal at the top of the viewport, which may work fine in most cases. However, if th ...

Floating elements are misaligned and not laying out correctly

I've encountered an issue where the footer div is appearing behind the right-hand side div. I have a central container div with two floated divs next to each other, and the footer is separate. I've spent hours trying to fix it but can't figu ...

Pictures in Windows 7 have shifted to the left

After a recent migration to the Windows 7 operating system at my company, I encountered an issue. While using the same Visual Studio 2010 master file that was working fine on my Windows XP machine, I noticed that all the images below were now shifted to t ...

The class "slick" in <col class="slick"> does not add any styling or effects

My interpretation of the col element is that it can be used to assign a class to all elements in a column of a table. However, I am experiencing difficulties with this approach. While I am able to apply the class to individual td elements, I am looking for ...

Does Google Cloud CDN not store data from the storage bucket?

I've been experimenting with combining a Load Balancer, Cloud Storage, and CDN for the past few weeks. Unfortunately, I have not been successful in getting it to work as expected. I uploaded some static files (2 jpgs, svgs, and css files) to a multi- ...

The unique design of the list extends beyond the boundary line

In my current project, I am utilizing next.js and MUI (material-ui). The component I am working with has the following structure: <Paper> <List> .. </List> </Paper> One issue I am facing is that when the list is scrolled, the ...

What is the best way to incorporate a custom modal created with HTML/CSS into my Bootstrap website?

I am facing an issue with the modal implementation on my bootstrap website. The modal is supposed to open when a user clicks a button. Strangely, it was working perfectly fine on another HTML/CSS file, but once I added it to this Bootstrap file, it stopped ...

A div element featuring a border with transparent edges on the top right and bottom left corners

Does anyone know how to code the border for the upper right corner and lower left corner of the box (as shown in the image below)? I would really appreciate some help. Thank you in advance! This is the HTML <div class="carouselle"> <div clas ...

Looking for a solution to fix the VueJS calculator that only works once?

My calculator project using VueJS, HTML and CSS is almost complete. However, I'm facing an issue where it only works once. For example, if I input 6x3, it correctly gives me 18. But if I then clear the result and try to input a new calculation like 3 ...

Unusual issue with horizontal menu display in Firefox on Mac

I recently completed a website project using Contao for a client (visit www.medivas.de). Everything is working perfectly except for one issue: the last menu item in the main navigation (located at the top, above the slideshow) is causing a line break. This ...

variations in menu functionality on firefox

Could you please take a look at the links below? It appears that there may be an issue with FireFox. The green top menu seems to be displaying incorrectly. In Chrome/IE, it appears in one line, but in FF, the last two links are on the second line despite s ...

Restoring text boxes to their original styling

I've been working on a jQuery script that scans through form input elements and turns their border color to red if they are empty. When the submit button is pressed, it reassesses the elements; this time I'm trying to revert the textbox back to i ...

Trouble with CSS in a WordPress theme utilizing a two-column layout

After setting up my wordpress blog (http://raptor.hk), I encountered an issue with aligning the right sidebar correctly. It seems like I have overlooked something in the CSS code. The div responsible for the sidebar has been assigned the name "rightbar". I ...