What is the best way to split CSS into multiple files for a single page application?

I have a project in Angular Kendo where I am working on developing a single page application. The plan is to separate the business logic into different pages, each with its own set of JavaScript, CSS, and HTML files that are dynamically loaded based on user interaction.

As part of the single page architecture, all JavaScript and CSS files need to be included in the main page for dynamically loading other pages.

My question is, how should I divide the CSS styles for each screen? Do I need to use different class/id names for each screen?

Is it possible to structure it like this:

// Page1.html
<div id="myPage1">
   <div class="something">Page</div>
</div>

// Page1.css
#myPage1 .something { color: black; }


// Page2.html
<div id="myPage2">
   <div class="something">Page</div>
</div>

// Page2.css
#myPage2 .something { color: blue; }

Both `Page1.css` and `Page2.css` would be loaded at one time in the main layout. When `Page2.html` is loaded, the styles from `Page2.css` will be applied.

Which approach is the correct way to handle this situation?

Answer №1

To streamline your website's performance, it is advisable to consolidate all CSS code into one file rather than dividing it among multiple files. This can help reduce latency and simplify maintenance. Make sure to reference this consolidated CSS file in your index.html or main page for easy accessibility.

When organizing JavaScript code, consider placing each Angular Controller in its own separate file. To efficiently manage these files, consider using tools like Gulp, Grunt, Web Essentials, or MVC Bundling in Visual Studio to bundle them together effectively.

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

Guide to centering a list on a webpage using Bootstrap 3

Looking to create a centered list using the latest version of Bootstrap 3. Any tips? The desired list should be positioned in the middle of the page. ___________________________________________ | | | ...

Chrome is blocking my ajax cross-origin request, causing it to be cancelled

I have been working on a Google Chrome extension and encountering an issue with my ajax requests. Every time my extension sends a request, it ends up getting cancelled for some unknown reason. The following code snippet seems to be functioning properly: ...

Using an AngularJS variable declared within a promise

I am facing an issue where I am trying to call a service within a function and utilize the response data using .then. However, I am unable to retrieve any output from the function. var status; var getStatus = function(data,$scope){ registrationServ ...

Stunning slide-in and slide-out animation when transitioning between AngularJS states

When creating a single page application, I wanted to implement a slide-right effect for new pages and a slide-left effect for old pages when the user clicks on a button. To achieve this, I referred to a helpful resource () that demonstrated applying CSS cl ...

Introducing a new left overlay panel popup feature using jQuery Mobile

I'm currently developing a mobile app and I'm having trouble getting a popup to launch on the left overlay panel when clicked on "Sign out". I can't figure out where I'm going wrong. Can someone please provide some suggestions? Thank yo ...

Clearing all data in a form upon opening

Within a single portlet, I have organized two forms under separate tabs. What I am aiming for is that whenever I switch to tab 2, all fields within its associated form should automatically be cleared without the need for a button click. Even after attempti ...

Encountering difficulties obtaining server response while utilizing dropzone.js version 4

I am currently using dropzone.js version 4 to facilitate file uploads from a webpage to my server. While the upload process is functioning properly, I am encountering difficulty in retrieving the server response. It should be noted that I am creating the D ...

The email validation UI dialog is failing to display any dialog

<html> <head> <link rel="stylesheet" href="//code.jquery.com/ui/1.11.1/themes/smoothness/jquery-ui.css"> <script src="//code.jquery.com/jquery-1.10.2.js"></script> <script src="//code.jquery.com/ui/1.11.1/jquery-ui.js"& ...

How can I resolve the Vue warning about an unknown custom element <password-input>?

I've been working on resolving an error within a component, but unfortunately, I'm still encountering issues. The error message is as follows: [Vue warn]: Unknown custom element: - have you registered the component correctly? For recursive co ...

What is the best way to enclose text within a border on a button?

I am trying to create a button with a colored border around its text. Here is the code for my button: <input id="btnexit" class="exitbutton" type="button" value="Exit" name="btnExit"></input> Although I have already added CSS styles for the ...

Creating a unique button design that incorporates a select tag and utilizes a dynamic percentage width

How can I keep a custom button positioned 20 pixels away from the right side of a select dropdown menu with a fixed width percentage? Using percentages in the background-position property makes the button move too far when the window is large. Setting the ...

Utilizing the Context Object in a Slack API Bolt Project for seamless property transfer between methods

I'm currently working on a Slack app using the JavaScript Bolt framework. This app essentially listens for specific message keywords in channels and then forwards those messages to the users of the app. My main goal is to add a permalink to the forwa ...

Handlebars: Access to the property "some prop" has been denied as it is not considered an "independent property" of its parent object

Model of MongoDB: const mongoose = require('mongoose'); const ProductSchema = mongoose.Schema({ _id:mongoose.Schema.Types.ObjectId, Pid:Number, Pname:String, Price: Number, Pdesc: String, Picname: String }); module.ex ...

Extract data from ajax and send it to php

Struggling to retrieve the value from a dropdown menu and pass it to a PHP variable when the selection changes in an HTML form. Additionally, looking to dynamically update the options in a second dropdown based on the selection in the first one. Any help w ...

Creating categories in AngularJS using an array

<table class="table-striped table-bordered table-condensed"> <tbody> <tr ng-if="$index%3==0" ng-repeat="permission in vm.parent.getAllPermissions()"> <td ng-repeat="i in [0,1,2]" class="col-xs-2"> ...

The CSS navigation bar is not properly aligned in the center

This menu was constructed by me: JSBIN EDIT ; JSBIN DEMO Upon closer inspection, it appears that the menu is not centered in the middle of the bar; rather, it is centered higher up. My goal is to have it positioned lower, right in the middle. I ...

Getting a portion of a href link in Java using Selenium

I am looking to compare the current URL in Google Chrome with a specific href link that contains two URLs. I need to extract the first link from this specific href link: click here <a href="http://pubcontentqa.perion.com/dz2/html/interstitial.html?http ...

Choosing a nested object within a JavaScript object

Is there a way to select the style contents of an object within another object? The data consists of a json encoded array. In my scenario, I am using data[i].replies and getting 2. How can I access data[i].userstyles instead? It seems to be an object. h ...

Unable to Render Image with MEAN Stack

Currently, I am utilizing the MEAN stack and attempting to exhibit an image that is stored in my Mongo database. From what I can ascertain, the image is properly saved and transmitted to the browser since the Web Console shows the image in the Response Bod ...

Is it possible to have a single listener for all events within the jQuery event namespace?

Is it possible to create a handler that can listen to ALL events within a specific namespace in jQuery using $.fn.on, off, and trigger functions? For example: $(window).on(".event_namespace", function(e){ //handler }); $(window).trigger("testEvent.e ...