Error in React: Mixing units is not allowed when using vw and px together in the css calc() function

Is there a way to create fluid font size using the css calc() function in React without running into errors when mixing vw and px units?

font-size: calc(14px + (26px - 14px)) * ((100vw - 300px) / (1600 - 300));

I encountered an error where SASS was unable to compile 100vw - 300px.

What would be the most effective solution to resolve this issue?

Answer №1

When using the CSS calc function, it is important to remember that the multiply or divide operators require at least one unitless number as an operand.

According to MDN Docs:

* Multiplication. One of the arguments must be a <number>.

/ Division. The right-hand side should be a <number>.

The calculation provided violates this rule. In simple terms:

calc(26px * (100vw - 300px) / 1300);

This is incorrect because neither 26px nor (100vw - 300px) are unitless numbers.


Additionally, there is an extra closing parenthesis after 14px), causing a syntax error in your original question.

Answer №2

Providing the error would be helpful, but you might want to attempt this:

("100vw - 300px")

To explore further, check out Is it possible to use vh minus pixels in a CSS calc()?

Answer №3

Perhaps utilizing a CSS custom property (CSS variable) could be effective in your case:

Working with a CSS variable can help to obscure the value and its unit, especially for properties like width or height. Alternatively, you could encapsulate everything within a calc function.

div {
   --containerWidth: 80%;
   font-size: calc(12px + (18px - 12px)) * ((var(--containerWidth) - 200px) / (1200 - 200));
}

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

Utilize the React library to refresh the charjs dataset

Using the chartjs library, I was able to create smooth curves effortlessly. I have set up a basic input field where a number can be entered. My goal is to update this data line: [5, 7, 4, 2, 1, 8, 7, 2, 5] based on the number input. For instance, if the in ...

What is the process of retrieving JSX within an object using a function that searches for data (in the form of JSX) in a separate

I am trying to extract JSX content from another file called categoriesDetails and store it in an object (foundCategory) by using the find function to check the item.title. Below is my implementation: This is how I am retrieving the data and intending to p ...

Tips for eliminating delays in removing CSS transitions

Is there a way to smoothly transition a CSS property, make an immediate change in the CSS property value, and then reattach the transition without any delay? To better understand, consider the following example: if ($(".marquee").height() < $(".marqu ...

Is it necessary for me to confirm the button's disabled state during the onButtonClick event, or can I rely on the React Component to handle button disabling?

In my React component, I am utilizing a <button> with both onClick and disabled attributes. The functionality for the onButtonClick action and the state of isButtonDisabled are managed through a store using Zustand. The selector for isButtonDisabled ...

Using React-Paginate for easier pagination

I am trying to implement pagination for a list of items using react-paginate. Here is my component code: import React, { Component } from 'react'; import { connect } from 'react-redux'; import { Link, browserHistory } from 'react- ...

Having trouble with the href attribute not properly redirecting to a different page

I am trying to create a hyperlink for an option that will take users to another page. All the other options on the page lead to different sections within the same page. When I add CONTACT, it doesn't work. All the files are in the same ...

I wonder which Material Design repository is the source of this library

I am attempting to replicate the CSS style used in this particular Material Design-inspired web application: https://i.stack.imgur.com/utalx.png After exploring Materialize CSS and MUI, I have not been able to pinpoint the exact framework responsible for ...

Guide on creating a toggle effect for a div with querySelector and addEventListener

I utilized the email and password divs provided by Bootstrap. The CSS for the id dropdownlogin includes display: none; in this post, I hope that I have shared adequate information. <script> document.addEventListener('DOMContentLoaded', ...

Turn off caching for server components in next.js version 13 (without fetching)

My goal is to dynamically redirect users to different pages depending on their logged-in status. I encountered a problem where the initial call to getSession seemed to be cached somehow. getSession const fetchSession = async (): Promise<TSession> = ...

Bootstrap does not toggle the menu

My menu won't open when clicked and I can't figure out why. I've searched through various posts on StackOverflow and other websites, but still haven't found a solution. I may be new to Bootstrap, but I'm eager to learn. <link ...

What strategies can be used to effectively structure CSS and JavaScript in a project for optimal organization?

In my NetBeans project, I currently have a large web project with CSS files included in the header. Some CSS codes are needed on all pages, while others are only necessary for specific pages. I am looking to optimize high-traffic pages by removing any ...

Resetting all form fields in React components

Encountering an issue with my react application. When filling out the Last Name field and then opening the "Address Dialog" to enter an address, everything works fine. However, the problem arises when I've already entered the Last Name and then procee ...

How can JQuery be used to implement "scrolling" buttons and automatically update the main page with fresh data?

I am attempting to achieve the following: 1 - Use jQuery to update index.html with the content from output.html (only update when there are differences in data, and ideally only update the parts that have changed). 2 - Add two buttons in the header ...

Determining the currently active tab in Material UI: A simple guide

I am currently working with the Material UITabs component. I am facing an issue where I need to display details specific to each tab on hover, but my current setup shows the details for all tabs regardless of their active state. Below is how I have impleme ...

The Border Radius Problem in Google Chrome

Greetings! I am currently working on a project for a website and have encountered an issue with the gallery on the projects page. The images are meant to be displayed as circles, and when hovered over, they should maintain their circular shape and have a b ...

The error "Prop does not exist on type 'PropsWithChildren'" occurs when attempting to dispatch an action with react-redux

When attempting to dispatch the action, I am encountering this error: The error message reads: Property 'fetch_feed_loc' does not exist on type 'PropsWithChildren<{ onSubmitForm: any; }>'. Another error states: Property &apos ...

What is the best way to make a panel width adjust automatically to fit the screen width?

I have developed a React/Material UI application that includes a Portal which showcases a Panel and a Widget. Main App: Show Portal and Set Background Color to Blue export default function App() { return ( <div style={{ backgroundC ...

Header fixed vertically

I am currently working on a webpage with a minimum width requirement of 1124px. This means that whenever the browser window is smaller than that, the content of the page needs to be scrolled horizontally instead of smoothly transitioning into a responsive ...

website pages loading faster than images can load

I run a website dedicated to music instruments, and I have implemented a carousel feature that displays images at intervals. However, I encountered an issue when converting my high-resolution images to base64 for storage in the database ...

Generating dynamic PDF files from a database using React and Node

I recently developed a digital platform for conducting physical assessments. The process involves the user entering the patient's measurements into a form, which then triggers the generation of a PDF report containing a detailed table and calculations ...