Hiding the SVG element with pattern definitions using the 'display: none' property makes the patterns completely disappear from view

When I include a set of SVG definitions at the top of my body, I encounter an issue where the svg tag with zero height and width creates unnecessary space at the beginning of the document. To resolve this, I initially use display:none, but this leads to problems when referring back to these patterns later in the code.

<body>
<svg width="0" height="0" version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" >
<defs>
<g id="diamond">
  <path d="M50,7 L96,100 L50,193 L4,100 L50,7 Z"></path>
</g>
<g id="oval">
  <path d="M3,150 L3,50 C3,24.084 24,3 50,3 C76,3 97,24 97,50 L97,150 C97,176 75.916,197 50,197 C24.084,197 3,176 3,150 Z"></path>
</g>
<g id="squiggle">
  <path d="M25,77 C25,48 4,45 4,25 C4,14 18,3 40,3 C66,3 92,21 92,54 C92,86 75,100 75,123 C75,152 96,155 96,175 C96,186 82,197 60,197 C34,197 8,179 8,146 C8,114 25,100.02465 25,77 Z"></path>
</g>
<pattern id="triangle" width="10" height="10" patternUnits="userSpaceOnUse">
    <polygon points="5,0 10,10 0,10"></polygon>
</pattern>
<pattern id="green-stripes" x="10" y="10" width="10" height="10" patternUnits="userSpaceOnUse" style="display:inline-block;">
  <rect x="0" y="7" width="10" height="10" style="stroke: 1px; fill: green;"></rect>
</pattern>
<pattern id="red-stripes" x="10" y="10" width="10" height="10" patternUnits="userSpaceOnUse" style="display:inline-block;">
  <rect x="0" y="7" width="10" height="10" style="stroke: 1px; fill: red;"></rect>
</pattern>
<pattern id="purple-stripes" x="10" y="10" width="10" height="10" patternUnits="userSpaceOnUse" style="display:inline-block;">
  <rect x="0" y="7" width="10" height="10" style="stroke: 1px; fill: purple;"></rect>
</pattern>
</defs>
</svg>
<div class="topbar"></div>
<div id="game">
    <div class="cardrow">
    <div class="card purple-card" id="oqFqnSBH6zMRc5hJr" width="100" height="200">
  <svg width="100" height="200" z-index="-40" style="display:inline-block">
    <use xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="#diamond" style="stroke: purple; fill: none;"></use>
  </svg>
  <svg width="100" height="200" z-index="-40" style="display:inline-block">
    <use xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="#diamond" style="stroke: purple; fill: none;"></use>
  </svg>
  </div>
 <div class="card red-card" id="xPibDdNYe2vAgDYKK" width="100" height="200">
  <svg width="100" height="200" z-index="-40" style="display:inline-block">
    <use xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="#diamond" style="stroke: red; fill: none;"></use>
  </svg>
  <svg width="100" height="200" z-index="-40" style="display:inline-block">
    <use xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="#diamond" style="stroke: red; fill: none;"></use>
  </svg>
  </div>    
    <div class="card purple-card" id="MgATXd9RTv52MKFtp" width="100" height="200">
  <svg width="100" height="200" z-index="-40" style="display:inline-block">
    <use xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="#diamond" style="stroke: purple; fill: url(#purple-stripes);"></use>
  </svg>
  <svg width="100" height="200" z-index="-40" style="display:inline-block">
    <use xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="#diamond" style="stroke: purple; fill: url(#purple-stripes);"></use>
  </svg>
  </div>
    <div class="card red-card" id="mzpSYNnFXFX9SjPLa" width="100" height="200">
  <svg width="100" height="200" z-index="-40" style="display:inline-block">
    <use xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="#oval" style="stroke: red; fill: none;"></use>
  </svg>
  <svg width="100" height="200" z-index="-40" style="display:inline-block">
    <use xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="#oval" style="stroke: red; fill: none;"></use>
  </svg>
  <svg width="100" height="200" z-index="-40" style="display:inline-block">
    <use xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="#oval" style="stroke: red; fill: none;"></use>
  </svg>
  </div>
  </div>
</div></body>

http://jsfiddle.net/vahkogdk/

Answer №1

To prevent taking up unnecessary space with the definition svg, consider replacing display:none with position:absolute:

    <svg width="0" height="0" version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" style="position:absolute">

http://jsfiddle.net/vahkogdk/1/

Answer №2

One simple solution is to place the svg element within the head tag.

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

In my current Next.js project, I tried setting up a new [collectionId].jsx file within the pages directory, but I am facing issues with getting

In my current next.js project, I recently created a file named [collectionId].jsx within the pages directory. Interestingly, I noticed that Tailwind CSS does not seem to work properly with this file. However, when I renamed the file to [collectionId].js wi ...

I am not familiar with this HTML element, it's a mystery to me

As I develop a data-recollection view in HTML, I've recognized the necessity of creating something similar to this: To elaborate, it's an image created hastily, with an input where users can enter data. When they click the "+" button, another in ...

implementing jqBarGraph on my webpage

Hey there! I have been trying to add a graph to my HTML page for quite some time now. After doing some research, I discovered that using the jqBarGraph plug-in makes it easier to create the desired graph. Below you will find the code that I have on my webp ...

Identify the month in which there is no data available in the database

The existing query is working perfectly, but the issue is that there is no data for the current month (April), causing it not to list the month with 0 interviews. How can I make it so that the month is listed with 0 interviews? QUERY select distinct from ...

Animate.css bxSlider text animations

As I am in the process of setting up a slider for my website, I have encountered some issues. I am utilizing bxslider and wished to incorporate animations for the text on each slide by integrating animate.css. Animate.css is quite simple: just add "animat ...

Is it possible to assign numerical values to attributes in HTML code?

I'm unsure about something - is it possible to set an attribute value as a number? For example: <div data-check="1"></div> Is this the correct way to do it or not? I've heard conflicting opinions, with some people saying you shouldn ...

Utilize Express.js to load a random HTML page

Hey there, it's Nimit! I could really use some assistance with my code. I'm trying to figure out if it's possible to load different HTML pages on the same URL like www.xyz.com/home/random. Can more than one HTML page be randomly loaded? I ...

Having trouble with HTML on Eclipse for Android development?

There seems to be an issue with the HTML code in this program. When attempting to use x or any other HTML code, it does not format correctly. This is unusual as HTML has worked in the past. The problem occurs with the first string array and not with the ...

How can I position text below a ticked checkbox?

Having an issue with my HTML and jQuery code. Everything is working fine, but when I click on a checkbox, the text "FINISHED" appears below all checkboxes instead of just the one I clicked on. This is my html code: $('.label__checkbox').cli ...

Issue with displaying PDF files on Google Chrome due to a software glitch

Recently, I encountered a puzzling issue on Google Chrome. I am not sure if the error lies with Google or within my code. When I set the div as display: none; and then show it again, the PDF view only shows a grey background. However, if I zoom in or out, ...

What is the best method for binding variables in Vue.JS using moustache syntax and passing parameters to them?

If I have an HTML structure like this: <div> {{ CUSTOM_MESSAGE }} </div> And in my data: data() { return { CUSTOM_MESSAGE: 'Hey there! This message is for {{name}} from {{location}}' } } Is there a way to dynamically p ...

Substitute HTML data attributes

Hey everyone, I'm wondering how to change a data attribute in my code. For example, I am currently using a script for a video background, but I want to replace the video with a lighter version when viewed on a mobile device. In my JavaScript file, I h ...

Vertical Positioning of Tabs in Materialize CSS

My current challenge involves creating vertical tabs using materialize CSS, specifically in regards to positioning. The desired outcome is for the content to align at the same level when clicking on TAB 3. An example of the expected result can be seen in t ...

What is the best way to eliminate the bottom border of an input field?

Seeking advice on how to eliminate the border-bottom of an input field when typing starts. .car-list-input { font-family: 'Source Sans Pro', sans-serif; border-radius: 3px; font-size: 14px; font-weight: 400 !important; height: 35px; ...

Border extends across two separate rows

Let me illustrate my issue with a specific example where I am utilizing a single column with a rowspan: <table border="1" style="width:300px"> <tr> <td rowspan="2">Family</td> <td id="jill">Jill</td> <td>Smi ...

In Laravel, the text-overflow:ellipsis property fails to function properly when trying to display unescaped data

Encountering an issue with the property text-overflow:ellipsis. The ellipsis at the end of the truncated text is not showing up as expected. To maintain formatting without escaping data in ckeditor, I am using: {!! $treatment->description !!} speci ...

Modify CSS using JavaScript at a specific screen size

Currently, I am in the process of designing a responsive navigation system which involves implementing a button that dynamically changes the styling of the div #navigation using Javascript. The aim is to toggle between display: none; and display: block; ba ...

The CSS effect fails to take effect following an AJAX response

After a successful ajax response, I am attempting to apply a jQuery .css effect. However, it seems that this process is not working as expected because I am trying to retrieve the height of a newly created element and then applying it to another newly crea ...

Using multiple foreach loops within each other

After struggling with this issue for days, I've decided to seek some assistance :) I'm currently working on a project management tool that includes an edit-page where admins can modify project members and their roles within the project. My goal ...

Initial page load experiencing issues with paroller.js functionality

Upon hard refreshing the page in the middle section, there seems to be an issue with the paroller js transform: translateY(); CSS effect not functioning properly on the <div class="col col-6" data-paroller-factor="0.4" data-paroller-type="foreground" da ...