If a second instance is hidden, SVG will appear as a mysterious black box

Two separate div elements each contain an identical SVG graphic.

If I hide the first div by setting it to "display: none", the SVG in the second div appears as a gray box.

This issue is present in both Firefox and Chrome browsers.

Any insights into why this is happening and possible workarounds?

Sample code provided below:

P.S. The SVG was provided by our designer for use in the project.

(EDIT: Using Chrome Version 64.0.3282.167 and Firefox 45.9.0. In the actual project, the SVG is imported as a React Component, so any modifications would affect both instances)

$("#button").click(function(){
  $("#firstDiv").css("display","none");
})

$("#button2").click(function(){
  $("#firstDiv").css("display","inherit");
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://code.jquery.com/jquery-3.1.0.js"></script> 

<button id="button">Hide First Div</button>
<button id="button2">Show First Div</button>
<div id="firstDiv">
<?xml version="1.0" encoding="utf-8"?>
<svg height="100" version="1.1" id="Layer_1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px"
 viewBox="0 0 20 20" style="enable-background:new 0 0 20 20;" xml:space="preserve">
<style type="text/css">
.st0{filter:url(#Adobe_OpacityMaskFilter);}
.st1{fill:#FFFFFF;}
.st2{mask:url(#mask-2_1_);}
.st3{fill:#666666;}
</style>
<title>Icon/Close Copy</title>
<desc>Created with Sketch.</desc>
<g id="Cross" transform="translate(-1397.000000, -1518.000000)">
<g id="Group-44" transform="translate(0.000000, 1495.000000)">
... (SVG content omitted for brevity) ...
</svg>
</div>
  <div>
<svg height="100" version="1.1" id="Layer_1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px"
 viewBox="0 0 20 20" style="enable-background:new 0 0 20 20;" xml:space="preserve">
<style type="text/css">
.st0{filter:url(#Adobe_OpacityMaskFilter);}
.st1{fill:#FFFFFF;}
.st2{mask:url(#mask-2_1_);}
.st3{fill:#666666;}
</style>
<title>Icon/Close Copy</title>
<desc>Created with Sketch.</desc>
<g id="Cross" transform="translate(-1397.000000, -1518.000000)">
<g id="Group-44" transform="translate(0.000000, 1495.000000)">
... (SVG content omitted for brevity) ...
</svg>
</div>

Answer №1

You must ensure that each element, even within svg containers, has a unique id. Duplicating id values is not allowed and they should be distinct from one another.

Take a look at the code snippet below:

$("#button").click(function(){
  $("#firstDiv").css("display","none");
})

$("#button2").click(function(){
  $("#firstDiv").css("display","block");
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://code.jquery.com/jquery-3.1.0.js"></script> 

<button id="button">Hide First Div</button>
<button id="button2">Show First Div</button>
<div id="firstDiv">

<svg height="100" version="1.1" id="Layer_0" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px"
 viewBox="0 0 20 20" style="enable-background:new 0 0 20 20;" xml:space="preserve">
<style type="text/css">
.st0{filter:url(#Adobe_OpacityMaskFilter0);}
.st1{fill:#FFFFFF;}
.st2{mask:url(#mask-2_1_1);}
.st3{fill:#666666;}
</style>
<title>Icon/Close Copy</title>
<desc>Created with Sketch.</desc>
<g id="Cross" transform="translate(-1397.000000, -1518.000000)">
<g id="Group-44" transform="translate(0.000000, 1495.000000)">
<g id="Icon_x2F_Close" transform="translate(1391.000000, 17.000000)">
<g id="Combined-Shape" transform="translate(16.000000, 16.000000) rotate(-315.000000) translate(-16.000000, -16.000000) ">
</g>
<defs>
<filter id="Adobe_OpacityMaskFilter0" filterUnits="userSpaceOnUse" x="-6.6" y="-6.6" width="45.3" height="45.3">
<feColorMatrix  type="matrix" values="1 0 0 0 0  0 1 0 0 0  0 0 1 0 0  0 0 0 1 0"/>
</filter>
</defs>
<mask maskUnits="userSpaceOnUse" x="-6.6" y="-6.6" width="45.3" height="45.3" id="mask-2_1_1">
<g class="st0">
<path id="path-1_1_" class="st1" d="M16,14.6l7.8-7.8l1.4,1.4L17.4,16l7.8,7.8l-1.4,1.4L16,17.4l-7.8,7.8l-1.4-1.4l7.8-7.8
L6.8,8.2l1.4-1.4L16,14.6z"/>
</g>
</mask>
<g id="Color_x2F_Dark-Grey" class="st2">

<rect id="Rectangle-2" x="0" y="0" transform="matrix(0.7071 -0.7071 0.7071 0.7071 -6.6274 16)" class="st3" width="32" height="32"/>
</g>
</g>
</g>
</g>
</svg>
</div>

<div>
<svg height="100" version="1.1" id="Layer_1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px"
 viewBox="0 0 20 20" sstyle="enable-background:new 0 0 20 20;" xxml:sspace="preserve">
<style type="text/css">
.st0{filter:url(#Adobe_OpacityMaskFilter);}
.st1{fill:#FFFFFF;}
.st2{mask:url(#mask-2_1_);}
.st3{fill:#666666;}
</style>
<title>Icon/Close Copy</title>
<desc>Created with Sketch.</desc>
<g id="Cross" transform="translate(-1397.000000, -1518.000000)">
<g id="Group-44" transform="translate(0.000000, 1495.000000)">
<g id="Icon_x2F_Close" transform="translate(1391.000000, 17.000000)">
<g id="Combined-Shape" transform="translate(16.000000, 16.000000) rotate(-315.000000) translate(-16.000000, -16.000000) ">
</g>
<defs>
<filter id="Adobe_OpacityMaskFilter" filterUnits="userSpaceOnUse" x="-6.6" y="-6.6" width="45.3" height="45.3">
<feColorMatrix  type="matrix" values="1 0 0 0 0  0 1 0 0 0  0 0 1 0 0  0 0 0 1 0"/>
</filter>
</defs>
<mask maskUnits="userSpaceOnUse" x="-6.6" y="-6.6" width="45.3" height="45.3" id="mask-2_1_">
<g class="st0">
<path id="path-1_1_" class="st1" d="M16,14.6l7.8-7.8l1.4,1.4L17.4,16l7.8,7.8l-1.4,1.4L16,17.4l-7.8,7.8l-1.4-1.4l7.8-7.8
L6.8,8.2l1.4-1.4L16,14.6z"/>
</g>
</mask>
<g id="Color_x2F_Dark-Grey" class="st2">

<rect id="Rectangle-2" x="0" y="0" transform="matrix(0.7071 -0.7071 0.7071 0.7071 -6.6274 16)" class="st3" width="32" height="32"/>
</g>
</g>
</g>
</g>
</svg>
</div>

If you intend to use it as a 'delete' icon, your svg appears to be overly complex. It is recommended to simplify it by removing masks, groups, styles, defs, etc. An optimized version can be like this:

svg {width:50px; fill:#777}
<svg viewBox="0 0 96 96"><path d="M96 14L82 0 48 34 14 0 0 14l34 34L0 82l14 14 34-34 34 34 14-14-34-34z"/></svg>

Answer №2

To optimize the page loading speed and reduce redundancy, you can avoid duplicating the cross SVG code within every <div>. Instead, define the SVG once and use the <use> element to reference it in each div.

$("#button").click(function(){
  $("#firstDiv").css("display","none");
})

$("#button2").click(function(){
  $("#firstDiv").css("display","block");
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://code.jquery.com/jquery-3.1.0.js"></script> 


<button id="button">Hide First Div</button>
<button id="button2">Show First Div</button>
<div id="firstDiv">
  <svg width="100" height="100" id="close" viewBox="0 0 20 20">
    <use xlink:href="#Cross"/>
  </svg>
</div>

<div>
  <svg width="100" height="100" id="close" viewBox="0 0 20 20">
    <use xlink:href="#Cross"/>
  </svg>
</div>


<!-- hide the referenced SVG on the page by setting its width and height to 0 -->
<svg width="0" height="0" viewBox="0 0 20 20">
<style type="text/css">
.st1{fill:#FFFFFF;}
.st2{mask:url(#mask-2_1_1);}
.st3{fill:#666666;}
</style>
<g id="Cross" transform="translate(-1397.000000, -1518.000000)">
<g id="Group-44" transform="translate(0.000000, 1495.000000)">
<g id="Icon_x2F_Close" transform="translate(1391.000000, 17.000000)">
<g id="Combined-Shape" transform="translate(16.000000, 16.000000) rotate(-315.000000) translate(-16.000000, -16.000000) ">
</g>
<mask maskUnits="userSpaceOnUse" x="-6.6" y="-6.6" width="45.3" height="45.3" id="mask-2_1_1">
<g class="st0">
<path id="path-1_1_" class="st1" d="M16,14.6l7.8-7.8l1.4,1.4L17.4,16l7.8,7.8l-1.4,1.4L16,17.4l-7.8,7.8l-1.4-1.4l7.8-7.8
L6.8,8.2l1.4-1.4L16,14.6z"/>
</g>
</mask>
<g id="Color_x2F_Dark-Grey" class="st2">
<rect id="Rectangle-2" x="0" y="0" transform="matrix(0.7071 -0.7071 0.7071 0.7071 -6.6274 16)" class="st3" width="32" height="32"/>
</g>
</g>
</g>
</g>
</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

Can one create a set of rest arguments in TypeScript?

Looking for some guidance on working with rest parameters in a constructor. Specifically, I have a scenario where the rest parameter should consist of keys from an object, and I want to ensure that when calling the constructor, only unique keys are passed. ...

Tips for retaining the selected radio button in a Java web application even after a page refresh

As someone new to web development using the Stripes Framework, I am encountering an issue with radio buttons on a webpage. When one of the radio buttons is selected, a text box and Submit Button appear. After clicking the Submit button, the functionality ...

Displaying JSON data within a div section using Ajax and jQuery is not possible

I have generated a JSON in a specific format from an external PHP file: [ { "title": "Welcome!", "description": "The world has changed dramatically..", "image": "img/strawberry-wallpaper.jpg" } ] I am trying to use this data to populate a par ...

CSS3 List Style Floating Challenge

I have encountered a seemingly simple question, but I am struggling to find a solution. In my HTML code, I have two ul elements. Each li within these lists is styled with float:left in my CSS. The reason behind this styling choice is that I am working on ...

Bootstrap 4 - DropDown option - Element is positioned above the navigation bar

Currently facing an issue with the DropDown menu. Whenever I add padding to the DropDown menu (class - drop_link), it ends up pushing the entire element over the <nav>. I'm unsure of how to prevent this from occurring. I attempted to disable som ...

UCS-2 in Node.JS: Understanding Big-Endian Byte Order

Currently, I am utilizing Node.JS. In my project, I require support for big-endian UCS-2 buffers, which is not natively offered by Node's buffers that only support little-endian format. How can I achieve this specific requirement? ...

Utilizing Javascript to retrieve the current Controller and Action in Rails

Is it possible for JavaScript to determine the current controller and action in Rails? I had the idea of creating a hidden container to store the current controller and action, but I'm curious if there is a specific JavaScript function for this purpo ...

Verify optional chaining support in Angular app for browsers

Within my Angular application, I am looking to verify if a client's browser supports optional chaining (es2020) in order to load a library that contains both a modern ES version and a legacy one. The issue arises when the Angular compiler (which I su ...

Jssor Slider: Captions briefly overlap just as the slideshow begins to play

I am currently working on a slideshow with 3 caption-only slides. Here is the code snippet: <div id="sliderHeader_container" style="position: relative; width: 965px; height: 85px;"> <!-- Slides Container --> <div u="slides" style=" ...

Generate several invoices with just a single click using TypeScript

I'm interested in efficiently printing multiple custom HTML invoices with just one click, similar to this example: https://i.sstatic.net/hAQgv.png Although I attempted to achieve this functionality using the following method, it appears to be incorr ...

Can you employ a right-side offset in Bootstrap 4?

Is it possible to align a button with a textbox using the Bootstrap 4 layout system? https://i.sstatic.net/tN9II.png <link href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" rel="stylesheet"/> <div ...

The state object in Next.js appears to be missing

const [ values , setValues ] = React.useState({ input_type: '', elements: [] }) const addOption = () => { let newElements = values.elements newElements.push({ type: "option", ...

Error 404: This page seems to have gone missing. Django and react-router

Struggling to integrate reactjs and react-router (1.x) with my Django project. I'm finding it challenging to make everything work together seamlessly. For more details, you can check out the project on GitHub: https://github.com/liondancer/django-che ...

Is it possible to nest an HTML <span> inside a label tag in a Rails form_for?

Is it possible to nest a span element inside a form_for label tag? I am trying to achieve this in order to style a specific part of the label using CSS, such as making the text red. While it seems like valid HTML based on my research, it is causing some is ...

Challenge with the contrast of text color on dark mode with a backdrop of light color texture (image)

Currently, I am using Bootstrap 4 and I have set a light coloured .png image as the background for the navbar and body. The footer area has a dark background with off-white text colour. By default, the text colour is black to complement the light-coloured ...

What could be the reason for the Javascript function failing to run?

I need assistance in executing a function called "send()" which includes an AJAX request. This function is located in ajax.js (included in the code snippet) The Ajax success updates the src attribute of my image. The function seems to be working correctly ...

Ways to conceal a div using jQuery when the video source is missing

If there is no source in the video, I want to hide the video_wrapper class <div class="video_wrapper" style="width: 100%; height: 100%; display: none;"> <video id="df-video" playsinline="" webkit-playsinline="" style="height: 100%; width: 100%; ...

When using AutoComplete in MUI, I encountered an issue while attempting to assign a default value to the checkbox from an API. Instead of achieving the desired result, I received an error stating "(inter

Within this snippet, I am seeking to retrieve a default value from the API to populate a checkbox initially. I have employed the Material-UI Autocomplete component, which includes a defaultValue prop. Despite my efforts to utilize this prop, I am encounter ...

Guide on aligning text beneath and to the side of an image, and positioning them in the same row using CSS Bootstrap 4

I am looking to organize two different texts alongside and under an image within a single article. I also want these two articles to be displayed in the same row when the window size is col-md or larger. https://i.sstatic.net/ZjigH.jpg The second article ...

Enhance your website's accessibility by using JavaScript to allow the down and up arrow keys to function

Thank you for taking the time to read this, any feedback is greatly appreciated. The current script on my website is only partially functional (click "i" in the bottom right corner). Currently, the script will focus on the first link AFTER pressing the T ...