Fascinating CSS discovery regarding the inheritance of color and the currentColor property

Imagine you have the following piece of HTML:

<a id=link1><span>Link 1</span></a>
<a id=link2><span>Link 2</span></a>

Now, accompanying this HTML is the following CSS:

a { color: white; }
a:hover { color: green; }
#link1 span { color: white; }
#link2 span { color: inherit; }
a:hover span { color: currentColor; }

An interesting observation to note is that upon hovering, the span within link 1 will remain white because its color property is explicitly defined as white. On the other hand, the span in link 2 will turn green when hovered over, even though it inherits the parent's color value.

This brings us to question whether using currentColor can truly capture colors inherited through the inherit property. This behavior has been observed and tested on both Firefox and Chrome browsers.

Check out the Fiddle for a demo

* { font-family: 'trebuchet ms'; }
code { color: #c00; font-family: 'courier new'; font-size: .95em; }
a { display: block; color: white; background: black; padding: 1rem; margin: 1rem; cursor: pointer; text-align: center; }
a:hover { color: green; }
#link1 span { color: white; }
#link2 span { color: inherit; }
a:hover span { color: currentColor; }
<p>When hovering over link 1, the <code>span</code> inside retains its white color due to the explicit definition, making it the current color.</p>
<a id=link1><span>link 1 (I stay white)</span></a>
<p>However, the <code>span</code> within link 2 inherits the white color from its parent <code>a</code> element. Unfortunately, this inherited color is not recognized as the current color upon hover, resulting in the text turning green based on the rule <code>a:hover { color: green; }</code>.</p>
<a id=link2><span>link 2 (I go green)</span></a>

Answer №1

When the value of currentColor is used for the color property, it will actually inherit its value from the parent element's color property. MDN

While considering the above statement, we must also take into account CSS specificity:

#link1 span and #link2 span have higher specificity than a:hover span, meaning the values defined in this CSS rule will be prioritized.

This code snippet will cause the text to inherit the green color from its parent:

#link2 span {
    color: inherit;
}

Inheritance always occurs from the parent element in the document hierarchy, even if it is not the direct container. MDN

Alternatively, this will change the text to the color white:

#link1 span {
    color: white;
}

Answer №2

It is built this way, and you can confirm by replacing inherit with currentColor. According to the specification:

currentColor: The value of the ‘color’ property. The used value of the ‘currentColor’ keyword is the computed value of the ‘color’ property. If the ‘currentColor’ keyword is set on the ‘color’ property itself, it is treated as ‘color: inherit’.

There is a specific rule for the color property, which has been encountered here. Therefore, it correctly retrieves green from the computed property and retains it.

body { background: black; }
a { display: block; color: white; padding: 1rem; margin: 1rem; text-align: center; cursor: pointer; }
a:hover { color: green; }
#link1 span { color: white; }
#link2 span { color: inherit; }
a:hover span { color: currentColor; }
#link2:hover span { color: currentColor; }
#link3:hover span { color: inherit; }
<a id="link1"><span>link 1 (I stay white)</span></a>
<a id="link2"><span>link 2 (I go green w/ currentColor)</span></a>
<a id="link3"><span>link 3 (I go green w/ inherit)</span></a>

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

In IE11, using flex-direction column can cause flex items to overlap

Encountering a problem with flexbox in IE11. The flex-items overlap when using flex-direction: column: https://i.sstatic.net/fHBw0.png In chrome and safari, it appears like this: https://i.sstatic.net/c2h7P.png .container { display: flex; flex-d ...

These coding languages are becoming more popular; however, the <p> tag is nowhere to be found in the slid

Inspect and Fix the Code: Missing p tag when clicked HTML Slide up/down <div class="slideme" class="center"> <h1>Hello,</h1> <p>Can you see Me</p> </div> <but ...

Incorporating a URL into an HTML marquee

As a beginner in coding, I'm currently exploring how to incorporate a link into my marquee. My goal is to eliminate any color change or underline animation as well. Do you have any recommendations? .Marquee { color: #da6fe2; background-color: t ...

Unable to auto-resize content and wrapper div in CSS3

HTML <div id="nav"> <div id="nav_wrapper"> <ul> <li><a href="index.html">Home</a></li><li> <a href="#">Licensing</a></li><li> ...

Adding padding to the top causes the scrollbar to hide at the bottom in Firefox

My HTML page includes the following configured DIV: <div id="contentDiv" style="padding-top: 20px; padding-left: 20px; overflow: auto; height: 100%;" > The content within this DIV is large enough to require a vertical scrollbar. When viewed in IE, ...

PHP and WordPress are experiencing issues with character encoding

On certain pages of my website, the character encoding appears to be clean, while on others it is not good. For instance, you might see something like this: Flu¨e, instead of this: Flüe. I am currently using Wordpress with my own theme. My PHP documen ...

What could be the reason my website is not saving the user input information to the database?

Hello! I am a novice programmer attempting to create a basic registration function on my website. I have set up a simple form for users to input their information, but I am facing an issue where the user data is not being stored in my database. ...

Using Javascript to Add a Back Button in Jira Velocity Template

Currently, I am working on developing a custom velocity template for our Jira instance. One issue I'm encountering is with inserting a button to go back one page using JavaScript. Despite my efforts, the button doesn't seem to be functioning as e ...

The required keys [:id] are not included in the route matches

As a beginner in Rails, I've encountered similar issues that I can't seem to resolve. Here are my routes: resources :users do resources :items end My model setup is as follows: class Item < ActiveRecord::Base belongs_to :user end cl ...

Master Page Image Vanishes After Moving .aspx Page to a Directory

Apologies if this seems like a silly question, but I've been unable to find any information on this issue. I have a master page located in the root of my solution explorer and have just added an .aspx page in a new folder. The master page is linking t ...

Angular provides the capability to sort through data using various criteria

I have received an array of objects in a specific format from the server, which may contain more than 2 objects. [ {processId : 1, processName : "process1", country : "germany", companyCode:"IB" , companyHiringType:"FRE", masterClauses:[ {cl ...

How can I prevent the arrow keys and space bar from causing my webpage to shift downwards?

I created a game where the arrow keys control movement and the space bar shoots, but every time I press the down key or space bar, it scrolls to the bottom of the web page. Is there a way to adjust the CSS so that it focuses on the game div tag without ...

Responsive Bootstrap 4 navigation bar featuring collapsible menu and various elements displayed on the right side

I have been struggling for quite some time to create a mobile menu using Bootstrap 4. My goal is to display the 3 brand names and the toggle button on the same row in the mobile menu. However, when I click the toggle button, the 3 brands end up below the c ...

Creating SVG paths using coordinates for THREE.js

I copied the exact code from this ThreeJs Example designed for generating a three-dimensional City Model. I've created an SVG path outlining city boundaries using Google Maps and now I'm trying to use the above code to create a similar 3D object ...

Implementing Object Value Assignment to an Element Using jQuery

I recently received a set of data via an API request: https://i.stack.imgur.com/YGe2D.jpg In order to display this data as selectable options, I included the following code snippet in my AJAX script: $.each($(value.routes), function(index, route){ $ ...

Display and conceal specific table cells based on a user's search for a singular cell

I recently wrote a jQuery script to filter rows in a table based on user input. However, I encountered an issue with my current code when trying to search for a specific string within the table. Here's the scenario: a|b|c|d| e|f|g|h| i ...

Unable to programmatically modify the src attribute on embed player - experiencing issues

I have been attempting to modify the src attribute in order to play a YouTube video using JavaScript, but so far I haven't had any success. Here is the code snippet I've been trying to use: $("#youtube-player").setAttribute('src', obj ...

Arranging Images, Buttons, and Text Vertically in React with CSS

https://i.sstatic.net/0WaeI.png In my current application, whenever the button is clicked, a random image is generated but it results in everything appearing bunched up. I would like the images, which are of different sizes, to be displayed with the dog&a ...

Arrange items in the navigation bar

Currently, I am in the process of setting up a small navigation bar that includes a search bar and a login feature. However, I have hit a roadblock as my usual method of using display:flex is not working for this particular case. Instead, I attempted to ...

Submission form not functional at this time

I'm having trouble with this form submission, can someone offer assistance? My issue involves using bootstrap css to submit a simple form. I'm facing a small problem that I can't seem to figure out, any help would be appreciated. <di ...