In order for SVG clip-path to function properly, it must be defined within the same <svg> element

One of the challenges I encountered was with an SVG star that I had defined externally and included within the DOM using use:

<svg>
  <defs>
    <symbol id="Star" viewBox="0 0 256 244">
      <polygon points="128 0 168.79 78.61 256 93.2 194 156.37 207.11 244 128 204.43 48.89 244 62 156.37 0 93.2 87.21 78.61 128 0"/>
    </symbol>
  </defs>
</svg>

<svg class="icon star" viewBox="0 0 256 244">
  <title>Add Favorite</title>
  <use xlink:href="#Star"></use>
</svg>

To make the star appear to be filling in from the outside, I worked on adding a transition for stroke-width and clipping the SVG with the same path:

*HTML*

<svg class="icon star" viewBox="0 0 256 244">
  <defs>
    <clipPath id="clip" viewBox="0 0 256 244">
      <polygon points="128 0 168.79 78.61 256 93.2 194 156.37 207.11 244 128 204.43 48.89 244 62 156.37 0 93.2 87.21 78.61 128 0"/>
    </clipPath>
  </defs>

  <title>Add Favorite</title>
  <use xlink:href="#Star" clip-path="url(#clip)"></use>
</svg>


*CSS*

.icon {
  margin: 50px;
  width: 50px;
  height: 50px;
}
.star {
  fill: #0F0;
  stroke: #F00;
  stroke-width: 1px;
  transition: stroke-width 200ms linear;
}
.star:hover {
  stroke-width: 50%;
}

The implementation was successful and here is a working fiddle

However, when I tried moving the clipPath definition up to the top...

<svg style="display: none;">
  <defs>
    <clipPath id="clip" viewBox="0 0 256 244">
      <polygon points="128 0 168.79 78.61 256 93.2 194 156.37 207.11 244 128 204.43 48.89 244 62 156.37 0 93.2 87.21 78.61 128 0"/>
    </clipPath>
  </defs>

  <defs>
    <symbol id="Star" viewBox="0 0 256 244" clip-path="url(#clip)">
      <polygon points="128 0 168.79 78.61 256 93.2 194 156.37 207.11 244 128 204.43 48.89 244 62 156.37 0 93.2 87.21 78.61 128 0"/>
    </symbol>
  </defs>
</svg>

<svg class="icon star" viewBox="0 0 256 244">
  <title>Add Favorite</title>
  <use xlink:href="#Star"></use>
</svg>

In this scenario, it didn't work anymore. Here is the not working fiddle.

I attempted including the clip-path="url(#clip)" within the use below but faced the same result. Similarly, when I added it within the CSS:

.star {
  ...
  clip-path: url(#clip);
}

Hence, there is a difference between the two methods. If required, I will stick to the first method, although ideally, I prefer keeping the DOM clean with separate SVG definitions - or at least grouped together at the top.

Answer №1

Avoid using display: none when dealing with SVG content as it disables CSS and renders properties like clip-path ineffective.

Instead, consider using width="0" and height="0" (or adjust the outer SVG width and height using CSS if needed).

Answer №2

Following Robert Longson's advice, it is recommended to steer clear of using display: none.

Solving the problem by substituting it with width: 0; height: 0; produced positive results.

Check out this link for more details!

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

There seems to be an issue in Angular as it is unable to retrieve /

I'm encountering an issue with my simple application where I am receiving the error message "Cannot GET /." Also, in the console, I see this error: TypeError: Cannot read property 'checked' of null at Object.SL_BBL_locer. I'm unsure ab ...

Using Python to extract the title of a hyperlink from an HTML website

<a class="inst" href="http://www.tsetmc.com/loader.aspx?ParTree=151311&amp;i=9987529074833218" target="9987529074833218">كشت و صنعت آبشيرين</a> I am trying to extract the name of the link ("كشت و صنعت آبشيرين" ...

Utilizing Angular controller variables to customize confirm message in HTML

Struggling to include an Angular variable in a confirm message. Despite multiple attempts, I have not been successful with any solution: <a onclick="return confirm('Do you want to manage ' + {{item.name}} + ' with ...?')" ng-hre ...

"How to Use jQuery to Delete a Class from an Array of Form Elements

I am working on a form that includes multiple drop-down menus like the one shown below: <select name="i_task_user[]" class="required"><option value="">- Select -</option></select> When the form is loaded, I want to remove the clas ...

Ways to gather all the elements on each page

Currently engrossed in a web scraping endeavor for a car website utilizing Selenium. One potential roadblock I've encountered is that the code seems to only iterate over the initial car element on the page, instead of going through the entirety of ava ...

Assign the hidden field's value to be the same as the input field's value using JavaScript

This may seem like a silly question, but I'm really hoping to find the best answer. Here's my issue: I have an input field and a hidden field. How can I set the value of the input field to be the same as the value of the hidden field? <input ...

Detect prohibited terms with Jquery

Check out this jsfiddle. I need assistance with a feature where certain specific words trigger an alert for the user when typed. Currently, if I type a phrase like I love ants, the entire line gets alerted. However, I want only the word ants to trigger th ...

Converting Scalable Vector Graphics to Hypertext Markup Language and

So I've got this SVG file called 7.svg. What's the best way to show this image using HTML or CSS on my index.html page? ...

Display webpage in a fancybox

I've searched extensively for sample cases on the internet, but unfortunately, I couldn't locate any. Is there a way to upload a fresh HTML file using fancybox? $('#Create').click(function() { $.fancybox({ What content should be in ...

PHP loading incorrect CSS file

I have a question about how I am linking a CSS file in my PHP program: <html> <head> <title>Untitled Document</title> <link href='sa_mal_link_1.css' rel='stylesheet' type='text/css'> </head> ...

Continue duplicating image to stretch header across entire screen

Looking to create a seamless connection between a Header image (2300x328px) and another image (456x328px) so that the header fills the entire width available without overlapping due to a pattern. I need the second image to extend the header to th ...

Achieving right-aligned buttons in Bootstrap

Is there a way to align a button to the left inside a div that has the class justify-content-center applied? Here is the code I tried: <div class="card-header justify-content-center"> <div> <div class="ml-5"> Text i ...

The CSS rule that is determined to have the nearest bounding class will emerge victorious

Imagine this interesting situation in HTML: <div class="red"> <div class="blue"> ... <-- maybe more nested parents <div class="x">Test</div> </div> </div> If we consider the following ...

A tutorial on allowing a background element to capture the click event triggered by a foreground element

Within a container div, I have a background and foreground div. I want the click event on the foreground div to be passed through to the background div for handling the event. How can this be achieved in Angular 2+? Here is an example structure of my div ...

Tips for confining the draggable div within its container

I've been working with React and TypeScript, and I recently added the W3School drag div example to my React app. However, I'm facing an issue where the draggable div is moving outside of the container. Can someone please guide me on how to confin ...

Is it possible to change the background color of a Bootstrap button using CSS overrides?

Desired Outcome https://i.sstatic.net/EjEXG.gif Actual Result https://i.sstatic.net/BmXYG.gif The goal is to toggle the red-background class, so that when hovering over the button-bullet, its background-color changes to #FCC1C5. Avoiding the use of .b ...

Tips for inserting an image within a horizontal list of items

Struggling to find a solution here. I have a menu with a unique bar design between list elements, featuring breaks at the top and bottom. I attempted to use li:after in the CSS to insert an image, but encountered issues with padding and margin. Below is t ...

In CSS, the primary consideration is consistently maintaining a uniform height for the main section

In the process of developing our website, we have a main section (div main and mainholder) with a maximum height set at 770px. Whenever the content in this section exceeds that height, it gets cut off on the page. However, we want the height of this sect ...

Navigating to a webpage using a dropdown menu selection and a button press

I'm new to Angular and wondering if it's possible to select an option from a dropdown menu and then be redirected to a specific page based on that selection <mat-label >Login As</mat-label> <mat-select> ...

Distinguishing keyboard and mouse events while focusing in React app

I have been working on implementing keyboard navigation focus outline for accessibility. The pseudo class :focus-visible works well on all elements except for input elements like text or textarea. It seems that inputs always have this pseudo class active s ...