Angular 8 can sometimes cause CSS to become disorganized upon page load

In the process of developing my angular project, I've integrated pre-designed HTML templates. Implementing lazy loading for page loading has led to an issue where the CSS appears distorted, as shown in the attached gif image. https://i.sstatic.net/7UZr5.gif

To provide a common navigation bar and footer across components, I have created separate navbar and footer components with all necessary CSS links included. Here is a snippet from my homepagecomponent.html file:

<app-basehome></app-basehome>
<app-navbarhome></app-navbarhome>
<body>
// Remaining HTML content
</body>

<app-footerhome></app-footerhome>

Seeking assistance in understanding the cause of this issue and potential solutions, I suspect it could be related to lazy loading implementation.

The routing setup within my app.routing module is as follows:

import { NgModule } from '@angular/core';
import { Routes, RouterModule } from '@angular/router';

const routes: Routes = [
  {
    path:'',
    loadChildren:'./website/website.module#WebsiteModule'
  },

  {
    path: 'admin',
    loadChildren: './admin/admin.module#AdminModule'
  }

];

@NgModule({
  imports: [RouterModule.forRoot(routes)],
  exports: [RouterModule]
})
export class AppRoutingModule { }

CSS files are stored in the src/assets folder with corresponding links provided in the following manner:

 <!-- Owl Carousel ->
        <link href="/assets/home/common/owl-carousel/css/owl.carousel.min.css" rel="stylesheet" type="text/css">
        <!-- Fancybox -->
        <link rel="stylesheet" href="/assets/home/common/fancybox/jquery.fancybox.min.css" type="text/css" />   
        <!-- Theme Style -->
        <link href="/assets/home/css/style.css" rel="stylesheet" type="text/css">

Answer №1

Rather than directly integrating the files into component html files, it is recommended to import them in style.css (global style.css) as shown below:

@import "/assets/home/common/fancybox/jquery.fancybox.min.css";

For further information, you can refer to this guide.

Answer №2

It is a common issue with Angular applications that the CSS is applied after the application has fully loaded.

To work around this, you can create a separate .css file containing all the styles that need to be applied with higher priority and include it in the index.html file in the head section. This should provide a noticeable improvement, although it may not completely solve the problem.

Alternatively, you can add your styles directly within the <style> tags on the page itself, rather than importing them via a link. This method can further improve performance by 15% - 20%, despite being less aesthetically pleasing.


If anyone has a more effective solution, I would greatly appreciate your suggestion.

Answer №3

I recently discovered an effective solution on a particular website which I decided to test out.

Alex Jover's Preload technique

Check out the article here: https://www.digitalocean.com/community/tutorials/html-preload-prefetch

To implement this, simply add the following code snippet in the head tag of your index.html file:

<link
  rel="preload"
  as="style"
  onload="this.rel = 'stylesheet'"
  href='https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.1.1/css/all.min.css'>

Upon testing, I observed that this approach led to the page getting stuck without displaying anything (even my loading spinner) until the CSS file was fully loaded by the browser.

Answer №4

Converting CSS link into Angular.json configuration file

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

Incorporating Chip into a Material-UI DataGrid column

I'm having trouble displaying data of a specific column inside a chip. I attempted to use the Chip component in my code: StackBlitz Demo Web Link: Live Demo I tried to incorporate it using: import Chip from '@mui/material/Chip'; but c ...

Is there a way to condense a paragraph of text without using a span or div element?

There is a common scenario where I often encounter this issue: <span> <p>hello world</p> </span> It involves reducing the width of a paragraph to fit the text, but unfortunately it leads to clutter in my HTML. Is there a way to ...

Verify whether a variable includes the tag name "img."

Currently, I am working with a variable that holds user input in HTML format. This input may consist of either plain text or an image. I am looking to determine whether the user has entered an image or just simple text. Here is an example of user entry: t ...

The JavascriptExecutor in Selenium with Java is experiencing issues and is not functioning properly for Firefox version 24.0

In a few of my test cases, I've been using the following command because it's quite useful when trying to click on a hidden element that only appears when hovered over some context: ((JavascriptExecutor)driver).executeScript("$('selector_fo ...

Libraries that provide webkit support for all browsers

Looking for a library that supports CSS webkit across various browsers, including older ones like ie6. Preferably written in JavaScript. Any recommendations? ...

Is there a more concise method to reference the parent scope in AngularJS in this scenario?

Is there a shorter way to reference the parent scope within this controller? $scope.tables = []; $scope.newTable = function(){ $scope.tables.push({name:"Table " + ($scope.tables.length+1),cols:[]}); $scope.selected = $scope.tables.length-1; }; I ...

Failed to send JSON data to WebMethod

I am encountering difficulties while attempting to send JSON to a WebMethod. I have provided the method I am using below. If there is a more efficient approach, please advise me. My goal is to store the JSON object in a database. JavaScript function TEST ...

How can you set an input field to be initially read-only and then allow editing upon clicking a button using Vue.js?

//I have two divs that need to be set as readonly initially. When an edit button is clicked, I want to remove the readonly attribute and make them editable. <div> <input type="text" placeholder="<a href="/cdn-cgi/l/email-protection ...

Activate animation while scrolling the page

I am using a progress bar with Bootstrap and HTML. Below is the code snippet: $(".progress-bar").each(function () { var progressBar = $(this); progressBar.animate({ width: progressBar.data('width') + '%' }, 1500); }); <body> & ...

Creating a production build with sourcemaps in Angular using the CLI

How can I preserve sourcemaps after the production build process? Currently, my command appears as follows: "build-prod": "ng build --app=release -prod && cp -R lang dist" I attempted to modify it to: ng build --app=release --sourceMap=true -prod && cp ...

Looking for guidance on restructuring a JSON object?

As I prepare to restructure a vast amount of JSON Object data for an upcoming summer class assignment, I am faced with the challenge of converting it into a more suitable format. Unfortunately, the current state of the data does not align with my requireme ...

Utilize Tailwind CSS in React to dynamically highlight the active navigation item on click

Check out my navigation bar code: <nav className="bg-white shadow dark:bg-gray-800"> <div className="container flex items-center justify-center p-6 mx-auto text-gray-600 capitalize dark:text-gray-300"> <Link ...

Making a node.js API call using multipart/form-data

Submitting Data Using POST https://i.sstatic.net/iVTDd.png Node.js Data Capture Method without the Use of Express and Body-Parser FORM DATA: ORDERID: MID: TXNID: TXNAMOUNT: 5.00 PAYMENTMODE: NB CURRENCY: INR TXNDATE: 2021-06-14+23%3A02%3A30.0 STATUS: ...

Angular 6: Inconsistent performance of [(ngModel)] functionality

In my Angular 6 project, I am utilizing Bootstrap Modals to implement a specific functionality. Upon clicking the modify button, a modal window should appear with pre-filled values. https://i.sstatic.net/PIg0L.jpg Although I am using template-driven form ...

Recursive methodology for building DOM tree structure

var div = { element:'div', parent:'body', style: { width:'100px', height:'100px', border:'1px solid black' } child: { element:'input', type:'text', ...

The value of a checkbox in Ionic 2

I have implemented the code for reading checkbox values in Ionic 2 following the answer provided. However, I encountered an error message in the console: Cannot read property 'indexOf' of undefined Here is my home.html code: <form #leadsF ...

Instructions on how to position the footer below the content and make it show inline

This question seems to be a duplicate, with only one unanswered answer and no discussion. The original poster did not confirm if the solution worked. I have tried the suggested CSS but it does not solve my problem, which I believe is the same. My layout i ...

What is the best way to notify the user if the percentage entered is not a numeric value?

My role is to notify the user when the entered value exceeds the acceptable range: if (document.myForm.outputPercentage.value <= 0 || document.myForm.outputPercentage.value >= 100) { alert( "Please enter a percentage between 1 and 100 ...

Are you facing difficulties while trying to utilize useContext in your React application?

I have a basic React application where I need to implement useContext. (by the way, I am using Vite + React) Below is the code for my Context.jsx file: import React, {useContext} from 'react'; const emailContext = React.createContext(); expor ...

What is the process for updating a particular div element?

I am currently developing a webpage that allows users to select an item, and the relevant information will be displayed. On this page, I have incorporated two buttons: btnBuy0 and btnBuy1. The functionality I am aiming for is that when BtnBuy0 is clicked ...