Is there a way, preferably without using JS, to make the container in following code snippet to wrap the scaled, and more generically, transformed child element, i.e. solid red outer border fully contains dashed blue border?
BTW, this appears to be a browser bug as it violates the default box model behavior that parent's size is auto adjusted to fit children.
#container {
border: 1px solid red;
}
#scaled {
border: 1px dashed blue;
transform: scale(3, 3);
transform-origin: 0 0;
}
<div id="container">
container
<div id="scaled">
scaled 3x
</div>
</div>
transform-box: fill-box is used to make the transform-origin the center of the bounding box, so the rectangle spins in place. Without it, the transform origin is the center of the SVG canvas, and so you get a very different effect. Full credit for this example goes to Pogany; see this codepen for a live version.
A parent is an element that is directly above and connected to an element in the document tree. In the diagram below, the <div> is a parent to the <ul>. A child is an element that is directly below and connected to an element in the document tree. In the diagram above, the <ul> is a child to the <div>.
Definition and Usage The transform-origin property allows you to change the position of transformed elements. 2D transformations can change the x- and y-axis of an element. 3D transformations can also change the z-axis of an element. To better understand the transform-origin property, view a demo.
The default value of the transform origin is at 50% 50%, which is exactly the center of any given element. The transform-origin can be specified using offset keywords, length values, or percentage values.
There is no way to do this without using JavaScript, but it is also not a browser bug. CSS transforms happen in the graphics pipeline, after the page flow is calculated and every non-transformed element's position and size are determined.
This means that CSS transforms do not cause the size of any other element to be recalculated, and that is why the container is not being resized to contain the transformed child element. This is actually a feature of transform
meant to improve performance by avoiding layout recalculation entirely.
The only way you can do this cleanly is to apply the transform to the parent element, which it seems like you're trying to get away from. If you want it to be dynamic, and you want to stay away from JS, there is unfortunately no other way.
#container {
border: 1px solid red;
transform: scale(3, 3);
transform-origin: 0 0;
}
#scaled {
border: 1px dashed blue;
}
<div id="container">
container
<div id="scaled">
scaled 3x
</div>
</div>
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