Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Make the layout use the transformed coordinate (CSS)

Is there a way to make HTML/CSS to use the transformed coordinates in layout calculations? E.g. if I scale a div, I want the next div to automatically start on the scaled div’s boundary.

body {
  padding:50px;
}

.a {
  width:100px;
  height:100px;
  background-color:red;
  transform:scale(1.2)
}

.b {
  width:100px;
  height:100px;
  background-color:blue;
}
<div class="a">
  hello
</div>

<div class="b">
  world
</div>

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Is there a way to make HTML/CSS to use the transformed coordinates in layout calculations?

Yes, that’s what the transform property is for.

However, transform (like many other properties) only applies to an element and its descendants not its siblings. This is by-design. Furthermore, properties like transform (and opacity, and others) create a new stacking context which basically messes-up z-index ordering.

Anyway, to get what you want, you can use a different approach: as you’re using a constant scale of 1.2 you can use that to calculate a positive bottom margin on .a to bump siblings down, like so:

body {
  padding:50px;
}

.a {
  width: 100px;
  height: 100px;
  background-color: red;
  transform: scale(1.2); /* i.e. 1.0 + 0.2 */
  margin-bottom: 10px; /* ( 0.2 * 100px ) / 2 // the divide-by-2 is because the scale is centered, so a 20% total scale results in a 10% scale at each end of the axis */
}

.b {
  width: 100px;
  height: 100px;
  background-color: blue;
}
  <div class="a">
    hello
  </div>

  <div class="b">
    world
  </div>
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading