SVG failing to display properly

I need to place a close button in the top right corner of my page.

Here is the code for the SVG:

.icon {
    width: 24px;
    height: 24px;
    /* any changes to the fill attribute have no effect */
    fill: currentColor;
}
<svg class="icon">
<use xlink:href="#close" />
</svg>

<!-- additional code -->

<!-- SVG Icon Library -->
<svg style="display: none;">
<symbol id=close viewBox="0 0 24 24">
    <path d="M19 6.41 17.59 5 12 10.59 6.41 5 5 6.41 10.59 12 5 17.59 6.41 19 12 13.41 17.59 19 19 17.59 13.41 12z"></path>
</symbol>
</svg>

The close icon is not appearing on the page.

However, I have another example with a different icon that does work...

.icon {
    width: 24px;
    height: 24px;
    /* any changes to the fill attribute have no effect */
    fill: currentColor;
}
<svg class="icon">
<use xlink:href="#info-outline" />
</svg>

<!-- additional code -->

<!-- SVG Icon Library -->
<svg style="display: none;">
<symbol id=info-outline viewBox="0 0 24 24">
    <path d="M11 17h2v-6h-2v6zm1-15C6.48 2 2 6.48 2 12s4.48 10 10 10 10-4.48 10-10S17.52 2 12 2zm0 18c-4.41 0-8-3.59-8-8s3.59-8 8-8 8 3.59 8 8-3.59 8-8 8zM11 9h2V7h-2v2z"></path>
</symbol>
</svg>

Both examples are similar, and it's puzzling why the close icon is not showing up.

I have tried adjusting the size and fill attribute of the SVG, but it seems to have no impact. The Dev-Tools confirm that the icon has a normal size. Other SVG icons work as expected, so there must be something specific to the close icon causing the issue.

Answer №1

If you're looking for standard, well-known icons that are not original or designer-made, consider using a Google character font.
You can check out and choose from a variety of Google icons here.

Material Icons come in five styles with different downloadable sizes and densities, all based on the core Material Design principles.

Select the desired icons and add them to your HTML by their names - just click on the icon name info and then copy the code.
Here's an example showing how to use the close and info icons along with adding color change effects when hovering over them.

.material-symbols-outlined {
  margin: 10px;
  transition: transform 0.4s ease-in-out;
}
.material-symbols-outlined:hover {
  transform: scale(2);
  color: red;
}
<link rel="stylesheet" href="https://fonts.googleapis.com/css2?family=Material+Symbols+Outlined:opsz,wght,FILL,GRAD@48,400,0,0" />
<span class="material-symbols-outlined"> close</span>
 <span class="material-symbols-outlined">info</span>

Answer №2

When working with SVGs, I prefer to create an image instead of directly embedding the SVG code in the HTML. This approach simplifies handling the viewBox property and allows me to easily import the SVG into my project for use in either CSS or HTML. In my CSS, I simply set the width and height as needed. Here's an example:

body {

  margin: 0;
  height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;

}

#MySQL {

  width: 200px;
  height: 200px;

}
<!DOCTYPE html>
<html lang="en" theme="">
<head>
   <meta charset="UTF-8">
   <meta http-equiv="X-UA-Compatible" content="IE=edge">
   <meta name="viewport" content="width=device-width, initial-scale=1.0">
   <link rel="stylesheet" href="style.css">
   <title>Example</title>
</head>
<body>

   <img src="https://svgshare.com/i/qCH.svg" alt="MySQL" id="MySQL">

</body>
</html>

Answer №3

Welcome to the year 2023!

Say goodbye to Internet Explorer as it is officially dead.

All modern browsers now fully support native JavaScript Web Components
(and have been doing so for the past 5 years! ever since Edge made the switch to Chromium)

No more need for old-fashioned <symbol> tricks

We can now use semantic HTML to display SVG icons

   <svg-icon></svg-icon>
   <svg-icon is="info"></svg-icon>
   <svg-icon is="close"></svg-icon>

This will output:

The beauty of it all is that defining a Custom Element is no longer time-sensitive. All previously created (but undefined) tags will automatically upgrade themselves.

customElements.define("svg-icon", class extends HTMLElement{
  connectedCallback(){
    let size = "148px";
    Object.assign( this.style , {
      width         : size,
      height        : size,
      display       : "inline-block",
      cursor        : "pointer"
    });
    let dpath = {
      "close" : "M19 6.41 17.59 5 12 10.59 6.41 5 5 6.41 10.59 12 5 17.59 6.41 19 12 13.41 17.59 19 19 17.59 13.41 12z",
      "info"  : "M11 17h2v-6h-2v6zm1-15C6.48 2 2 6.48 2 12s4.48 10 10 10 10-4.48 10-10S17.52 2 12 2zm0 18c-4.41 0-8-3.59-8-8s3.59-8 8-8 8 3.59 8 8-3.59 8-8 8zM11 9h2V7h-2v2z"
    }[ this.getAttribute("is") || "info" ];
    this.innerHTML = `<svg viewBox="0 0 24 24"><path d="${dpath}"/></svg>`;
  }
})
svg-icon[is="close"] {
    fill: red;
}
svg-icon:hover {
    fill: green;
}
<svg-icon></svg-icon>
<svg-icon is="info"></svg-icon>
<svg-icon is="close"></svg-icon>

Answer №4

There are a number of issues that need attention:

  • The ID attributes should not be enclosed in quotes (though most Firefox and Chromium browsers are forgiving).
  • currentColor must be specified either on the <symbol> or <path> level to avoid fill colors defaulting to black.
  • It's important to double check IDs for any duplicates.

.icon {
    width: 24px;
    height: 24px;
    fill: currentColor;
}


p{
  color:red
}

.green{
  color:green
}
<p><svg class="icon">
    <use href="#close" />
  </svg>
</p>
<p class="green">
  <svg class="icon">
    <use href="#info-outline" />
  </svg>
</p>


<!-- SVG Icon Library -->
<svg style="width:0; height:0;">
  <symbol id="close" viewBox="0 0 24 24" fill="currentColor">
    <path d="M19 6.41 17.59 5 12 10.59 6.41 5 5 6.41 10.59 12 5 17.59 6.41 19 12 13.41 17.59 19 17.59 13.41 12z" />
  </symbol>
  <symbol id="info-outline" viewBox="0 0 24 24" fill="currentColor">
    <path d="M11 17h2v-6h-2v6zm1-15C6.48 2 2 6.48 2 12s4.48 10 10 10 10-4.48 10-10S17.52 2 12 2zm0 18c-4.41 0-8-3.59-8-8s3.59-8 8-8 8 3.59 8 8-3.59 8-8 8zM11 9h2V7h-2v2z" />
  </symbol>
</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

Tips on using CSS to hide elements on a webpage with display:none

<div class="span9"> <span class="disabled">&lt;&lt; previous</span><span class="current numbers">1</span> <span class="numbers"><a href="/index/page:2">2</a></span> <span class="num ...

The sass compiler has produced two separate CSS files, but I am only able to link one of them to the index.html file

Everything was going smoothly with my SASS compiler. The styles were reacting perfectly in the live web server. But then, out of nowhere, it stopped working. Upon closer inspection, I realized that the extension had created a new CSS file. Instead of compi ...

Utilizing Bootstrap 4: The Use of an anchor tag in a button element

I am facing a situation where I have an icon represented by the HTML code <a>, which acts as a link redirecting to a different page. This <a> is nested inside a collapsible button, meaning that clicking on the icon expands the menu before actua ...

Leveraging Selenium to extract text from a dynamically populated DIV using JavaScript

I am currently utilizing Selenium to automatically retrieve all comments from a New York Times article. Once the comments are loaded, my goal is to extract them and save the information for future use. However, upon inspecting the source code of the articl ...

How can we modify this function to interpret multiple selections at once?

For the task of displaying multiple selections from a scrolling list to an alert, I have implemented the following function: var toppings = ""; function displaySelectedToppings() { var topList = document.getElementById('to ...

Failure to highlight items when using the multiple select function

After adding a select all button to a multiple select, I encountered an issue. Although all the items are being selected, they are not highlighted when clicking on the select all button. Below is the code snippet: <div class="label_hd">Profiles* {{ ...

Screen readers are unable to "read" text that is identified through aria-describedby

When enhancing my web application for accessibility, I encountered a challenge. I have implemented two required fields in a form that should display separate error messages as spans. To accomplish this, I am utilizing aria-invalid to signal when the fields ...

Issue with border-color in CSS on Chrome tables

Here is a table style I have defined for selected elements: tr[selected] { background: #FFF; border-color: #000000; color: #000000; } To apply this style, I use JavaScript: $this.unbind().change(function () { element = $(this).parent ...

The text within my div is spilling over the edges despite my attempts to use text align and overflow properties

body { font-family: Arial; font-size: 15px; } .container { position: relative; max-width: 1800px; height: auto; margin: 0 auto; text-align: center; width: 90%; } .container img { vertical-align: center; } .container .content { pos ...

Utilizing ng-repeat to loop through a div element that consists of various nested tags

I need to display multiple tabs, with each tab having a table where the values are populated from a database. The number of tabs is dynamic and fetched from another page of the application. All tables have the same structure but different values. How can I ...

Can anyone explain why my navbar text is not aligning in the center?

I am struggling to center the text in one of my two navbars. As a newcomer to both navbars and bootstrap, I am having difficulty figuring out why I can't achieve this. Should I be making changes on the bootstrap side or can it be fixed within the < ...

What is the best method to showcase an array representing a key-value pair enclosed in {} as a list item within VueJS?

I have a specific object structure with a key that contains an array as its value. How can I present this information to the user in a list format? allComponents: [ {name: 'Standard field', uses: ['Inconsistent inputs', 'Formul ...

execute numerous Jssor Caption instances simultaneously

I have a Jssor slider and I want to create an animation for the first slide. My goal is to make two images come from the sides and merge in the center. I tried using a div for each image with a caption, and it works well. However, the second image starts ...

Restrict HTML Elements Based on Their Size

I have a text file with a substantial amount of content that I need to display in an HTML format. The challenge is that I only want to show a portion of the text on the screen, but I am unsure of the exact amount that needs to be displayed. What I do know ...

Prevent the onClick function of the outer div from being triggered by clicking on the inner div

Similar Question: Stop parent container click event from firing when hyperlink clicked My issue is <div onClick="someJScode();"> ... <div></div> ... </div> I need to prevent someJScode() from executing when the inner di ...

Efficiently Organizing Data Using Coldfusion Loops in Columns and Rows

My issue lies in pulling data from a database to display on my website. I have three keys/attributes (columns) - PostedDate, DataImage, and Source that need to be shown together in one div with the posted date at the top, image in the middle, and source at ...

Is there a way to share the Username and Password without the need to manually input it?

My goal is to develop a C++ application for my classmates at school. Currently, they are required to visit our school's website and navigate to the login page. Once there, they enter their username and password, log in, and proceed to find their spec ...

Updating style in Javascript can sometimes be a bit tricky

What's preventing this from working? localStorage.fontSize contains the correct value, but the initial document.body.style.fontSize = localStorage.fontSize + "pt"; doesn't update the style. <script type="text/javascript> if(!localStorage. ...

How can I customize the appearance of a checkbox button in JavaFX?

I am currently working on styling a JavaFX scene with CSS in a stylesheet. The goal is to apply styles to all the "basic" elements of the scene when it loads. My issue lies in finding the correct code combination to change the background color of a button ...

Different methods for dynamically altering the style attribute

Here's some code I wrote: <html> <head> <style> .containerTitle { background-color:silver; text-align:center; font-family:'Segoe UI'; font-size:18px; font-weight:bold; height:30px; } ...