Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

707
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda