Discover the complete compilation of CSS class regulations (derived from various stylesheets) along with their currently active properties for the chosen element

Is there a way to retrieve all the matched CSS Selectors for a selected element and access the properties of each active class applied to that element?

I have researched methods like using getMatchedCSSRules and checking out

However, I am hesitant to use the cssutilities library because it does not update dynamically if changes are made to stylesheets on the page via Javascript. It creates its own rules array that needs to be constantly updated after any changes made using Javascript in any style tags.

The goal is to replicate what getMatchedCSSRules provides in Chrome but with an additional property for each rule's property, indicating whether it is currently active or overridden by another class.

This functionality should be compatible with Webkit and Firefox (I am utilizing a polyfill for Gecko for getMatchedCSSRules)

The desired return format would look something like this - CSSRulesAffectingElement = {

     rule : {
           text:"<css rule's text>",
           properties : {
              property1:{value:<value>,status:<active,cancelled>}
           }
        }
     }

For example - //indicating when fontsize is coming from a different rule

colorclass:
{
    csstext:'background-color:red;font-size:12px',
    properties: 
    {
        Background-color:
        {
            value:'red',
            status:'active'
        },
        Font-size:
        {
            value:'12px',
            status:'cancelled'
        }
    }
}

Answer №1

In my opinion, it may seem difficult to achieve unless directly parsing CSS files. I delved into this topic and discovered that less.js is capable of handling this task.

If you're interested, you might want to explore their code on GitHub:

https://github.com/less/less.js

Answer №2

After extensive research and examining the LESS source code, along with using firebug, I came to the realization that the most efficient approach was to utilize the library and customize it to meet my specific requirements.

The getCSSRules() function provides a comprehensive list of all the css classes (including inherited ones) and their corresponding properties that impact the element. While this method may be resource-intensive, I strategically implemented it in my application to perfectly serve its purpose.

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

Rearrange div elements following an ajax request based on a data attribute and applying the .animate method

I am dealing with a collection of div elements, each assigned a unique numeric id and data-position in sequential order (1 for the first in the list, 2 for the second, and so on). Upon making an ajax call using jQuery, the response is a JSON object that r ...

Ajax is malfunctioning and failing to fulfill my needs

I cannot get Ajax to submit no matter what. I've been struggling for hours... script: <script> $(document).ready( $("#submit").click(function(e) { e.preventDefault(); $.ajax({ url: "https://maps.googleapis.com/maps/ ...

Focusing solely on the presence of lowercase characters within the text field

Is it possible to apply a different color to lowercase letters in a text area? I am looking to highlight all lowercase letters in red while keeping the uppercase letters black. ...

Hold off on submitting the form until the location has been obtained

I am facing an issue with my application where it tries to retrieve location settings from the browser, which takes some time. I would like this process to run when the page loads so that the data is available when needed. However, if a user clicks the s ...

Updating partial data in MVC without having to reload the entire HTML content is a straightforward process that

I am looking to update partial view data without refreshing the HTML. I have utilized Json to fetch the data, but it seems like there might be some issues in the script. The Success part is not being executed. [HttpPost] public ...

CSS/jQuery animation alignment problem

Exploring an animation using CSS transitions and jQuery has been my recent project. The concept involves presenting the user with clickable divs to load a new page. When a div is clicked, it expands to cover the entire screen and transition to the next pag ...

Leveraging the power of Angular's ng-class directive in conjunction with d3.js on

Struggling to implement the angular ng-class directive with the d3js library within an svg element without success. HTML <div ng-controller="MainCtrl"> <div id="svgContainer"></div> <button id="swicthBtn" ng-click="switchStatus( ...

Tips for testing nested HTTP calls in unit tests

I am currently in the process of unit testing a function that looks like this: async fetchGreatHouseByName(name: string) { const [house] = await this.httpGetHouseByName(name); const currentLord = house.currentLord ? house.currentLord : '957'; ...

The React modal window stubbornly refuses to close

import c from '../Profile.module.css'; import { useState } from 'react'; import { createPortal } from 'react-dom'; import Modal from '../Modal/Modal'; const TransactionItem = (props) => { const modalRoot = ...

Utilize AxiosAbstraction to transmit a Patch request from a Vue.js application to a PHP backend

I need help with sending a PATCH request to update the birthdate of a user (promotor) from Vue.js frontend to PHP backend. The issue I'm facing is that the new date of birth is not getting saved in the database, and the existing date of birth in the d ...

Tips for modifying a request api through a select form in a REACT application

Apologies if this question seems a bit basic, but I need some help. I am working on creating a film catalog using The Movie Database API. I have successfully developed the home and search system, but now I am struggling to implement a way to filter the fi ...

The Flowbite CSS plugin seems to be malfunctioning when attempting to include it within the tailwind.config.js file

Incorporating Flowbite (both JS and CSS) into my application has been a bit of a challenge. I managed to successfully insert the JS using Webpack and import 'flowbite' in the entry point JS file. As for the CSS, I followed the documentation and ...

Experimenting with code within a window event listener function

I have a component in my AngularJS application that is functioning correctly. However, when it comes to test coverage, everything after the 'window.addEventListener('message',' part is not being covered. Should I create a mock object f ...

Intermittent Z-index conflicts in need of a solution - seeking guidance

Is there a way to make the h1 heading and img image elements appear on top of the opaque div they are enclosed in? I want them to look unaffected by the transparency of their parent div. Here is a Fiddle demonstrating my issue: <div id="main"> ...

Is there a way to align the image next to the form and ensure they are both the same size?

I've been struggling to resize the image to match the form size, but I can't seem to get it right. Can anyone provide me with some guidance on this issue? I have obtained this contact form from a website that I plan to modify slightly, but I need ...

Trigger an event in Javascript/ASP.net following a dropdownlist selection

On a single HTML page, I have incorporated three dropdown lists for country, city, and district with an initial blank selected value, along with a Google map. The aim is to automatically center and zoom in on the map whenever users make selections from the ...

Transforming a canvas into a div element

Hey there, I'm looking to swap out one canvas for another but I'm not sure how it will look on the browser. Any help would be greatly appreciated. <div id="juego"> <canvas width="203" height="256" id="1" class="bloque"></canvas& ...

How can I transform this in JavaScript so that it returns a Promise?

Currently, I am trying to implement code that I found in a SaaS's example. The goal is to retrieve the correct URL for a specific action. The sample code provided by the SaaS looks like this, but I would like to modify it so that it returns a Promise ...

Three image resources combined to create a unique border design in CSS

I am currently working on designing a webpage and am in need of creating a background border. I have access to three image resources for this task: one for the left side, another for the top, and the third for the right side. My struggle lies in finding s ...

Why does the browser indicate that a GET request has been sent but the promise does not return anything?

Having trouble with a puzzling issue and unable to find any solutions online. I am attempting to make a basic HTTP get request to receive a JSON object from an API I created (using express+CORS). I have experimented with both Axios and VueResource, yet en ...