Set colors to CSS variables

Within my CSS I define several color variables, such as:

$bkg-color: #ffffff

While white is the default color, $bkg-color can also be customized by the user and saved. In my TypeScript file, I have:

backgroundColor?: string = '';

The data from the GET call is stored in pageDetails, where I check if there is a specific backgroundColor:

if(pageDetails.backgroundColor) { 
this.backgroundColor = pageDetails.backgroundColor; 
}

Due to the way my page content is displayed using v-html='displayedHtml', I cannot directly bind the background color class in the HTML.

How can I set $bkg-color to this.backgroundColor (if it exists) or else set it to the default of white?

I am working with Vue.js and TypeScript, and while I understand how to set the default color like this: $bkg-color: #ffffff !default;, I am unsure how to dynamically assign it based on my data.

Answer №1

To change the background color, you can utilize CSS variables which allow for dynamic changes at runtime. If you want to conditionally set a background color based on an existing value or default to white, you can achieve this using CSS variables.

It's important to note that SCSS variables are only available during compile time and are replaced by their values in the final CSS file generated. To address this limitation, consider using CSS custom properties like variables instead of SCSS variables.

If the original question was not addressed properly, please let me know so I can provide further clarification.

Here is a demo showcasing how CSS variables can be dynamically changed at runtime.

Please keep in mind that while you won't be able to directly use functions like lighten/darken with CSS variables, you can implement similar functionality using JavaScript and library functions like scaleColor for immutable color scaling with better precision.

For example, here's a simple implementation of scaleColor method in JavaScript that allows you to darken or lighten colors:

ColorUtils.scaleColor("#294e80", -35);  //darken color by 35%
ColorUtils.scaleColor("#F6F6F6", 35); // lighten color by 35%

View a TypeScript playground with code snippets demonstrating the usage of scaleColor function.

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

What could be causing my div to display a horizontal scrollbar in Firefox?

Here's a straightforward test scenario: <html> <body> <div style="border:2px solid black; overflow: auto;"> x </div> </body> </html> Upon rendering, a horizontal scrollbar appears! I was using F ...

``Troubleshooting Problem with Tailwind's Flex Box Responsive Grid and Card Elements

Starting point: Preview https://i.sstatic.net/zfzBU.jpg Code : <div class="container my-12 mx-auto"> <div className="flex flex-wrap "> {error ? <p>{error.message}</p> : null} {!isLoading ...

Converting an HTML form into a Django form while maintaining the original styling

I have a basic HTML form that I would like to use for collecting sign-in information with a Django backend. Below is my simple non-Django HTML form: <form id="contactForm" method="POST"> <div class="row"> <div class="form-group ...

Show the Select component in a grid format with a list view, displaying three items per row

Within my react Dropdown component, there is currently a list view displayed when an item is selected. However, I am looking to modify it so that the items are arranged in a grid format with 3 items per row. Sample Code: import { Dropdown } from 'pri ...

Retain the formatting of HTML while extracting the text

Is there a simple way to extract text from HTML source without losing formatting (such as line breaks and spaces)? Currently, my text extraction process looks like this: page_title_element = driver.find_element_by_xpath("x-path") page_title = pa ...

One way to showcase a single piece of data without the need for looping is by utilizing the `

I am encountering an issue. Why does the insertAdjacentHTML("afterend") output keep looping? I only want to display "1" once, not repeatedly. var btn = document.getElementById("btnClick"); btn.addEventListener("click", function (e) { e.preventDefaul ...

Using discord.js to conveniently set up a guild along with channels that are equipped with custom

When Discord devs introduced this feature, I can't seem to wrap my head around how they intended Discord.GuildManager#create to function. How could they possibly have expected it to work with Discord.GuildCreateOptions#channels[0], for instance, { ...

The nested div escapes from its parent div despite the application of clearfix and the correct positioning of relative and absolute elements

Hey there, I have a question about using relative and absolute positioning along with applying clearfix to the main container in my code. The layout is quite simple - a nav-bar with a drop-down menu at the top, followed by a hero image spanning the screen, ...

What is the best way to loop through each child element and retrieve a specific element?

I am working on a find() function that searches for an element by tag name within a nested structure. I have attempted to iterate through all the children elements to locate the matching element. function find(tag: string) { let children = data.childr ...

Accessing the final element of an Observable<array[]> in Angular 5: A step-by-step guide

Currently, I am utilizing angularfire2 alongside Angular 5 for my project development. Below is the code snippet: import { Component, OnInit } from '@angular/core'; import { AngularFirestore, AngularFirestoreCollection, AngularFirestoreDocument ...

Contrasting hierarchy of text-decoration and text-shadow in Firefox compared to Chrome

When viewing this HTML/CSS code in Firefox and Chrome, the text-decoration and text-shadow properties appear to be swapped between browsers. .foo { font-size:2em; text-decoration:underline LightBlue 1ex; text-decoration-skip-ink:none; ...

Setting up VSCode to run various tasks

My TypeScript project in Visual Studio Code has a specific task outlined as follows: { "version": "0.1.0", // The command is tsc. "command": "tsc", // Show the output window only if unrecognized errors occur. "showOutput": "silent", // Und ...

tips for concealing the shadow of a draggable div during the dragging process

I am facing an issue with a draggable div. When I drag the div, the shadow also moves along with it. I want to find a way to hide this shadow while dragging. <div draggable="true" (dragstart)="mousedown($event)" (drag)="dra ...

Utilize ng-repeat to display a series of images, with the first image being placed within a unique div

My challenge lies in displaying product images using ng-repeat, where one image is located in a separate div. Let me share my code and explain what is happening. The API response provides product details and images [{"id_product":"1000","name":"Nikolaus ...

Steps for positioning a div with list items to match the height of its parent container

I've been grappling with this problem for a long time now. Within the parent div "wrapper," there is a child div called "sidebar-wrapper" that contains an ul with li elements. I'm trying to make the height of "sidebar-wrapper" adjust to match t ...

Unable to execute script tag on PHP page loading

When I make an AJAX request to fetch JavaScript wrapped in <script> tags that needs to be inserted on the current page, how can I ensure that the code executes upon insertion? Here's the snippet I'm using to add the code: function display ...

Ensure the Next/Image component fits neatly inside a div without distorting its aspect ratio, and keep the

I've been attempting to style a NextJS image in a way that it has rounded corners, expands to fit its container div (which is blue in the image), and maintains its aspect ratio (only known at runtime). However, what I currently have is not working as ...

Adding data to the SharePoint RichText Control using the WinForms WebBrowser Control

I am struggling to modify the content of an SP Rich Text field using the WinForms web browser control. While changing the values of other controls is straightforward, I have encountered difficulties with Rich Text fields. I found some suggestions on this b ...

Issue encountered while using Material-UI makeStyles: unable to access property 'down' due to being undefined

I'm currently in the process of developing my own website using Material-UI and I'm facing an issue with making it responsive. My goal is to hide an image on smaller screens by utilizing [theme.breakpoints.down('md')]. However, I keep e ...

Optimizing web content for iOS Safari with min-height media queries

I'm currently working on a Bootstrap-based photo browser that needs to have a responsive 4:3 aspect ratio. To achieve this, I have implemented the following approach: #carousel { width: 320px; height: 240px; ... } Additionally, I utilize media ...