The function getComputedStyle(elem).getPropertyValue('fontSize') returns incorrect values when the font size of the element is specified in em units

My current issue involves trying to obtain the font size of an element in the following manner:

 getComputedStyle(MyTargetElement , "").getPropertyValue('font-size')

The result is coming back as 16px, when it should actually be 14px. Why is this discrepancy occurring?

<RootElement /> fontsze:14px 
  <GranParent /> fontsze:1rem 
    <Parent /> fontsze:1em
      <MyTargetElement /> fontsze:1em

Where did I go wrong in this scenario?

Answer №1

The main element of a ReactJS application is not the actual root HTML element (<html>), which serves as the foundation for rem calculations. Instead, you need to mount your application within another element, often not even a direct descendant of the <body> element.

If you haven't specified a font-size for the <html> element, the browser will default to its own stylesheet, resulting in a computed value of 16px for the 1rem-1em-1em chain.

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 is the purpose of using "recaptcha_get_html" function to echo?

Hello everyone. I've been struggling to incorporate Google's reCAPTCHA into my project and I keep encountering an error when trying to use the recaptcha_get_html function. It keeps showing up as undefined and despite searching high and low, I can ...

`how to extract the href attribute value from the hyperlink that triggered a modal in jQuery UI`

Recently, I began diving into Jquery and javascript coding. As a beginner, it feels odd to refer to myself as a noob due to my age. Here's the scenario: I have a hyperlink that triggers a dialogue box and sets a cookie. The dialog asks the user, "Are ...

Tips for invoking an Android function from an AngularJS directive?

I am facing an issue with my HTML that uses an AngularJS directive. This HTML file is being used in an Android WebView, and I want to be able to call an Android method from this directive (Learn how to call Android method from JS here). Below is the code ...

Create dynamic automatic titles in HTML with JavaScript

Below is the code snippet to add an image with a link to the home-page and an h1 with the document name (if there isn't one already). This HTML code includes a JavaScript file reference in the <head> section and uses a <h1> tag for the ti ...

Is there a way to modify an existing job in Kue Node.js after it has been created?

Utilizing Kue, I am generating employment opportunities. jobs.create('myQueue', { 'title':'test', 'job_id': id ,'params': params } ) .delay(milliseconds) .removeOnComplete( true ) ...

The Bootstrap modal form fails to properly handle the POST method when sending data to the server

I am encountering an issue with a button that triggers a modal form <a href="#" class="btn btn-primary" data-toggle="modal" data-target="#agregarProducto">Add Material</a> The modal appears as shown below: https://i.stack.imgur.com/J39x9.pn ...

Is it possible to modify the font size of all text that shares a particular font size?

Lately, I've been pondering on a question. A few years ago, I created a website using regular CSS and now I want to make some changes to the font sizes. While I know that CSS variables are the recommended solution for this situation, I'm curious ...

When testing, React does not trigger a rerender when a state change occurs

As part of my testing process, I am currently working on a React component that features a simple switch from Material-ui. This switch is designed to update a boolean field and functions properly when the application is run locally. During the testing pha ...

Display a <Dialog /> from Material-UI automatically every hour

Looking for a way to automatically show a popup every hour? I utilize the <Dialog /> component provided by the material-ui library. Can you guide me on setting this up? <Dialog fullScreen onClose={this.hidePopup} open={this.state.popupShow ...

Resizing images in different web browsers: Chrome, IE, and Safari

I'm currently in the process of building a website that utilizes the jquery tabs api. Each tab on the site contains an image that I would like to resize dynamically as the browser window size changes. Here is an example of one of the tabs: <li st ...

Set the DIV element to match the height of its container

I'm currently using this HTML code, and it's functioning correctly. However, I would like the height of the div to adjust dynamically based on the tab's content, rather than specifying a fixed height of 200px. Using a percentage value for th ...

Guide on making an NPM package with a web worker integration

Currently, I am in the process of developing an npm package that incorporates a web worker with type module. The issue arises when I try to link this package to a React application for testing purposes since the application cannot locate the worker.js file ...

I have come to realize that in my creation, my method does not function flawlessly as

I'm currently experimenting with creating a simple code using vue.js and d3.js. The goal is to plot some charts within the code, so I am attempting to allocate space for the charts by defining a method('add_svg') and executing the method in ...

dynamically display elements within aframe based on specific conditions

In my aframe scene, there are three icosahedrons, a complex particle system, and a cursor that fuses on objects within the scene. The performance of the scene is affected when the particles are visible because the cursor attempts to fuse with every particl ...

Customize Next Js styling based on the user's browser requirements

There's a curious issue when viewing my website through Facebook or LinkedIn - the background color suddenly changes to a vibrant green, but only in those particular webviews. However, when I view it through other apps like Twitter or Instagram, the ...

Is there a way to customize the color of the icons on material-table for the actions of onRowAdd, onRowUpdate, and onRowDelete

I recently experimented with the material-table library to perform basic CRUD operations. Utilizing onRowAdd, onRowUpdate, and onRowDelete, I was able to incorporate icons for each function. However, I am interested in changing the color of these icons. Ca ...

Limit Range of jQuery UI Slider Button

How can I modify the jQuery UI slider range to keep the button within the slider div and prevent it from overlapping as shown in the screenshots below? ...

Experience a glint in the React Suspense with React Router - Struggling with CSS properties post utilizing Suspense and Lazy

I'm experiencing an issue with my code where the CSS properties are not working properly when I wrap the code in suspense. The page loads and the suspense functions as expected, but the styling is not being applied. However, if I remove the suspense, ...

Developing middleware for managing event handlers

Scenario: I am tasked with managing multiple events that necessitate an "available client". Therefore, in each event handler, my first step is to attempt to acquire an available client. If no client is available, I will send a "Service unavailable" messag ...

Sending arguments to material-ui AutoComplete component

Is there a way to pass additional parameters to a function in material-ui's AutoComplete component? I want to pass something other than just the value in the onUpdateInput tag. I am trying to accomplish what is shown in this React example: https://re ...