What is the best way to establish personalized CSS styles specific to each subdomain?

I am in the process of establishing a multi-tenant application with a single instance and single database setup. The backend infrastructure is built using Ruby on Rails, while the frontend is handled by a separate AngularJS app integrated with a Rails framework.

To identify the subdomain and consequently determine the tenant, I have implemented a resolve function on an abstract parent state. Once the specific tenant is identified, my goal is to access CSS variable values stored in a configuration file on the front-end. These values would then be utilized to generate the main styles.css file containing classes referenced throughout the project.

While I have heard that CSS pre-processors such as Sass and Less can facilitate this task, I lack experience with either one. As a result, I am currently struggling to configure this setup effectively.

If anyone could provide guidance or offer code examples to help me navigate through this challenge, it would be greatly appreciated. Thank you!

Answer №1

Neither Sass nor Less will meet your requirements as they are pre-compiled before the browser loads them. Essentially, the browser only recognizes the compiled css file.

There are various approaches you can take to achieve your objective. I am not well-versed in Ruby, so my suggestions regarding server language will be general. These suggestions are not comprehensive solutions as I do not have full knowledge of your situation; rather, they are meant to provide some guidance.

The most effective method would likely involve using server logic to assign a different class to the body tag and then utilize that class to determine which styles are applied to the page. For example:

/* It's advisable to have fallback styles */
body {
  color: black;
  background: white;
}

body.style-one p {
  color: red;
  background: blue;
}

body.style-two p {
  color: blue;
  background: red;
}
<body class="style-one">
  <p>This text will be red.</p>

Another option is to modify the body tag's class using JavaScript, allowing users to change the theme of the page.


Alternatively, you could implement similar server logic to output a secondary <link rel="stylesheet"...> tag to load one of two stylesheets. This approach enables you to neatly separate a large number of rules for different themes into their own files.


One approach I've utilized (with PHP) involves generating the stylesheet dynamically based on GET variables and outputting it as a CSS file. However, this method may result in losing some browser caching benefits. Here's an example:

<?php
header("Content-Type: text/css");

if( $_GET['theme'] == 'one' ) {

  echo 'p { color: red; }';

} else {
  
  echo 'p { color: blue; }';
  
}
?>

a {
  color: green;
}
<link rel="stylesheet" href="style.css.php?theme=one">

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

The Label method in the Html helper does not display the id

The creation of the checkbox and its label was done using an HTML helper: @Html.CheckBoxFor(m=>m[i].checkExport) @Html.LabelFor(m=>m[i].checkExport) To customize the label, I added the following CSS in a separate file: input[type="checkbox"] + l ...

Experiencing difficulties replicating two auto-scrolling divs

I have implemented a script to automatically slide two different divs. Here is the code I am using: The HTML: <div id="gallery"> <div id="slider" style="width: 6000px; left: -500px;"> <div><aside class="widget widget_testimoni ...

What is causing the ng-show function to malfunction after building with phonegap?

My current javascript framework for my PhoneGap app is the Ionic Framework. I have a specific item on one of my pages that I want to make expandable and collapsible by clicking an anchor on the page. Here is what the code looks like: In the HTML file: & ...

Can you explain the mechanics behind the animation of the upvote button on steemit.com?

Behold the upvote button of steemit.com: <span class="Icon chevron-up-circle" style="display: inline-block; width: 1.12rem; height: 1.12rem;"> <svg enable-background="new 0 0 33 33" version="1.1" viewBox="0 0 33 33" xml:space="preserve" xmlns=" ...

Can you create different width sizes using two types in HTML?

As I delve into learning HTML, I find myself curious about the possibility of combining multiple size types for width. For instance, is it possible to set a width that is both 10px and 3vw? While I understand that this isn't directly supported, I wond ...

The text alignment function of justify is not functioning properly

Trying to find a way to validate the content of this article, but I'm hitting a roadblock... <p style="text-align:justify;" class="custom-article"> Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed tincidunt velit vitae risus t ...

The HTML image link is adorned with a tiny blue checkmark underneath

My a tag with an image inside seems to be extending below the image, resulting in a small blue tic mark on the bottom right side. I've attempted different CSS solutions such as setting border to none, but nothing has resolved the issue. Any assistance ...

The attempt to initiate the MongoDB server was unsuccessful. dbexit reported an error with code 48 within the MongoDB system

After updating MongoDB, I encountered an error. I attempted to restart the MongoDB service, but the error persists. ...

I've encountered a peculiar error that is new to me while using bootstrap and javascript. Can anyone shed light on what this error signifies?

Hey there! I've come across this error in the console while attempting to utilize Bootstrap. It seems that a style is being refused from 'http://127.0.0.1:5500/node_modules/font-awesome/css/font-awesome.min.css' due to its MIME type ('t ...

What is the best way to style the currently selected option element in a select dropdown?

I am trying to change the font color of specific option elements within a select dropdown by adding style="color: #D4D4D4". When I apply this style directly to an option element like <option value="TEST" style="color: #D4D4D4">TEST</option>, it ...

Maintain the basic structure while ensuring divs are properly aligned

Behold, my div structure! ONE // at the top level TWO THREE // two divs side by side in the middle level FOUR // residing at the bottom <div id="ONE"> <div></div> <div><img src="logo.png"></div> </div> < ...

Adding a default option to my AngularJS select box

React const SnazzyContext = React.createContext(); function App() { return ( <SnazzyContext.Provider value={getList()}> <MenuSideController /> </SnazzyContext.Provider> ); } function getList() { // Function to fetch ...

Position the ion-radio element in the middle of the layout

As I work on creating a privacy page in HTML using Ionic 3, my desired output should resemble the following: However, with my current code, the result I'm seeing is different from what I expected: This snippet showcases the code I am working with: ...

"Need help on Tumblr: how can I make a div container wrap around a photo

Check out this website. Is there a way to make the div snugly wrap around the image? Currently, it seems to be sticking 4px below the image (as indicated by the red line I added in the background of the div). I'm puzzled as to where those additional ...

How can you conceal an element once the ngHide animation has completed in Angular's ngAnimate?

Is there a way for the display to be set to "none" after the ng-hide animation has completed, instead of immediately when the element is hidden? ...

Continue duplicating image to stretch header across entire screen

Looking to create a seamless connection between a Header image (2300x328px) and another image (456x328px) so that the header fills the entire width available without overlapping due to a pattern. I need the second image to extend the header to th ...

Struggling to create a photo grid design that meets my expectations

I'm struggling to recreate this particular photo grid. Any tips on how I can achieve it? Check out the image here Just to clarify, I had a working prototype see image description using flexbox. However, one image didn't fit well so I resorted to ...

Achieving overlapping div layouts with CSS styling

Displayed above are 4 different divs: red, green, and blue. I am seeking to have the blue div positioned directly below the green div, which contains the text "JACKSON". Additionally, I do not want the green div to expand beyond its content, nor do I want ...

Obtaining Master-Detail table data via WebAPI

I have a scenario where I am working with a database that consists of two tables linked by a foreign key relationship, following the classic master-detail structure. My goal is to retrieve all the data from these tables using ASP.NET MVC WebAPI calls from ...

Troubleshooting issues with adding rows to an HTML table

I want to include two buttons at the top of my page: Save and Cancel. These buttons should only appear after clicking the Submit button on the Customer Information panel. However, when trying this, the Customer Information panel is pushed down while I al ...