Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

706
Views
Change CSS theme in Angular

Im currently working on a Angular2 application with webpack and Im trying to set differents css themes according to the user.

For example : When the user connect, If it's a boy, I want to have my backgrounds blue, and if it's a girl I want the backgrounds to be pink.

Simply changing the css value with setAttribute or style.property wont work because the DOM is destroyed when changing tab in the application, it needs to be kinda permanent.

I've tried using different css stylesheets (1 for each theme) and linking them to my html with javascript when the user connect. Problem is, webpack is always adding automatically my css to my html when building the app.

Thanks for the help.

over 4 years ago · Santiago Trujillo
3 answers
Answer question

0

In your css, make a rule like :

.is-boy{
    background: blue;
}

.is-girl{
    background: pink;
}

and declare in you angular app a scope var like $scope.userSex = 'boy';

and on your body use ngClass like this

<body [ngClass]="{'is-boy': userSex === 'boy', 'is-girl': userSex === 'girl'}" ...
over 4 years ago · Santiago Trujillo Report

0

:host-context selector

You could use the :host-context selector to apply styles to your component based on the parent component.

styles:[`
    :host-context(.parent1) div{
      border: 1px solid blue;
    }

    :host-context(.parent2) div{
      border: 1px solid blue;
    }
  `]

This allows you to conditionally apply styles based on a the selector that wraps the component.

plunker

edit:

So in your case - your parent would have a div with class .boy and a div with class .girl

You could load these containing divs with some flag controlled by ngIf

over 4 years ago · Santiago Trujillo Report

0

If you want to be permanent store class value in localStorage. To set the theme use ngClass with variable set to theme you need.

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!