Jquery scaling settings

Can you explain why I am unable to set the scale for this div?

var a = 0.1;
var b = 0.1;
var scale = a+b;
$(".item").css('scale', scale)
.item {
  width: 100px;
  height: 100px;
  background: blue;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="item"></div>

Answer №1

According to the provided information:

When a numeric value is passed, jQuery automatically converts it to a string and appends "px" at the end. If the property requires units other than pixels, make sure to convert the value to a string and include the appropriate units before using the method.

Hence, it is important for the parameter to be in string format rather than a number:

var width = 50;
var height = 75;
var dimension = width + height;
$(".box").css('dimension', dimension.toString())
.box {
  width: 200px;
  height: 150px;
  background-color: green;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="box"></div>

Answer №2

Instead of using jQuery, you can achieve the same result with plain JavaScript and CSS variables. For example, you can set the scale property using

.css("scale", `${scale}`)
or
.css("scale", scale.toString())
as suggested by @Tom.

Here is an example using JavaScript and CSS Variable:

const a = 0.1;
const b = 0.1;
const scale = a + b;

document.querySelectorAll(".item").forEach((elItem) => {
  elItem.style.setProperty("--scale", scale);
});
.item {
  width: 100px;
  height: 100px;
  background: blue;
  scale: var(--scale, 1); /* Use --scale CSSVar or default to 1 */
}
<div class="item"></div>

Check out MDN's guide on Using CSS custom properties
Learn more about CSS var() on MDN

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

When scrolling, custom cursor object disappears from view as it moves along with the mouse

My goal is to have a custom object follow the mouse by changing the translate property and assigning x and y values to it. Everything works fine initially, the object appears and moves within the first frame. However, when scrolling down, the transform pro ...

Error code E401 is being encountered with npm, indicating either an incorrect password has been provided or the

My Node version is 10.15.0 and my NPM version is currently at 6.8.4. After updating npm to 14.16.0 and node to 7.6.2, I encountered the following error - npm ERR! code E401 npm ERR! Incorrect or missing password. npm ERR! If you were trying to log in, ...

d3js operates effectively after the encoding and decoding of data has been completed

Currently, I'm diving into creating a data visualization using d3js paired with hierarchical layout. The data I am working with has the following structure: 0 / | \ / | \ 1 5 3 \ | | \ | | ...

Unable to transmit information back to React

Recently stepping into the world of React and Node.js, I have successfully created a function in my Node.js application that executes a Python script using child process. However, I seem to be facing a challenge with my router post method named pythonExecu ...

What is the method to retrieve a value from CKEditor?

Within my view, I have this input field: <div class="treatment-service"> <%= f.input :service_id, as: :select, collection: current_partner.services.pluck(:name, :id), :label => "Serviciu" %> </div> <%= f.input :description, :as = ...

Arrange text in a line below neatly in a list

My goal is to line up items on the same row, as shown in the screenshot. I need the opening hours to align consistently in the list. The data is retrieved from a database and displayed using Vue. This is how I currently display the opening hours and days. ...

What strategies can be used to accurately position a rotated image within a rotated div?

Below is the code snippet I am using: <div class="container"> <img class="img"/> <div class="underlay"></div> </div> The image is a picture of some rotated text by 10 degrees, although the ...

Iterate through an array and append individual elements to a fresh array - ensuring only a single item is present in the new

I have been working on a project where I need to call a backend API and retrieve a JSON response. I have come across various solutions, but none seem to address my specific problem. The JSON data returned from the API is structured like this: [ { ...

The issue with disappearing HTML elements on input focus was resolved in iOS 8.3

I encountered a peculiar issue that bears resemblance to the fixed element problems on iOS highlighted on Stack Overflow, but with a unique twist. This anomaly only occurs when the parent document is scrollable and involves the use of an iframe. Below is a ...

Can you explain the significance of the ">" symbol within CSS coding?

Seeking some quick points for those in the know. Can someone provide a thorough explanation of the meaning and proper usage of the > symbol? Appreciate it. ...

Exploring React's Primary Button Color Implementation

I am attempting to customize the buttons in my theme for various states such as contained, primary, and hover. However, my current approach is not yielding the desired results. VIEW CODESANDBOX DEMO: CLICK HERE theme/overrides/MuiButton.js import palet ...

Disable automatic playback once the page has loaded (Bootstrap carousel in Twitter)

I'm having an issue with my slider not autoplaying after the page loads. However, when I right-click, autoplay seems to turn on. What could be causing this problem? $('#myCarousel').on('slid', '', function() { var $t ...

Replace the value within an array

Is there a smart method for substituting or replacing a value within an array? For example: var array = [[1,2,3,'null'],['null',2,3,4],['null','null','null','null'],[1,1,1,1]] The desired outpu ...

Using ElectronJS requires the usage of the import keyword to load ES Modules

I've recently delved into Electron development by exploring the Electron Docs. I opted for ES6 syntax with import/export, while the documentation showcased the use of require. To align with ES Module standards, I updated my package.json file with typ ...

Using Node.js to extract text from a local file on Azure using OCR technology

I recently started using the Azure OCR Service to extract text from images (https://learn.microsoft.com/de-de/azure/cognitive-services/Computer-vision/quickstarts/javascript#OCR). While things have been going smoothly so far with uploaded images, I am now ...

Retrieving the Windows user's username via Typescript or Javascript is an essential

How can I retrieve the current Windows username in my Angular 4 application using TypeScript or JavaScript? I am specifically working with the Chrome browser. ...

Getting an error when running forEach() on div elements in JavaScript

I am facing an issue with changing the height of divs in an array. Even though I am able to log the names of the divs correctly, when I try to set their height, I encounter a "this is undefined" error message in the console. Despite successfully logging a ...

The CSS table-cell element causes adjacent table-cell content to reposition

The table inside the "center" div is causing the contents in the "left" div to be offset by a few pixels from the top (about 8 in my browser). When you add some text before the table, this offset disappears. Why is this happening? Is there a way to preven ...

Leveraging the Power of CSS in Your Express Applications

Struggling to make my CSS links functional while working on localhost. Currently, when I view Index.html on my server, it displays as plain text without any styling. Even after trying the express middleware, the CSS files are still not being served, result ...

When using React, the auto-fill suggestions in forms are not displayed

Looking to enhance a React panel for adding new products, I've developed a separate auto-complete class. This special class should display an input field along with a list of suggested autofill items below. However, it seems that only the input elemen ...