I'm using css3 scale transform to scale a div that contains other divs inside.
The problem I have is that some of the inner divs I need to keep as they were, basically as if they were not scaled, their size should not change, however I do need to scale the parent div with everything else inside.
How can you reverse the scaling in some of the divs?
I am trying to apply an inverse scaling.
If the overall div had applied a value of 1.5 , I'm trying to find what value I should now scale the other divs to revert them visually to how they looked before.
The inverse of any scale operation is
1 / <scale>
so by scaling the container by1.5
you would need to scale the children by1 / 1.5 = 0.6
Unfortunately, according to the specification you cannot just use CSS like:
since scale is defined as
scale(<number>[, <number>])
where
<number>
isSo you'll have to do the calculation yourself or could use a dynamic stylesheet language like LESS which supports these sort of operations.
Demo (webkit only)
If the parent div has been scaled by a factor of
1.5
, then you need to scale the children by a factor of1/1.5 = 0.(6)
to keep their size constant.example
In general, in order to cancel for a child element a scale transform that has been applied on the parent and has a scale factor of
a
, you need to apply another scale transform of factor1/a
on the child itself.You either need to:
transform
on the childIf you, as suggested, use the inverted the scale factor, then note that the child does not necessarily keep its original size during the scale transformation -- this matters of course only if you let the scale transforms have a duration (e.g. by using
transition: transform;
).You could use a CSS variable to store your scale factor, and then use
calc()
to calculate the inverse scale for the child elements that have a certain class:⋅ ⋅ ⋅
In case you don't want any of the child elements to scale, another way of doing it would be to style your wrapper as a pseudo element :