Determine through code if a CSS file is being utilized

Is there a way to automate a CSS audit for file-level info without rendering the page? Could Selenium be used for this purpose?

I've come across browser plugins that can help audit CSS files, but manually reviewing results for each page would be time-consuming.

Motivation

Our web app has accumulated various CSS files in its template header over time, causing conflicts between different rules. To streamline our styling process, I plan to relocate older references from the site template to individual page headers.

To understand which CSS stylesheets are being utilized where on the site, my current idea is to crawl the entire website and track their usage.

UPDATE

Considering the potential for unintentional rule matches, I'm beginning to think that scripting this process may not be feasible. It might be necessary to review each page individually, as some pages could rely on conflicting styles.

Furthermore, I have doubts about the accuracy of static CSS checkers, especially when dealing with template files. Certain rules may only apply at runtime due to server-side elements or JavaScript manipulation.

Answer №1

It is not possible to directly check if a file is being used on a website, as browsers will load all files regardless. However, you can programmatically check if a particular CSS rule is being applied. One way to do this is by adding a unique rule to each of your CSS files.

In each file, add the following code:

UNIQUE_RULE::after {
    content: ' ';
    background: url(/track/?page=filename.css);
}

Note: Using a unique rule is important in order to avoid conflicts with other rules.

Browser will only load background images for specific tags that are present in the DOM tree. To ensure that a file is being used, you will need to apply one of its unique rules and then use a tool like Selenium to navigate through all the pages on your site.

Once your test is complete, you can search through your access logs for any missing /track/ file requests. This same method can be applied to check individual CSS rules as well, without having to modify the Selenium setup.

While setting this up may take some time initially, it can be a useful tool once everything is in place.

Although not the most elegant solution, it is functional.

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

Encountering Error 401 when attempting to login to Zoom using Selenium and Python on a Raspberry Pi device

I'm currently delving into the world of using Selenium and my objective is to open Zoom through a Python program on a Raspberry Pi 4. Upon execution of the provided code snippet, the program successfully carries out its tasks; it opens Zoom in a brows ...

Are DIV elements really impossible to click using selenium Web Driver?

Can DIV elements be clicked using selenium Web Driver? For example, I'm having trouble clicking the delete button in Gmail. https://i.stack.imgur.com/zsyio.png I've been trying to locate the element using the XPATH = //div[@aria-label='De ...

Seeking out information on our family's ancestry

I am currently developing a Family Tree feature for my web application using HTML5 specifications. Despite researching and coming across various JS samples, none seem to meet my specific requirements. I have explored options like JIT, Rafael, and GoJS, but ...

Troubleshooting a unique CSS bug in jQuery mouseover functionality

Check out this pen: https://codepen.io/anon/pen/eKzEVX?editors=1111 I recently created a Form Select in Laravel: {!! Form::select('status_id', $statuses, $post->status_id, ['class' => 'form-control post-sub-items-label &apo ...

How to keep text always locked to the front layer in fabric.js without constantly bringing it to the front

Is it possible to achieve this functionality without using the following methods? canvas.sendBackwards(myObject) canvas.sendToBack(myObject) I am looking to upload multiple images while allowing them to be arranged forward and backward relative to each o ...

unable to establish a connection to SQL in [PHP]

I'm having an issue with my code and need some help fixing it. The website is supposed to allow users to add, edit, and delete data. However, when I fill out the fields and click "add", it redirects me to a blank page. Here's the code that I have ...

What is the method for extracting an entire space-separated string into a PHP variable from HTML options?

When the select tag fetches options from a MySQL database and assigns a value to a PHP variable for echoing, only the first part of the text is displayed. For example : Option: Vijay Sharma Echo Output: Vijay <select name="facultyname" oncha ...

Navigating in a Curved Path using Webkit Transition

Currently, I am working on a simple project to learn and then incorporate it into a larger project. I have a basic box that I want to move from one position to another using CSS webkit animations and the translate function for iOS hardware acceleration. I ...

What steps can be taken to resolve the problem of 'TimeoutException' not being recognized in Python Selenium?

Hey everyone, I trust you are doing well. I am currently facing an issue with extracting data from a specific URL. driver = webdriver.Chrome() driver.set_window_size(1200, 800) driver.get(DEGIRO_URL) loginUsername = driver.find_element_by_xpath("//inp ...

When using React, I noticed that adding a new product causes its attributes to change after adding another product with different attributes on the same page

Imagine you are browsing the product page for a Nike T-shirt. You select black color and size S, adding it to your cart. The cart now shows 1 Nike T-SHIRT with attributes color: black, size: S. However, if you then switch to white color and size M on the ...

triangle shape filled with a gradient that transitions between two colors

I have two triangles displayed at the bottom of my page - one on the left and one on the right. The right triangle is currently transparent but I want to add a gradient effect to it. For the triangle-bottom-right, I'd like the gradient to go from rgb ...

Blurry font rendering occurs when an element is scaled using CSS

I want to add a scale animation to a bootstrap button, but the text appears blurry. I've tried all the solutions mentioned here. How can I resolve this issue? Below is the code snippet (text is slightly less blurry in the snippet compared to my pr ...

Retrieve the chosen option from the drop-down menu

Can anyone assist me in extracting the selected value from a combo box and then displaying it? Below is a snippet of code that demonstrates what I have attempted so far. <label class="col-sm-4 control-label">Issue Type:</label> <div> ...

CSS designs that adapt flawlessly to high-resolution mobile devices with perfect responsiveness

Currently, I am implementing max-width: 768px to modify the appearance of my website. However, with the increasing number of high-resolution devices available in the market such as 4K mobile phones, I am wondering how I can detect them. Should I consider ...

Troubleshooting: Why are my Angular 8 Carousel Slide Animations not functioning

Looking to create a carousel slideshow with images sliding from right to left and smoothly transition to the next image. All the necessary code can be found in this STACKBLITZ Here is the HTML snippet: <ngb-carousel *ngIf="images" [showNavigationArro ...

Exploring Rails 6: Enhancing Web Design with CSS Image Display

I've been attempting to update my webpage background with an image through CSS, but I'm struggling to reference one of my Rails 6 images in the process. Despite trying options like asset-url and image-url, I haven't been successful. However, ...

Include image hover text without using HTML

I am looking to add a hover effect to some images using CSS and JS since I cannot directly edit the HTML file. The goal is to have centered text pop out when hovering over certain images. I know the div class of the image but unfortunately, I cannot add te ...

How can you ensure that divs stay the same size and appear next to each other without resorting to using

I need assistance with organizing my website layout as shown below: https://i.sstatic.net/Dpof9.png The image div will display an image of variable size, which needs to be vertically and horizontally centered. The text div will contain a large amount of ...

The navigation bar at the top of the page is causing content to be blocked on mobile screens, but not on desktop screens

My Fixed-Top navbar is causing an issue on mobile screens, as it is covering the top content of the page. This problem doesn't occur on PC screens. When I resize the browser width to fit a mobile screen, the navbar overlaps with the body content... &l ...

"The event triggers the function with an incorrect parameter, leading to the execution of the function with

Currently, I am working with a map and a corresponding table. The table displays various communities, each of which has a polygon displayed on the map. My goal is to have the polygons highlighted on the map when hovering over a specific element in the tabl ...