Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

conditional loading of stylesheet

I have a site with clients. Every client can have it's own theme and when a user of a certain client is logged in, the company theme must be loaded. In the application.css.scss I have a line like this for every company:

@import "_theme_x.css.scss";
@import "_theme_y.css.scss";
@import "_theme_z.css.scss";

How can I load only e.g. theme_x when a user of company x is logged in and not load theme_y and theme_z? Or is there a better way of doing this? Thanks!

like image 854
John Avatar asked Mar 27 '12 14:03

John


People also ask

Can you do conditional in CSS?

CSS Conditional Rules is a CSS module that allows to define a set of rules that will only apply based on the capabilities of the processor or the document the style sheet is being applied to.


1 Answers

If themes are large, you might want to separate them from the application.css and load them conditionally in your layout. For example, if you have a helper theme_stylesheet in application_helper which returns the name of the theme the client is using:

# application.html.erb
<%= stylesheet_link_tag 'application', theme_stylesheet %>

If they are small, I like namespacing. Leave your application.css as-is, but modify the themes to use a top-level rule on the body. Place a tag on the body to select the theme. The beauty of this is you can dynamically change the theme.

<body class="theme-<%= theme_stylesheet %>">
  ...
</body>

_theme_x.css.scss

body.theme-x {
  ...
}
like image 166
aceofspades Avatar answered Sep 19 '22 17:09

aceofspades