Utilize CSS to selectively apply the mix-blend-mode property to targeted elements

In my project, I am aiming to utilize the mix-blend-mode property to give certain SVG paths the appearance of an erasing path by blending them with a background image on the stroke. However, I have encountered a challenge where the mix-blend property affects all paths instead of selectively targeting specific groups of elements. Here is the code snippet I have come up with:

g.erasing image{
mix-blend-mode:  lighten;
}
<html>

<body>

  <svg height="400" width="450">
    <!-- this is the background image -->
    <image id="background" xlink:href="https://s3-us-west-2.amazonaws.com/s.cdpn.io/3/Harry-Potter-1-.jpg" width="400" height="450"></image>

      <g class="drawing">
         <!-- these are the drawing paths -->
         <path  d="M 100 350 l 150 -300" stroke="red" stroke-width="8" fill="none" />
         <path  d="M 250 50 l 150 300" stroke="red" stroke-width="8" fill="none" />
         <path  d="M 175 200 l 150 0" stroke="green" stroke-width="8" fill="none" />
         <path  d="M 100 350 q 150 -300 300 0" stroke="blue" stroke-width="8" fill="none" />
      </g>


      <g class="erasing">  
          <!-- these are the erasing paths -->
         <path d="M 0 0 L 400 450" stroke="black" stroke-width="20"  />
         <path d="M 0 0 L 200 300" stroke="black" stroke-width="20"  />
         <image xlink:href="https://s3-us-west-2.amazonaws.com/s.cdpn.io/3/Harry-Potter-1-.jpg" width="400" height="450"></image>
      </g>

  </svg>

</body>
</html>

I am looking for a solution that will allow me to apply the mix-blend-mode property only to specific groups of elements while keeping other elements unaffected as illustrated in the following image: https://i.sstatic.net/JcuTK.png

Note: Although masking is a potential solution, it tends to be slow in some browsers.

Answer №1

To apply the effect of mix-blend-mode to specific elements, you can utilize the g element with the property isolation: isolate;.

circle{
  mix-blend-mode: lighten;
}
g{
  isolation: isolate;
}
<svg width="200px" height="200px">
  <rect width="100%" height="100%" fill="pink"/>
  <circle cx="100" cy="80" r="60" fill="red"/>
  <circle cx="70" cy="130" r="60" fill="green"/>
  <circle cx="130" cy="130" r="60" fill="blue"/>
</svg>
<svg width="200px" height="200px">
  <rect width="100%" height="100%" fill="pink"/>
  <g>
    <circle cx="100" cy="80" r="60" fill="red"/>
    <circle cx="70" cy="130" r="60" fill="#0f0"/>
    <circle cx="130" cy="130" r="60" fill="blue"/>
  </g>
</svg>

In this scenario, using the mask element might be more suitable.

g.drawing{
  mask: url(#erasing);
}
<svg height="400" width="450">
  <!-- background image -->
  <image id="background" xlink:href="https://s3-us-west-2.amazonaws.com/s.cdpn.io/3/Harry-Potter-1-.jpg" width="400" height="450"></image>
  <g class="drawing">
     <!-- drawing paths -->
     <path  d="M 100 350 l 150 -300" stroke="red" stroke-width="8" fill="none" />
     <path  d="M 250 50 l 150 300" stroke="red" stroke-width="8" fill="none" />
     <path  d="M 175 200 l 150 0" stroke="green" stroke-width="8" fill="none" />
     <path  d="M 100 350 q 150 -300 300 0" stroke="blue" stroke-width="8" fill="none" />
  </g>
  <defs>
    <mask id="erasing">
      <rect width="100%" height="100%" fill="white"/>
      <!-- erasing paths -->
      <path d="M 0 0 L 400 450" stroke="black" stroke-width="20"  />
      <path d="M 0 0 L 200 300" stroke="black" stroke-width="20"  />
    </mask>
  </defs>
</svg>

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

Menu button is expanded without the need to click

The Bootstrap 5 button extends the default behavior and does not collapse. Here is the code: <nav id="header-nav" class="navbar navbar-expand-lg navbar-light"> <div class="container"> <a class= ...

Tips for adjusting the Bootstrap grid layout for mobile viewing

As a beginner in web design and utilizing bootstrap, I am creating a website with two columns of size 6 each within a row. || Col-1 || Col-1 || [Web View] ||||||||||| || Col-1 || ||||||||||| || Col-2 || ||||||||||| [Mobile View] However, I would like th ...

Creating a unique post type in Wordpress

Currently, I am in the process of converting a Bootstrap one-page template into a WordPress template. My goal is to incorporate a custom post type that will display items in the services portfolio while maintaining the same CSS styling. Here is the code sn ...

How to create a thumbnail hover effect with CSS3 and javascript, overcoming the z-axis issue

Currently, I am working on creating a set of thumbnails that enlarge when hovered over. The initial setup achieves the zoom effect using CSS3 transform:scale and ease-in-out. However, the issue is that the enlarged images overlap each other due to sharing ...

Creating a unique look for unordered list items in a dropdown navigation

I'm currently working on creating a drop-down menu using nested unordered lists. The functionality of the menu is all set, but I'm running into some styling issues. The main link that triggers the drop-down should have a blue background with whit ...

Gorgeous Stew encounters a cautionary message before encountering a glitch in the middle of the

Currently, I am systematically going through every Wikipedia page related to a specific date (from January 1 to December 31). My primary focus is to extract the names of individuals who celebrate their birthday on that particular day. However, as I reach A ...

Encountered a parsing error when attempting to integrate SCSS with webpack and babel setup

I am facing an issue while trying to integrate SCSS into my webpack and babel setup. When running npm run build, I encounter an error that I'm unfamiliar with. As a beginner in using webpack and babel, I'm unsure about the necessary changes requ ...

Is there a way to convert various elements sharing the same class into a list of array items?

Essentially, I am dealing with multiple elements sharing the same class name. My goal is to retrieve an array of integers from an API and then iterate through each element with this class name, replacing them with elements from the array sequentially. For ...

Utilizing BeautifulSoup in Python to Retrieve Content Enclosed by Header Tags

Exploring ways to retrieve the storyline of a movie from its Wikipedia page using Python and BeautifulSoup, as I'm still getting acquainted with the whole process. The following contains the initial code snippet. <h2><span class="mw-headlin ...

Are desktop values taking precedence over the mobile media query?

Currently, I am facing an issue with aligning icons (images) under each title on a page that lists various classes. The icon needs to be centered below the title on both desktop and mobile versions of the site. However, I have encountered a problem where u ...

The left border consistently occupies any leftover space within the container

I have encountered an issue with my code where the border does not fill the empty space in the div container when there is only one item li present, but works fine if there are multiple items. The problem can be seen in the image below: https://i.sstatic. ...

Steps for removing the bottom border for the last child in the Material UI Box Component

I have a Box Component where ListItems are wrapped. For each child, I have a border-bottom of 1px solid class set for the Box component, but I don't want it for the last child. How can I achieve that? {data.map((item, i) => { return ...

Is it possible to transform the Bootstrap5 sidebar, currently positioned above the block content, into a sticky element on

After creating a Django blog using bootstrap5 and following guidance from the python crash-course book, I encountered an issue with my sidebar placement. Despite trying various methods, I struggled to "override" my blocks in order to keep my sidebar fixed ...

Arranging text within a td cell

This is what I currently have: <tr> <td colspan="4" style="font-size:7pt; font-color:red; font-weight: bold;"> PLEASE NOTE: SPECIFY THE ARTICLE IN AS MUCH DETAIL AS POSSIBLE </td> </tr> However, the text does not a ...

Ensure that the div stays in the same position regardless of the screen resolution

On my webpage, I have a page header with a select menu that needs to be properly positioned on top of the background image. How can I ensure it stays in place when the screen resolution changes? Here is how it looks on a larger resolution: <div c ...

What is the optimal location for storing numerous blocks of text (without utilizing a database)?

If I have a control used on 10 different pages with a help icon, each page needs to display its own unique paragraph of help text. The selection of which help text to show is determined by a JavaScript function. In my scenario, having 10 paragraphs will n ...

Adding data to the SharePoint RichText Control using the WinForms WebBrowser Control

I am struggling to modify the content of an SP Rich Text field using the WinForms web browser control. While changing the values of other controls is straightforward, I have encountered difficulties with Rich Text fields. I found some suggestions on this b ...

Tips for showcasing the contents of a file on a website

Greetings! I am a newcomer to the world of web development and I am seeking a method to showcase file content on a webpage. Presently, I have succeeded in loading text file content and displaying it in a large text box. However, I am now interested in di ...

Troubleshooting scope evaluation in AngularJS within style tags on IE9 not functioning

My issue involves a div block with a style attribute that includes left:{{left}}px; and right:{{right}}px. Even though $scope.left and $scope.right are being updated, my item still doesn't move as expected. I have created a fiddle to demonstrate th ...

Connect an existing function to JQuery animation

There is a function in place that changes the background image of a vertical website when a navigation menu item is clicked. <script type="text/javascript"> $(function() { $('a.panel-home').click(function(){ $("#bgimg").attr(' ...