2

I need to mix units in a dynamically generated div, but this doesn't render (IE left is still the default after that line is executed).

var tab3 = document.createElement('div');
tab3.className = 'tab';
tab3.style.display = 'none';
tab2.style.left = 'calc(~"200px+40vw")px';
2
  • 3
    Please click edit, then [<>] snippet editor and produce a minimal reproducible example Commented Oct 30, 2019 at 9:50
  • 1
    Do you actually inject tab3 somewhere in your DOM ? What is tab2 ? Commented Oct 30, 2019 at 9:53

1 Answer 1

4

You've got several mistakes in your calc expression:

calc(~"200px+40vw")px -> calc(200px + 40vw)

  1. According to @JohnWeisz's comment - calc(~"200px+40vw")px is a Less expression, ~"..." is used to prevent Less from processing the addition operation from the source stylesheet files, but that's both invalid syntax and unnecessary from CSS string context.
  2. @G-Cyr adds that white space is required on both sides of the + and - operators. The * and / operators can be used without white space around them.

Example:

var tab3 = document.createElement('div');
tab3.className = 'tab';
tab3.style.left = 'calc(20px + 40vw)';
  
document.body.append(tab3);
.tab {
  position: absolute;
  width: 20vw;
  height: 20vh;
  background: red;
}

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

3 Comments

Explanation: calc(~"200px+40vw")px is a Less expression, ~ is used to prevent Less from processing the addition operation from the source stylesheet files, but that's both invalid syntax and unnecessary from JS string context.
@JohnWeisz - I was adding my explanation to the answer, but I'll take your nice comment instead :)
also In addition, white space is required on both sides of the + and - operators. (The * and / operators can be used without white space around them.) drafts.csswg.org/css-values-3/#calc-syntax would be worth to remind ... :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.