2

Usually using jquery we ll give css as like this

$("p").css("margin", "20px");

In my project i have code like this

var descMargin = productImage + productName + 26;
$(".product-compare-modal__grid--label").css("margin-top",descMargin);});

store it in a variable and giving.

here we are not mentioning any "px" or "%"

Is this the correct method of using? or I need to change?

7
  • You should concat px to the variable Commented Sep 1, 2015 at 11:32
  • I believe jQuery infers pixels where no unit is specified. Commented Sep 1, 2015 at 11:34
  • That would work absolutely fine. Commented Sep 1, 2015 at 11:34
  • @Utkanos: Only if it's a number, not if it's a string. Commented Sep 1, 2015 at 11:42
  • 1
    @Utkanos: As far as I can tell, nothing. It just passes it along, and the style object ignores it. Makes sense: It's easy and quick to test if a property or argument's value is a number, but checking whether it's an all-digits string is much more overhead. So they just do the number check and assume you know what you're doing if you pass a string. Commented Sep 1, 2015 at 12:01

2 Answers 2

6

Is this the correct method of using?

It's fine if your descMargin is meant to be in pixels (not % or em or rem or ...), and if it's actually a number (I'm guessing from your code snippet that it is).

If you just use a number, jQuery converts it to a string and appends px for you. From the documentation:

When a number is passed as the value, jQuery will convert it to a string and add px to the end of that string. If the property requires units other than px, convert the value to a string and add the appropriate units before calling the method.

What you don't want to do is pass it a string that's all digits. So don't give it "26", for instance; but 26 is fine.

So:

// This:
$("p").css("margin", 20);
// Does the same thing as this:
$("p").css("margin", "20px");
// But DON'T do this:
$("p").css("margin", "20"); // <== Wrong

Live Example:

var div = $("div");
test("50px");
setTimeout(test.bind(null, 40), 300);
setTimeout(test.bind(null, "30"), 600);

function test(value) {
  var rawValue = String(value).replace(/\D/g, '');
  var rawPadding;
  var result;
  
  div.css("padding-left", value);
  var result = div.css("padding-left").replace(/\D/g, '') == rawValue ? "worked" : "failed";
  $("<p>")
    .html("Result for " + JSON.stringify(value) + ": " + result)
    .appendTo(document.body);
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div>text</div>

Sign up to request clarification or add additional context in comments.

Comments

1

As i know that the by default when you pass value using the .css() that value will be taken as the "px" you want change the measure unit to the css property you need to give it manually.

this the way how you can give it manually

var descMargin = productImage + productName + 26;


$(".product-compare-modal__grid--label").css("margin-top",descMargin + "px");

$(".product-compare-modal__grid--label").css("margin-top",descMargin + "%");

$(".product-compare-modal__grid--label").css("margin-top",descMargin + "em" );

$(".product-compare-modal__grid--label").css("margin-top",descMargin + "pt" );

and here is the unit list you can apply

CSS units's list

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.