Using Angular 6 to access colors defined in a CSS file through the ngStyle directive

I am looking to utilize the color defined in my CSS file for my ngStyle directive in my HTML code.

Below is the current setup in my HTML:

 [ngStyle]="{backgroundColor: isDarkStyle() ? '#29434e' : isNormalStyle() ? '#cfd8dc' : isLightStyle() ? 'white': ''}"

Here are the color definitions from my CSS file:

//colors for background

$light-background: white;
$normal-background: #cfd8dc;
$dark-background: #29434e;

What I want is:

[ngStyle]="{backgroundColor: isDarkStyle() ? '$dark-background' : isNormalStyle() ? '$normal-background' : isLightStyle() ? '$light-background': ''}"

Is there a way I can make this work? Appreciate any help you can provide. Thank you :)

Answer №1

Utilize the [ngClass] directive

Check out the live example on stackblitz:https://stackblitz.com/edit/hello-angular-6-refjye?file=src%2Fapp%2Fapp.component.html

.light{
background: white;
}
.normal{
background: #cfd8dc;
}
.dark{
background: #29434e;
}

within the Html structure

<p [ngClass]="isDarkStyle() ? 'dark' : isLightStyle() ? 'light': isNormalStyle()?'normal':''">
  Start editing to witness some enchantment occur :)
</p>

Answer №2

check out the solution

From my understanding of your query:

Here is the HTML code snippet:

<h1 [ngStyle]="{backgroundColor: isDarkMode() ? 'black' : isNormalMode() ? 'blue' : isLightMode() ? 'yellow': ''}">Sample Text</h1>

Corresponding TypeScript logic:

isDarkMode() {
    return false;
}

isNormalMode() {
    return true;
}

isLightMode() {
    return true;
}

Answer №3

You can utilize either ngClass or ngStyle

ngClass is necessary to specify a class in your stylesheet

<h1>ngClass</h1>
<p [attr.class]="state">
  Start editing to see some magic happen :)
</p>
<button (click)="state = 'light'">light</button>
<button (click)="state = 'dark'">dark</button>
<button (click)="state = 'normal'">normal</button>

ngStyle allows you to change specific CSS properties like background-color

<h1>ngStyle</h1>
<p [ngStyle]="{'background-color':color}">
    Start editing to see some magic happen :)
</p>
<button (click)="color = '#fff'">light</button>
<button (click)="color = '#cfd8dc'">dark</button>
<button (click)="color = '#29434e'">normal</button>

Check out this stackblitz example

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 selected option in Bootstrap is displayed twice in the Bootstrap modal

I am facing an issue with Bootstrap Select-box showing multiple times in a bootstrap modal wizard. I have tried various solutions from Stack Overflow but none of them seem to work. A screenshot of the problem can be seen below: https://i.sstatic.net/glp3E ...

Validating Email Addresses and Passwords Using JavaScript

I have implemented a registration page where users can create accounts, and I am facing issues with the validation process. Specifically, I want to validate if the email is valid, if the two email fields match, if the password meets the length requirement, ...

Tips for formatting numbers in Angular 2 using TypeScript

For example, my numeric value is (651156) and I need to format it automatically as (6,51,156), using TypeScript. ...

Increase the padding and add a border to separate the list items in bootstrap 4

I have a header with some links and I want to create space and add a border between them. I attempted using ::before pseudo-content, but it's not displaying the content where I intended. .header_links_ul { list-style: none; margin: 0; paddin ...

Saving information in the database without using a form

Just wondering, is it possible to create an <input type="text" class="some-class-here" value > field without using a form tag and then capture the input value? I was thinking of using the "keypress" function in jQuery to store the input value in a v ...

Controlling the first displayed page with JQuery Multi-Page functionality

Utilizing JQuery Mobile's multi-page approach, I am looking to optimize the order in which pages load. As a novice in JQuery and JavaScript, I am unsure how to go about this. I aim to verify the user's credentials through local storage upon init ...

Incorporate dc.js into your Cumulocity web application

Our team is facing a challenge while trying to integrate dc.js into our Cumulocity web application. While the standalone version works perfectly, we encounter issues when attempting to use it within Cumulocity. Below is the code for our standalone applica ...

Apply a CSS class once a TypeScript function evaluates to true

Is it possible to automatically apply a specific CSS class to my Div based on the return value of a function? <div class="{{doubleClick === true ? 'cell-select' : 'cell-deselect'}}"></div> The doubleClick function will ret ...

Issues with the CSS styling

Struggling with styling my menu and can't seem to get it right. Feeling a bit lost as I'm new to this. .customHorizontalList { list-style: none; display: flex; flex-direction: row; } .customHorizontalList>li { margin-ri ...

Challenges with implementing @Html.EditorForModel

Currently, I am developing a web application using asp.net's mvc-5 framework. In this project, I have implemented the following model class: public class Details4 { [HiddenInput(DisplayValue=false)] public string RESOURCENAME { se ...

Struggling with clicking on an <a> tag using Python Selenium that leads to a popup dialog box

Currently, I am honing my web scraping abilities by utilizing the World Surf League website. So far, I have not extracted any data; instead, I am focusing on navigating the site using Selenium. However, I have hit a roadblock where I am unable to 'cli ...

Display various sets of data from multiple models within a single view

My project is focused on Asp.net Mvc and I am encountering a challenge in displaying multiple model data in one view. However, I do not want to showcase it in a list or grid format, instead, I prefer a Form View style. The two modal classes involved are Pr ...

I want to hide jqvmap when viewing on mobile devices

I'm currently working on my website at . I have a template that I'm using as a guide, but I want to make the map disappear on mobile view and replace it with a dropdown list. Can anyone suggest what code I should use for this? Appreciate any hel ...

Am I using the if statement correctly?

Can someone confirm if I am using the if statement correctly in this script? Or is it not possible to use an if statement within this code? <script type="text/javascript" charset="utf-8"> $(document).ready(function () { $('#customerTable&ap ...

How can you delete using JavaScript by pressing the "Delete" key?

Is it possible to programmatically trigger the deletion of text on an HTML web page using JavaScript or jQuery? I am looking for a way to replicate the functionality of clicking the "Delete" button without requiring users to physically click on it. Inste ...

Do overlay elements have to be positioned at the bottom of the HTML body every time?

As I delve into the world of JavaScript frameworks, one common trend I've noticed is that elements like dialogs, tooltips, and alerts usually appear at the end of the body tag. With my own implementation of these UI elements, I am determined to make ...

Implementing a stylish gradient background with HTML 5 progress bar tag

I am trying to customize an HTML5 progress bar tag in a unique way: The background of the bar should have a gradient with a fixed width of 100%, but I want the gradient to only be visible where the value of the bar is... progress[value]::-webkit-progres ...

Tips for invoking a function from a JavaScript file within an Angular component

This particular query remains unanswered and pertains to AngularJS. I am seeking a solution specifically for Angular, as none of the existing answers online seem to be effective in my case. Here is an outline of my code: Columns.js export class Columns { ...

Creating a jquery DataTable from HTML generated by angular $sce can be done by following these steps

I have created an Angular controller that takes data and generates an HTML table from it. The generated table is being displayed on the page. Now, I want to apply jQuery data tables using that scope variable. The variable contains the HTML code of the tabl ...

Angular2 - using one centralized store to manage state across multiple components with unique API calls

Imagine having a main component called HomeComponent, containing multiple nested components named TeamsStandingComponent. TeamsStandingComponent is responsible for displaying data fetched from an API call using a common but private store called TeamsStandi ...