Update the CSS variable for the application's styling without affecting the component's individual styling

In the realm of my application, css variables dominate the styling landscape, particularly with font-size.

:root {
      --font_size_base: 16px;
    }
    body {
      font-size: var(--font_size_base);
    }
    

An issue arises when I attempt to craft a component that alters this root variable and dynamically increases the font size. However, within this particular component, I require the font size to remain fixed.

Within the component resides an HTML input type range:

ngAfterContentInit() {
        this.form.valueChanges.subscribe((value) => {
          if (value.fontSize) {
            this.document.documentElement.style.setProperty(
              '--font_size_base',
              `${value.fontSize}px`
            );
          }
        });
      }
    

While this successfully modifies the font size, it inadvertently impacts the fonts contained within the component. I have attempted CSS changes through view encapsulation set to shadowdom, yet this proves futile as the styling is applied via a style tag directly on the body element, thus still affecting my component.

Efforts to assign a value within the component's internal CSS have proven fruitless:

:host {
        --font_size_base: 12;
      }
    

Is there a viable solution to this predicament? For further insight, please refer to this stackblitz showcasing the scenario: https://stackblitz.com/edit/angular-ivy-hghqm3?file=src/app/controller/controller.component.css

Answer №1

To adjust the font size of your component, you have a couple of options:

:host {
  font-size: 12px;
}

or

:host {
  --font_size_base: 12px;
  font-size: var(--font_size_base);
}

Answer №2

Another option is to utilize the styles.css file

app-controller
{
  --font_size_base: 12px;
}

Note that app-controler serves as the "selector" for your component

Additionally, in your controller.css file

.child{
  font-size:var(--font_size_base)
}

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

Tips for securely implementing JSON web tokens when integrating an external application with the WordPress REST API

I have a query regarding JWT. Let's consider this situation. A -> wordpress site with wp rest api enabled; B -> External application (for example, a simple javascript/jQuery app) Suppose I want to make a post request or create a new post on t ...

Is there a way to create a JavaScript-driven search filter that updates automatically?

My website showcases a lineup of League of Legends champion icons, with one example shown below: <div class = "champion"> <p>Aatrox <img class = "face_left" src = "images/small/Aatrox.png"> <div class = "name" onmouseover="if(cha ...

Is jQuery or CSS necessary for differentiating between a standard image and a highlighted image?

Apologies if this has been asked before, but I'm in need of your assistance. Here's what I'm looking for: Imagine I have an image that is clickable in its normal state: https://i.sstatic.net/pGYyG.png Now, when I click on that image, I wa ...

Using Vue.js to update content in input files

I have a Vue.js template file that includes data containing various documents. Within the page, there is a table with rows that feature upload file input buttons, structured like this: <tr v-for="(doc, index) in documents"> <td :id="'doc-& ...

Element in Vue.js not recognized

Recently, I decided to dive into Vue.js as a beginner and started working on an app to manage my daily tasks. However, I encountered Vue Components along the way and ran into an error that has me puzzled: vue.js:1023 [Vue warn]: Unknown custom element: ...

Adjust the color of the button upon hovering with CSS

I'm having trouble changing the text color from white to black when hovering over the button. The code seems fine, but the text color isn't changing. Can anyone help me figure out how to make it work? .main__btn { font-size: 1.2rem; back ...

Is it possible to enable auto-completion for IDs in a separate CSS file using WebStorm 7?

I am new to using WebStorm 7 and currently working on building a simple HTML/CSS website. Initially, I included my CSS within the HTML file using the style tag, but now I have decided to move it to a separate file. Auto completion is functioning for all h ...

Utilizing a TypeScript getter or local variable to reference a service variable within an Angular component

Suppose you have an array of objects stored in a service export class DataService { private _dataSources : Array<DataSource> contructor(private callerService: CallerService){ this._dataSources = this.callerService.getDataSources(); // fetc ...

The issue with React Native - constant repetition of function

React native - I am experiencing a constant repetition of _getUser function. Can someone please assist me in troubleshooting this issue? const _getUser = async () => { setLoading(false); var userid = await AsyncStorage.getItem('userid' ...

The function document.getElementById(...) is failing to retrieve the text data from the table as expected

Greetings fellow HTML and JavaScript novice! I've got a table with input cells in the bottom row: <table class="convtbl" id="table1"> <tr> <td>Distance 1 (in miles)</td> <td>Time ...

Transferring JSON information from a dialog component to another component

In my application, I have 2 components named list and details, which are nested inside a parent component called customer. When the user clicks the delete button within the details component, a dialog window pops up like this: https://i.sstatic.net/hd4t3. ...

Setting a callback function as a prop for react-paginate in TypeScript: A step-by-step guide

When using react-paginate, there is a prop called onPageChange with the following type: onPageChange?(selectedItem: { selected: number }): void; After implementing it like this: const onPageChange = (selected): void => { console.log(selected); } ...

Switch Between More and Less Text - Implement Smooth Transition on Shopify Using Javascript

I recently implemented a More/Less toggle button using resources from this website. The functionality is there, but I now want to add a smooth transition effect. When the user clicks on "read more," I would like the hidden content to gradually appear, and ...

Unable to establish a connection to localhost using Javascript due to net::ERR_CONNECTION_REFUSED error

My setup includes a node.js express server running my API on port 7070 and a Flask server hosting my webpage on port 5000. While I can successfully access my API from the server using curl with curl http://localhost:7070/latest, I encounter issues when try ...

Storing a date in MySQL using Vue.js and Node.js

My current tech stack consists of nodejs and express.js for the backend, vuejs for the frontend, and mysql as the database. I am facing an issue where I cannot send a date retrieved from localStorage to my mysql database. Whenever I try to send the date, ...

Eliminate any blank brackets in a JSON structure

Looking to extract certain elements from a JSON string. Input : "sample": [{},{},{},{},{},{},{}], Output : "sample": [], Attempted solution : var jsonConfig = JSON.stringify(jsonObj); var result = jsonConfig.replace(/[{},]/g, ''); // Global ...

Forcing ng-dirty in AngularJS form validation

When using AngularJS for form validation, I want all required fields to be marked as erroneous when the user clicks submit. To achieve this, I am utilizing input.ng-dirty.ng-invalid to style the controls with errors. My goal is to set ng-dirty on required ...

Converting an array to an object using underscore: a beginner's guide

My array consists of different subjects: var subject = ["Tamil", "English", "Math"]; Now, I want to transform this array into an object, structured like this: [{ "name": "Tamil" }, { "name": "English" }, { "name": "Math" }] ...

Ways to focus on TinyMCE editor for customization

Would it be possible to target the entire TinyMCE editor in order to apply some styling, such as adding 10px of padding around it? I'm using TinyMCE 4 and starting with the HTML code shown below results in TinyMCE generating additional HTML. I would ...

Looking for the properties of the ServerResponse object in node.js - where can I locate them?

Currently, I've been diving into the book Node.js In Action. Chapter 9 introduces a message module with a function that has left me puzzled about the 'this' object when calling res.message. To gain clarity, I decided to print out the name of ...