What could be the reason my SVGs are not displaying within the def group?

I am encountering a strange behavior that I cannot explain. I utilized grunt svg sprite to generate an SVG sprite containing all of my SVG files from a specific directory.

The structure of the sprite looks like this:

<svg width="0" height="0" style="position:absolute">
  <defs>
     //my svgs
  </defs>
</svg>

Here is a fiddle showcasing the issue: https://jsfiddle.net/swf8rtog/

The original intention was to import these SVG groups inline within the body tag to avoid an additional request for an icon font. By using

use xlink:href="#id_of_the_original_svg"
, you can reference an SVG icon for multiple uses. A functional example can be seen on this page:

I attempted to style the SVGs using CSS, applying attributes such as width, height, display: block, color, fill, etc., but nothing seemed to work.

Interestingly, when not loaded inside a "def" group, the icons appear correctly: https://jsfiddle.net/vtnLnsrh/

So, what could be causing this issue? What am I overlooking?

Answer №1

<defs are typically hidden from view as they contain definitions to be used later.

According to MDN

SVG allows graphical objects to be defined for later reuse. It is recommended that, wherever possible, referenced elements be defined inside a defs element. Defining these elements inside of a defs element promotes understandability of the SVG content and thus promotes accessibility.

Graphical elements defined in a defs will not be directly rendered. You can use a <use> element to render those elements wherever you want on the viewport.

<svg>
  <use xlink:href = "#star"/>
</svg>

<svg style="display:none">
  <defs>
    <svg viewbox="0 0 18 28" id="angle-down">
      <path d="M16.797 11.5q0 .203-.156.36l-7.28 7.28q-.156.156-.36.156t-.358-.156l-7.28-7.28q-.157-.157-.157-.36t.156-.36l.782-.78q.156-.156.36-.156t.358.156L9 16.5l6.142-6.14q.156-.156.36-.156t.358.156l.78.78q.157.157.157.36z" />
    </svg>
    <svg viewbox="0
    ... (remaining SVG icons removed for brevity)
    </svg>
    </svg>
    </defs>
</svg>

<svg>
 <use xlink:href="#star" />
</svg>

Note: The SVG holding the defs will take up space on the page so it's customary to hide it with display:none.

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

Ways to center Bootstrap panel in the middle of a webpage

Is there a way to center this entire panel on the web page? I could use some guidance with this. Does anyone have recommendations for a good book to learn Bootstrap design? <div class="panel panel-default" style="max-width:500px;margin-left:auto;margi ...

Switch the video source using JavaScript

I've been experimenting with creating a custom dropdown (ul li list) that allows users to select a different video to play, which is then loaded into the background of the page. Once the user clicks on the dropdown, it should switch to a different vi ...

I have to define a specific identifier in Django so that I can easily incorporate it into Bootstrap later on

I am working on creating a portfolio in Django. I have a section in my HTML code that connects to my jobs list in Django using {% for ... %} in order to display images, summaries, and titles of my projects. However, there is an ID within this div that refe ...

Executing JavaScript code does not execute CSS code

Having some trouble with my Javascript and CSS. The script is functioning well, but when I use the filtering function, the CSS stops working and only the filtered results are displayed on the HTML page. Looking for advice on how to fix the Javascript. & ...

Conceal button divider on pager in Jqgrid

Within my grid setup, there are 3 buttons placed in the pager section: 'Refresh', 'Constution', and 'Developed'. These buttons are separated by two vertical navSeparators. Upon grid load, the 'Developed' button is hi ...

The function designed to create in-line TailwindCSS classNames may work inconsistently in React and NextJS environments

Currently, I am utilizing TailwindCSS to style a web application. One frustrating aspect is that when attempting to add before: and after: pseudo-elements, it must be done individually. For instance, take the navigation drop-down button styling: <span ...

Unexpected appearance of DOM elements

I've come across an unusual bug in a web application I've been developing that is reproducible in Chrome and Safari, but not in Firefox. To witness the bug, go to www.lastcalc.com and type a single uppercase letter. The letter will be immediatel ...

Constructing a horizontal slider using vertically floating divs

Struggling to create a basic horizontal image slider using overflow:hidden and floating divs. Despite efforts, the divs keep stacking vertically instead of horizontally. Numerous online examples have been attempted without success. HTML: <div id="slid ...

The process for changing the textContent to X when an image is clicked

How can I create a function that changes the text content to 'X' when an image is clicked? I already have a function that updates the title based on the image dataset, but combining the two functions has been unsuccessful. Can someone help me con ...

What is causing the left scroll to not work with negative values?

My design features three blocks stacked on top of each other with an additional block at the bottom below the middle. Left <--------> Middle<---------->Right -----------------Bottom------------------ I have a couple of queries. Why is scr ...

The HTML form is appearing properly, however, it is failing to submit

Recently, I made some changes to a form that was previously working fine, but now it refuses to submit. Initially, I suspected the problem was caused by adding a Select field. However, even after removing it, the form still doesn't submit. Upon furth ...

Assign a class to a dynamically loaded element on a webpage

As I work on developing my website, I am facing an issue that I need help with. My header is stored in a separate HTML document and it looks like this: <div class="topnav" id="myTopnav"> <a href="Index.html" id=" ...

How can we make Internet Explorer 11 mimic Internet Explorer 9 in HTML?

How can I ensure that Internet Explorer 11 uses CSS styles from IE9 in my HTML? I have tried the following code: <script runat="server"> private void Page_PreRender(object sender, EventArgs e) { var meta = new HtmlMeta() ...

The timeline aesthetic can be achieved solely through the use of HTML, CSS, and potentially some Bootstrap functionalities

https://i.sstatic.net/EsDAE.png Up to this point, I've only been able to make it this far. I'm using a sample code from w3school https://i.sstatic.net/G1FcQ.png I've been attempting to get the year in the same position as in the image, bu ...

HTML: What is the functionality of the reset button?

What is the inner workings of the reset button (input type="reset)? (I am interested in enhancing it to clear inputs after a post in ASP.NET MVC.) ...

Dynamically align text in the center of an image, vertically

I'm trying to create a hover effect where an image and text are displayed in the center of the image when someone hovers over it. Here is how the HTML looks: <article> <div class="entry-content"> <a href="http://www.linktopost.com"> ...

How to modify the styling of an input element in React without directly manipulating the input itself

I have collected responses from a survey I created and now I want to showcase the results. The responses are rated on a scale from 1 to 5, and I would like to display them similar to the screenshot. Each number should be presented within a square, with ...

Reposition the navbar-brand

I need to adjust the position of my navbar-brand slightly to the right as it is currently overlapping with another button. How can I achieve this in my HTML code? Here's a snippet: <nav class="navbar navbar-expand-lg navbar-light bg-light"> ...

Tips for aligning numerous rows in a single column using Bootstrap

I have a col div with 3 row divs that I am trying to center within the col div. Currently, they are aligned to the left side of the col div as shown in the image. I am struggling to figure out how to place them in the center of the col div. https://i.ssta ...

Is there a term similar to "Rise above the Rest" in the world of Web Development?

Hey, I've encountered a new issue right now. Currently, I have two elements that are fixed to the top and bottom of the page. However, the elements in between them are overlapping the top element. Even though I tried keeping both elements fixed, th ...