What is the best way to display a backup CSS while waiting for the main CSS to load?

I am currently experiencing a unique issue with Google Search Console.

It seems that the Mobile Usability test suite fails to load CSS roughly half of the time, resulting in errors in the report.

For reference, here is the affected page:

When the CSS file fails to load, the page appears like this:

While I investigate why Google is struggling to load the CSS, I am looking for a workaround to provide temporary CSS styles to use before the main CSS file loads, for example:

body { background: #040404; color: #e5e7eb; }

a { margin: 10px; display: inline-block; color: #2E8EFB; }

I can inline this CSS directly into the HTML, but it causes conflicts with the main CSS once it loads. Is there a way to clear the previous CSS once the new CSS is loaded?

Answer №1

To ensure a safe replacement of temporary styles without accidentally removing other styles, place them in a cascade layer and utilize revert-layer to remove them. For example:

<style>
  @layer temporary {
    body { background: #040404; color: #e5e7eb; }
    a { margin: 10px; display: inline-block; color: #2E8EFB; }
  }
</style>

In a linked style sheet, add this code after the <style> element:

@layer temporary {
  body, a { all: revert-layer; }
}

<style>
  @layer temporary {
    body { background: #040404; color: #e5e7eb; }
    a { margin: 10px; display: inline-block; color: #2E8EFB; }
  }
</style>
<style>
  /* Not a temporary style - This should not be reverted by 
   * the linked style sheet 
   */
  body { background:green }
</style>
<link rel="stylesheet" href="data:text/css, 
  /* Data-url for demonstration purposes. Would actually be
   * in an external file
   */
  @layer temporary {
    body, a { all: revert-layer; }
  }
">
Hello World

Answer №2

If you want to easily remove temporary styles, consider placing them in a <style> tag. However, if you prefer using inline style attributes, you can remove those styles by querying for the elements and setting style = "". For example, you can use

document.querySelector('body').style = ""
.

To remove these styles after your CSS has loaded, you can add an onload event listener directly to the <link> tag. I tested this method locally and it worked perfectly.

<head>
  <!-- -->
  <link
    rel="stylesheet"
    href="--your-intended-css--"
    onload="
      // if using a style tag, remove it from the DOM
      document.getElementById('temp-styles').remove();

      // if using inline styles, set each element's style to ''
      document.querySelector('body').style = '';
    "
  >
  
  <style id="temp-styles">
    body {
      background-color: red!important;
      font-size: 20rem!important;
    }
  </style>
</head>

<body style="background-color: blue!important;">
  <!-- -->

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

Executing operations on checkboxes on a webpage without access to model files

I am facing an issue with accessing the models file due to encryption in the software. Currently, my checkboxes are implemented using Ajax within a PHP query. Since it is Ajax-based, I am unable to manipulate actions through the URL. My goal is to extract ...

Using jquery/offset to position a div causes it to float, but it does not take

<script type="text/javascript"> $(document).ready(function () { var top = $('#rt_outer').offset().top - parseFloat($('#rt_outer').css('marginTop').replace(/auto/, 0)); $(window).scroll(function (event) { // det ...

Transferring HTML information to Flask

I'm struggling with passing a value from a text box in a web application to a Flask application. Despite my efforts, the request object in Flask does not seem to contain the data I need. Can anyone provide assistance with this issue? Below are the rel ...

Solution for fixing the position of a div scrollbar under the browser scrollbar

When working on my web app, I faced an issue with a fixed div modal that takes up the entire screen. The problem is that the scrollbar for this modal is not visible, only the body scrollbar can be seen. It seems like the fixed div's scrollbar is posit ...

Creating a custom HTML5 canvas with images from a JSON array

I am currently working on a project to develop a unique slideshow feature by hosting images in an external JSON array and then loading them onto an HTML page. The next step is to insert these images onto an HTML5 canvas. While I have successfully loaded a ...

HTML5 Applications with Intel XDK

I'm currently working on an HTML5 application using Intel's XDK platform for building in Android. However, I'm facing an issue where the application crashes when the keyboard pops up upon entering text. Can anyone provide assistance with thi ...

Issues with the Content Editable Functionality

While working on my website, I encountered a strange issue. For some reason, I can't seem to get the contenteditable="true" attribute to work on the heading with the ID "hl". It would be awesome if someone could help me figure out how to mak ...

What is the best way to adjust the footer width to match the sidebar using Bootstrap 5?

Currently working on a Bootstrap 5 template for my website, but being new to it makes it a bit challenging. Specifically struggling with aligning the footer to fit the full width of the sidebar. <!DOCTYPE html> <html lang="en"> <head&g ...

What is the process for combining AngularJS filters?

I currently have a fragment object like this: [{id: 721, titulo: "Cotizador Gastos Médicos Bx+ 2019", descripcion: "Cotizador Gastos Médicos Bx+ Tarifas 2019", updateDate: "2020-03-25 14:30:22.0", idCategoria: "1", …}, {id: 88, titulo: "Cotizador GMM ...

Dynamic item addition feature activated by button on contact form

I am looking to create a form that includes the standard name, phone, email fields as well as a dropdown for selecting products and a text box for inputting quantity. The unique aspect is allowing users to add multiple products (dropdown and quantity textb ...

What could be the reason why the .click function is not functioning as

I'm completely new to jQuery and I'm working with an HTML element that looks like this: <input type="button" value="Cancel" class="supportButton" id="whitelist.onCancelUploadButton" onclick="whitelist.onCancelUpload();"> By using the foll ...

bootstrap used for creating horizontal radio buttons

I'm attempting to horizontally align radio buttons within a form without relying on the Bootstrap library. The following code achieves this: <form id="test_form"> <div class="control-group"> <label for="Q1">First question</ ...

Troubleshooting Issue: Ionic 3 and Angular ngForm not transmitting data

Attempting to create a basic crud app with ionic 3, I am encountering an issue where new data cannot be inserted into the database. The problem seems to lie in the PHP server receiving an empty post array. Below is my Ionic/Angular code: Insert.html < ...

Retrieve the value of the second child element in a table cell when clicking on it with Javascript

I have created a table with one row that includes the title of the month and a cell containing an inner table with multiple rows and cells. I want to display the content of the second cell in the second table as a modal box or alert dialog when it is click ...

Mobile devices are not showing the fixed header correctly

Recently, I was requested to lend a hand with a certain website: www.backincontrolbook.com Interestingly, when viewed on a PC browser, everything appears as intended. However, switching over to a mobile device (iPad and iPhone specifically), the header de ...

Having difficulty retrieving precise HTML information for data scraping purposes

I'm a beginner in python and exploring web scraping for a project on . Currently, I'm using selenium to automate the search for a CUSIP and then extract specific details about it. Although I've successfully automated navigation from EMMA&a ...

What is a solution to prevent style.css from being recognized as the Jekyll Page?

Currently, I am utilizing an expression {% assign mypages = site.pages | sort: "order" %} {% for page in mypages %} {% unless page.exclude %} <a href="{{page.url|absolute_url}}"> {{ page.shortname }} <span class="rate">{% include indexmod.h ...

Craving assistance in coding permutations

Thank you for responding. I want to express my gratitude for your efforts in trying to assist me. Unfortunately, I have posted this problem on multiple websites and no one has been willing to help. Regarding my code, my objective is to count permutations. ...

Erase blob_over from the Wordpress menu hover effect

I've created a unique style for the Donate button on this website, but I'm struggling to remove the hover effect. Any suggestions on where to start? Website URL: This is my Custom Class CSS: .donate { background:#4A1383; padding:0px 10px 0px 1 ...

The dropdown UL element loses its opacity in Internet Explorer

After creating a basic CSS dropdown menu, I encountered a strange issue when viewing it in IE. In the screenshot provided, only the shadow is visible and the content inside the element appears to be transparent. I have not utilized transparency anywhere o ...