Nested Calc Operations

Nested calc operations

Apparently you have to assign px or % to all numbers that are not being multiplied or divided.

width: calc((100% / 7) - 2px);

Well I feel dumb now. Haha.

How do I rewrite my nested CSS calc operation

Nested calc is fine, but note that is not supported in IE11. Also, you need to maintain the spacing convention:

p {
padding: calc(calc(5px + 5px) + calc(5px + 5px));
border: 1px solid black;
}
<p>Hello! I have a padding of 20px!</p>

calc() function inside another calc() in CSS

It is possible to use calc() inside another calc().

An example:

div{
width: calc(100% - (1% + 30px));/* calc(1% + 30px) is nested inside calc()*/
}
div p{
width: calc(100% - 30px);/*100% is total width of the div*/
}

Update on nested calc with css variables:

.foo {
--widthA: 100px;
--widthB: calc(var(--widthA) / 2);
--widthC: calc(var(--widthB) / 2);
width: var(--widthC);
}

After all variables are expanded, widthC's value will be calc( calc( 100px / 2) / 2), then when it's assigned to .foo's width property, all inner calc()s (no matter how deeply nested) will be flattened to just parentheses, so the width property's value will be eventually calc( ( 100px / 2) / 2), i.e. 25px. In short: a calc() inside of a calc() is identical to just parentheses.

So, the current spec as well proves this using parentheses inside calc() is nested calc.

Learn more about css variables here.

Why the CSS calc() function is not working?

You need to add spaces between operators, it's a common mistake to forget them. We can also nest operation using calc as many as we want but they are equivalent to simple parentheses.

From the documentation:

Note: The + and - operators must be surrounded by whitespace. For
instance, calc(50% -8px) will be parsed as a percentage followed by a
negative length—an invalid expression—while calc(50% - 8px) is a
percentage followed by a subtraction operator and a length. Likewise,
calc(8px + -50%) is treated as a length followed by an addition
operator and a negative percentage.

The * and / operators do not require whitespace, but adding it for
consistency is both allowed and recommended.

Note: It is permitted to nest calc() functions, in which case the
inner ones are treated as simple parentheses.

.one {
background: red;
width: calc(100% - 150px);
margin-top: calc(20px + calc(40px * 2)); /*Same as calc(20px + (40px * 2))*/
height: calc(100px - 10px);
padding: calc(5% + 10px) calc(5% - 5px);
}
<div class="one">

</div>

How to write a function which has 2 nested functions inside and let computes sum / difference for any number of given items?

You can also use a dictionary to define your operations, and make a very readable and simple function with space for expansion:

def calc(a: int): return lambda op: {
'+': lambda b: calc(a+b),
'-': lambda b: calc(a-b),
'/': lambda b: calc(a/b),
'*': lambda b: calc(a*b),
}.get(op, a)

print(calc(2)('+')(3)('-')(10)('/')(10)('*')(-100)('='))

How to nest calc-variables within other calc-variables in Sass?

You cannot nest calc() function inside another calc().

Current compiled CSS:

margin: calc(calc(25px - 10px) /4); /* Incorrect syntax */
padding: calc(calc(25px - 10px) *2); /* Incorrect syntax */

Solution:

Calculate the size variable directly.

$size: $height - 10px;

Forked Codepen

How to calculate/update values for a nested list in python?

That is because the shallow copy of list. aa = [[0] * 2] * 2 creates shallow copy, try aa = [[0]*2 for i in range(2)]

aa = [[0]*2]*2

print(aa[0]) #[0,0]
print(aa[1]) # [0,0]

aa[0][0] = 2
aa[0][1] = 2

print(aa[0]) #[2,2]
print(aa[1]) # [2,2] !!

# +-------+-------+
# | aa[0] | aa[1] |
# +-------+-------+
# | |
# | |
# \ /
# [0 , 0]

# Both referring to the same list
# So whatever operation you do in aa[0] will reflect in aa[1] and vice versa
# Because effectively they are pointing to same list
# id(aa[0]) == id(aa[1]) ---> True
aa = [[0]*2 for i in range(2)]

for i in range(2):
for j in range(2):
for k in range(0, 10):
aa[i][j] += k
[[45, 45], [45, 45]]


Related Topics



Leave a reply



Submit