The <svg> > <path> combination is not being properly targeted by the selector

Currently, I am working on a project that involves the use of SVG icons. I would like these icons to have a consistent style without being affected by selectors.

I have encountered an issue where I am unable to apply styling to the <path> elements within the <svg> using a selector.

In the provided code snippet, the styling with .ribbon-color is functional. However, when attempting to implement multiple colors on a single page with .yellow-ribbon .ribbon-color, it does not work as expected.

<!DOCTYPE html>
<html>

<head>
  <title></title>


  <style>
    .svg-block {
      width: 0px;
      height: 0px;
      overflow: hidden;
    }

 /*This is working*/
    .ribbon-color {
      fill: red;
    }

 /*This is Not working*/
    .yellow-ribbon .ribbon-color {
      fill: yellow;
    }
  </style>

  <script>

  </script>
</head>

<body>

  <div class="svg-block">
    <svg xmlns="http://www.w3.org/2000/svg">

      <g id="ribbon-Color" width="55.425" height="71.707" viewBox="0 0 55.425 71.707">
        <g id="Group_10971" data-name="Group 10971" transform="translate(-340.947 -751.995)">
          <path class="ribbon-color" id="Path_1970" data-name="Path 1970" d="M-17661.223-23233v5.037h4.4l-2.02-2.312Z"
            transform="translate(18002.17 23984.998)" />
          <path class="ribbon-color" id="Path_1971" data-name="Path 1971" d="M-17661.223-23233v6.512h4.346l-2-2.988Z"
            transform="translate(18053.248 24050.188)" />
          <path class="ribbon-color" id="Subtraction_8" data-name="Subtraction 8"
            d="M55.425,71.7l0,0L0,0H33L55.425,29.006V71.7Z" transform="translate(340.947 751.998)" />
        </g>
      </g>




    </svg>
  </div>


  <div class="red-ribbon"> <svg width="55.425" height="71.707" viewBox="0 0 55.425 71.707">
      <use xlink:href="#ribbon-Color"></use>
    </svg> </div>

    
  <div class="yellow-ribbon"> <svg width="55.425" height="71.707" viewBox="0 0 55.425 71.707">
      <use xlink:href="#ribbon-Color"></use>
    </svg> </div>



</body>

</html>

Answer №1

Try a different approach, consider utilizing CSS variables for this task.

.svg-block {
  width: 0px;
  height: 0px;
  overflow: hidden;
}

.ribbon-color {
  fill: var(--c, red);
}

.yellow-ribbon {
  --c: yellow;
}
<div class="svg-block">
  <svg xmlns="http://www.w3.org/2000/svg">
      <g id="ribbon-Color" width="55.425" height="71.707" viewBox="0 0 55.425 71.707">
        <g id="Group_10971" data-name="Group 10971" transform="translate(-340.947 -751.995)">
          <path class="ribbon-color" id="Path_1970" data-name="Path 1970" d="M-17661.223-23233v5.037h4.4l-2.02-2.312Z"
            transform="translate(18002.17 23984.998)" />
          <path class="ribbon-color" id="Path_1971" data-name="Path 1971" d="M-17661.223-23233v6.512h4.346l-2-2.988Z"
            transform="translate(18053.248 24050.188)" />
          <path class="ribbon-color" id="Subtraction_8" data-name="Subtraction 8"
            d="M55.425,71.7l0,0L0,0H33L55.425,29.006V71.7Z" transform="translate(340.947 751.998)" />
        </g>
      </g>
    </svg>
</div>


<div class="red-ribbon"> <svg width="55.425" height="71.707" viewBox="0 0 55.425 71.707">
      <use xlink:href="#ribbon-Color"></use>
    </svg> </div>

<div class="yellow-ribbon"> <svg width="55.425" height="71.707" viewBox="0 0 55.425 71.707">
      <use xlink:href="#ribbon-Color"></use>
    </svg> </div>

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

Storing user input from HTML forms in a MySQL database table with PDO

Currently, I am in the process of developing a webpage that will contain multiple forms for users to fill out and submit. Once submitted, PHP will be responsible for executing a query to insert all the data into the table within their respective fields. I ...

What is causing this faint red line to persist within the parent container?

https://i.sstatic.net/U3dyK.png How can I get rid of the red line on the left side that is not disappearing, even though I've set the child div to white with a width of 50%? body{ height: 100vh; position: relative; } .main-container{ width: ...

Why is my CSS not showing up in Live Server when I preview it from Visual Studio?

Every time I use the "Go Live" option with Live Server (Visual Studio extension), I only get a preview of my plain HTML file. However, when I manually open the HTML file from the folder containing both HTML and CSS files, the page loads correctly with the ...

Changing the color of a div element dynamically with JavaScript

Is there a way to improve the code below so that the background color of this div box changes instantly when clicked, without needing to click twice? function updateBackgroundColor(platz_id) { if(document.getElementById(platz_id).style.backgroundCol ...

Tips for positioning a div between two vertically aligned input elements

I'm trying to figure out how to place a div between two inputs with the same height and vertically aligned. It seems like a simple task, but for some reason, the code below isn't working: input{ width: 35%; border: 1px solid #A7A7A7; ...

Execution of the PHP code within the div element with the id 'upload' is not working properly when trying to use xhr.open("POST", $id("upload").action, true)

Within my .js file, I came across the following line: xhr.open("POST", $id("upload").action, true); xhr.setRequestHeader("X_FILENAME", file.name); xhr.send(file); In the index.php file, there is a form structured like this: <form id="upload" action=" ...

Tips for centering a DIV with nearly 100% width and full height

<div class="main"></div> <style> .main{ background-color:black; position:absolute; width:calc(100% - 40px); height:calc(100% - 40px); margin:20px; </style> H ...

What is causing this regular expression to not find any matches in the VSCode search and replace function?

Currently utilizing VSCode for my code editing needs. https://i.sstatic.net/49OmA.png Displayed below is a screenshot illustrating the search options I have chosen: https://i.sstatic.net/ILcNv.png The regex expression I'm attempting to use functio ...

Is it possible to modify the colors of a box and text when hovering over it?

I am currently working on a styled subscribe button and have encountered an issue. I want the background color of the entire box to change, along with the text color, when hovering anywhere on the box. However, my current code only changes the text color w ...

JavaScript tutorial: Locate a specific word in a file and display the subsequent word

Seeking assistance with a task: I need to open a locally stored server file, search for a specific word, and then print the next word after finding the specified one. I have managed to write code that successfully opens the file, but it currently prints e ...

React app (storybook) experiencing loading issues with @font-face

I am struggling to load custom fonts from a local directory. Can someone provide assistance? Below is the code I am currently using: @font-face { font-family: 'My Custom Font'; src: url('./fonts/MyCustomFont.eot'); src: url(&apo ...

Exploring how CSS affects backgrounds in the Google Chrome browser

Having an issue with the background on my website. In Firefox and other browsers, the background appears much lighter and whiter compared to Chrome. This is the CSS code for my background: body {background:#ffffff url(../../images/background.jpg); direct ...

I'm having some trouble with my jQuery.get() function in the javascript Saturday(), can anyone help me figure out what I'm doing wrong?

Can anyone help me troubleshoot my jQuery.get() method in the saturday() JavaScript function? Here is the code snippet I have been working on. This is what I have in my index.html file: <html> <head> <title>jVectorMap demo</title> ...

Tips for designing a mobile-friendly table on a small device

I am working on a basic app featuring a simple table with four columns. My goal is to make the table responsive, with each column stacking vertically below the other. I am searching for a solution that does not involve the use of Javascript. Currently, my ...

Determine the selected option in the dropdown menu upon loading the page and when clicked

I am working on capturing the value of a drop-down list whenever it is changed or when the page loads. The aim is to display different div elements based on the selected option in the report field - either State or Year. Any assistance with this would be ...

Error in designing the ReactJS navbar (utilizing internal Bootstrap CSS)

I am facing an issue with the navigation bar in my project. When I use a link to an external source for the navigation bar, it works fine. However, when the internet connection is slow, I notice a brief loading time for the navigation bar which is quite bo ...

The responsiveness of Bootstrap 5 columns in Chrome inspection appears to break after reaching medium size, yet functions properly in other areas

I am currently working with cards within a grid system, but I am encountering an issue with resizing when the column size is smaller than medium in the mobile view inside the inspect section. The width does not expand to 100%, regardless of how much I try ...

Using Swift Mailer to add a subject to an email by including the 'mailto:' HTML tag

I am currently using Swift_mailer in PHP to send out automated emails. Recently, I have been tasked with enhancing the email messages by including contact information. To enable users to provide feedback, I want to utilize the html 'mailto' funct ...

Is there a way to ensure that a table cell appears on a new line?

I am struggling with formatting an HTML table created by an MVC WebGrid. The table has 5 columns, and I want the first 4 columns to occupy 100% of the width while the 5th column sits underneath also taking up 100% of the width. <tr> <td cla ...

Guidelines for ensuring that a radio button must be chosen prior to submission

I'm struggling with implementing AngularJS validation for a set of questions with radio answer choices. I want to ensure that the user selects an answer before moving on to the next question by clicking "Next". Check out my code below: EDIT: Please k ...