Learn how to alter the website's overall appearance by changing the background or text color with a simple click on a color using Angular

Is there a way to dynamically change the background color or text color of the entire website when a user clicks on a color from one component to another? I know I need to use the Output decorator, but how can I implement this?

style.component.html

<div>
    <h2>background colors</h2>
    <div class="theme-options">
        <div class="theme-white"></div>
        <div class="theme-blue"></div>
        <div class="theme-orange"></div>
        <div class="theme-green"></div>
    </div>
    <hr>
    <h2>text Color</h2>
    <div class="theme-options">
        <div class="theme-white"></div>
        <div class="theme-blue"></div>
        <div class="theme-orange"></div>
        <div class="theme-green"></div>
    </div>
</div>

app.component.html

<app-signin></app-signin>
<app-style></app-style>

Answer №1

I recently created a simplified version to showcase the usage of the background-color property, which can also be applied to the text-color.

Step 1:
Firstly, we must incorporate a unique onclick trigger to the buttons/boxes. This trigger will activate a script upon being clicked.

Step 2:
We introduce a function that eliminates any existing classes responsible for the current background-color. This is achieved using the following code snippet:

document.querySelector("body").classList.remove("class");
. Essentially, this removes any prior classes applied to the body tag.

Step 3:
To set the desired background color, the same JavaScript command is used but with add instead of

remove</code. The line of code looks like this: <code>document.querySelector("body").classList.add("class");
.

Step 4:
Finally, we make adjustments in the CSS file corresponding to the specified class.


There are certainly ways to optimize and condense the script further. Nevertheless, I believe that this method is straightforward and easily reproducible, especially for beginners.

function textWhite() {
  document.querySelector("body").classList.remove("background-blue"); document.querySelector("body").classList.remove("background-orange"); document.querySelector("body").classList.remove("background-green");
  document.querySelector("body").classList.add("background-white");
}

function textBlue() {
document.querySelector("body").classList.remove("background-white"); document.querySelector("body").classList.remove("background-orange"); document.querySelector("body").classList.remove("background-green");
document.querySelector("body").classList.add("background-blue");
}

function textOrange() {
  document.querySelector("body").classList.remove("background-blue"); document.querySelector("body").classList.remove("background-white"); document.querySelector("body").classList.remove("background-green");
  document.querySelector("body").classList.add("background-orange");
}

function textGreen() {
document.querySelector("body").classList.remove("background-white"); document.querySelector("body").classList.remove("background-orange"); document.querySelector("body").classList.remove("background-blue");
document.querySelector("body").classList.add("background-green");
}
.background-white {
  background-color: white;
}

.background-blue {
  background-color: blue;
}

.background-orange {
  background-color: orange;
}

.background-green {
  background-color: green;
}
<body>
  <h2>background colors</h2>
  <div class="theme-options">
    <div onclick="textWhite()" class="theme-white">White</div>
    <div onclick="textBlue()" class="theme-blue">Blue</div>
    <div onclick="textOrange()" class="theme-orange">Orange</div>
    <div onclick="textGreen()" class="theme-green">Green</div>
  </div>
  <hr>
  <h2>text Color</h2>
  <div class="theme-options">
    <div class="theme-white">White</div>
    <div class="theme-blue">Blue</div>
    <div class="theme-orange">Orange</div>
    <div class="theme-green">Green</div>
  </div>
</body>

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

choose particular column in every row from the table's header class name

Every <th> in a table has a class, while the <td> elements do not. Is there a way to style all corresponding <td> elements with the same styles as their <th> counterparts using only plain css without any script? Furthermore, since ...

Pointer-enhanced Material UI Popup

Is there a way to implement a Popup that looks like the image provided using @Material-ui? https://material-ui.com/ I attempted to use Popover, however Popper does not include a pointer like in the image. https://i.stack.imgur.com/atz0G.png ...

Is Angular's forkJoin being phased out?

Upon opening my Angular project today, I came across a warning message that said: The utilization of forkJoin is marked as deprecated: resultSelector is deprecated, it is recommended to use pipe to map instead (deprecation) https://i.sstatic.net/vFpeu.pn ...

Unlock the power of Angular pipes in window.open with URL parameters!

<div class="member-img" onclick="window.open(childEpisode.File_URL | fullPath)"> </div> The fullPath function concatenates the domain part to the relative URL stored in file_URL. However, there seems to be an issue as it i ...

Creating a dropdown menu in Ionic 2 with dynamically populated options and a pre-selected value using ng

After scouring the depths of the internet, I have yet to find a suitable solution to my current dilemma. The code I have generates a list of options dynamically from an API and is functioning perfectly. <ion-select formControlName="d ...

Having trouble getting my JavaScript code to function properly on Firefox browser

I have created a script where the cursor automatically moves to the next form field once it reaches its maximum length, in this case 1. Here is the JavaScript code: window.onload=function(){ var container = document.getElementsByClassName("container")[0] ...

Having EventListeners set up across a single CSS class returns null when applied to different types of elements simultaneously

I want to create floating labels that resize dynamically when an input is clicked, similar to modern forms. I am using vanilla JS exclusively for this task. Currently, the setup works with the <input> tag. However, it does not work with the <text ...

How to Keep Images Within Table Borders From Overlapping?

I am in the process of experimenting with creating a newsletter and have developed a template. I am very new to design and have created a table of album releases that is not fitting within the border I established. How can I resolve this issue? The code s ...

Angular's ng toolkit universal experiencing loading issues and tools are malfunctioning

I encountered the following issue npm run build: prod commands are not functioning correctly ERROR: Cannot read property 'length' of undefined ERROR: If you think that this error shouldn't occur, please report it here: https://githu ...

The images in Bootstrap-Grid at 1920 resolution stay compact

Is there a way to make images grow beyond the 1280 resolution? This is how it appears at 1920 resolution https://i.sstatic.net/JmYc7.png And this is at 1280 resolution https://i.sstatic.net/Sk19K.png <div id="logokutu1" class="d ...

How to display a PDF in Angular 6 using a byte array retrieved from a WebAPI

Having trouble opening a PDF from byte array sent by WebAPI. This is my Service: fetchPdfDocument(): Observable<any> { return this.httpClient .get(this.configuration.serverUrl + this.configuration.getPdfDoc, { re ...

Mastering the utilization of React props within a Tailwind component

Looking to implement a straightforward button component in a Next App using Tailwind CSS, where I can pass values such as background color, text color, etc. through props and use them to render different types of buttons within my application. Take a look ...

Adding the "input-invalid" class to the input doesn't take effect until I click outside of the input field

When the zipcode field is updated, an ajax call is made. If there are no zipcodes available, I want to mark it as invalid by adding the "input-invalid" class. However, the red border validation only appears when clicking outside of the input field. Is ther ...

Attempting to toggle the menu with jQuery does not yield any results

I am fairly new to the world of javascript and jQuery and I'm encountering some issues that are proving difficult to troubleshoot. I am experimenting with the .toggle function on a menu list, but when I click the temporary button I've set up for ...

The creation of fsm.WriteStream is invalid as it is not a recognized constructor

Can you help me with this issue? I am attempting to install @ng-idle/keepalive using the command npm install --save @ng-idle/core, but I encountered the following error: npm ERR! fsm.WriteStream is not a constructor npm ERR! Log files were not written due ...

Having trouble displaying an image in your React project?

I'm having an issue with my code where the logo is not displaying, even though it should be in the src -> pages -> images folder. I can't seem to figure out why this is happening. import dress from "./images/dress.png"; const I1 = () => ...

What could be causing my CSS navigation toggle button to malfunction?

My attempt at creating a toggle button for tablets and phones seems to be failing. Despite the javascript class being triggered when I click the toggle button, it is not functioning as expected... https://i.stack.imgur.com/j5BN8.png https://i.stack.imgur. ...

Ending the Infinite Scroll in Ionic 3 When Data Runs Out

I am having an issue with my json data where I need to figure out how to stop the infinite scroll once there is no more data available. Can anyone help me implement this feature? Below is the code snippet for reference: handleDataLoad(currentCount) ...

Issue with Typescript express application utilizing express-openid-connect wherein cookies are not being retained, resulting in an infinite loop of redirects

For a while now, I've been facing a block with no resolution in sight for this particular issue. Hopefully, someone out there can lend a hand. Background I have a TS express application running on Firebase functions. Additionally, I utilize a custom ...

Generics in Typescript implemented for a React component that accepts an array of records along with an array of

I am currently working on developing a straightforward typed react component designed to display a table from an array of objects. The input data is structured as follows: // array of records containing data to render in the table data = [ { one: 1, ...