Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to fit HTML parent element's bounding box to a CSS transformed child?

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>
like image 477
abbr Avatar asked Dec 26 '15 20:12

abbr


People also ask

How do you transform a box in CSS?

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.

What is parent element in CSS?

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>.

What is Transformorigin?

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.

Which choice is the default value for the transform origin property?

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.


1 Answers

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>
like image 182
Maximillian Laumeister Avatar answered Sep 20 '22 05:09

Maximillian Laumeister