Opposing the fill attribute with the current color

Is there a way to reverse or eliminate the effect of fill: currentColor?

.svg-icon * {
  /* Targeting all sub-elements to customize icons with a specific color */
  fill: currentColor;
}

.svg-icon.iconLogoGlyph * {
  /* How do I cancel out fill: currentColor further down the cascade? */
  fill: inherit;
  /* I want to display the original colors from the SVG */
}
<svg role="icon" class="svg-icon iconLogoGlyph" width="25" height="30" viewBox="0 0 25 30"><g fill="none"><path fill="#BCBBBB" d="M21 27v-8h3v11H0V19h3v8z"></path><path fill="#F48024" d="M5.402 19.101l13.561 1.96.164-2.38-13.256-2.547-.469 2.967zM7.2 12.3l12 5.6 1.1-2.4-12-5.6-1.1 2.4zm3.4-5.9l10.2 8.5 1.7-2-10.2-8.5-1.7 2zM17.1.2L15 1.8l7.9 10.6 2.1-1.6L17.1.2zM5 25h14v-3H5v3z"></path></g></svg>

I possess a series of icons created with SVG. Some of these icons include in-line styles with color indications. The common use case is to disregard these in-line fills and have them adopt the parent's color by specifying fill: currentColor in CSS. However, in certain instances, such as our logo, we'd prefer these inner colors to be visible. In other cases, like a footer, we'd like to replace the colors with a single chosen color.

Due to various reasons™, I am restrained to these classes present in the library. Each icon carries the class svg-icon and a more specific class, such as iconLogoGlyph in this scenario.

How can I nullify fill: currentColor solely using CSS, given that I am unable to modify the classes? I've tried using :not to target all classes except .iconLogoGlyph, but this approach does not allow me to utilize fill: currentColor when necessary.

What is the antithesis of fill: currentColor? It's neither fill: inherit nor fill: none. I require something along the lines of

fill: just take what's inside the SVG
🤔

Answer №1

If you want to optimize your SVG files, you can achieve this by slightly adjusting the structure. Instead of defining the fill directly on the path like this:

<path fill="#BCBBBB" d="..."></path>

You can wrap the path within a group tag and specify the fill on the group level like this:

<g fill="#BCBBBB"><path d="..."></path></g>

By doing this, the fill property works more efficiently as per the SVG specification:

  1. The fill property doesn't apply to group elements, so setting it on the group level doesn't interfere with the individual path fill.

  2. Fill is inherited, so the fill defined on the group will be inherited by the path objects inside it.

In your CSS, using *:not(g) instead of just * will optimize the default, making the path use the current color set by color properties. When using fill: inherit, the path will revert to inheriting the fill value from the parent group.

Here is an example of CSS code:

.svg-icon *:not(g) {
  fill: currentColor;
}

.svg-icon.iconLogoGlyph *:not(g) {
  fill: inherit;
}

You only need to apply this SVG structure change to icons where you want to utilize native colors. Icons that always need to use the current color do not require this adjustment.

Answer №2

Unfortunately, there is no specific property value for fill that meets your exact needs. To achieve the desired effect of recoloring specific icons, it is recommended to utilize CSS selectors that target those icons directly.

Answer №3

If you're looking for an alternative approach, consider making a slight adjustment to your global fill: currentColor rule and also modify the colors of SVGs to maintain their original colors.

You can update your global rule to something similar to the following:

path:not(.important), circle:not(.important) {
    fill: currentColor;
}

Additionally, make sure to add the important class to all filled parts within your SVGs, for example:

<path class="important" fill="#FFF" ...

This method, unlike using the <g> tag, allows for easy replacement of all necessary parts automatically using editor tools.

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

Stylish Material Design Icons

Our website utilizes CSS classes to display icons across various pages. .alert { /* used for "alert" type messages */ background: url(images/alerts.png) no-repeat left top; padding: 5px 0 15px 35px; margin: 0; } Use: <p id="ctl00_ctl00_ContentMessagePa ...

Tips for eliminating the page margin in Java when converting HTML using iTextPdf with HTMLConverter

No matter how hard I've tried, setting the body with padding: 0 and margin: 0, as well as attempting to set the doc() with setMargin, nothing seems to be effective ...

Design layout in Angular Material that wraps the content within it

Is there a way to achieve this specific layout with the "plus" icon positioned below the card layout? https://i.sstatic.net/W5ZRM.png I have attempted the code below: <section layout="column" style="background-color: black;"> <md-c ...

What are the solutions for resolving problems with the display and functionality of a multi-page form in Internet Explorer?

I am currently working on a form and you can view it at this link: http://jsfiddle.net/pPWr3/14/ While the form displays correctly in Chrome and Firefox, I am experiencing display and functionality issues when using Internet Explorer 9. The problems inclu ...

I am currently working on creating a timer and I am looking to position these three buttons in a circular formation

I want to create a stopwatch with 3 responsive buttons arranged in a circular layout. I have successfully built a responsive circle and now need help adding the buttons inside the circle next to the timer. To view the code, click here: https://jsfiddle.ne ...

At a specific media query breakpoint, links are disabled while the rest continue to function without any

Today I encountered a puzzling issue on my website with a responsive layout that utilizes @media queries and breakpoints. At one specific breakpoint (or resolution, as I am still learning the terminology), the links in the navigation bar are inexplicably d ...

Guide for positioning buttons in front of an image using HTML and CSS

I need assistance placing buttons in specific positions above an image carousel using HTML and CSS code. Despite researching various resources, I am unable to resolve this issue. This is the HTML implementation: <!-- Image where buttons should be placed ...

Tips for eliminating the entire link from the footer section

Whenever a link is hovered over, a white box displaying the full URL appears at the bottom left of the browser. Is there a way to remove this feature from my website? I appreciate any assistance with this issue. Thank you! ...

Unlocking the Secret to Rotating a GroundOverlay on Google Maps

I am currently working on a map project that involves adding shapes and locations onto the map and saving it. However, I am encountering an issue with groundoverlay rotation. Even though there is no built-in rotation property in the Google Maps documenta ...

What is the best way to eliminate the final border in a row that has been applied using the <

I have a table where the tr is styled with a border. I am looking to eliminate the border from the last td in the tr. Here is an example: <table> <thead> <tr> <th>a</th> <th>b</th> <th&g ...

Method for eliminating double quotes from a string bound in Angular

https://i.sstatic.net/fWF8M.png https://i.sstatic.net/fWF8M.png Check out this code snippet: var app = angular.module('umovie-app'); app.directive('renamable', function($sce) { return { scope: { model: '=model', ...

Safari 5.1.7 causing unusual behavior on the website

Despite running smoothly in most browsers, this website goes haywire and crashes on Safari when viewed on a Mac. I've attempted troubleshooting by removing the Google map, CSS transforms, and the Stellar jQuery plugin, but the issue persists. Does any ...

Aligning the H3 and Anchor tags at the bottom of the page

I am currently utilizing Bootstrap 3.0 and hoping to achieve a layout similar to this: Favorites Add I want the "Favorites" text to be aligned on the left and enclosed in an H3 tag, while the "Add" link is aligned on the right within an anc ...

Right-align SELECT-OPTIONS text

Here are the screenshots of the form I'm currently working on. I am aiming to create a select box in the form where the text within the options is aligned to the right. After an option is selected, the chosen text should be displayed as illustrated i ...

utilizing a dynamic value within CSS modules for class naming

Struggling to incorporate a variable into a CSS module class name in Next.js. Finding it challenging to determine the correct syntax. The variable is fetched from the API: const type = red Here's how I'm attempting to achieve it: <div clas ...

Can the styling and variables of Bootstrap4 be altered dynamically?

I have recently started working on a project that utilizes Bootstrap4 for design and layout. However, there is now a need for the ability to change certain core styles (such as font-face, primary color, and background color) dynamically at runtime. Curren ...

text box with an immobile header

As the browser window size decreases, the layout changes. However, when scrolling down, the search text box moves up and is no longer visible due to its lack of fixation. How can I make the search text box stay fixed as I scroll down? I tried implementing ...

Utilizing Fancybox with a polymer paper-card: A step-by-step guide

I have a collection of paper-cards and I am looking for guidance on integrating the fancybox library to display the content of each paper-card. Here is a sample of a paper-card: <paper-card heading="Emmental" image="http://placehold.it/350x150/FF ...

Selection of child elements using CSS selectors

I have the following HTML snippet and I want to target the second child with the class name "embed": <div class="embed-container">test</div> This is what I tried: .feature-row .threecolumns .caption:nth-child(2) { border: 1px solid red; ...

Tips for achieving a static background image while allowing scrolling on a webpage in asp.net

I am struggling to implement this on my website. The background image needs to be fixed while the content of the webpage should scroll in asp.net <head runat="server"> <title></title> <style type="text/css"> body { backgr ...