I have this div (id="myDiv"), when the div is under css - scale all it's children are under scale as well. I need the div to enlarge but not the children, in a "creative way" i tried 2 things which didn't work.. how to scale a div without scaling it's content ?
HTML
<div id="myDiv">
<h3>Song</h3>
<p>
Strawberry fields forever
</p>
<div>
jQuery
$("#myDiv").css("transform", "scale(2,3)");
//I tried to...
$("#myDiv").children().css("transform", "scale(-2,-3)");
$("#myDiv").children().css("transform", "scale(0,0)");
In that case, the problem is that the transform origin is set to 50% 50% by default. This is good for rotations (you typically want to rotate something around its center) but not really for scaling; your div gets shrunk down to the center of where it would originally be. (with the proper prefixes) to the style for #toBeScaled.
But things aren’t so happy if there are a bunch of HTML element children just doing what regular ol HTML elements do. Let’s say this is the kind of thing we’re after: CSS alone can’t really do this. But CSS is still the answer! transform: scale (); is what we need. It scales things perfectly proportionally.
If you scale to 0, your content will hide and if you scale to negatie value, it will flip. Child elements will always inherit scale from its parent so setting scale for children to 1 wont help either.
The HTML element creates an inline frame for embedding a third-party content. Scaling the content of an element may be needed if you want to display an object in the area not matching its original size.
You need to calculate backwards for the child elements.
For instance, you are scaling the div by 200% in the x axis...to reduce the children back to 100% you need to scale down to 50% (0.5)
Ditto for the y-axis 300%...therefore 33% (0.3)
CSS
.scaled { /* the parent */
transform: scale(2,3);
transform-origin:top left;
}
.scaled * { /* the children */
transform: scale(.5, .33);
}
Jsfiddle Demo
NOTE: As you can see there are transform-origin issues as well and other spacing issues youl will need to address.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With